]> git.sesse.net Git - vlc/blobdiff - modules/audio_filter/audiobargraph_a.c
vout_window: inline vout_window_Control()
[vlc] / modules / audio_filter / audiobargraph_a.c
index b34f6b5b07c3ba1bf110b912ac20fd94fdee5184..81275d6be447ca8116a0bf3ad7165017a40bb26b 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * audiobargraph_a.c : audiobargraph audio plugin for vlc
  *****************************************************************************
- * Copyright (C) 2002-2009 VLC authors and VideoLAN
+ * Copyright (C) 2002-2014 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Clement CHESNIN <clement.chesnin@gmail.com>
@@ -113,16 +113,22 @@ struct filter_sys_t
 static int Open( vlc_object_t *p_this )
 {
     filter_t     *p_filter = (filter_t *)p_this;
-    filter_sys_t *p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
-    if( !p_sys )
+    filter_sys_t *p_sys = p_filter->p_sys = malloc(sizeof(*p_sys));
+    if (!p_sys)
         return VLC_ENOMEM;
 
-    p_sys->bargraph = !!var_CreateGetInteger( p_filter, CFG_PREFIX "bargraph" );
-    p_sys->bargraph_repetition = var_CreateGetInteger( p_filter, CFG_PREFIX "bargraph_repetition" );
-    p_sys->silence = !!var_CreateGetInteger( p_filter, CFG_PREFIX "silence" );
-    p_sys->time_window = var_CreateGetInteger( p_filter, CFG_PREFIX "time_window" ) * 1000;
-    p_sys->alarm_threshold = var_CreateGetFloat( p_filter, CFG_PREFIX "alarm_threshold" );
-    p_sys->repetition_time = var_CreateGetInteger( p_filter, CFG_PREFIX "repetition_time" ) * 1000;
+    static const char *const options[] = {
+        "bargraph", "bargraph_repetition", "silence", "time_window",
+        "alarm_threshold", "repetition_time", NULL
+    };
+    config_ChainParse(p_filter, CFG_PREFIX, options, p_filter->p_cfg);
+
+    p_sys->bargraph = !!var_CreateGetInteger(p_filter, CFG_PREFIX "bargraph");
+    p_sys->bargraph_repetition = var_CreateGetInteger(p_filter, CFG_PREFIX "bargraph_repetition");
+    p_sys->silence = !!var_CreateGetInteger(p_filter, CFG_PREFIX "silence");
+    p_sys->time_window = var_CreateGetInteger(p_filter, CFG_PREFIX "time_window") * 1000;
+    p_sys->alarm_threshold = var_CreateGetFloat(p_filter, CFG_PREFIX "alarm_threshold");
+    p_sys->repetition_time = var_CreateGetInteger(p_filter, CFG_PREFIX "repetition_time") * 1000;
     p_sys->counter = 0;
     p_sys->first = NULL;
     p_sys->last = NULL;
@@ -133,12 +139,30 @@ static int Open( vlc_object_t *p_this )
     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
     p_filter->pf_audio_filter = DoWork;
 
-    var_Create( p_filter->p_libvlc, "audiobargraph_v-alarm", VLC_VAR_BOOL );
-    var_Create( p_filter->p_libvlc, "audiobargraph_v-i_values", VLC_VAR_STRING );
+    var_Create(p_filter->p_libvlc, "audiobargraph_v-alarm", VLC_VAR_BOOL);
+    var_Create(p_filter->p_libvlc, "audiobargraph_v-i_values", VLC_VAR_STRING);
 
     return VLC_SUCCESS;
 }
 
+static void SendValues(filter_t *p_filter, float *value, int nbChannels)
+{
+    char message[256];
+    size_t len = 0;
+
+    for (int i = 0; i < nbChannels; i++) {
+        if (len >= sizeof(message))
+            break;
+        len += snprintf(message + len, sizeof (message),"%f:", value[i]);
+    }
+
+    message[len-1] = '\0';
+    //msg_Dbg(p_filter, "values: %s", message);
+
+    var_SetString(p_filter->p_libvlc, "audiobargraph_v-i_values",
+            message);
+}
+
 /*****************************************************************************
  * DoWork: treat an audio buffer
  ****************************************************************************/
