]> git.sesse.net Git - vlc/blob - modules/audio_filter/chorus_flanger.c
c84b43f99e5023e32bd61da09859c64e669a1d67
[vlc] / modules / audio_filter / chorus_flanger.c
1 /*****************************************************************************
2  * chorus_flanger: Basic chorus/flanger/delay audio filter
3  *****************************************************************************
4  * Copyright (C) 2009-12 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Srikanth Raju < srikiraju at gmail dot com >
8  *          Sukrit Sangwan < sukritsangwan at gmail dot com >
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <math.h>
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33
34 #include <vlc_aout.h>
35 #include <vlc_filter.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40
41 static int  Open     ( vlc_object_t * );
42 static void Close    ( vlc_object_t * );
43 static block_t *DoWork( filter_t *, block_t * );
44 static int paramCallback( vlc_object_t *, char const *, vlc_value_t ,
45                           vlc_value_t , void * );
46 static int reallocate_buffer( filter_t *, filter_sys_t * );
47
48 struct filter_sys_t
49 {
50     /* TODO: Cleanup and optimise */
51     int i_cumulative;
52     int i_channels, i_sampleRate;
53     float f_delayTime, f_feedbackGain;  /* delayTime is in milliseconds */
54     float f_wetLevel, f_dryLevel;
55     float f_sweepDepth, f_sweepRate;
56
57     float f_offset;
58     int i_step;
59     float f_temp;
60     float f_sinMultiplier;
61
62     /* This data is for the the circular queue which stores the samples. */
63     int i_bufferLength;
64     float * p_delayLineStart, * p_delayLineEnd;
65     float * p_write;
66 };
67
68 /*****************************************************************************
69  * Module descriptor
70  *****************************************************************************/
71
72
73 vlc_module_begin ()
74     set_description( N_("Sound Delay") )
75     set_shortname( N_("Delay") )
76     set_help( N_("Add a delay effect to the sound") )
77     set_category( CAT_AUDIO )
78     set_subcategory( SUBCAT_AUDIO_AFILTER )
79     add_shortcut( "delay" )
80     add_float( "delay-time", 20, N_("Delay time"),
81         N_("Time in milliseconds of the average delay. Note average"), true )
82     add_float( "sweep-depth", 6, N_("Sweep Depth"),
83         N_("Time in milliseconds of the maximum sweep depth. Thus, the sweep "
84             "range will be delay-time +/- sweep-depth."), true )
85     add_float( "sweep-rate", 6, N_("Sweep Rate"),
86         N_("Rate of change of sweep depth in milliseconds shift per second "
87            "of play"), true )
88     add_float_with_range( "feedback-gain", 0.5, -0.9, 0.9,
89         N_("Feedback Gain"), N_("Gain on Feedback loop"), true )
90     add_float_with_range( "wet-mix", 0.4, -0.999, 0.999,
91         N_("Wet mix"), N_("Level of delayed signal"), true )
92     add_float_with_range( "dry-mix", 0.4, -0.999, 0.999,
93         N_("Dry Mix"), N_("Level of input signal"), true )
94     set_capability( "audio filter", 0 )
95     set_callbacks( Open, Close )
96 vlc_module_end ()
97
98 /**
99  * small_value: Helper function
100  * return high pass cutoff
101  */
102 static inline float small_value()
103 {
104     /* allows for 2^-24, should be enough for 24-bit DACs at least */
105     return ( 1.0 / 16777216.0 );
106 }
107
108 /**
109  * Open: initialize and create stuff
110  * @param p_this
111  */
112 static int Open( vlc_object_t *p_this )
113 {
114     filter_t *p_filter = (filter_t*)p_this;
115     filter_sys_t *p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
116     if( !p_sys )
117         return VLC_ENOMEM;
118
119     p_sys->i_channels       = aout_FormatNbChannels( &p_filter->fmt_in.audio );
120     p_sys->f_delayTime      = var_CreateGetFloat( p_this, "delay-time" );
121     p_sys->f_sweepDepth     = var_CreateGetFloat( p_this, "sweep-depth" );
122     p_sys->f_sweepRate      = var_CreateGetFloat( p_this, "sweep-rate" );
123     p_sys->f_feedbackGain   = var_CreateGetFloat( p_this, "feedback-gain" );
124     p_sys->f_dryLevel       = var_CreateGetFloat( p_this, "dry-mix" );
125     p_sys->f_wetLevel       = var_CreateGetFloat( p_this, "wet-mix" );
126     var_AddCallback( p_this, "delay-time", paramCallback, p_sys );
127     var_AddCallback( p_this, "sweep-depth", paramCallback, p_sys );
128     var_AddCallback( p_this, "sweep-rate", paramCallback, p_sys );
129     var_AddCallback( p_this, "feedback-gain", paramCallback, p_sys );
130     var_AddCallback( p_this, "dry-mix", paramCallback, p_sys );
131     var_AddCallback( p_this, "wet-mix", paramCallback, p_sys );
132
133     if( p_sys->f_delayTime < 0.0)
134     {
135         msg_Err( p_filter, "Delay Time is invalid" );
136         free(p_sys);
137         return VLC_EGENERIC;
138     }
139
140     if( p_sys->f_sweepDepth > p_sys->f_delayTime || p_sys->f_sweepDepth < 0.0 )
141     {
142         msg_Err( p_filter, "Sweep Depth is invalid" );
143         free( p_sys );
144         return VLC_EGENERIC;
145     }
146
147     if( p_sys->f_sweepRate < 0.0 )
148     {
149         msg_Err( p_filter, "Sweep Rate is invalid" );
150         free( p_sys );
151         return VLC_EGENERIC;
152     }
153
154     /* Max delay = delay + depth. Min = delay - depth */
155     p_sys->i_bufferLength = p_sys->i_channels * ( (int)( ( p_sys->f_delayTime
156                 + p_sys->f_sweepDepth ) * p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
157
158     msg_Dbg( p_filter , "Buffer length:%d, Channels:%d, Sweep Depth:%f, Delay "
159             "time:%f, Sweep Rate:%f, Sample Rate: %d", p_sys->i_bufferLength,
160             p_sys->i_channels, p_sys->f_sweepDepth, p_sys->f_delayTime,
161             p_sys->f_sweepRate, p_filter->fmt_in.audio.i_rate );
162     if( p_sys->i_bufferLength <= 0 )
163     {
164         msg_Err( p_filter, "Delay-time, Sample rate or Channels was incorrect" );
165         free(p_sys);
166         return VLC_EGENERIC;
167     }
168
169     p_sys->p_delayLineStart = calloc( p_sys->i_bufferLength, sizeof( float ) );
170     if( !p_sys->p_delayLineStart )
171     {
172         free( p_sys );
173         return VLC_ENOMEM;
174     }
175
176     p_sys->i_cumulative = 0;
177     p_sys->i_step = p_sys->f_sweepRate > 0 ? 1 : 0;
178     p_sys->f_offset = 0;
179     p_sys->f_temp = 0;
180
181     p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
182     p_sys->p_write = p_sys->p_delayLineStart;
183
184     if( p_sys->f_sweepDepth < small_value() ||
185             p_filter->fmt_in.audio.i_rate < small_value() ) {
186         p_sys->f_sinMultiplier = 0.0;
187     }
188     else {
189         p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
190             ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
191     }
192     p_sys->i_sampleRate = p_filter->fmt_in.audio.i_rate;
193
194     p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
195     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
196     p_filter->pf_audio_filter = DoWork;
197
198     return VLC_SUCCESS;
199 }
200
201 /**
202  * sanitize: Helper function to eliminate small amplitudes
203  * @param f_value pointer to value to clean
204  */
205 static inline void sanitize( float * f_value )
206 {
207     if ( fabs( *f_value ) < small_value() )
208         *f_value = 0.0f;
209 }
210
211
212 /**
213  * DoWork : delays and finds the value of the current frame
214  * @param p_filter This filter object
215  * @param p_in_buf Input buffer
216  * @return Output buffer
217  */
218 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
219 {
220     struct filter_sys_t *p_sys = p_filter->p_sys;
221     int i_chan;
222     unsigned i_samples = p_in_buf->i_nb_samples; /* number of samples */
223     /* maximum number of samples to offset in buffer */
224     int i_maxOffset = floor( p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000 );
225     float *p_out = (float*)p_in_buf->p_buffer;
226     float *p_in =  (float*)p_in_buf->p_buffer;
227
228     float *p_ptr, f_temp = 0;/* f_diff = 0, f_frac = 0;*/
229
230     /* Process each sample */
231     for( unsigned i = 0; i < i_samples ; i++ )
232     {
233         /* Sine function as a oscillator wave to calculate sweep */
234         p_sys->i_cumulative += p_sys->i_step;
235         p_sys->f_offset = sinf( (p_sys->i_cumulative) * p_sys->f_sinMultiplier )
236                 * floorf(p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000);
237         if( abs( p_sys->i_step ) > 0 )
238         {
239             if( p_sys->i_cumulative >=  floor( p_sys->f_sweepDepth *
240                         p_sys->i_sampleRate / p_sys->f_sweepRate ))
241             {
242                 p_sys->f_offset = i_maxOffset;
243                 p_sys->i_step = -1 * ( p_sys->i_step );
244             }
245             if( p_sys->i_cumulative <= floor( -1 * p_sys->f_sweepDepth *
246                         p_sys->i_sampleRate / p_sys->f_sweepRate ) )
247             {
248                 p_sys->f_offset = -i_maxOffset;
249                 p_sys->i_step = -1 * ( p_sys->i_step );
250             }
251         }
252         /* Calculate position in delay */
253         int offset = floor( p_sys->f_offset );
254         p_ptr = p_sys->p_write + ( i_maxOffset - offset ) * p_sys->i_channels;
255
256         /* Handle Overflow */
257         if( p_ptr < p_sys->p_delayLineStart )
258         {
259             p_ptr += p_sys->i_bufferLength - p_sys->i_channels;
260         }
261         if( p_ptr > p_sys->p_delayLineEnd - 2*p_sys->i_channels )
262         {
263             p_ptr -= p_sys->i_bufferLength - p_sys->i_channels;
264         }
265         /* For interpolation */
266 /*        f_frac = ( p_sys->f_offset - (int)p_sys->f_offset );*/
267         for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
268         {
269 /*            if( p_ptr <= p_sys->p_delayLineStart + p_sys->i_channels )
270                 f_diff = *(p_sys->p_delayLineEnd + i_chan) - p_ptr[i_chan];
271             else
272                 f_diff = *( p_ptr - p_sys->i_channels + i_chan )
273                             - p_ptr[i_chan];*/
274             f_temp = ( *( p_ptr + i_chan ) );//+ f_diff * f_frac;
275             /*Linear Interpolation. FIXME. This creates LOTS of noise */
276             sanitize(&f_temp);
277             p_out[i_chan] = p_sys->f_dryLevel * p_in[i_chan] +
278                 p_sys->f_wetLevel * f_temp;
279             *( p_sys->p_write + i_chan ) = p_in[i_chan] +
280                 p_sys->f_feedbackGain * f_temp;
281         }
282         if( p_sys->p_write == p_sys->p_delayLineStart )
283             for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
284                 *( p_sys->p_delayLineEnd - p_sys->i_channels + i_chan )
285                     = *( p_sys->p_delayLineStart + i_chan );
286
287         p_in += p_sys->i_channels;
288         p_out += p_sys->i_channels;
289         p_sys->p_write += p_sys->i_channels;
290         if( p_sys->p_write == p_sys->p_delayLineEnd - p_sys->i_channels )
291         {
292             p_sys->p_write = p_sys->p_delayLineStart;
293         }
294
295     }
296     return p_in_buf;
297 }
298
299 /**
300  * Close: Destructor
301  * @param p_this pointer to this filter object
302  */
303 static void Close( vlc_object_t *p_this )
304 {
305     filter_t *p_filter = ( filter_t* )p_this;
306     filter_sys_t *p_sys = p_filter->p_sys;
307
308     var_DelCallback( p_this, "delay-time", paramCallback, p_sys );
309     var_DelCallback( p_this, "sweep-depth", paramCallback, p_sys );
310     var_DelCallback( p_this, "sweep-rate", paramCallback, p_sys );
311     var_DelCallback( p_this, "feedback-gain", paramCallback, p_sys );
312     var_DelCallback( p_this, "wet-mix", paramCallback, p_sys );
313     var_DelCallback( p_this, "dry-mix", paramCallback, p_sys );
314     var_Destroy( p_this, "delay-time" );
315     var_Destroy( p_this, "sweep-depth" );
316     var_Destroy( p_this, "sweep-rate" );
317     var_Destroy( p_this, "feedback-gain" );
318     var_Destroy( p_this, "wet-mix" );
319     var_Destroy( p_this, "dry-mix" );
320
321     free( p_sys->p_delayLineStart );
322     free( p_sys );
323 }
324
325 /******************************************************************************
326  * Callback to update parameters on the fly
327  ******************************************************************************/
328 static int paramCallback( vlc_object_t *p_this, char const *psz_var,
329                           vlc_value_t oldval, vlc_value_t newval, void *p_data )
330 {
331     filter_t *p_filter = (filter_t *)p_this;
332     filter_sys_t *p_sys = (filter_sys_t *) p_data;
333
334     if( !strncmp( psz_var, "delay-time", 10 ) )
335     {
336         /* if invalid value pretend everything is OK without updating value */
337         if( newval.f_float < 0 )
338             return VLC_SUCCESS;
339         p_sys->f_delayTime = newval.f_float;
340         if( !reallocate_buffer( p_filter, p_sys ) )
341         {
342             p_sys->f_delayTime = oldval.f_float;
343             p_sys->i_bufferLength = p_sys->i_channels * ( (int)
344                             ( ( p_sys->f_delayTime + p_sys->f_sweepDepth ) * 
345                               p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
346         }
347     }
348     else if( !strncmp( psz_var, "sweep-depth", 11 ) )
349     {
350         if( newval.f_float < 0 || newval.f_float > p_sys->f_delayTime)
351             return VLC_SUCCESS;
352         p_sys->f_sweepDepth = newval.f_float;
353         if( !reallocate_buffer( p_filter, p_sys ) )
354         {
355             p_sys->f_sweepDepth = oldval.f_float;
356             p_sys->i_bufferLength = p_sys->i_channels * ( (int)
357                             ( ( p_sys->f_delayTime + p_sys->f_sweepDepth ) * 
358                               p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
359         }
360     }
361     else if( !strncmp( psz_var, "sweep-rate", 10 ) )
362     {
363         if( newval.f_float > p_sys->f_sweepDepth )
364             return VLC_SUCCESS;
365         p_sys->f_sweepRate = newval.f_float;
366         /* Calculate new f_sinMultiplier */
367         if( p_sys->f_sweepDepth < small_value() ||
368                 p_filter->fmt_in.audio.i_rate < small_value() ) {
369             p_sys->f_sinMultiplier = 0.0;
370         }
371         else {
372             p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
373                 ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
374         }
375     }
376     else if( !strncmp( psz_var, "feedback-gain", 13 ) )
377         p_sys->f_feedbackGain = newval.f_float;
378     else if( !strncmp( psz_var, "wet-mix", 7 ) )
379         p_sys->f_wetLevel = newval.f_float;
380     else if( !strncmp( psz_var, "dry-mix", 7 ) )
381         p_sys->f_dryLevel = newval.f_float;
382
383     return VLC_SUCCESS;
384 }
385
386 static int reallocate_buffer( filter_t *p_filter,  filter_sys_t *p_sys )
387 {
388     p_sys->i_bufferLength = p_sys->i_channels * ( (int)( ( p_sys->f_delayTime
389            + p_sys->f_sweepDepth ) * p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
390
391     float *temp = realloc( p_sys->p_delayLineStart, p_sys->i_bufferLength );
392     if( unlikely( !temp ) )
393     {
394         msg_Err( p_filter, "Couldnt reallocate buffer for new delay." );
395         return 0;
396     }
397     free( p_sys->p_delayLineStart );
398     p_sys->p_delayLineStart = temp;
399     p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
400     free( temp );
401     return 1;
402 }