فهرست منبع

UI: Add new volume meter handling

This adds support for the more smooth volume levels that accounts for
both level and magnitude.  Currently, it just averages level and
magnitude, later on a full audio meter control can be made that would
properly utilize level, magnitude, and peak.

Also cleaned up the code a bit and removed some trailing whitespace.
jp9000 11 سال پیش
والد
کامیت
ab1543977f
2فایلهای تغییر یافته به همراه66 افزوده شده و 18 حذف شده
  1. 62 17
      obs/volume-control.cpp
  2. 4 1
      obs/volume-control.hpp

+ 62 - 17
obs/volume-control.cpp

@@ -1,33 +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));
 }
 
-
-/* [Danni] This may be a bit too resource intensive for such a simple 
-		   application. */
-
 void VolControl::OBSVolumeLevel(void *data, calldata_t calldata)
 {
 	VolControl *volControl = static_cast<VolControl*>(data);
-	int v = calldata_int(calldata, "volumelevel");
-		
+	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(int, v));
+		Q_ARG(float, result));
 }
 
 void VolControl::VolumeChanged(int vol)
@@ -37,9 +61,23 @@ void VolControl::VolumeChanged(int vol)
 	signalChanged = true;
 }
 
-void VolControl::VolumeLevel(int vol)
+void VolControl::VolumeLevel(float level)
 {
-	volMeter->setValue(vol); /* linear */
+	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)
@@ -53,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();
@@ -86,10 +128,13 @@ VolControl::VolControl(OBSSource source_)
 	volMeter->setMaximum(10000);
 	volMeter->setTextVisible(false);
 
-	// [Danni] Temporary color.
-	QString testColor = "QProgressBar {border: 0px} QProgressBar::chunk {width: 1px; background-color: #AA0000;}";
+	/* [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);
 	textLayout->addWidget(volLabel);
@@ -108,7 +153,7 @@ VolControl::VolControl(OBSSource source_)
 			"volume", OBSVolumeChanged, this);
 
 	signal_handler_connect(obs_source_signalhandler(source),
-		"volumelevel", OBSVolumeLevel, this);
+		"volume_level", OBSVolumeLevel, this);
 
 	QWidget::connect(slider, SIGNAL(valueChanged(int)),
 			this, SLOT(SliderChanged(int)));
@@ -120,5 +165,5 @@ VolControl::~VolControl()
 			"volume", OBSVolumeChanged, this);
 
 	signal_handler_disconnect(obs_source_signalhandler(source),
-		"volumelevel", OBSVolumeLevel, this);
+		"volume_level", OBSVolumeLevel, this);
 }

+ 4 - 1
obs/volume-control.hpp

@@ -19,13 +19,16 @@ private:
 	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(int vol);
+	void VolumeLevel(float level);
 	void SliderChanged(int vol);
 
 public: