[vlc-devel] [PATCH] chorus_flanger.c : bit of cleanup

Sukrit Sangwan sukritsangwan at gmail.com
Wed Mar 14 02:16:36 CET 2012


I have fixed some issues(sustained noise of drum beats) and some TODOs and
added callback.
I have attached the patch
*>>> Inline explanations*


diff --git a/modules/audio_filter/chorus_flanger.c
b/modules/audio_filter/chorus_flanger.c
index efeade6..5f82f23 100644
--- a/modules/audio_filter/chorus_flanger.c
+++ b/modules/audio_filter/chorus_flanger.c
@@ -5,6 +5,7 @@
  * $Id$
  *
  * Author: Srikanth Raju < srikiraju at gmail dot com >
+ * Modified : Sukrit Sangwan < sukritsangwan at gmail dot com >
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -43,10 +44,11 @@
 /*****************************************************************************
  * Local prototypes

*****************************************************************************/
-
 static int  Open     ( vlc_object_t * );
 static void Close    ( vlc_object_t * );
 static block_t *DoWork( filter_t *, block_t * );
+static int paramCallback( vlc_object_t *, char const *, vlc_value_t ,
+                          vlc_value_t , void * );

 struct filter_sys_t
 {
@@ -57,8 +59,8 @@ struct filter_sys_t
     float f_wetLevel, f_dryLevel;
     float f_sweepDepth, f_sweepRate;

-    float f_step,f_offset;
-    int i_step,i_offset;
+    float f_offset;
+    int i_step;
     float f_temp;
     float f_sinMultiplier;

@@ -66,13 +68,15 @@ struct filter_sys_t
     int i_bufferLength;
     float * pf_delayLineStart, * pf_delayLineEnd;
     float * pf_write;
+
+    /* Used if callback fails to allocate buffer, *
+     * in that case dont free the buffer twice    */
+    bool b_free_buf;
 };

 /*****************************************************************************
  * Module descriptor

*****************************************************************************/
-
-
 vlc_module_begin ()
     set_description( N_("Sound Delay") )
     set_shortname( N_("Delay") )
@@ -80,7 +84,7 @@ vlc_module_begin ()
     set_category( CAT_AUDIO )
     set_subcategory( SUBCAT_AUDIO_AFILTER )
     add_shortcut( "delay" )
