]> git.sesse.net Git - vlc/blob - modules/audio_filter/chorus_flanger.c
Qt: fix selection of dropdown-menu of playlist view mode
[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;
116
117     if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
118     {
119         msg_Err( p_filter, "input and output formats are not similar" );
120         return VLC_EGENERIC;
121     }
122
123     if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
124         p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
125     {
126         p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
127         p_filter->fmt_out.audio.i_format = VLC_CODEC_FL32;
128         msg_Warn( p_filter, "bad input or output format" );
129     }
130
131     p_filter->pf_audio_filter = DoWork;
132
133     p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
134     if( !p_sys )
135         return VLC_ENOMEM;
136
137     p_sys->i_channels       = aout_FormatNbChannels( &p_filter->fmt_in.audio );
138     p_sys->f_delayTime      = var_CreateGetFloat( p_this, "delay-time" );
139     p_sys->f_sweepDepth     = var_CreateGetFloat( p_this, "sweep-depth" );
140     p_sys->f_sweepRate      = var_CreateGetFloat( p_this, "sweep-rate" );
141     p_sys->f_feedbackGain   = var_CreateGetFloat( p_this, "feedback-gain" );
142     p_sys->f_dryLevel       = var_CreateGetFloat( p_this, "dry-mix" );
143     p_sys->f_wetLevel       = var_CreateGetFloat( p_this, "wet-mix" );
144     var_AddCallback( p_this, "delay-time", paramCallback, p_sys );
145     var_AddCallback( p_this, "sweep-depth", paramCallback, p_sys );
146     var_AddCallback( p_this, "sweep-rate", paramCallback, p_sys );
147     var_AddCallback( p_this, "feedback-gain", paramCallback, p_sys );
148     var_AddCallback( p_this, "dry-mix", paramCallback, p_sys );
149     var_AddCallback( p_this, "wet-mix", paramCallback, p_sys );
150
151     if( p_sys->f_delayTime < 0.0)
152     {
153         msg_Err( p_filter, "Delay Time is invalid" );
154         free(p_sys);
155         return VLC_EGENERIC;
156     }
157
158     if( p_sys->f_sweepDepth > p_sys->f_delayTime || p_sys->f_sweepDepth < 0.0 )
159     {
160         msg_Err( p_filter, "Sweep Depth is invalid" );
161         free( p_sys );
162         return VLC_EGENERIC;
163     }
164
165     if( p_sys->f_sweepRate < 0.0 )
166     {
167         msg_Err( p_filter, "Sweep Rate is invalid" );
168         free( p_sys );
169         return VLC_EGENERIC;
170     }
171
172     /* Max delay = delay + depth. Min = delay - depth */
173     p_sys->i_bufferLength = p_sys->i_channels * ( (int)( ( p_sys->f_delayTime
174                 + p_sys->f_sweepDepth ) * p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
175
176     msg_Dbg( p_filter , "Buffer length:%d, Channels:%d, Sweep Depth:%f, Delay "
177             "time:%f, Sweep Rate:%f, Sample Rate: %d", p_sys->i_bufferLength,
178             p_sys->i_channels, p_sys->f_sweepDepth, p_sys->f_delayTime,
179             p_sys->f_sweepRate, p_filter->fmt_in.audio.i_rate );
180     if( p_sys->i_bufferLength <= 0 )
181     {
182         msg_Err( p_filter, "Delay-time, Sample rate or Channels was incorrect" );
183         free(p_sys);
184         return VLC_EGENERIC;
185     }
186
187     p_sys->p_delayLineStart = calloc( p_sys->i_bufferLength, sizeof( float ) );
188     if( !p_sys->p_delayLineStart )
189     {
190         free( p_sys );
191         return VLC_ENOMEM;
192     }
193
194     p_sys->i_cumulative = 0;
195     p_sys->i_step = p_sys->f_sweepRate > 0 ? 1 : 0;
196     p_sys->f_offset = 0;
197     p_sys->f_temp = 0;
198
199     p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
200     p_sys->p_write = p_sys->p_delayLineStart;
201
202     if( p_sys->f_sweepDepth < small_value() ||
203             p_filter->fmt_in.audio.i_rate < small_value() ) {
204         p_sys->f_sinMultiplier = 0.0;
205     }
206     else {
207         p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
208             ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
209     }
210     p_sys->i_sampleRate = p_filter->fmt_in.audio.i_rate;
211
212     return VLC_SUCCESS;
213 }
214
215 /**
216  * sanitize: Helper function to eliminate small amplitudes
217  * @param f_value pointer to value to clean
218  */
219 static inline void sanitize( float * f_value )
220 {
221     if ( fabs( *f_value ) < small_value() )
222         *f_value = 0.0f;
223 }
224
225
226 /**
227  * DoWork : delays and finds the value of the current frame
228  * @param p_filter This filter object
229  * @param p_in_buf Input buffer
230  * @return Output buffer
231  */
232 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
233 {
234     struct filter_sys_t *p_sys = p_filter->p_sys;
235     int i_chan;
236     unsigned i_samples = p_in_buf->i_nb_samples; /* number of samples */
237     /* maximum number of samples to offset in buffer */
238     int i_maxOffset = floor( p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000 );
239     float *p_out = (float*)p_in_buf->p_buffer;
240     float *p_in =  (float*)p_in_buf->p_buffer;
241
242     float *p_ptr, f_temp = 0;/* f_diff = 0, f_frac = 0;*/
243
244     /* Process each sample */
245     for( unsigned i = 0; i < i_samples ; i++ )
246     {
247         /* Sine function as a oscillator wave to calculate sweep */
248         p_sys->i_cumulative += p_sys->i_step;
249         p_sys->f_offset = sinf( (p_sys->i_cumulative) * p_sys->f_sinMultiplier )
250                 * floorf(p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000);
251         if( abs( p_sys->i_step ) > 0 )
252         {
253             if( p_sys->i_cumulative >=  floor( p_sys->f_sweepDepth *
254                         p_sys->i_sampleRate / p_sys->f_sweepRate ))
255             {
256                 p_sys->f_offset = i_maxOffset;
257                 p_sys->i_step = -1 * ( p_sys->i_step );
258             }
259             if( p_sys->i_cumulative <= floor( -1 * p_sys->f_sweepDepth *
260                         p_sys->i_sampleRate / p_sys->f_sweepRate ) )
261             {
262                 p_sys->f_offset = -i_maxOffset;
263                 p_sys->i_step = -1 * ( p_sys->i_step );
264             }
265         }
266         /* Calculate position in delay */
267         int offset = floor( p_sys->f_offset );
268         p_ptr = p_sys->p_write + ( i_maxOffset - offset ) * p_sys->i_channels;
269
270         /* Handle Overflow */
271         if( p_ptr < p_sys->p_delayLineStart )
272         {
273             p_ptr += p_sys->i_bufferLength - p_sys->i_channels;
274         }
275         if( p_ptr > p_sys->p_delayLineEnd - 2*p_sys->i_channels )
276         {
277             p_ptr -= p_sys->i_bufferLength - p_sys->i_channels;
278         }
279         /* For interpolation */
280 /*        f_frac = ( p_sys->f_offset - (int)p_sys->f_offset );*/
281         for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
282         {
283 /*            if( p_ptr <= p_sys->p_delayLineStart + p_sys->i_channels )
284                 f_diff = *(p_sys->p_delayLineEnd + i_chan) - p_ptr[i_chan];
285             else
286                 f_diff = *( p_ptr - p_sys->i_channels + i_chan )
287                             - p_ptr[i_chan];*/
288             f_temp = ( *( p_ptr + i_chan ) );//+ f_diff * f_frac;
289             /*Linear Interpolation. FIXME. This creates LOTS of noise */
290             sanitize(&f_temp);
291             p_out[i_chan] = p_sys->f_dryLevel * p_in[i_chan] +
292                 p_sys->f_wetLevel * f_temp;
293             *( p_sys->p_write + i_chan ) = p_in[i_chan] +
294                 p_sys->f_feedbackGain * f_temp;
295         }
296         if( p_sys->p_write == p_sys->p_delayLineStart )
297             for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
298                 *( p_sys->p_delayLineEnd - p_sys->i_channels + i_chan )
299                     = *( p_sys->p_delayLineStart + i_chan );
300
301         p_in += p_sys->i_channels;
302         p_out += p_sys->i_channels;
303         p_sys->p_write += p_sys->i_channels;
304         if( p_sys->p_write == p_sys->p_delayLineEnd - p_sys->i_channels )
305         {
306             p_sys->p_write = p_sys->p_delayLineStart;
307         }
308
309     }
310     return p_in_buf;
311 }
312
313 /**
314  * Close: Destructor
315  * @param p_this pointer to this filter object
316  */
317 static void Close( vlc_object_t *p_this )
318 {
319     filter_t *p_filter = ( filter_t* )p_this;
320     filter_sys_t *p_sys = p_filter->p_sys;
321
322     var_DelCallback( p_this, "delay-time", paramCallback, p_sys );
323     var_DelCallback( p_this, "sweep-depth", paramCallback, p_sys );
324     var_DelCallback( p_this, "sweep-rate", paramCallback, p_sys );
325     var_DelCallback( p_this, "feedback-gain", paramCallback, p_sys );
326     var_DelCallback( p_this, "wet-mix", paramCallback, p_sys );
327     var_DelCallback( p_this, "dry-mix", paramCallback, p_sys );
328     var_Destroy( p_this, "delay-time" );
329     var_Destroy( p_this, "sweep-depth" );
330     var_Destroy( p_this, "sweep-rate" );
331     var_Destroy( p_this, "feedback-gain" );
332     var_Destroy( p_this, "wet-mix" );
333     var_Destroy( p_this, "dry-mix" );
334
335     free( p_sys->p_delayLineStart );
336     free( p_sys );
337 }
338
339 /******************************************************************************
340  * Callback to update parameters on the fly
341  ******************************************************************************/
342 static int paramCallback( vlc_object_t *p_this, char const *psz_var,
343                           vlc_value_t oldval, vlc_value_t newval, void *p_data )
344 {
345     filter_t *p_filter = (filter_t *)p_this;
346     filter_sys_t *p_sys = (filter_sys_t *) p_data;
347
348     if( !strncmp( psz_var, "delay-time", 10 ) )
349     {
350         /* if invalid value pretend everything is OK without updating value */
351         if( newval.f_float < 0 )
352             return VLC_SUCCESS;
353         p_sys->f_delayTime = newval.f_float;
354         if( !reallocate_buffer( p_filter, p_sys ) )
355         {
356             p_sys->f_delayTime = oldval.f_float;
357             p_sys->i_bufferLength = p_sys->i_channels * ( (int)
358                             ( ( p_sys->f_delayTime + p_sys->f_sweepDepth ) * 
359                               p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
360         }
361     }
362     else if( !strncmp( psz_var, "sweep-depth", 11 ) )
363     {
364         if( newval.f_float < 0 || newval.f_float > p_sys->f_delayTime)
365             return VLC_SUCCESS;
366         p_sys->f_sweepDepth = newval.f_float;
367         if( !reallocate_buffer( p_filter, p_sys ) )
368         {
369             p_sys->f_sweepDepth = oldval.f_float;
370             p_sys->i_bufferLength = p_sys->i_channels * ( (int)
371                             ( ( p_sys->f_delayTime + p_sys->f_sweepDepth ) * 
372                               p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
373         }
374     }
375     else if( !strncmp( psz_var, "sweep-rate", 10 ) )
376     {
377         if( newval.f_float > p_sys->f_sweepDepth )
378             return VLC_SUCCESS;
379         p_sys->f_sweepRate = newval.f_float;
380         /* Calculate new f_sinMultiplier */
381         if( p_sys->f_sweepDepth < small_value() ||
382                 p_filter->fmt_in.audio.i_rate < small_value() ) {
383             p_sys->f_sinMultiplier = 0.0;
384         }
385         else {
386             p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
387                 ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
388         }
389     }
390     else if( !strncmp( psz_var, "feedback-gain", 13 ) )
391         p_sys->f_feedbackGain = newval.f_float;
392     else if( !strncmp( psz_var, "wet-mix", 7 ) )
393         p_sys->f_wetLevel = newval.f_float;
394     else if( !strncmp( psz_var, "dry-mix", 7 ) )
395         p_sys->f_dryLevel = newval.f_float;
396
397     return VLC_SUCCESS;
398 }
399
400 static int reallocate_buffer( filter_t *p_filter,  filter_sys_t *p_sys )
401 {
402     p_sys->i_bufferLength = p_sys->i_channels * ( (int)( ( p_sys->f_delayTime
403            + p_sys->f_sweepDepth ) * p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
404
405     float *temp = realloc( p_sys->p_delayLineStart, p_sys->i_bufferLength );
406     if( unlikely( !temp ) )
407     {
408         msg_Err( p_filter, "Couldnt reallocate buffer for new delay." );
409         return 0;
410     }
411     free( p_sys->p_delayLineStart );
412     p_sys->p_delayLineStart = temp;
413     p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
414     free( temp );
415     return 1;
416 }