2014-05-03 22:54:38 -07:00
|
|
|
#include "volume-control.hpp"
|
|
|
|
#include "qt-wrappers.hpp"
|
2017-02-24 03:37:28 -08:00
|
|
|
#include "obs-app.hpp"
|
2015-03-22 14:58:07 -07:00
|
|
|
#include "mute-checkbox.hpp"
|
2015-04-04 06:19:22 -07:00
|
|
|
#include "slider-absoluteset-style.hpp"
|
2017-07-01 19:03:37 -07:00
|
|
|
#include <obs-audio-controls.h>
|
2014-06-03 04:51:27 -07:00
|
|
|
#include <util/platform.h>
|
2017-02-05 21:51:50 -08:00
|
|
|
#include <util/threading.h>
|
2014-05-03 22:54:38 -07:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QVBoxLayout>
|
2015-06-20 18:56:07 -07:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QVariant>
|
2014-05-03 22:54:38 -07:00
|
|
|
#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;
|
|
|
|
|
2017-02-05 21:56:01 -08:00
|
|
|
QWeakPointer<VolumeMeterTimer> VolumeMeter::updateTimer;
|
|
|
|
|
2016-01-07 19:48:36 -08:00
|
|
|
void VolControl::OBSVolumeChanged(void *data, float db)
|
2014-05-03 22:54:38 -07:00
|
|
|
{
|
2016-01-07 19:48:36 -08:00
|
|
|
Q_UNUSED(db);
|
2014-05-03 22:54:38 -07:00
|
|
|
VolControl *volControl = static_cast<VolControl*>(data);
|
|
|
|
|
2014-11-19 07:41:07 -08:00
|
|
|
QMetaObject::invokeMethod(volControl, "VolumeChanged");
|
2014-05-03 22:54:38 -07:00
|
|
|
}
|
|
|
|
|
2016-01-07 19:48:36 -08:00
|
|
|
void VolControl::OBSVolumeLevel(void *data, float level, float mag,
|
|
|
|
float peak, float muted)
|
2014-05-20 07:26:18 -07:00
|
|
|
{
|
|
|
|
VolControl *volControl = static_cast<VolControl*>(data);
|
2014-06-03 04:51:27 -07:00
|
|
|
|
2017-02-05 21:56:01 -08:00
|
|
|
if (muted)
|
|
|
|
level = mag = peak = 0.0f;
|
|
|
|
|
|
|
|
volControl->volMeter->setLevels(mag, level, peak);
|
2014-05-20 07:26:18 -07:00
|
|
|
}
|
|
|
|
|
2015-03-22 14:58:07 -07:00
|
|
|
void VolControl::OBSVolumeMuted(void *data, calldata_t *calldata)
|
|
|
|
{
|
|
|
|
VolControl *volControl = static_cast<VolControl*>(data);
|
|
|
|
bool muted = calldata_bool(calldata, "muted");
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(volControl, "VolumeMuted",
|
|
|
|
Q_ARG(bool, muted));
|
|
|
|
}
|
|
|
|
|
2014-11-19 07:41:07 -08:00
|
|
|
void VolControl::VolumeChanged()
|
2014-05-03 22:54:38 -07:00
|
|
|
{
|
2015-01-02 10:57:01 -08:00
|
|
|
slider->blockSignals(true);
|
2014-11-19 07:41:07 -08:00
|
|
|
slider->setValue((int) (obs_fader_get_deflection(obs_fader) * 100.0f));
|
2015-01-02 10:57:01 -08:00
|
|
|
slider->blockSignals(false);
|
|
|
|
|
2014-12-28 03:31:21 -08:00
|
|
|
updateText();
|
2014-05-03 22:54:38 -07:00
|
|
|
}
|
|
|
|
|
2015-03-22 14:58:07 -07:00
|
|
|
void VolControl::VolumeMuted(bool muted)
|
|
|
|
{
|
|
|
|
if (mute->isChecked() != muted)
|
|
|
|
mute->setChecked(muted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::SetMuted(bool checked)
|
|
|
|
{
|
|
|
|
obs_source_set_muted(source, checked);
|
|
|
|
}
|
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
void VolControl::SliderChanged(int vol)
|
|
|
|
{
|
2014-11-19 07:41:07 -08:00
|
|
|
obs_fader_set_deflection(obs_fader, float(vol) * 0.01f);
|
2014-12-28 03:31:21 -08:00
|
|
|
updateText();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::updateText()
|
|
|
|
{
|
2017-02-24 03:37:28 -08:00
|
|
|
QString db = QString::number(obs_fader_get_db(obs_fader), 'f', 1)
|
|
|
|
.append(" dB");
|
|
|
|
volLabel->setText(db);
|
|
|
|
|
|
|
|
bool muted = obs_source_muted(source);
|
|
|
|
const char *accTextLookup = muted
|
|
|
|
? "VolControl.SliderMuted"
|
|
|
|
: "VolControl.SliderUnmuted";
|
|
|
|
|
|
|
|
QString sourceName = obs_source_get_name(source);
|
|
|
|
QString accText = QTStr(accTextLookup).arg(sourceName, db);
|
|
|
|
|
|
|
|
slider->setAccessibleName(accText);
|
2014-05-03 22:54:38 -07:00
|
|
|
}
|
|
|
|
|
2014-07-08 11:58:48 -07:00
|
|
|
QString VolControl::GetName() const
|
|
|
|
{
|
|
|
|
return nameLabel->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::SetName(const QString &newName)
|
|
|
|
{
|
|
|
|
nameLabel->setText(newName);
|
|
|
|
}
|
|
|
|
|
2015-06-20 18:56:07 -07:00
|
|
|
void VolControl::EmitConfigClicked()
|
|
|
|
{
|
|
|
|
emit ConfigClicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
VolControl::VolControl(OBSSource source_, bool showConfig)
|
2014-05-03 22:54:38 -07:00
|
|
|
: source (source_),
|
2014-06-03 04:51:27 -07:00
|
|
|
levelTotal (0.0f),
|
2014-11-19 07:41:07 -08:00
|
|
|
levelCount (0.0f),
|
2014-11-23 12:34:15 -08:00
|
|
|
obs_fader (obs_fader_create(OBS_FADER_CUBIC)),
|
|
|
|
obs_volmeter (obs_volmeter_create(OBS_FADER_LOG))
|
2014-05-03 22:54:38 -07:00
|
|
|
{
|
2015-03-22 14:58:07 -07:00
|
|
|
QHBoxLayout *volLayout = new QHBoxLayout();
|
2014-05-03 22:54:38 -07:00
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout();
|
|
|
|
QHBoxLayout *textLayout = new QHBoxLayout();
|
2015-06-20 18:56:07 -07:00
|
|
|
QHBoxLayout *botLayout = new QHBoxLayout();
|
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();
|
2015-03-22 14:58:07 -07:00
|
|
|
mute = new MuteCheckBox();
|
2014-05-03 22:54:38 -07:00
|
|
|
slider = new QSlider(Qt::Horizontal);
|
|
|
|
|
|
|
|
QFont font = nameLabel->font();
|
|
|
|
font.setPointSize(font.pointSize()-1);
|
|
|
|
|
2017-02-24 03:37:28 -08:00
|
|
|
QString sourceName = obs_source_get_name(source);
|
|
|
|
|
|
|
|
nameLabel->setText(sourceName);
|
2014-05-03 22:54:38 -07:00
|
|
|
nameLabel->setFont(font);
|
|
|
|
volLabel->setFont(font);
|
|
|
|
slider->setMinimum(0);
|
|
|
|
slider->setMaximum(100);
|
2014-11-19 07:41:07 -08:00
|
|
|
|
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);
|
|
|
|
|
2017-02-24 03:37:28 -08:00
|
|
|
bool muted = obs_source_muted(source);
|
|
|
|
mute->setChecked(muted);
|
|
|
|
mute->setAccessibleName(
|
|
|
|
QTStr("VolControl.Mute").arg(sourceName));
|
2015-03-22 14:58:07 -07:00
|
|
|
|
|
|
|
volLayout->addWidget(slider);
|
|
|
|
volLayout->addWidget(mute);
|
|
|
|
volLayout->setSpacing(5);
|
|
|
|
|
2015-06-20 18:56:07 -07:00
|
|
|
botLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
botLayout->setSpacing(0);
|
|
|
|
botLayout->addLayout(volLayout);
|
|
|
|
|
|
|
|
if (showConfig) {
|
|
|
|
config = new QPushButton(this);
|
|
|
|
config->setProperty("themeID", "configIconSmall");
|
|
|
|
config->setFlat(true);
|
|
|
|
config->setSizePolicy(QSizePolicy::Maximum,
|
|
|
|
QSizePolicy::Maximum);
|
|
|
|
config->setMaximumSize(22, 22);
|
|
|
|
config->setAutoDefault(false);
|
|
|
|
|
2017-02-24 03:37:28 -08:00
|
|
|
config->setAccessibleName(QTStr("VolControl.Properties")
|
|
|
|
.arg(sourceName));
|
|
|
|
|
2015-06-20 18:56:07 -07:00
|
|
|
connect(config, &QAbstractButton::clicked,
|
|
|
|
this, &VolControl::EmitConfigClicked);
|
|
|
|
|
|
|
|
botLayout->addWidget(config);
|
|
|
|
}
|
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
mainLayout->setContentsMargins(4, 4, 4, 4);
|
|
|
|
mainLayout->setSpacing(2);
|
|
|
|
mainLayout->addItem(textLayout);
|
2014-05-20 07:26:18 -07:00
|
|
|
mainLayout->addWidget(volMeter);
|
2015-06-20 18:56:07 -07:00
|
|
|
mainLayout->addItem(botLayout);
|
2014-05-03 22:54:38 -07:00
|
|
|
|
|
|
|
setLayout(mainLayout);
|
|
|
|
|
2016-01-07 19:48:36 -08:00
|
|
|
obs_fader_add_callback(obs_fader, OBSVolumeChanged, this);
|
|
|
|
obs_volmeter_add_callback(obs_volmeter, OBSVolumeLevel, this);
|
2014-05-20 07:26:18 -07:00
|
|
|
|
2015-03-22 14:58:07 -07:00
|
|
|
signal_handler_connect(obs_source_get_signal_handler(source),
|
|
|
|
"mute", OBSVolumeMuted, this);
|
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
QWidget::connect(slider, SIGNAL(valueChanged(int)),
|
|
|
|
this, SLOT(SliderChanged(int)));
|
2015-03-22 14:58:07 -07:00
|
|
|
QWidget::connect(mute, SIGNAL(clicked(bool)),
|
|
|
|
this, SLOT(SetMuted(bool)));
|
2014-11-19 07:41:07 -08:00
|
|
|
|
|
|
|
obs_fader_attach_source(obs_fader, source);
|
2014-11-23 12:34:15 -08:00
|
|
|
obs_volmeter_attach_source(obs_volmeter, source);
|
|
|
|
|
2015-04-04 06:19:22 -07:00
|
|
|
slider->setStyle(new SliderAbsoluteSetStyle(slider->style()));
|
|
|
|
|
2014-11-19 07:41:07 -08:00
|
|
|
/* Call volume changed once to init the slider position and label */
|
|
|
|
VolumeChanged();
|
2014-05-03 22:54:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
VolControl::~VolControl()
|
|
|
|
{
|
2016-01-07 19:48:36 -08:00
|
|
|
obs_fader_remove_callback(obs_fader, OBSVolumeChanged, this);
|
|
|
|
obs_volmeter_remove_callback(obs_volmeter, OBSVolumeLevel, this);
|
2014-11-19 07:41:07 -08:00
|
|
|
|
2015-03-22 14:58:07 -07:00
|
|
|
signal_handler_disconnect(obs_source_get_signal_handler(source),
|
|
|
|
"mute", OBSVolumeMuted, this);
|
|
|
|
|
2014-11-19 07:41:07 -08:00
|
|
|
obs_fader_destroy(obs_fader);
|
2014-11-23 12:34:15 -08:00
|
|
|
obs_volmeter_destroy(obs_volmeter);
|
2014-05-03 22:54:38 -07:00
|
|
|
}
|
2014-07-14 09:32:39 -07:00
|
|
|
|
2015-01-13 23:01:14 -08:00
|
|
|
QColor VolumeMeter::getBkColor() const
|
|
|
|
{
|
|
|
|
return bkColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setBkColor(QColor c)
|
|
|
|
{
|
|
|
|
bkColor = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getMagColor() const
|
|
|
|
{
|
|
|
|
return magColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setMagColor(QColor c)
|
|
|
|
{
|
|
|
|
magColor = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getPeakColor() const
|
|
|
|
{
|
|
|
|
return peakColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setPeakColor(QColor c)
|
|
|
|
{
|
|
|
|
peakColor = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getPeakHoldColor() const
|
|
|
|
{
|
|
|
|
return peakHoldColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setPeakHoldColor(QColor c)
|
|
|
|
{
|
|
|
|
peakHoldColor = c;
|
|
|
|
}
|
|
|
|
|
2017-08-11 18:58:40 -07:00
|
|
|
QColor VolumeMeter::getClipColor1() const
|
|
|
|
{
|
|
|
|
return clipColor1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setClipColor1(QColor c)
|
|
|
|
{
|
|
|
|
clipColor1 = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getClipColor2() const
|
|
|
|
{
|
|
|
|
return clipColor2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setClipColor2(QColor c)
|
|
|
|
{
|
|
|
|
clipColor2 = c;
|
|
|
|
}
|
|
|
|
|
2015-01-13 23:01:14 -08:00
|
|
|
|
2014-07-14 09:32:39 -07:00
|
|
|
VolumeMeter::VolumeMeter(QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
setMinimumSize(1, 3);
|
|
|
|
|
2015-01-13 23:01:14 -08:00
|
|
|
//Default meter color settings, they only show if there is no stylesheet, do not remove.
|
2014-07-14 09:32:39 -07:00
|
|
|
bkColor.setRgb(0xDD, 0xDD, 0xDD);
|
|
|
|
magColor.setRgb(0x20, 0x7D, 0x17);
|
|
|
|
peakColor.setRgb(0x3E, 0xF1, 0x2B);
|
|
|
|
peakHoldColor.setRgb(0x00, 0x00, 0x00);
|
2017-07-01 19:03:37 -07:00
|
|
|
|
|
|
|
clipColor1.setRgb(0x7F, 0x00, 0x00);
|
|
|
|
clipColor2.setRgb(0xFF, 0x00, 0x00);
|
|
|
|
|
2017-02-05 21:56:01 -08:00
|
|
|
updateTimerRef = updateTimer.toStrongRef();
|
|
|
|
if (!updateTimerRef) {
|
|
|
|
updateTimerRef = QSharedPointer<VolumeMeterTimer>::create();
|
2017-02-14 15:00:52 -08:00
|
|
|
updateTimerRef->start(34);
|
2017-02-05 21:56:01 -08:00
|
|
|
updateTimer = updateTimerRef;
|
|
|
|
}
|
2014-07-14 09:32:39 -07:00
|
|
|
|
2017-02-05 21:56:01 -08:00
|
|
|
updateTimerRef->AddVolControl(this);
|
2014-07-23 01:46:42 -07:00
|
|
|
}
|
|
|
|
|
2017-02-05 21:56:01 -08:00
|
|
|
VolumeMeter::~VolumeMeter()
|
2014-07-23 01:46:42 -07:00
|
|
|
{
|
2017-02-05 21:56:01 -08:00
|
|
|
updateTimerRef->RemoveVolControl(this);
|
2014-07-14 09:32:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setLevels(float nmag, float npeak, float npeakHold)
|
|
|
|
{
|
2017-02-05 21:56:01 -08:00
|
|
|
uint64_t ts = os_gettime_ns();
|
|
|
|
QMutexLocker locker(&dataMutex);
|
|
|
|
|
|
|
|
mag += nmag;
|
|
|
|
peak += npeak;
|
|
|
|
peakHold += npeakHold;
|
|
|
|
multiple += 1.0f;
|
|
|
|
lastUpdateTime = ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void VolumeMeter::calcLevels()
|
|
|
|
{
|
|
|
|
uint64_t ts = os_gettime_ns();
|
|
|
|
QMutexLocker locker(&dataMutex);
|
|
|
|
|
|
|
|
if (lastUpdateTime && ts - lastUpdateTime > 1000000000) {
|
|
|
|
mag = peak = peakHold = 0.0f;
|
|
|
|
multiple = 1.0f;
|
|
|
|
lastUpdateTime = 0;
|
|
|
|
}
|
2014-07-23 01:46:42 -07:00
|
|
|
|
2017-02-05 21:56:01 -08:00
|
|
|
if (multiple > 0.0f) {
|
|
|
|
curMag = mag / multiple;
|
|
|
|
curPeak = peak / multiple;
|
|
|
|
curPeakHold = peakHold / multiple;
|
2014-07-23 01:46:42 -07:00
|
|
|
|
2017-02-05 21:56:01 -08:00
|
|
|
mag = peak = peakHold = multiple = 0.0f;
|
|
|
|
}
|
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();
|
|
|
|
|
2017-02-05 21:56:01 -08:00
|
|
|
calcLevels();
|
|
|
|
|
|
|
|
int scaledMag = int((float)width * curMag);
|
|
|
|
int scaledPeak = int((float)width * curPeak);
|
|
|
|
int scaledPeakHold = int((float)width * curPeakHold);
|
2014-07-14 09:32:39 -07:00
|
|
|
|
2017-07-01 19:03:37 -07:00
|
|
|
float db = obs_volmeter_get_cur_db(OBS_FADER_LOG, curPeakHold);
|
|
|
|
|
2014-07-14 09:32:39 -07:00
|
|
|
gradient.setStart(qreal(scaledMag), 0);
|
|
|
|
gradient.setFinalStop(qreal(scaledPeak), 0);
|
2017-07-01 19:03:37 -07:00
|
|
|
gradient.setColorAt(0, db == 0.0f ? clipColor1 : magColor);
|
|
|
|
gradient.setColorAt(1, db == 0.0f ? clipColor2 : peakColor);
|
2014-07-14 09:32:39 -07:00
|
|
|
|
|
|
|
// RMS
|
|
|
|
painter.fillRect(0, 0,
|
|
|
|
scaledMag, height,
|
2017-07-01 19:03:37 -07:00
|
|
|
db == 0.0f ? clipColor1 : magColor);
|
2014-07-14 09:32:39 -07:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
}
|
2017-02-05 21:56:01 -08:00
|
|
|
|
|
|
|
void VolumeMeterTimer::AddVolControl(VolumeMeter *meter)
|
|
|
|
{
|
|
|
|
volumeMeters.push_back(meter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeterTimer::RemoveVolControl(VolumeMeter *meter)
|
|
|
|
{
|
|
|
|
volumeMeters.removeOne(meter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeterTimer::timerEvent(QTimerEvent*)
|
|
|
|
{
|
|
|
|
for (VolumeMeter *meter : volumeMeters)
|
|
|
|
meter->update();
|
|
|
|
}
|