-    add_float( "delay-time", 40, N_("Delay time"),
+    add_float( "delay-time", 20, N_("Delay time"),
*>>> 20 ms avg delay is better for the effect, so default value changed*
         N_("Time in milliseconds of the average delay. Note average"),
true )
     add_float( "sweep-depth", 6, N_("Sweep Depth"),
         N_("Time in milliseconds of the maximum sweep depth. Thus, the
sweep "
@@ -138,12 +142,16 @@ static int Open( vlc_object_t *p_this )
         return VLC_ENOMEM;

     p_sys->i_channels       = aout_FormatNbChannels(
&p_filter->fmt_in.audio );
-    p_sys->f_delayTime      = var_CreateGetFloat( p_this, "delay-time" );
-    p_sys->f_sweepDepth     = var_CreateGetFloat( p_this, "sweep-depth" );
-    p_sys->f_sweepRate      = var_CreateGetFloat( p_this, "sweep-rate" );
-    p_sys->f_feedbackGain   = var_CreateGetFloat( p_this, "feedback-gain"
);
-    p_sys->f_dryLevel       = var_CreateGetFloat( p_this, "dry-mix" );
-    p_sys->f_wetLevel       = var_CreateGetFloat( p_this, "wet-mix" );
+
+#define CREATE_VAR( stor, var ) \
+        p_sys->stor = var_CreateGetFloat( p_filter, var ); \
+        var_AddCallback( p_filter, var, paramCallback, p_sys );
+    CREATE_VAR( f_delayTime,    "delay-time" );
+    CREATE_VAR( f_sweepDepth,   "sweep-depth" );
+    CREATE_VAR( f_sweepRate,    "sweep-rate" );
+    CREATE_VAR( f_feedbackGain, "feedback-gain" );
+    CREATE_VAR( f_dryLevel,     "dry-mix" );
+    CREATE_VAR( f_wetLevel,     "wet-mix" );

     if( p_sys->f_delayTime < 0.0)
     {
@@ -176,7 +184,7 @@ static int Open( vlc_object_t *p_this )
             p_sys->f_sweepRate, p_filter->fmt_in.audio.i_rate );
     if( p_sys->i_bufferLength <= 0 )
     {
-        msg_Err( p_filter, "Delay-time, Sampl rate or Channels was
incorrect" );
+        msg_Err( p_filter, "Delay-time, Sample rate or Channels was
incorrect" );
         free(p_sys);
         return VLC_EGENERIC;
     }
@@ -187,12 +195,11 @@ static int Open( vlc_object_t *p_this )
         free( p_sys );
         return VLC_ENOMEM;
     }
+    p_sys->b_free_buf = true;

     p_sys->i_cumulative = 0;
-    p_sys->f_step = p_sys->f_sweepRate / 1000.0;
     p_sys->i_step = p_sys->f_sweepRate > 0 ? 1 : 0;
     p_sys->f_offset = 0;
-    p_sys->i_offset = 0;
*>>> Drop unused variables*
     p_sys->f_temp = 0;

     p_sys->pf_delayLineEnd = p_sys->pf_delayLineStart +
p_sys->i_bufferLength;
@@ -211,7 +218,6 @@ static int Open( vlc_object_t *p_this )
     return VLC_SUCCESS;
 }

-
 /**
  * sanitize: Helper function to eliminate small amplitudes
  * @param f_value pointer to value to clean
@@ -244,23 +250,19 @@ static block_t *DoWork( filter_t *p_filter, block_t
*p_in_buf )
     /* Process each sample */
     for( unsigned i = 0; i < i_samples ; i++ )
     {
-        /* Use a sine function as a oscillator wave. TODO */
-        /* f_offset = sinf( ( p_sys->i_cumulative ) *
p_sys->f_sinMultiplier ) *
-         * (int)floor(p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000);
-         */
-
-        /* Triangle oscillator. Step using ints, because floats give
rounding */
-        p_sys->i_offset+=p_sys->i_step;
-        p_sys->f_offset = p_sys->i_offset * p_sys->f_step;
+        /* Sine function as a oscillator wave to calculate sweep */
+        p_sys->i_cumulative += p_sys->i_step;
+        p_sys->f_offset = sinf( (p_sys->i_cumulative) *
p_sys->f_sinMultiplier )
+                * (int)floor(p_sys->f_sweepDepth * p_sys->i_sampleRate /
1000);
         if( abs( p_sys->i_step ) > 0 )
         {
-            if( p_sys->i_offset >=  floor( p_sys->f_sweepDepth *
+            if( p_sys->i_cumulative >=  floor( p_sys->f_sweepDepth *
                         p_sys->i_sampleRate / p_sys->f_sweepRate ))
             {
                 p_sys->f_offset = i_maxOffset;
                 p_sys->i_step = -1 * ( p_sys->i_step );
             }
-            if( p_sys->i_offset <= floor( -1 * p_sys->f_sweepDepth *
+            if( p_sys->i_cumulative <= floor( -1 * p_sys->f_sweepDepth *
                         p_sys->i_sampleRate / p_sys->f_sweepRate ) )
             {
                 p_sys->f_offset = -i_maxOffset;
@@ -269,8 +271,7 @@ static block_t *DoWork( filter_t *p_filter, block_t
*p_in_buf )
         }
         /* Calculate position in delay */
         int offset = floor( p_sys->f_offset );
-        pf_ptr = p_sys->pf_write + i_maxOffset * p_sys->i_channels +
-            offset * p_sys->i_channels;
+        pf_ptr = p_sys->pf_write + ( i_maxOffset - offset ) *
p_sys->i_channels;
*>>> although i_maxOffset +/- offset produce same effect, i changed to "-"
so that it seems what it really is
>>> (for more delay the pf_ptr should point more backwards in the buffer)
>>> +/- produce same effect because its value oscillates between +max and
-max*

         /* Handle Overflow */
         if( pf_ptr < p_sys->pf_delayLineStart )
@@ -285,15 +286,18 @@ static block_t *DoWork( filter_t *p_filter, block_t
*p_in_buf )
         f_frac = ( p_sys->f_offset - (int)p_sys->f_offset );
         for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
         {
-            f_diff =  *( pf_ptr + p_sys->i_channels + i_chan )
-                        - *( pf_ptr + i_chan );
-            f_temp = ( *( pf_ptr + i_chan ) );//+ f_diff * f_frac);
-            /*Linear Interpolation. FIXME. This creates LOTS of noise */
*>>> i changed pf_ptr + i_channels to pf_ptr - i_channels because to access
previous sample you must point backwards*
+            if( pf_ptr <= p_sys->pf_delayLineStart + p_sys->i_channels )
+                f_diff = *(p_sys->pf_delayLineEnd + i_chan) - *(pf_ptr +
i_chan);
+            else
+                f_diff = *( pf_ptr - p_sys->i_channels + i_chan )
+                            - *( pf_ptr + i_chan );
*>>> although it was written to fix linear interpolation, i have still used
it because it wasn't the cause of noise.*
+            /* Linear Interpolation. */
+            f_temp = *( pf_ptr + i_chan ) + f_diff * f_frac;
             sanitize(&f_temp);
+            *( p_sys->pf_write + i_chan ) = p_in[i_chan] +
+                p_sys->f_feedbackGain * *(pf_ptr + i_chan);
             p_out[i_chan] = p_sys->f_dryLevel * p_in[i_chan] +
                 p_sys->f_wetLevel * f_temp;
*>>> The real cause of noise. copying the current sample after modifying
it. i have copied it first and then modified*
-            *( p_sys->pf_write + i_chan ) = p_in[i_chan] +
-                p_sys->f_feedbackGain * f_temp;
         }
         if( p_sys->pf_write == p_sys->pf_delayLineStart )
             for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
@@ -320,7 +324,83 @@ 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;
+#define DEL_VAR( var ) \
+        var_DelCallback( p_filter, var, paramCallback, p_sys ); \
+        var_Destroy( p_filter, var );
+    DEL_VAR( "delay-time" );
+    DEL_VAR( "sweep-depth" );
+    DEL_VAR( "sweep-rate" );
+    DEL_VAR( "feedback-gain" );
+    DEL_VAR( "wet-mix" );
+    DEL_VAR( "dry-mix" );
+
+    if( p_sys->b_free_buf == true )
+        free( p_sys->pf_delayLineStart );
+    free( p_sys );
+}
+
+/******************************************************************************
+ * Callback to update parameters on the fly
+
******************************************************************************/
+static int paramCallback( vlc_object_t *p_this, char const *psz_var,
+                          vlc_value_t oldval, vlc_value_t newval, void
*p_data )
+{
+    VLC_UNUSED(oldval);
+    filter_t *p_filter = (filter_t *)p_this;
+    filter_sys_t *p_sys = (filter_sys_t *) p_data;
+
+    if( !strcmp( psz_var, "delay-time" ) )
+    {
+        /* if invalid value pretend everything is OK without updating
value */
+        if( newval.f_float < 0 )
+            return VLC_SUCCESS;
+        p_sys->f_delayTime = newval.f_float;
+        goto allocate_new_buffer;
+    }
+    else if( !strcmp( psz_var, "sweep-depth" ) )
+    {
+        if( newval.f_float < 0 || newval.f_float > p_sys->f_delayTime)
+            return VLC_SUCCESS;
+        p_sys->f_sweepDepth = newval.f_float;
+        goto allocate_new_buffer;
+    }
+    else if( !strcmp( psz_var, "sweep-rate" ) )
+    {
+        if( newval.f_float > p_sys->f_sweepDepth )
+            return VLC_SUCCESS;
+        p_sys->f_sweepRate = newval.f_float;
+        /* Calculate new f_sinMultiplier */
+        if( p_sys->f_sweepDepth < small_value() ||
+                p_filter->fmt_in.audio.i_rate < small_value() ) {
+            p_sys->f_sinMultiplier = 0.0;
+        }
+        else {
+            p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
+                ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate
) ;
+        }
+    }
+    else if( !strcmp( psz_var, "feedback-gain" ) )
+        p_sys->f_feedbackGain = newval.f_float;
+    else if( !strcmp( psz_var, "wet-mix" ) )
+        p_sys->f_wetLevel = newval.f_float;
+    else if( !strcmp( psz_var, "dry-mix" ) )
+        p_sys->f_dryLevel = newval.f_float;
+
+    return VLC_SUCCESS;
+
+allocate_new_buffer:
+    p_sys->i_bufferLength = p_sys->i_channels * ( (int)( (
p_sys->f_delayTime
+                + p_sys->f_sweepDepth ) *
p_filter->fmt_in.audio.i_rate/1000 ) + 1 );

     free( p_sys->pf_delayLineStart );
-    free( p_sys );
+    p_sys->pf_delayLineStart = calloc( p_sys->i_bufferLength, sizeof(
float ) );
+    if( !p_sys->pf_delayLineStart )
+    {
+        p_sys->b_free_buf = false;
+        msg_Dbg( p_filter, "Couldnt allocate buffer for delay" );
+        Close( p_this );
+    }
+    p_sys->pf_delayLineEnd = p_sys->pf_delayLineStart +
p_sys->i_bufferLength;
+
+    return VLC_SUCCESS;
 }

*>>> I have also added a small patch for stereo_widen.c
>>> i had earlier provided a long description which doesnt fit in the
preferences box
>>> (box becomes very large to accomodate full description in a single line)
>>> i changed a description into help and a added a small description*
diff --git a/modules/audio_filter/stereo_widen.c
b/modules/audio_filter/stereo_widen.c
index 5aa97f2..974bd45 100644
--- a/modules/audio_filter/stereo_widen.c
+++ b/modules/audio_filter/stereo_widen.c
@@ -52,7 +52,7 @@ struct filter_sys_t
                          * allocate buffer, then dont free it twice */
 };

-#define DESCRIPTION N_("This filter enhances stereo effect by \
+#define HELP_TEXT N_("This filter enhances stereo effect by \
             suppressing mono,i.e. signal common to both channels, \
             and by delaying the signal of left into right and vice versa \
             thereby widening stereo effect")
@@ -75,7 +75,8 @@ struct filter_sys_t

*****************************************************************************/
 vlc_module_begin ()
     set_shortname( N_("stereo_widen") )
-    set_description( DESCRIPTION )
+    set_description( N_("Stereo Enhancer") )
+    set_help( HELP_TEXT )
     set_category( CAT_AUDIO )
     set_subcategory( SUBCAT_AUDIO_AFILTER )
     set_capability( "audio filter", 0 )


-- 
Regards
Sukrit Sangwan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20120313/85bc36b9/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: improve chorus_flanger effect.patch
Type: application/octet-stream
Size: 11974 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20120313/85bc36b9/attachment.obj>


More information about the vlc-devel mailing list