2016-02-23 23:16:51 +00:00
|
|
|
#include "volume-control.hpp"
|
|
|
|
#include "qt-wrappers.hpp"
|
2017-04-19 19:54:15 +00:00
|
|
|
#include "obs-app.hpp"
|
2016-02-23 23:16:51 +00:00
|
|
|
#include "mute-checkbox.hpp"
|
2019-07-27 12:47:10 +00:00
|
|
|
#include "slider-ignorewheel.hpp"
|
2016-02-23 23:16:51 +00:00
|
|
|
#include "slider-absoluteset-style.hpp"
|
2018-02-19 19:54:37 +00:00
|
|
|
#include <QFontDatabase>
|
2016-02-23 23:16:51 +00:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QPainter>
|
2018-12-16 16:14:58 +00:00
|
|
|
#include <QStyleFactory>
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
#define CLAMP(x, min, max) ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
|
2019-07-27 12:47:10 +00:00
|
|
|
#define FADER_PRECISION 4096.0
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
QWeakPointer<VolumeMeterTimer> VolumeMeter::updateTimer;
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
void VolControl::OBSVolumeChanged(void *data, float db)
|
|
|
|
{
|
|
|
|
Q_UNUSED(db);
|
2019-09-22 21:19:10 +00:00
|
|
|
VolControl *volControl = static_cast<VolControl *>(data);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
QMetaObject::invokeMethod(volControl, "VolumeChanged");
|
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
void VolControl::OBSVolumeLevel(void *data,
|
2019-09-22 21:19:10 +00:00
|
|
|
const float magnitude[MAX_AUDIO_CHANNELS],
|
|
|
|
const float peak[MAX_AUDIO_CHANNELS],
|
|
|
|
const float inputPeak[MAX_AUDIO_CHANNELS])
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
VolControl *volControl = static_cast<VolControl *>(data);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
volControl->volMeter->setLevels(magnitude, peak, inputPeak);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::OBSVolumeMuted(void *data, calldata_t *calldata)
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
VolControl *volControl = static_cast<VolControl *>(data);
|
2016-02-23 23:16:51 +00:00
|
|
|
bool muted = calldata_bool(calldata, "muted");
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(volControl, "VolumeMuted",
|
2019-09-22 21:19:10 +00:00
|
|
|
Q_ARG(bool, muted));
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::VolumeChanged()
|
|
|
|
{
|
|
|
|
slider->blockSignals(true);
|
2019-09-22 21:19:10 +00:00
|
|
|
slider->setValue(
|
|
|
|
(int)(obs_fader_get_deflection(obs_fader) * FADER_PRECISION));
|
2016-02-23 23:16:51 +00:00
|
|
|
slider->blockSignals(false);
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
updateText();
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::VolumeMuted(bool muted)
|
|
|
|
{
|
|
|
|
if (mute->isChecked() != muted)
|
|
|
|
mute->setChecked(muted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::SetMuted(bool checked)
|
|
|
|
{
|
|
|
|
obs_source_set_muted(source, checked);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::SliderChanged(int vol)
|
|
|
|
{
|
2019-07-27 12:47:10 +00:00
|
|
|
obs_fader_set_deflection(obs_fader, float(vol) / FADER_PRECISION);
|
2016-02-23 23:16:51 +00:00
|
|
|
updateText();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::updateText()
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
QString text;
|
|
|
|
float db = obs_fader_get_db(obs_fader);
|
|
|
|
|
|
|
|
if (db < -96.0f)
|
|
|
|
text = "-inf dB";
|
|
|
|
else
|
|
|
|
text = QString::number(db, 'f', 1).append(" dB");
|
|
|
|
|
|
|
|
volLabel->setText(text);
|
2017-04-19 19:54:15 +00:00
|
|
|
|
|
|
|
bool muted = obs_source_muted(source);
|
2019-09-22 21:19:10 +00:00
|
|
|
const char *accTextLookup = muted ? "VolControl.SliderMuted"
|
|
|
|
: "VolControl.SliderUnmuted";
|
2017-04-19 19:54:15 +00:00
|
|
|
|
|
|
|
QString sourceName = obs_source_get_name(source);
|
|
|
|
QString accText = QTStr(accTextLookup).arg(sourceName, db);
|
|
|
|
|
|
|
|
slider->setAccessibleName(accText);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString VolControl::GetName() const
|
|
|
|
{
|
|
|
|
return nameLabel->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::SetName(const QString &newName)
|
|
|
|
{
|
|
|
|
nameLabel->setText(newName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolControl::EmitConfigClicked()
|
|
|
|
{
|
|
|
|
emit ConfigClicked();
|
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
void VolControl::SetMeterDecayRate(qreal q)
|
|
|
|
{
|
|
|
|
volMeter->setPeakDecayRate(q);
|
|
|
|
}
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
void VolControl::setPeakMeterType(enum obs_peak_meter_type peakMeterType)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
volMeter->setPeakMeterType(peakMeterType);
|
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
VolControl::VolControl(OBSSource source_, bool showConfig, bool vertical)
|
2019-09-22 21:19:10 +00:00
|
|
|
: source(std::move(source_)),
|
|
|
|
levelTotal(0.0f),
|
|
|
|
levelCount(0.0f),
|
|
|
|
obs_fader(obs_fader_create(OBS_FADER_LOG)),
|
|
|
|
obs_volmeter(obs_volmeter_create(OBS_FADER_LOG)),
|
|
|
|
vertical(vertical)
|
2018-12-16 16:14:58 +00:00
|
|
|
{
|
2016-02-23 23:16:51 +00:00
|
|
|
nameLabel = new QLabel();
|
2019-09-22 21:19:10 +00:00
|
|
|
volLabel = new QLabel();
|
|
|
|
mute = new MuteCheckBox();
|
2019-07-27 12:47:10 +00:00
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
QString sourceName = obs_source_get_name(source);
|
2018-12-16 16:14:58 +00:00
|
|
|
setObjectName(sourceName);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
if (showConfig) {
|
|
|
|
config = new QPushButton(this);
|
|
|
|
config->setProperty("themeID", "configIconSmall");
|
|
|
|
config->setFlat(true);
|
|
|
|
config->setSizePolicy(QSizePolicy::Maximum,
|
2019-09-22 21:19:10 +00:00
|
|
|
QSizePolicy::Maximum);
|
2016-02-23 23:16:51 +00:00
|
|
|
config->setMaximumSize(22, 22);
|
|
|
|
config->setAutoDefault(false);
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
config->setAccessibleName(
|
|
|
|
QTStr("VolControl.Properties").arg(sourceName));
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
connect(config, &QAbstractButton::clicked, this,
|
|
|
|
&VolControl::EmitConfigClicked);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout;
|
2016-02-23 23:16:51 +00:00
|
|
|
mainLayout->setContentsMargins(4, 4, 4, 4);
|
|
|
|
mainLayout->setSpacing(2);
|
2018-12-16 16:14:58 +00:00
|
|
|
|
|
|
|
if (vertical) {
|
|
|
|
QHBoxLayout *nameLayout = new QHBoxLayout;
|
|
|
|
QHBoxLayout *controlLayout = new QHBoxLayout;
|
|
|
|
QHBoxLayout *volLayout = new QHBoxLayout;
|
2019-09-22 21:19:10 +00:00
|
|
|
QHBoxLayout *meterLayout = new QHBoxLayout;
|
2018-12-16 16:14:58 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
volMeter = new VolumeMeter(nullptr, obs_volmeter, true);
|
|
|
|
slider = new SliderIgnoreScroll(Qt::Vertical);
|
2018-12-16 16:14:58 +00:00
|
|
|
|
|
|
|
nameLayout->setAlignment(Qt::AlignCenter);
|
|
|
|
meterLayout->setAlignment(Qt::AlignCenter);
|
|
|
|
controlLayout->setAlignment(Qt::AlignCenter);
|
|
|
|
volLayout->setAlignment(Qt::AlignCenter);
|
|
|
|
|
|
|
|
nameLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
nameLayout->setSpacing(0);
|
|
|
|
nameLayout->addWidget(nameLabel);
|
|
|
|
|
|
|
|
controlLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
controlLayout->setSpacing(0);
|
|
|
|
|
|
|
|
if (showConfig)
|
|
|
|
controlLayout->addWidget(config);
|
|
|
|
|
|
|
|
controlLayout->addItem(new QSpacerItem(3, 0));
|
|
|
|
// Add Headphone (audio monitoring) widget here
|
|
|
|
controlLayout->addWidget(mute);
|
|
|
|
|
|
|
|
meterLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
meterLayout->setSpacing(0);
|
|
|
|
meterLayout->addWidget(volMeter);
|
|
|
|
meterLayout->addWidget(slider);
|
|
|
|
|
|
|
|
volLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
volLayout->setSpacing(0);
|
|
|
|
volLayout->addWidget(volLabel);
|
|
|
|
|
|
|
|
mainLayout->addItem(nameLayout);
|
|
|
|
mainLayout->addItem(volLayout);
|
|
|
|
mainLayout->addItem(meterLayout);
|
|
|
|
mainLayout->addItem(controlLayout);
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
volMeter->setFocusProxy(slider);
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
setMaximumWidth(110);
|
|
|
|
} else {
|
2019-09-22 21:19:10 +00:00
|
|
|
QHBoxLayout *volLayout = new QHBoxLayout;
|
2018-12-16 16:14:58 +00:00
|
|
|
QHBoxLayout *textLayout = new QHBoxLayout;
|
2019-09-22 21:19:10 +00:00
|
|
|
QHBoxLayout *botLayout = new QHBoxLayout;
|
2018-12-16 16:14:58 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
volMeter = new VolumeMeter(nullptr, obs_volmeter, false);
|
|
|
|
slider = new SliderIgnoreScroll(Qt::Horizontal);
|
2018-12-16 16:14:58 +00:00
|
|
|
|
|
|
|
textLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
textLayout->addWidget(nameLabel);
|
|
|
|
textLayout->addWidget(volLabel);
|
|
|
|
textLayout->setAlignment(nameLabel, Qt::AlignLeft);
|
2019-09-22 21:19:10 +00:00
|
|
|
textLayout->setAlignment(volLabel, Qt::AlignRight);
|
2018-12-16 16:14:58 +00:00
|
|
|
|
|
|
|
volLayout->addWidget(slider);
|
|
|
|
volLayout->addWidget(mute);
|
|
|
|
volLayout->setSpacing(5);
|
|
|
|
|
|
|
|
botLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
botLayout->setSpacing(0);
|
|
|
|
botLayout->addLayout(volLayout);
|
|
|
|
|
|
|
|
if (showConfig)
|
|
|
|
botLayout->addWidget(config);
|
|
|
|
|
|
|
|
mainLayout->addItem(textLayout);
|
|
|
|
mainLayout->addWidget(volMeter);
|
|
|
|
mainLayout->addItem(botLayout);
|
2019-07-27 12:47:10 +00:00
|
|
|
|
|
|
|
volMeter->setFocusProxy(slider);
|
2018-12-16 16:14:58 +00:00
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
setLayout(mainLayout);
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
QFont font = nameLabel->font();
|
2019-09-22 21:19:10 +00:00
|
|
|
font.setPointSize(font.pointSize() - 1);
|
2018-12-16 16:14:58 +00:00
|
|
|
|
|
|
|
nameLabel->setText(sourceName);
|
|
|
|
nameLabel->setFont(font);
|
|
|
|
volLabel->setFont(font);
|
2019-07-27 12:47:10 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
slider->setMinimum(0);
|
2019-07-27 12:47:10 +00:00
|
|
|
slider->setMaximum(int(FADER_PRECISION));
|
2018-12-16 16:14:58 +00:00
|
|
|
|
|
|
|
bool muted = obs_source_muted(source);
|
|
|
|
mute->setChecked(muted);
|
|
|
|
mute->setAccessibleName(QTStr("VolControl.Mute").arg(sourceName));
|
2016-02-23 23:16:51 +00:00
|
|
|
obs_fader_add_callback(obs_fader, OBSVolumeChanged, this);
|
|
|
|
obs_volmeter_add_callback(obs_volmeter, OBSVolumeLevel, this);
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
signal_handler_connect(obs_source_get_signal_handler(source), "mute",
|
|
|
|
OBSVolumeMuted, this);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
QWidget::connect(slider, SIGNAL(valueChanged(int)), this,
|
|
|
|
SLOT(SliderChanged(int)));
|
|
|
|
QWidget::connect(mute, SIGNAL(clicked(bool)), this,
|
|
|
|
SLOT(SetMuted(bool)));
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
obs_fader_attach_source(obs_fader, source);
|
|
|
|
obs_volmeter_attach_source(obs_volmeter, source);
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
QString styleName = slider->style()->objectName();
|
|
|
|
QStyle *style;
|
|
|
|
style = QStyleFactory::create(styleName);
|
|
|
|
if (!style) {
|
|
|
|
style = new SliderAbsoluteSetStyle();
|
|
|
|
} else {
|
|
|
|
style = new SliderAbsoluteSetStyle(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
style->setParent(slider);
|
|
|
|
slider->setStyle(style);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
/* Call volume changed once to init the slider position and label */
|
|
|
|
VolumeChanged();
|
|
|
|
}
|
|
|
|
|
2020-03-25 08:07:22 +00:00
|
|
|
void VolControl::EnableSlider(bool enable)
|
|
|
|
{
|
|
|
|
slider->setEnabled(enable);
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
VolControl::~VolControl()
|
|
|
|
{
|
|
|
|
obs_fader_remove_callback(obs_fader, OBSVolumeChanged, this);
|
|
|
|
obs_volmeter_remove_callback(obs_volmeter, OBSVolumeLevel, this);
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
signal_handler_disconnect(obs_source_get_signal_handler(source), "mute",
|
|
|
|
OBSVolumeMuted, this);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
obs_fader_destroy(obs_fader);
|
|
|
|
obs_volmeter_destroy(obs_volmeter);
|
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
QColor VolumeMeter::getBackgroundNominalColor() const
|
|
|
|
{
|
|
|
|
return backgroundNominalColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setBackgroundNominalColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
backgroundNominalColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getBackgroundWarningColor() const
|
|
|
|
{
|
|
|
|
return backgroundWarningColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setBackgroundWarningColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
backgroundWarningColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getBackgroundErrorColor() const
|
|
|
|
{
|
|
|
|
return backgroundErrorColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setBackgroundErrorColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
backgroundErrorColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getForegroundNominalColor() const
|
|
|
|
{
|
|
|
|
return foregroundNominalColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setForegroundNominalColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
foregroundNominalColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getForegroundWarningColor() const
|
|
|
|
{
|
|
|
|
return foregroundWarningColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setForegroundWarningColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
foregroundWarningColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getForegroundErrorColor() const
|
|
|
|
{
|
|
|
|
return foregroundErrorColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setForegroundErrorColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
foregroundErrorColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getClipColor() const
|
|
|
|
{
|
|
|
|
return clipColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setClipColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
clipColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getMagnitudeColor() const
|
|
|
|
{
|
|
|
|
return magnitudeColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setMagnitudeColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
magnitudeColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getMajorTickColor() const
|
|
|
|
{
|
|
|
|
return majorTickColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setMajorTickColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
majorTickColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QColor VolumeMeter::getMinorTickColor() const
|
|
|
|
{
|
|
|
|
return minorTickColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setMinorTickColor(QColor c)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
minorTickColor = std::move(c);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal VolumeMeter::getMinimumLevel() const
|
|
|
|
{
|
|
|
|
return minimumLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setMinimumLevel(qreal v)
|
|
|
|
{
|
|
|
|
minimumLevel = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal VolumeMeter::getWarningLevel() const
|
|
|
|
{
|
|
|
|
return warningLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setWarningLevel(qreal v)
|
|
|
|
{
|
|
|
|
warningLevel = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal VolumeMeter::getErrorLevel() const
|
|
|
|
{
|
|
|
|
return errorLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setErrorLevel(qreal v)
|
|
|
|
{
|
|
|
|
errorLevel = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal VolumeMeter::getClipLevel() const
|
|
|
|
{
|
|
|
|
return clipLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::setClipLevel(qreal v)
|
|
|
|
{
|
|
|
|
clipLevel = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal VolumeMeter::getMinimumInputLevel() const
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
return minimumInputLevel;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
void VolumeMeter::setMinimumInputLevel(qreal v)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
minimumInputLevel = v;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
qreal VolumeMeter::getPeakDecayRate() const
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
return peakDecayRate;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
void VolumeMeter::setPeakDecayRate(qreal v)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
peakDecayRate = v;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
qreal VolumeMeter::getMagnitudeIntegrationTime() const
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
return magnitudeIntegrationTime;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
void VolumeMeter::setMagnitudeIntegrationTime(qreal v)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
magnitudeIntegrationTime = v;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
qreal VolumeMeter::getPeakHoldDuration() const
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
return peakHoldDuration;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
void VolumeMeter::setPeakHoldDuration(qreal v)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
peakHoldDuration = v;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
qreal VolumeMeter::getInputPeakHoldDuration() const
|
|
|
|
{
|
|
|
|
return inputPeakHoldDuration;
|
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
void VolumeMeter::setInputPeakHoldDuration(qreal v)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
inputPeakHoldDuration = v;
|
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
void VolumeMeter::setPeakMeterType(enum obs_peak_meter_type peakMeterType)
|
|
|
|
{
|
|
|
|
obs_volmeter_set_peak_meter_type(obs_volmeter, peakMeterType);
|
|
|
|
switch (peakMeterType) {
|
|
|
|
case TRUE_PEAK_METER:
|
|
|
|
// For true-peak meters EBU has defined the Permitted Maximum,
|
|
|
|
// taking into account the accuracy of the meter and further
|
|
|
|
// processing required by lossy audio compression.
|
|
|
|
//
|
|
|
|
// The alignment level was not specified, but I've adjusted
|
2020-03-25 08:07:22 +00:00
|
|
|
// it compared to a sample-peak meter. Incidentally Youtube
|
2018-12-16 16:14:58 +00:00
|
|
|
// uses this new Alignment Level as the maximum integrated
|
|
|
|
// loudness of a video.
|
|
|
|
//
|
|
|
|
// * Permitted Maximum Level (PML) = -2.0 dBTP
|
|
|
|
// * Alignment Level (AL) = -13 dBTP
|
|
|
|
setErrorLevel(-2.0);
|
|
|
|
setWarningLevel(-13.0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAMPLE_PEAK_METER:
|
|
|
|
default:
|
|
|
|
// For a sample Peak Meter EBU has the following level
|
|
|
|
// definitions, taking into account inaccuracies of this meter:
|
|
|
|
//
|
|
|
|
// * Permitted Maximum Level (PML) = -9.0 dBFS
|
|
|
|
// * Alignment Level (AL) = -20.0 dBFS
|
|
|
|
setErrorLevel(-9.0);
|
|
|
|
setWarningLevel(-20.0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
void VolumeMeter::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
setFocus(Qt::MouseFocusReason);
|
2019-09-22 21:19:10 +00:00
|
|
|
event->accept();
|
2019-07-27 12:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
|
|
|
QApplication::sendEvent(focusProxy(), event);
|
|
|
|
}
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
VolumeMeter::VolumeMeter(QWidget *parent, obs_volmeter_t *obs_volmeter,
|
2019-09-22 21:19:10 +00:00
|
|
|
bool vertical)
|
|
|
|
: QWidget(parent), obs_volmeter(obs_volmeter), vertical(vertical)
|
2018-02-19 19:54:37 +00:00
|
|
|
{
|
2020-10-12 20:28:36 +00:00
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent, true);
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
// Use a font that can be rendered small.
|
|
|
|
tickFont = QFont("Arial");
|
|
|
|
tickFont.setPixelSize(7);
|
|
|
|
// Default meter color settings, they only show if
|
|
|
|
// there is no stylesheet, do not remove.
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundNominalColor.setRgb(0x26, 0x7f, 0x26); // Dark green
|
|
|
|
backgroundWarningColor.setRgb(0x7f, 0x7f, 0x26); // Dark yellow
|
|
|
|
backgroundErrorColor.setRgb(0x7f, 0x26, 0x26); // Dark red
|
|
|
|
foregroundNominalColor.setRgb(0x4c, 0xff, 0x4c); // Bright green
|
|
|
|
foregroundWarningColor.setRgb(0xff, 0xff, 0x4c); // Bright yellow
|
|
|
|
foregroundErrorColor.setRgb(0xff, 0x4c, 0x4c); // Bright red
|
|
|
|
clipColor.setRgb(0xff, 0xff, 0xff); // Bright white
|
|
|
|
magnitudeColor.setRgb(0x00, 0x00, 0x00); // Black
|
|
|
|
majorTickColor.setRgb(0xff, 0xff, 0xff); // Black
|
|
|
|
minorTickColor.setRgb(0xcc, 0xcc, 0xcc); // Black
|
|
|
|
minimumLevel = -60.0; // -60 dB
|
|
|
|
warningLevel = -20.0; // -20 dB
|
|
|
|
errorLevel = -9.0; // -9 dB
|
|
|
|
clipLevel = -0.5; // -0.5 dB
|
|
|
|
minimumInputLevel = -50.0; // -50 dB
|
|
|
|
peakDecayRate = 11.76; // 20 dB / 1.7 sec
|
|
|
|
magnitudeIntegrationTime = 0.3; // 99% in 300 ms
|
|
|
|
peakHoldDuration = 20.0; // 20 seconds
|
|
|
|
inputPeakHoldDuration = 1.0; // 1 second
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
channels = (int)audio_output_get_channels(obs_get_audio());
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
handleChannelCofigurationChange();
|
2017-04-19 19:54:15 +00:00
|
|
|
updateTimerRef = updateTimer.toStrongRef();
|
|
|
|
if (!updateTimerRef) {
|
|
|
|
updateTimerRef = QSharedPointer<VolumeMeterTimer>::create();
|
2020-10-01 20:15:25 +00:00
|
|
|
updateTimerRef->setTimerType(Qt::PreciseTimer);
|
|
|
|
updateTimerRef->start(16);
|
2017-04-19 19:54:15 +00:00
|
|
|
updateTimer = updateTimerRef;
|
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
updateTimerRef->AddVolControl(this);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
VolumeMeter::~VolumeMeter()
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2017-04-19 19:54:15 +00:00
|
|
|
updateTimerRef->RemoveVolControl(this);
|
2018-12-16 16:14:58 +00:00
|
|
|
delete tickPaintCache;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
void VolumeMeter::setLevels(const float magnitude[MAX_AUDIO_CHANNELS],
|
2019-09-22 21:19:10 +00:00
|
|
|
const float peak[MAX_AUDIO_CHANNELS],
|
|
|
|
const float inputPeak[MAX_AUDIO_CHANNELS])
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2017-04-19 19:54:15 +00:00
|
|
|
uint64_t ts = os_gettime_ns();
|
|
|
|
QMutexLocker locker(&dataMutex);
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
currentLastUpdateTime = ts;
|
|
|
|
for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
|
|
|
|
currentMagnitude[channelNr] = magnitude[channelNr];
|
|
|
|
currentPeak[channelNr] = peak[channelNr];
|
|
|
|
currentInputPeak[channelNr] = inputPeak[channelNr];
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case there are more updates then redraws we must make sure
|
|
|
|
// that the ballistics of peak and hold are recalculated.
|
|
|
|
locker.unlock();
|
|
|
|
calculateBallistics(ts);
|
2017-04-19 19:54:15 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
inline void VolumeMeter::resetLevels()
|
|
|
|
{
|
|
|
|
currentLastUpdateTime = 0;
|
|
|
|
for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
|
|
|
|
currentMagnitude[channelNr] = -M_INFINITE;
|
|
|
|
currentPeak[channelNr] = -M_INFINITE;
|
|
|
|
currentInputPeak[channelNr] = -M_INFINITE;
|
|
|
|
|
|
|
|
displayMagnitude[channelNr] = -M_INFINITE;
|
|
|
|
displayPeak[channelNr] = -M_INFINITE;
|
|
|
|
displayPeakHold[channelNr] = -M_INFINITE;
|
|
|
|
displayPeakHoldLastUpdateTime[channelNr] = 0;
|
|
|
|
displayInputPeakHold[channelNr] = -M_INFINITE;
|
|
|
|
displayInputPeakHoldLastUpdateTime[channelNr] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void VolumeMeter::handleChannelCofigurationChange()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&dataMutex);
|
|
|
|
|
|
|
|
int currentNrAudioChannels = obs_volmeter_get_nr_channels(obs_volmeter);
|
|
|
|
if (displayNrAudioChannels != currentNrAudioChannels) {
|
|
|
|
displayNrAudioChannels = currentNrAudioChannels;
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
// Make room for 3 pixels meter, with one pixel between each.
|
|
|
|
// Then 9/13 pixels for ticks and numbers.
|
|
|
|
if (vertical)
|
|
|
|
setMinimumSize(displayNrAudioChannels * 4 + 14, 130);
|
|
|
|
else
|
|
|
|
setMinimumSize(130, displayNrAudioChannels * 4 + 8);
|
2018-02-19 19:54:37 +00:00
|
|
|
|
|
|
|
resetLevels();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool VolumeMeter::detectIdle(uint64_t ts)
|
|
|
|
{
|
2018-12-16 16:14:58 +00:00
|
|
|
double timeSinceLastUpdate = (ts - currentLastUpdateTime) * 0.000000001;
|
2018-02-19 19:54:37 +00:00
|
|
|
if (timeSinceLastUpdate > 0.5) {
|
|
|
|
resetLevels();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
inline void
|
|
|
|
VolumeMeter::calculateBallisticsForChannel(int channelNr, uint64_t ts,
|
|
|
|
qreal timeSinceLastRedraw)
|
2018-02-19 19:54:37 +00:00
|
|
|
{
|
|
|
|
if (currentPeak[channelNr] >= displayPeak[channelNr] ||
|
2019-09-22 21:19:10 +00:00
|
|
|
isnan(displayPeak[channelNr])) {
|
2018-02-19 19:54:37 +00:00
|
|
|
// Attack of peak is immediate.
|
|
|
|
displayPeak[channelNr] = currentPeak[channelNr];
|
|
|
|
} else {
|
|
|
|
// Decay of peak is 40 dB / 1.7 seconds for Fast Profile
|
|
|
|
// 20 dB / 1.7 seconds for Medium Profile (Type I PPM)
|
|
|
|
// 24 dB / 2.8 seconds for Slow Profile (Type II PPM)
|
2018-12-16 16:14:58 +00:00
|
|
|
float decay = float(peakDecayRate * timeSinceLastRedraw);
|
2018-02-19 19:54:37 +00:00
|
|
|
displayPeak[channelNr] = CLAMP(displayPeak[channelNr] - decay,
|
2019-09-22 21:19:10 +00:00
|
|
|
currentPeak[channelNr], 0);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (currentPeak[channelNr] >= displayPeakHold[channelNr] ||
|
2019-09-22 21:19:10 +00:00
|
|
|
!isfinite(displayPeakHold[channelNr])) {
|
2018-02-19 19:54:37 +00:00
|
|
|
// Attack of peak-hold is immediate, but keep track
|
|
|
|
// when it was last updated.
|
|
|
|
displayPeakHold[channelNr] = currentPeak[channelNr];
|
|
|
|
displayPeakHoldLastUpdateTime[channelNr] = ts;
|
|
|
|
} else {
|
|
|
|
// The peak and hold falls back to peak
|
|
|
|
// after 20 seconds.
|
2019-09-22 21:19:10 +00:00
|
|
|
qreal timeSinceLastPeak =
|
|
|
|
(uint64_t)(ts -
|
|
|
|
displayPeakHoldLastUpdateTime[channelNr]) *
|
|
|
|
0.000000001;
|
2018-02-19 19:54:37 +00:00
|
|
|
if (timeSinceLastPeak > peakHoldDuration) {
|
|
|
|
displayPeakHold[channelNr] = currentPeak[channelNr];
|
|
|
|
displayPeakHoldLastUpdateTime[channelNr] = ts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentInputPeak[channelNr] >= displayInputPeakHold[channelNr] ||
|
2019-09-22 21:19:10 +00:00
|
|
|
!isfinite(displayInputPeakHold[channelNr])) {
|
2018-02-19 19:54:37 +00:00
|
|
|
// Attack of peak-hold is immediate, but keep track
|
|
|
|
// when it was last updated.
|
|
|
|
displayInputPeakHold[channelNr] = currentInputPeak[channelNr];
|
|
|
|
displayInputPeakHoldLastUpdateTime[channelNr] = ts;
|
|
|
|
} else {
|
|
|
|
// The peak and hold falls back to peak after 1 second.
|
2019-09-22 21:19:10 +00:00
|
|
|
qreal timeSinceLastPeak =
|
|
|
|
(uint64_t)(
|
|
|
|
ts -
|
|
|
|
displayInputPeakHoldLastUpdateTime[channelNr]) *
|
2018-02-19 19:54:37 +00:00
|
|
|
0.000000001;
|
|
|
|
if (timeSinceLastPeak > inputPeakHoldDuration) {
|
|
|
|
displayInputPeakHold[channelNr] =
|
|
|
|
currentInputPeak[channelNr];
|
2019-09-22 21:19:10 +00:00
|
|
|
displayInputPeakHoldLastUpdateTime[channelNr] = ts;
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isfinite(displayMagnitude[channelNr])) {
|
|
|
|
// The statements in the else-leg do not work with
|
|
|
|
// NaN and infinite displayMagnitude.
|
2018-12-16 16:14:58 +00:00
|
|
|
displayMagnitude[channelNr] = currentMagnitude[channelNr];
|
2018-02-19 19:54:37 +00:00
|
|
|
} else {
|
|
|
|
// A VU meter will integrate to the new value to 99% in 300 ms.
|
|
|
|
// The calculation here is very simplified and is more accurate
|
|
|
|
// with higher frame-rate.
|
2019-09-22 21:19:10 +00:00
|
|
|
float attack =
|
|
|
|
float((currentMagnitude[channelNr] -
|
|
|
|
displayMagnitude[channelNr]) *
|
|
|
|
(timeSinceLastRedraw / magnitudeIntegrationTime) *
|
|
|
|
0.99);
|
|
|
|
displayMagnitude[channelNr] =
|
|
|
|
CLAMP(displayMagnitude[channelNr] + attack,
|
|
|
|
(float)minimumLevel, 0);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void VolumeMeter::calculateBallistics(uint64_t ts,
|
2019-09-22 21:19:10 +00:00
|
|
|
qreal timeSinceLastRedraw)
|
2017-04-19 19:54:15 +00:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&dataMutex);
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++)
|
2018-02-19 19:54:37 +00:00
|
|
|
calculateBallisticsForChannel(channelNr, ts,
|
2019-09-22 21:19:10 +00:00
|
|
|
timeSinceLastRedraw);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
void VolumeMeter::paintInputMeter(QPainter &painter, int x, int y, int width,
|
2019-09-22 21:19:10 +00:00
|
|
|
int height, float peakHold)
|
2018-02-19 19:54:37 +00:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&dataMutex);
|
2018-12-16 16:14:58 +00:00
|
|
|
QColor color;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (peakHold < minimumInputLevel)
|
|
|
|
color = backgroundNominalColor;
|
|
|
|
else if (peakHold < warningLevel)
|
|
|
|
color = foregroundNominalColor;
|
|
|
|
else if (peakHold < errorLevel)
|
|
|
|
color = foregroundWarningColor;
|
|
|
|
else if (peakHold <= clipLevel)
|
|
|
|
color = foregroundErrorColor;
|
|
|
|
else
|
|
|
|
color = clipColor;
|
|
|
|
|
|
|
|
painter.fillRect(x, y, width, height, color);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
void VolumeMeter::paintHTicks(QPainter &painter, int x, int y, int width,
|
2019-09-22 21:19:10 +00:00
|
|
|
int height)
|
2018-02-19 19:54:37 +00:00
|
|
|
{
|
|
|
|
qreal scale = width / minimumLevel;
|
|
|
|
|
|
|
|
painter.setFont(tickFont);
|
|
|
|
painter.setPen(majorTickColor);
|
|
|
|
|
|
|
|
// Draw major tick lines and numeric indicators.
|
2019-09-22 21:19:10 +00:00
|
|
|
for (int i = 0; i >= minimumLevel; i -= 5) {
|
2018-12-16 16:14:58 +00:00
|
|
|
int position = int(x + width - (i * scale) - 1);
|
2018-02-19 19:54:37 +00:00
|
|
|
QString str = QString::number(i);
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (i == 0 || i == -5)
|
2018-02-19 19:54:37 +00:00
|
|
|
painter.drawText(position - 3, height, str);
|
2018-12-16 16:14:58 +00:00
|
|
|
else
|
2018-02-19 19:54:37 +00:00
|
|
|
painter.drawText(position - 5, height, str);
|
|
|
|
painter.drawLine(position, y, position, y + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw minor tick lines.
|
|
|
|
painter.setPen(minorTickColor);
|
|
|
|
for (int i = 0; i >= minimumLevel; i--) {
|
2018-12-16 16:14:58 +00:00
|
|
|
int position = int(x + width - (i * scale) - 1);
|
|
|
|
if (i % 5 != 0)
|
2018-02-19 19:54:37 +00:00
|
|
|
painter.drawLine(position, y, position, y + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
void VolumeMeter::paintVTicks(QPainter &painter, int x, int y, int height)
|
|
|
|
{
|
|
|
|
qreal scale = height / minimumLevel;
|
|
|
|
|
|
|
|
painter.setFont(tickFont);
|
|
|
|
painter.setPen(majorTickColor);
|
|
|
|
|
|
|
|
// Draw major tick lines and numeric indicators.
|
2019-09-22 21:19:10 +00:00
|
|
|
for (int i = 0; i >= minimumLevel; i -= 5) {
|
2018-12-16 16:14:58 +00:00
|
|
|
int position = y + int((i * scale) - 1);
|
|
|
|
QString str = QString::number(i);
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
painter.drawText(x + 5, position + 4, str);
|
|
|
|
else if (i == -60)
|
|
|
|
painter.drawText(x + 4, position, str);
|
|
|
|
else
|
|
|
|
painter.drawText(x + 4, position + 2, str);
|
|
|
|
painter.drawLine(x, position, x + 2, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw minor tick lines.
|
|
|
|
painter.setPen(minorTickColor);
|
|
|
|
for (int i = 0; i >= minimumLevel; i--) {
|
|
|
|
int position = y + int((i * scale) - 1);
|
|
|
|
if (i % 5 != 0)
|
|
|
|
painter.drawLine(x, position, x + 1, position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CLIP_FLASH_DURATION_MS 1000
|
|
|
|
|
|
|
|
void VolumeMeter::ClipEnding()
|
|
|
|
{
|
|
|
|
clipping = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::paintHMeter(QPainter &painter, int x, int y, int width,
|
2019-09-22 21:19:10 +00:00
|
|
|
int height, float magnitude, float peak,
|
|
|
|
float peakHold)
|
2018-02-19 19:54:37 +00:00
|
|
|
{
|
|
|
|
qreal scale = width / minimumLevel;
|
|
|
|
|
|
|
|
QMutexLocker locker(&dataMutex);
|
2019-09-22 21:19:10 +00:00
|
|
|
int minimumPosition = x + 0;
|
|
|
|
int maximumPosition = x + width;
|
|
|
|
int magnitudePosition = int(x + width - (magnitude * scale));
|
|
|
|
int peakPosition = int(x + width - (peak * scale));
|
|
|
|
int peakHoldPosition = int(x + width - (peakHold * scale));
|
|
|
|
int warningPosition = int(x + width - (warningLevel * scale));
|
|
|
|
int errorPosition = int(x + width - (errorLevel * scale));
|
|
|
|
|
|
|
|
int nominalLength = warningPosition - minimumPosition;
|
|
|
|
int warningLength = errorPosition - warningPosition;
|
|
|
|
int errorLength = maximumPosition - errorPosition;
|
2018-02-19 19:54:37 +00:00
|
|
|
locker.unlock();
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (clipping) {
|
|
|
|
peakPosition = maximumPosition;
|
|
|
|
}
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (peakPosition < minimumPosition) {
|
|
|
|
painter.fillRect(minimumPosition, y, nominalLength, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundNominalColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(warningPosition, y, warningLength, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundWarningColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(errorPosition, y, errorLength, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundErrorColor);
|
2018-02-19 19:54:37 +00:00
|
|
|
} else if (peakPosition < warningPosition) {
|
2019-09-22 21:19:10 +00:00
|
|
|
painter.fillRect(minimumPosition, y,
|
|
|
|
peakPosition - minimumPosition, height,
|
|
|
|
foregroundNominalColor);
|
|
|
|
painter.fillRect(peakPosition, y,
|
|
|
|
warningPosition - peakPosition, height,
|
|
|
|
backgroundNominalColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(warningPosition, y, warningLength, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundWarningColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(errorPosition, y, errorLength, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundErrorColor);
|
2018-02-19 19:54:37 +00:00
|
|
|
} else if (peakPosition < errorPosition) {
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(minimumPosition, y, nominalLength, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundNominalColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(warningPosition, y,
|
2019-09-22 21:19:10 +00:00
|
|
|
peakPosition - warningPosition, height,
|
|
|
|
foregroundWarningColor);
|
|
|
|
painter.fillRect(peakPosition, y, errorPosition - peakPosition,
|
|
|
|
height, backgroundWarningColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(errorPosition, y, errorLength, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundErrorColor);
|
2018-02-19 19:54:37 +00:00
|
|
|
} else if (peakPosition < maximumPosition) {
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(minimumPosition, y, nominalLength, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundNominalColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(warningPosition, y, warningLength, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundWarningColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(errorPosition, y, peakPosition - errorPosition,
|
2019-09-22 21:19:10 +00:00
|
|
|
height, foregroundErrorColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(peakPosition, y,
|
2019-09-22 21:19:10 +00:00
|
|
|
maximumPosition - peakPosition, height,
|
|
|
|
backgroundErrorColor);
|
2020-10-01 20:15:25 +00:00
|
|
|
} else if (int(magnitude) != 0) {
|
2018-12-16 16:14:58 +00:00
|
|
|
if (!clipping) {
|
|
|
|
QTimer::singleShot(CLIP_FLASH_DURATION_MS, this,
|
2019-09-22 21:19:10 +00:00
|
|
|
SLOT(ClipEnding()));
|
2018-12-16 16:14:58 +00:00
|
|
|
clipping = true;
|
|
|
|
}
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
int end = errorLength + warningLength + nominalLength;
|
|
|
|
painter.fillRect(minimumPosition, y, end, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
QBrush(foregroundErrorColor));
|
2018-12-16 16:14:58 +00:00
|
|
|
}
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (peakHoldPosition - 3 < minimumPosition)
|
2019-09-22 21:19:10 +00:00
|
|
|
; // Peak-hold below minimum, no drawing.
|
2018-12-16 16:14:58 +00:00
|
|
|
else if (peakHoldPosition < warningPosition)
|
|
|
|
painter.fillRect(peakHoldPosition - 3, y, 3, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundNominalColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
else if (peakHoldPosition < errorPosition)
|
|
|
|
painter.fillRect(peakHoldPosition - 3, y, 3, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundWarningColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
else
|
|
|
|
painter.fillRect(peakHoldPosition - 3, y, 3, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundErrorColor);
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (magnitudePosition - 3 >= minimumPosition)
|
|
|
|
painter.fillRect(magnitudePosition - 3, y, 3, height,
|
2019-09-22 21:19:10 +00:00
|
|
|
magnitudeColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
}
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
void VolumeMeter::paintVMeter(QPainter &painter, int x, int y, int width,
|
2019-09-22 21:19:10 +00:00
|
|
|
int height, float magnitude, float peak,
|
|
|
|
float peakHold)
|
2018-12-16 16:14:58 +00:00
|
|
|
{
|
|
|
|
qreal scale = height / minimumLevel;
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
QMutexLocker locker(&dataMutex);
|
2019-09-22 21:19:10 +00:00
|
|
|
int minimumPosition = y + 0;
|
|
|
|
int maximumPosition = y + height;
|
|
|
|
int magnitudePosition = int(y + height - (magnitude * scale));
|
|
|
|
int peakPosition = int(y + height - (peak * scale));
|
|
|
|
int peakHoldPosition = int(y + height - (peakHold * scale));
|
|
|
|
int warningPosition = int(y + height - (warningLevel * scale));
|
|
|
|
int errorPosition = int(y + height - (errorLevel * scale));
|
|
|
|
|
|
|
|
int nominalLength = warningPosition - minimumPosition;
|
|
|
|
int warningLength = errorPosition - warningPosition;
|
|
|
|
int errorLength = maximumPosition - errorPosition;
|
2018-12-16 16:14:58 +00:00
|
|
|
locker.unlock();
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (clipping) {
|
|
|
|
peakPosition = maximumPosition;
|
|
|
|
}
|
2018-02-19 19:54:37 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (peakPosition < minimumPosition) {
|
|
|
|
painter.fillRect(x, minimumPosition, width, nominalLength,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundNominalColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(x, warningPosition, width, warningLength,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundWarningColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(x, errorPosition, width, errorLength,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundErrorColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
} else if (peakPosition < warningPosition) {
|
2019-09-22 21:19:10 +00:00
|
|
|
painter.fillRect(x, minimumPosition, width,
|
|
|
|
peakPosition - minimumPosition,
|
|
|
|
foregroundNominalColor);
|
|
|
|
painter.fillRect(x, peakPosition, width,
|
|
|
|
warningPosition - peakPosition,
|
|
|
|
backgroundNominalColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(x, warningPosition, width, warningLength,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundWarningColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(x, errorPosition, width, errorLength,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundErrorColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
} else if (peakPosition < errorPosition) {
|
2019-09-22 21:19:10 +00:00
|
|
|
painter.fillRect(x, minimumPosition, width, nominalLength,
|
|
|
|
foregroundNominalColor);
|
|
|
|
painter.fillRect(x, warningPosition, width,
|
|
|
|
peakPosition - warningPosition,
|
|
|
|
foregroundWarningColor);
|
|
|
|
painter.fillRect(x, peakPosition, width,
|
|
|
|
errorPosition - peakPosition,
|
|
|
|
backgroundWarningColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(x, errorPosition, width, errorLength,
|
2019-09-22 21:19:10 +00:00
|
|
|
backgroundErrorColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
} else if (peakPosition < maximumPosition) {
|
|
|
|
painter.fillRect(x, minimumPosition, width, nominalLength,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundNominalColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
painter.fillRect(x, warningPosition, width, warningLength,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundWarningColor);
|
|
|
|
painter.fillRect(x, errorPosition, width,
|
|
|
|
peakPosition - errorPosition,
|
|
|
|
foregroundErrorColor);
|
|
|
|
painter.fillRect(x, peakPosition, width,
|
|
|
|
maximumPosition - peakPosition,
|
|
|
|
backgroundErrorColor);
|
2018-02-19 19:54:37 +00:00
|
|
|
} else {
|
2018-12-16 16:14:58 +00:00
|
|
|
if (!clipping) {
|
|
|
|
QTimer::singleShot(CLIP_FLASH_DURATION_MS, this,
|
2019-09-22 21:19:10 +00:00
|
|
|
SLOT(ClipEnding()));
|
2018-12-16 16:14:58 +00:00
|
|
|
clipping = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int end = errorLength + warningLength + nominalLength;
|
|
|
|
painter.fillRect(x, minimumPosition, width, end,
|
2019-09-22 21:19:10 +00:00
|
|
|
QBrush(foregroundErrorColor));
|
2017-04-19 19:54:15 +00:00
|
|
|
}
|
2018-12-16 16:14:58 +00:00
|
|
|
|
|
|
|
if (peakHoldPosition - 3 < minimumPosition)
|
2019-09-22 21:19:10 +00:00
|
|
|
; // Peak-hold below minimum, no drawing.
|
2018-12-16 16:14:58 +00:00
|
|
|
else if (peakHoldPosition < warningPosition)
|
|
|
|
painter.fillRect(x, peakHoldPosition - 3, width, 3,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundNominalColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
else if (peakHoldPosition < errorPosition)
|
|
|
|
painter.fillRect(x, peakHoldPosition - 3, width, 3,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundWarningColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
else
|
|
|
|
painter.fillRect(x, peakHoldPosition - 3, width, 3,
|
2019-09-22 21:19:10 +00:00
|
|
|
foregroundErrorColor);
|
2018-12-16 16:14:58 +00:00
|
|
|
|
|
|
|
if (magnitudePosition - 3 >= minimumPosition)
|
|
|
|
painter.fillRect(x, magnitudePosition - 3, width, 3,
|
2019-09-22 21:19:10 +00:00
|
|
|
magnitudeColor);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeter::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
2018-02-19 19:54:37 +00:00
|
|
|
uint64_t ts = os_gettime_ns();
|
|
|
|
qreal timeSinceLastRedraw = (ts - lastRedrawTime) * 0.000000001;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
const QRect rect = event->region().boundingRect();
|
2019-09-22 21:19:10 +00:00
|
|
|
int width = rect.width();
|
2018-12-16 16:14:58 +00:00
|
|
|
int height = rect.height();
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
handleChannelCofigurationChange();
|
|
|
|
calculateBallistics(ts, timeSinceLastRedraw);
|
|
|
|
bool idle = detectIdle(ts);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
// Draw the ticks in a off-screen buffer when the widget changes size.
|
2018-12-16 16:14:58 +00:00
|
|
|
QSize tickPaintCacheSize;
|
|
|
|
if (vertical)
|
|
|
|
tickPaintCacheSize = QSize(14, height);
|
|
|
|
else
|
|
|
|
tickPaintCacheSize = QSize(width, 9);
|
|
|
|
if (tickPaintCache == nullptr ||
|
2019-09-22 21:19:10 +00:00
|
|
|
tickPaintCache->size() != tickPaintCacheSize) {
|
2018-02-19 19:54:37 +00:00
|
|
|
delete tickPaintCache;
|
|
|
|
tickPaintCache = new QPixmap(tickPaintCacheSize);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
QColor clearColor(0, 0, 0, 0);
|
|
|
|
tickPaintCache->fill(clearColor);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
QPainter tickPainter(tickPaintCache);
|
2018-12-16 16:14:58 +00:00
|
|
|
if (vertical) {
|
|
|
|
tickPainter.translate(0, height);
|
|
|
|
tickPainter.scale(1, -1);
|
|
|
|
paintVTicks(tickPainter, 0, 11,
|
2019-09-22 21:19:10 +00:00
|
|
|
tickPaintCacheSize.height() - 11);
|
2018-12-16 16:14:58 +00:00
|
|
|
} else {
|
|
|
|
paintHTicks(tickPainter, 6, 0,
|
2019-09-22 21:19:10 +00:00
|
|
|
tickPaintCacheSize.width() - 6,
|
|
|
|
tickPaintCacheSize.height());
|
2018-12-16 16:14:58 +00:00
|
|
|
}
|
2018-02-19 19:54:37 +00:00
|
|
|
tickPainter.end();
|
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
// Actual painting of the widget starts here.
|
|
|
|
QPainter painter(this);
|
2020-10-12 20:28:36 +00:00
|
|
|
|
|
|
|
// Paint window background color (as widget is opaque)
|
|
|
|
QColor background = palette().color(QPalette::ColorRole::Window);
|
|
|
|
painter.fillRect(rect, background);
|
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (vertical) {
|
|
|
|
// Invert the Y axis to ease the math
|
|
|
|
painter.translate(0, height);
|
|
|
|
painter.scale(1, -1);
|
|
|
|
painter.drawPixmap(displayNrAudioChannels * 4 - 1, 7,
|
2019-09-22 21:19:10 +00:00
|
|
|
*tickPaintCache);
|
2018-12-16 16:14:58 +00:00
|
|
|
} else {
|
|
|
|
painter.drawPixmap(0, height - 9, *tickPaintCache);
|
|
|
|
}
|
2018-02-19 19:54:37 +00:00
|
|
|
|
|
|
|
for (int channelNr = 0; channelNr < displayNrAudioChannels;
|
2019-09-22 21:19:10 +00:00
|
|
|
channelNr++) {
|
2019-07-27 12:47:10 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
int channelNrFixed =
|
|
|
|
(displayNrAudioChannels == 1 && channels > 2)
|
|
|
|
? 2
|
|
|
|
: channelNr;
|
2019-07-27 12:47:10 +00:00
|
|
|
|
2018-12-16 16:14:58 +00:00
|
|
|
if (vertical)
|
|
|
|
paintVMeter(painter, channelNr * 4, 8, 3, height - 10,
|
2019-09-22 21:19:10 +00:00
|
|
|
displayMagnitude[channelNrFixed],
|
|
|
|
displayPeak[channelNrFixed],
|
|
|
|
displayPeakHold[channelNrFixed]);
|
2018-12-16 16:14:58 +00:00
|
|
|
else
|
|
|
|
paintHMeter(painter, 5, channelNr * 4, width - 5, 3,
|
2019-09-22 21:19:10 +00:00
|
|
|
displayMagnitude[channelNrFixed],
|
|
|
|
displayPeak[channelNrFixed],
|
|
|
|
displayPeakHold[channelNrFixed]);
|
2018-12-16 16:14:58 +00:00
|
|
|
|
|
|
|
if (idle)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// By not drawing the input meter boxes the user can
|
|
|
|
// see that the audio stream has been stopped, without
|
|
|
|
// having too much visual impact.
|
|
|
|
if (vertical)
|
|
|
|
paintInputMeter(painter, channelNr * 4, 3, 3, 3,
|
2019-07-27 12:47:10 +00:00
|
|
|
displayInputPeakHold[channelNrFixed]);
|
2018-12-16 16:14:58 +00:00
|
|
|
else
|
|
|
|
paintInputMeter(painter, 0, channelNr * 4, 3, 3,
|
2019-07-27 12:47:10 +00:00
|
|
|
displayInputPeakHold[channelNrFixed]);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
lastRedrawTime = ts;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
2017-04-19 19:54:15 +00:00
|
|
|
|
|
|
|
void VolumeMeterTimer::AddVolControl(VolumeMeter *meter)
|
|
|
|
{
|
|
|
|
volumeMeters.push_back(meter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeMeterTimer::RemoveVolControl(VolumeMeter *meter)
|
|
|
|
{
|
|
|
|
volumeMeters.removeOne(meter);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
void VolumeMeterTimer::timerEvent(QTimerEvent *)
|
2017-04-19 19:54:15 +00:00
|
|
|
{
|
|
|
|
for (VolumeMeter *meter : volumeMeters)
|
|
|
|
meter->update();
|
|
|
|
}
|