@@ -147,11 +171,8 @@ static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
     filter_sys_t *p_sys = p_filter->p_sys;
     float *p_sample = (float *)p_in_buf->p_buffer;
     float i_value[AOUT_CHAN_MAX];
-    ValueDate_t* current = NULL;
-    float sum;
-    int count = 0;
 
-    int nbChannels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
+    int nbChannels = aout_FormatNbChannels(&p_filter->fmt_in.audio);
 
     for (int i = 0; i < nbChannels; i++)
         i_value[i] = 0.;
@@ -175,31 +196,30 @@ static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
         new->value = max;
         new->date = p_in_buf->i_pts;
         new->next = NULL;
-        if (p_sys->last != NULL) {
+        if (p_sys->last != NULL)
             p_sys->last->next = new;
-        }
         p_sys->last = new;
-        if (p_sys->first == NULL) {
+        if (p_sys->first == NULL)
             p_sys->first = new;
-        }
 
         /* 3 - delete too old values */
-        while (p_sys->first->date < (new->date - p_sys->time_window)) {
+        while (p_sys->first->date < new->date - p_sys->time_window) {
             p_sys->started = 1; // we have enough values to compute a valid total
-            current = p_sys->first;
+            ValueDate_t *current = p_sys->first;
             p_sys->first = p_sys->first->next;
             free(current);
         }
 
         /* If last message was sent enough time ago */
-        if ((p_sys->started) && (p_in_buf->i_pts > p_sys->lastAlarm + p_sys->repetition_time)) {
+        if (p_sys->started && p_in_buf->i_pts > p_sys->lastAlarm + p_sys->repetition_time) {
 
             /* 4 - compute the RMS */
-            current = p_sys->first;
-            sum = 0.0;
+            ValueDate_t *current = p_sys->first;
+            float sum = 0.0;
+            int count = 0;
             while (current != NULL) {
                 sum += current->value;
-                count ++;
+                count++;
                 current = current->next;
             }
             sum /= count;
@@ -213,36 +233,10 @@ static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
         }
     }
 
-    /*for (i=0; i<nbChannels; i++) {
-        value[i] = abs(i_value[i]*100);
-        if ( value[i] > p_sys->value[i] - 6 )
-            p_sys->value[i] = value[i];
-        else
-            p_sys->value[i] = p_sys->value[i] - 6;
-    }*/
-
-    if (p_sys->bargraph) {
-        /* 6 - sent the message with the values for the BarGraph */
-        if ((nbChannels > 0) && (p_sys->counter%(p_sys->bargraph_repetition) == 0)) {
-            char message[256];
-            size_t j = 0;
-
-            for (int i = 0; i < nbChannels; i++) {
-                if (j >= sizeof (message))
-                    break;
-                j += snprintf(message + j, sizeof (message),"%f:", i_value[i]);
-            }
-
-            message[--j] = '\0';
-            msg_Dbg( p_filter, "values: %s", message );
-
-            var_SetString(p_filter->p_libvlc, "audiobargraph_v-i_values",
-                          message);
-        }
-    }
-
-    if (++p_sys->counter > p_sys->bargraph_repetition*100)
+    if (p_sys->bargraph && nbChannels > 0 && p_sys->counter++ > p_sys->bargraph_repetition) {
+        SendValues(p_filter, i_value, nbChannels);
         p_sys->counter = 0;
+    }
 
     return p_in_buf;
 }
@@ -255,13 +249,13 @@ static void Close( vlc_object_t *p_this )
     filter_t * p_filter = (filter_t *)p_this;
     filter_sys_t *p_sys = p_filter->p_sys;
 
-    var_Destroy( p_filter->p_libvlc, "audiobargraph_v-i_values" );
-    var_Destroy( p_filter->p_libvlc, "audiobargraph_v-alarm" );
+    var_Destroy(p_filter->p_libvlc, "audiobargraph_v-i_values");
+    var_Destroy(p_filter->p_libvlc, "audiobargraph_v-alarm");
 
     while (p_sys->first != NULL) {
         ValueDate_t *current = p_sys->first;
         p_sys->first = p_sys->first->next;
         free(current);
     }
-    free( p_sys );
+    free(p_sys);
 }