Merge pull request #90 from antihax/master
Added simple volume meter for reference of input levels.
This commit is contained in:
@@ -1,20 +1,57 @@
|
||||
#include "volume-control.hpp"
|
||||
#include "qt-wrappers.hpp"
|
||||
#include <util/platform.h>
|
||||
#include <QHBoxLayout>
|
||||
#include <QVBoxLayout>
|
||||
#include <QSlider>
|
||||
#include <QLabel>
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define VOL_MIN -96.0f
|
||||
#define VOL_MAX 0.0f
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
QMetaObject::invokeMethod(volControl, "VolumeChanged",
|
||||
Q_ARG(int, vol));
|
||||
QMetaObject::invokeMethod(volControl, "VolumeChanged", Q_ARG(int, vol));
|
||||
}
|
||||
|
||||
void VolControl::OBSVolumeLevel(void *data, calldata_t calldata)
|
||||
{
|
||||
VolControl *volControl = static_cast<VolControl*>(data);
|
||||
float level = calldata_float(calldata, "level");
|
||||
float mag = calldata_float(calldata, "magnitude");
|
||||
|
||||
/*
|
||||
* TODO: an actual volume control that can process level, mag, peak.
|
||||
*
|
||||
* for the time being, just average level and magnitude.
|
||||
*/
|
||||
float result = (level + mag) * 0.5f;
|
||||
|
||||
QMetaObject::invokeMethod(volControl, "VolumeLevel",
|
||||
Q_ARG(float, result));
|
||||
}
|
||||
|
||||
void VolControl::VolumeChanged(int vol)
|
||||
@@ -24,6 +61,25 @@ void VolControl::VolumeChanged(int vol)
|
||||
signalChanged = true;
|
||||
}
|
||||
|
||||
void VolControl::VolumeLevel(float level)
|
||||
{
|
||||
uint64_t curMeterTime = os_gettime_ns() / 1000000;
|
||||
|
||||
levelTotal += level;
|
||||
levelCount += 1.0f;
|
||||
|
||||
/* only update after a certain amount of time */
|
||||
if ((curMeterTime - lastMeterTime) > UPDATE_INTERVAL_MS) {
|
||||
lastMeterTime = curMeterTime;
|
||||
|
||||
float finalLevel = levelTotal / levelCount;
|
||||
volMeter->setValue(int(DBToLinear(finalLevel) * 10000.0f));
|
||||
|
||||
levelTotal = 0.0f;
|
||||
levelCount = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void VolControl::SliderChanged(int vol)
|
||||
{
|
||||
if (signalChanged) {
|
||||
@@ -35,12 +91,16 @@ void VolControl::SliderChanged(int vol)
|
||||
signal_handler_connect(obs_source_signalhandler(source),
|
||||
"volume", OBSVolumeChanged, this);
|
||||
}
|
||||
|
||||
volLabel->setText(QString::number(vol));
|
||||
}
|
||||
|
||||
VolControl::VolControl(OBSSource source_)
|
||||
: source (source_),
|
||||
signalChanged (true)
|
||||
signalChanged (true),
|
||||
lastMeterTime (0),
|
||||
levelTotal (0.0f),
|
||||
levelCount (0.0f)
|
||||
{
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout();
|
||||
QHBoxLayout *textLayout = new QHBoxLayout();
|
||||
@@ -48,6 +108,7 @@ VolControl::VolControl(OBSSource source_)
|
||||
|
||||
nameLabel = new QLabel();
|
||||
volLabel = new QLabel();
|
||||
volMeter = new QProgressBar();
|
||||
slider = new QSlider(Qt::Horizontal);
|
||||
|
||||
QFont font = nameLabel->font();
|
||||
@@ -60,7 +121,19 @@ VolControl::VolControl(OBSSource source_)
|
||||
slider->setMinimum(0);
|
||||
slider->setMaximum(100);
|
||||
slider->setValue(vol);
|
||||
//slider->setMaximumHeight(16);
|
||||
// slider->setMaximumHeight(13);
|
||||
|
||||
volMeter->setMaximumHeight(1);
|
||||
volMeter->setMinimum(0);
|
||||
volMeter->setMaximum(10000);
|
||||
volMeter->setTextVisible(false);
|
||||
|
||||
/* [Danni] Temporary color. */
|
||||
QString testColor = "QProgressBar "
|
||||
"{border: 0px} "
|
||||
"QProgressBar::chunk "
|
||||
"{width: 1px; background-color: #AA0000;}";
|
||||
volMeter->setStyleSheet(testColor);
|
||||
|
||||
textLayout->setContentsMargins(0, 0, 0, 0);
|
||||
textLayout->addWidget(nameLabel);
|
||||
@@ -71,6 +144,7 @@ VolControl::VolControl(OBSSource source_)
|
||||
mainLayout->setContentsMargins(4, 4, 4, 4);
|
||||
mainLayout->setSpacing(2);
|
||||
mainLayout->addItem(textLayout);
|
||||
mainLayout->addWidget(volMeter);
|
||||
mainLayout->addWidget(slider);
|
||||
|
||||
setLayout(mainLayout);
|
||||
@@ -78,6 +152,9 @@ VolControl::VolControl(OBSSource source_)
|
||||
signal_handler_connect(obs_source_signalhandler(source),
|
||||
"volume", OBSVolumeChanged, this);
|
||||
|
||||
signal_handler_connect(obs_source_signalhandler(source),
|
||||
"volume_level", OBSVolumeLevel, this);
|
||||
|
||||
QWidget::connect(slider, SIGNAL(valueChanged(int)),
|
||||
this, SLOT(SliderChanged(int)));
|
||||
}
|
||||
@@ -86,4 +163,7 @@ VolControl::~VolControl()
|
||||
{
|
||||
signal_handler_disconnect(obs_source_signalhandler(source),
|
||||
"volume", OBSVolumeChanged, this);
|
||||
|
||||
signal_handler_disconnect(obs_source_signalhandler(source),
|
||||
"volume_level", OBSVolumeLevel, this);
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <obs.hpp>
|
||||
#include <QWidget>
|
||||
#include <QProgressBar>
|
||||
|
||||
/* TODO: Make a real volume control that isn't terrible */
|
||||
|
||||
@@ -13,15 +14,21 @@ class VolControl : public QWidget {
|
||||
|
||||
private:
|
||||
OBSSource source;
|
||||
QLabel *nameLabel;
|
||||
QLabel *volLabel;
|
||||
QSlider *slider;
|
||||
bool signalChanged;
|
||||
QLabel *nameLabel;
|
||||
QLabel *volLabel;
|
||||
QProgressBar *volMeter;
|
||||
QSlider *slider;
|
||||
bool signalChanged;
|
||||
uint64_t lastMeterTime;
|
||||
float levelTotal;
|
||||
float levelCount;
|
||||
|
||||
static void OBSVolumeChanged(void *param, calldata_t calldata);
|
||||
static void OBSVolumeLevel(void *data, calldata_t calldata);
|
||||
|
||||
private slots:
|
||||
void VolumeChanged(int vol);
|
||||
void VolumeLevel(float level);
|
||||
void SliderChanged(int vol);
|
||||
|
||||
public:
|
||||
|
Reference in New Issue
Block a user