2014-05-03 22:54:38 -07:00
|
|
|
#include "volume-control.hpp"
|
|
|
|
#include "qt-wrappers.hpp"
|
2014-06-03 04:51:27 -07:00
|
|
|
#include <util/platform.h>
|
2014-05-03 22:54:38 -07:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QSlider>
|
|
|
|
#include <QLabel>
|
2014-07-14 09:32:39 -07:00
|
|
|
#include <QPainter>
|
2014-07-23 01:46:42 -07:00
|
|
|
#include <QTimer>
|
2014-05-03 22:54:38 -07:00
|
|
|
#include <string>
|
2014-06-03 04:51:27 -07:00
|
|
|
#include <math.h>
|
2014-05-03 22:54:38 -07:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-06-03 04:51:27 -07:00
|
|
|
#define VOL_MIN -96.0f
|
|
|
|
#define VOL_MAX 0.0f
|
|
|
|
|
2014-07-14 09:32:39 -07:00
|
|
|
/*
|
|
|
|
VOL_MIN_LOG = DBToLog(VOL_MIN)
|
|
|
|
VOL_MAX_LOG = DBToLog(VOL_MAX)
|
|
|
|
... just in case someone wants to use a smaller scale
|
|
|
|
*/
|
|
|
|
|
2014-06-03 04:51:27 -07:00
|
|
|
#define VOL_MIN_LOG -2.0086001717619175
|
|
|
|
#define VOL_MAX_LOG -0.77815125038364363
|
|
|
|
|
|
|
|
#define UPDATE_INTERVAL_MS 50
|
|
|
|
|
|
|
|
static inline float DBToLog(float db)
|
|
|
|
{
|
|
|
|
return -log10f(0.0f - (db - 6.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float DBToLinear(float db_full)
|
|
|
|
{
|
|
|
|
float db = fmaxf(fminf(db_full, VOL_MAX), VOL_MIN);
|
|
|
|
return (DBToLog(db) - VOL_MIN_LOG) / (VOL_MAX_LOG - VOL_MIN_LOG);
|
|
|
|
}
|
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
void VolControl::OBSVolumeChanged(void *data, calldata_t calldata)
|
|
|
|
{
|
|
|
|
VolControl *volControl = static_cast<VolControl*>(data);
|
|
|
|
int vol = (int)(calldata_float(calldata, "volume") * 100.0f + 0.5f);
|
|
|
|
|
2014-06-03 04:51:27 -07:00
|
|
|
QMetaObject::invokeMethod(volControl, "VolumeChanged", Q_ARG(int, vol));
|
2014-05-03 22:54:38 -07:00
|
|
|
}
|
|
|
|
|
2014-05-20 07:26:18 -07:00
|
|
|
void VolControl::OBSVolumeLevel(void *data, calldata_t calldata)
|
|
|
|
{
|
|
|
|
VolControl *volControl = static_cast<VolControl*>(data);
|
2014-07-14 09:32:39 -07:00
|
|
|
float peak = calldata_float(calldata, "level");
|
|
|
|
float mag = calldata_float(calldata, "magnitude");
|
|
|
|
float peakHold = calldata_float(calldata, "peak");
|
2014-06-03 04:51:27 -07:00
|
|
|
|
2014-05-20 07:26:18 -07:00
|
|
|
QMetaObject::invokeMethod(volControl, "VolumeLevel",
|
2014-07-14 09:32:39 -07:00
|
|
|
Q_ARG(float, mag),
|
|
|
|
Q_ARG(float, peak),
|
|
|
|
Q_ARG(float, peakHold));
|
2014-05-20 07:26:18 -07:00
|
|
|
}
|
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
void VolControl::VolumeChanged(int vol)
|
|
|
|
{
|
|
|
|
signalChanged = false;
|
|
|
|
slider->setValue(vol);
|
|
|
|
signalChanged = true;
|
|
|
|
}
|
|
|
|
|
2014-07-14 09:32:39 -07:00
|
|
|
void VolControl::VolumeLevel(float mag, float peak, float peakHold)
|
2014-05-20 07:26:18 -07:00
|
|
|
{
|
2014-06-03 04:51:27 -07:00
|
|
|
uint64_t curMeterTime = os_gettime_ns() / 1000000;
|
|
|
|
|
2014-07-14 09:32:39 -07:00
|
|
|
/*
|
|
|
|
Add again peak averaging?
|
|
|
|
*/
|
2014-06-03 04:51:27 -07:00
|
|
|
|
|
|
|
/* only update after a certain amount of time */
|
|
|
|
if ((curMeterTime - lastMeterTime) > UPDATE_INTERVAL_MS) {
|
2014-07-14 11:12:53 -07:00
|
|
|
float vol = (float)slider->value() * 0.01f;
|
2014-06-03 04:51:27 -07:00
|
|
|
lastMeterTime = curMeterTime;
|
2014-07-14 11:12:53 -07:00
|
|
|
volMeter->setLevels(DBToLinear(mag) * vol,
|
|
|
|
DBToLinear(peak) * vol,
|
|
|
|
DBToLinear(peakHold) * vol);
|
2014-06-03 04:51:27 -07:00
|
|
|
}
|
2014-05-20 07:26:18 -07:00
|
|
|
}
|
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
void VolControl::SliderChanged(int vol)
|
|
|
|
{
|
|
|
|
if (signalChanged) {
|
|
|
|
signal_handler_disconnect(obs_source_signalhandler(source),
|
|
|
|
"volume", OBSVolumeChanged, this);
|
|
|
|
|
2014-08-04 08:41:15 -07:00
|
|
|
obs_source_set_volume(source, float(vol)*0.01f);
|
2014-05-03 22:54:38 -07:00
|
|
|
|
|
|
|
signal_handler_connect(obs_source_signalhandler(source),
|
|
|
|
"volume", OBSVolumeChanged, this);
|
|
|
|
}
|
2014-06-03 04:51:27 -07:00
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
volLabel->setText(QString::number(vol));
|
|
|
|
}
|
|
|
|
|
2014-07-08 11:58:48 -07:00
|
|
|
QString VolControl::GetName() const
|
|
|
|
{
|
|
|
|
return nameLabel->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::SetName(const QString &newName)
|
|
|
|
{
|
|
|
|
nameLabel->setText(newName);
|
|
|
|
}
|
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
VolControl::VolControl(OBSSource source_)
|
|
|
|
: source (source_),
|
2014-06-03 04:51:27 -07:00
|
|
|
signalChanged (true),
|
|
|
|
lastMeterTime (0),
|
|
|
|
levelTotal (0.0f),
|
|
|
|
levelCount (0.0f)
|
2014-05-03 22:54:38 -07:00
|
|
|
{
|
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout();
|
|
|
|
QHBoxLayout *textLayout = new QHBoxLayout();
|
2014-08-04 08:41:15 -07:00
|
|
|
int vol = int(obs_source_get_volume(source) * 100.0f);
|
2014-05-03 22:54:38 -07:00
|
|
|
|
|
|
|
nameLabel = new QLabel();
|
|
|
|
volLabel = new QLabel();
|
2014-07-14 09:32:39 -07:00
|
|
|
volMeter = new VolumeMeter();
|
2014-05-03 22:54:38 -07:00
|
|
|
slider = new QSlider(Qt::Horizontal);
|
|
|
|
|
|
|
|
QFont font = nameLabel->font();
|
|
|
|
font.setPointSize(font.pointSize()-1);
|
|
|
|
|
2014-08-04 08:41:15 -07:00
|
|
|
nameLabel->setText(obs_source_get_name(source));
|
2014-05-03 22:54:38 -07:00
|
|
|
nameLabel->setFont(font);
|
|
|
|
volLabel->setText(QString::number(vol));
|
|
|
|
volLabel->setFont(font);
|
|
|
|
slider->setMinimum(0);
|
|
|
|
slider->setMaximum(100);
|
|
|
|
slider->setValue(vol);
|
2014-05-24 16:42:54 -07:00
|
|
|
// slider->setMaximumHeight(13);
|
2014-05-03 22:54:38 -07:00
|
|
|
|
|
|
|
textLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
textLayout->addWidget(nameLabel);
|
|
|
|
textLayout->addWidget(volLabel);
|
|
|
|
textLayout->setAlignment(nameLabel, Qt::AlignLeft);
|
|
|
|
textLayout->setAlignment(volLabel, Qt::AlignRight);
|
|
|
|
|
|
|
|
mainLayout->setContentsMargins(4, 4, 4, 4);
|
|
|
|
mainLayout->setSpacing(2);
|
|
|
|
mainLayout->addItem(textLayout);
|
2014-05-20 07:26:18 -07:00
|
|
|
mainLayout->addWidget(volMeter);
|
2014-05-03 22:54:38 -07:00
|
|
|
mainLayout->addWidget(slider);
|
|
|
|
|
|
|
|
setLayout(mainLayout);
|
|
|
|
|
|
|
|
signal_handler_connect(obs_source_signalhandler(source),
|
|
|
|
"volume", OBSVolumeChanged, this);
|
|
|
|
|
2014-05-20 07:26:18 -07:00
|
|
|
signal_handler_connect(obs_source_signalhandler(source),
|
2014-06-03 04:51:27 -07:00
|
|
|
"volume_level", OBSVolumeLevel, this);
|
2014-05-20 07:26:18 -07:00
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
QWidget::connect(slider, SIGNAL(valueChanged(int)),
|
|
|
|
this, SLOT(SliderChanged(int)));
|
|
|
|
}
|
|
|
|
|
|
|
|
VolControl::~VolControl()
|
|
|
|
{
|
|
|
|
signal_handler_disconnect(obs_source_signalhandler(source),
|
|
|
|
"volume", OBSVolumeChanged, this);
|
2014-05-20 07:26:18 -07:00
|
|
|
|
|
|
|
signal_handler_disconnect(obs_source_signalhandler(source),
|
2014-06-03 04:51:27 -07:00
|
|
|
"volume_level", OBSVolumeLevel, this);
|
2014-05-03 22:54:38 -07:00
|
|
|
}
|
2014-07-14 09:32:39 -07:00
|
|
|
|
|
|
|
VolumeMeter::VolumeMeter(QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
setMinimumSize(1, 3);
|
|
|
|
|
|
|
|
bkColor.setRgb(0xDD, 0xDD, 0xDD);
|
|
|
|
magColor.setRgb(0x20, 0x7D, 0x17);
|
|
|
|
peakColor.setRgb(0x3E, 0xF1, 0x2B);
|
|
|
|
peakHoldColor.setRgb(0x00, 0x00, 0x00);
|
|
|
|
|
2014-07-23 01:46:42 -07:00
|
|
|
resetTimer = new QTimer(this);
|
|
|
|
connect(resetTimer, SIGNAL(timeout()), this, SLOT(resetState()));
|
2014-07-14 09:32:39 -07:00
|
|
|
|
2014-07-23 01:46:42 -07:00
|
|
|
resetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::resetState(void)
|
|
|
|
{
|
2014-07-14 09:32:39 -07:00
|
|
|
setLevels(0.0f, 0.0f, 0.0f);
|
2014-07-23 01:46:42 -07:00
|
|
|
if (resetTimer->isActive())
|
|
|
|
resetTimer->stop();
|
2014-07-14 09:32:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setLevels(float nmag, float npeak, float npeakHold)
|
|
|
|
{
|
|
|
|
mag = nmag;
|
|
|
|
peak = npeak;
|
|
|
|
peakHold = npeakHold;
|
2014-07-23 01:46:42 -07:00
|
|
|
|
2014-07-14 09:32:39 -07:00
|
|
|
update();
|
2014-07-23 01:46:42 -07:00
|
|
|
|
|
|
|
if (resetTimer->isActive())
|
|
|
|
resetTimer->stop();
|
|
|
|
resetTimer->start(250);
|
|
|
|
|
2014-07-14 09:32:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(event);
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
QLinearGradient gradient;
|
|
|
|
|
|
|
|
int width = size().width();
|
|
|
|
int height = size().height();
|
|
|
|
|
|
|
|
int scaledMag = int((float)width * mag);
|
|
|
|
int scaledPeak = int((float)width * peak);
|
|
|
|
int scaledPeakHold = int((float)width * peakHold);
|
|
|
|
|
|
|
|
gradient.setStart(qreal(scaledMag), 0);
|
|
|
|
gradient.setFinalStop(qreal(scaledPeak), 0);
|
|
|
|
gradient.setColorAt(0, magColor);
|
|
|
|
gradient.setColorAt(1, peakColor);
|
|
|
|
|
|
|
|
// RMS
|
|
|
|
painter.fillRect(0, 0,
|
|
|
|
scaledMag, height,
|
|
|
|
magColor);
|
|
|
|
|
|
|
|
// RMS - Peak gradient
|
|
|
|
painter.fillRect(scaledMag, 0,
|
|
|
|
scaledPeak - scaledMag + 1, height,
|
|
|
|
QBrush(gradient));
|
|
|
|
|
|
|
|
// Background
|
|
|
|
painter.fillRect(scaledPeak, 0,
|
|
|
|
width - scaledPeak, height,
|
|
|
|
bkColor);
|
|
|
|
|
|
|
|
// Peak hold
|
2014-07-23 01:46:42 -07:00
|
|
|
if (peakHold == 1.0f)
|
2014-07-14 09:32:39 -07:00
|
|
|
scaledPeakHold--;
|
|
|
|
|
|
|
|
painter.setPen(peakHoldColor);
|
|
|
|
painter.drawLine(scaledPeakHold, 0,
|
|
|
|
scaledPeakHold, height);
|
|
|
|
|
|
|
|
}
|