Browse Source

libobs: Add update interval property to volmeter

Add a property to the volume meter that specifies the length of the
interval in which the audio data should be sampled before the
audio_levels signal is emitted.
fryshorts 11 years ago
parent
commit
e0ce484b81
2 changed files with 66 additions and 0 deletions
  1. 38 0
      libobs/obs-audio-controls.c
  2. 28 0
      libobs/obs-audio-controls.h

+ 38 - 0
libobs/obs-audio-controls.c

@@ -54,6 +54,10 @@ struct obs_volmeter {
 	obs_source_t           *source;
 	enum obs_fader_type    type;
 	float                  cur_db;
+
+	unsigned int           channels;
+	unsigned int           update_ms;
+	unsigned int           update_frames;
 };
 
 static const char *fader_signals[] = {
@@ -291,6 +295,15 @@ static void volmeter_source_destroyed(void *vptr, calldata_t *calldata)
 	obs_volmeter_detach_source(volmeter);
 }
 
+static void volmeter_update_audio_settings(obs_volmeter_t *volmeter)
+{
+	audio_t *audio            = obs_get_audio();
+	const unsigned int sr     = audio_output_get_sample_rate(audio);
+
+	volmeter->channels        = audio_output_get_channels(audio);
+	volmeter->update_frames   = volmeter->update_ms * sr / 1000;
+}
+
 obs_fader_t *obs_fader_create(enum obs_fader_type type)
 {
 	struct obs_fader *fader = bzalloc(sizeof(struct obs_fader));
@@ -521,6 +534,8 @@ obs_volmeter_t *obs_volmeter_create(enum obs_fader_type type)
 	}
 	volmeter->type = type;
 
+	obs_volmeter_set_update_interval(volmeter, 50);
+
 	return volmeter;
 fail:
 	obs_volmeter_destroy(volmeter);
@@ -597,3 +612,26 @@ signal_handler_t *obs_volmeter_get_signal_handler(obs_volmeter_t *volmeter)
 	return (volmeter) ? volmeter->signals : NULL;
 }
 
+void obs_volmeter_set_update_interval(obs_volmeter_t *volmeter,
+		const unsigned int ms)
+{
+	if (!volmeter || !ms)
+		return;
+
+	pthread_mutex_lock(&volmeter->mutex);
+	volmeter->update_ms = ms;
+	volmeter_update_audio_settings(volmeter);
+	pthread_mutex_unlock(&volmeter->mutex);
+}
+
+unsigned int obs_volmeter_get_update_interval(obs_volmeter_t *volmeter)
+{
+	if (!volmeter)
+		return 0;
+
+	pthread_mutex_lock(&volmeter->mutex);
+	const unsigned int interval = volmeter->update_ms;
+	pthread_mutex_unlock(&volmeter->mutex);
+
+	return interval;
+}

+ 28 - 0
libobs/obs-audio-controls.h

@@ -208,6 +208,34 @@ EXPORT void obs_volmeter_detach_source(obs_volmeter_t *volmeter);
 EXPORT signal_handler_t *obs_volmeter_get_signal_handler(
 		obs_volmeter_t *volmeter);
 
+/**
+ * @brief Set the update interval for the volume meter
+ * @param volmeter pointer to the volume meter object
+ * @param ms update interval in ms
+ *
+ * This sets the update interval in milliseconds that should be processed before
+ * the resulting values are emitted by the levels_updated signal. The resulting
+ * number of audio samples is rounded to an integer.
+ *
+ * Please note that due to way obs does receive audio data from the sources
+ * this is no hard guarantee for the timing of the signal itself. When the
+ * volume meter receives a chunk of data that is multiple the size of the sample
+ * interval, all data will be sampled and the values updated accordingly, but
+ * only the signal for the last segment is actually emitted.
+ * On the other hand data might be received in a way that will cause the signal
+ * to be emitted in shorter intervals than specified here under some
+ * circumstances.
+ */
+EXPORT void obs_volmeter_set_update_interval(obs_volmeter_t *volmeter,
+		const unsigned int ms);
+
+/**
+ * @brief Get the update interval currently used for the volume meter
+ * @param volmeter pointer to the volume meter object
+ * @return update interval in ms
+ */
+EXPORT unsigned int obs_volmeter_get_update_interval(obs_volmeter_t *volmeter);
+
 #ifdef __cplusplus
 }
 #endif