From 014a33380776270754d3d73fadd2f3156ffa0a6f Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sun, 23 Nov 2014 21:34:15 +0100 Subject: [PATCH] obs/libobs: Use new volume meter api This replaces the old code for the audio meter that was using calculations in two different places with the new audio meter api. The source signal will now emit simple levels instead of dB values, in order to avoid dB conversion and calculation in the source. The GUI on the other hand now expects simple position values from the volume meter api with no knowledge about dB calculus either. That way all code that handles those conversions is in one place, with the option to easily add new mappings that can be used everywhere. --- libobs/obs-source.c | 13 ++----------- obs/volume-control.cpp | 42 ++++++++++-------------------------------- obs/volume-control.hpp | 1 + 3 files changed, 13 insertions(+), 43 deletions(-) diff --git a/libobs/obs-source.c b/libobs/obs-source.c index c45dd99fb..37df484f1 100644 --- a/libobs/obs-source.c +++ b/libobs/obs-source.c @@ -627,15 +627,6 @@ static inline void handle_ts_jump(obs_source_t *source, uint64_t expected, reset_audio_timing(source, ts, os_time); } -#define VOL_MIN -96.0f -#define VOL_MAX 0.0f - -static inline float to_db(float val) -{ - float db = 20.0f * log10f(val); - return isfinite(db) ? db : VOL_MIN; -} - static void calc_volume_levels(struct obs_source *source, float *array, size_t frames, float volume) { @@ -665,8 +656,8 @@ static void calc_volume_levels(struct obs_source *source, float *array, UNUSED_PARAMETER(volume); - rms_val = to_db(sqrtf(sum_val / (float)count)); - max_val = to_db(sqrtf(max_val)); + rms_val = sqrtf(sum_val / (float)count); + max_val = sqrtf(max_val); if (max_val > source->vol_max) source->vol_max = max_val; diff --git a/obs/volume-control.cpp b/obs/volume-control.cpp index e14d9f10d..76fb1a5f7 100644 --- a/obs/volume-control.cpp +++ b/obs/volume-control.cpp @@ -12,31 +12,8 @@ using namespace std; -#define VOL_MIN -96.0f -#define VOL_MAX 0.0f - -/* - VOL_MIN_LOG = DBToLog(VOL_MIN) - VOL_MAX_LOG = DBToLog(VOL_MAX) - ... just in case someone wants to use a smaller scale - */ - -#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) { Q_UNUSED(calldata); @@ -73,11 +50,8 @@ void VolControl::VolumeLevel(float mag, float peak, float peakHold) /* only update after a certain amount of time */ if ((curMeterTime - lastMeterTime) > UPDATE_INTERVAL_MS) { - float vol = (float)slider->value() * 0.01f; lastMeterTime = curMeterTime; - volMeter->setLevels(DBToLinear(mag) * vol, - DBToLinear(peak) * vol, - DBToLinear(peakHold) * vol); + volMeter->setLevels(mag, peak, peakHold); } } @@ -103,7 +77,8 @@ VolControl::VolControl(OBSSource source_) lastMeterTime (0), levelTotal (0.0f), levelCount (0.0f), - obs_fader (obs_fader_create(OBS_FADER_CUBIC)) + obs_fader (obs_fader_create(OBS_FADER_CUBIC)), + obs_volmeter (obs_volmeter_create(OBS_FADER_LOG)) { QVBoxLayout *mainLayout = new QVBoxLayout(); QHBoxLayout *textLayout = new QHBoxLayout(); @@ -141,13 +116,15 @@ VolControl::VolControl(OBSSource source_) signal_handler_connect(obs_fader_get_signal_handler(obs_fader), "volume_changed", OBSVolumeChanged, this); - signal_handler_connect(obs_source_get_signal_handler(source), - "volume_level", OBSVolumeLevel, this); + signal_handler_connect(obs_volmeter_get_signal_handler(obs_volmeter), + "levels_updated", OBSVolumeLevel, this); QWidget::connect(slider, SIGNAL(valueChanged(int)), this, SLOT(SliderChanged(int))); obs_fader_attach_source(obs_fader, source); + obs_volmeter_attach_source(obs_volmeter, source); + /* Call volume changed once to init the slider position and label */ VolumeChanged(); } @@ -157,10 +134,11 @@ VolControl::~VolControl() signal_handler_disconnect(obs_fader_get_signal_handler(obs_fader), "volume_changed", OBSVolumeChanged, this); - signal_handler_disconnect(obs_source_get_signal_handler(source), - "volume_level", OBSVolumeLevel, this); + signal_handler_disconnect(obs_volmeter_get_signal_handler(obs_volmeter), + "levels_updated", OBSVolumeLevel, this); obs_fader_destroy(obs_fader); + obs_volmeter_destroy(obs_volmeter); } VolumeMeter::VolumeMeter(QWidget *parent) diff --git a/obs/volume-control.hpp b/obs/volume-control.hpp index 87c03ee57..c43324694 100644 --- a/obs/volume-control.hpp +++ b/obs/volume-control.hpp @@ -36,6 +36,7 @@ private: float levelTotal; float levelCount; obs_fader_t *obs_fader; + obs_volmeter_t *obs_volmeter; static void OBSVolumeChanged(void *param, calldata_t *calldata); static void OBSVolumeLevel(void *data, calldata_t *calldata);