input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / audio_filter / chorus_flanger.c
blob9f64ac4b07601167c653853116ab98d244006bae
1 /*****************************************************************************
2 * chorus_flanger: Basic chorus/flanger/delay audio filter
3 *****************************************************************************
4 * Copyright (C) 2009-12 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Srikanth Raju < srikiraju at gmail dot com >
8 * Sukrit Sangwan < sukritsangwan at gmail dot com >
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <math.h>
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_aout.h>
35 #include <vlc_filter.h>
37 /*****************************************************************************
38 * Local prototypes
39 *****************************************************************************/
41 typedef struct filter_sys_t filter_sys_t;
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
45 static block_t *DoWork( filter_t *, block_t * );
46 static int paramCallback( vlc_object_t *, char const *, vlc_value_t ,
47 vlc_value_t , void * );
48 static int reallocate_buffer( filter_t *, filter_sys_t * );
50 struct filter_sys_t
52 /* TODO: Cleanup and optimise */
53 int i_cumulative;
54 int i_channels, i_sampleRate;
55 float f_delayTime, f_feedbackGain; /* delayTime is in milliseconds */
56 float f_wetLevel, f_dryLevel;
57 float f_sweepDepth, f_sweepRate;
59 float f_offset;
60 int i_step;
61 float f_temp;
62 float f_sinMultiplier;
64 /* This data is for the the circular queue which stores the samples. */
65 int i_bufferLength;
66 float * p_delayLineStart, * p_delayLineEnd;
67 float * p_write;
70 /*****************************************************************************
71 * Module descriptor
72 *****************************************************************************/
75 vlc_module_begin ()
76 set_description( N_("Sound Delay") )
77 set_shortname( N_("Delay") )
78 set_help( N_("Add a delay effect to the sound") )
79 set_category( CAT_AUDIO )
80 set_subcategory( SUBCAT_AUDIO_AFILTER )
81 add_shortcut( "delay" )
82 add_float( "delay-time", 20, N_("Delay time"),
83 N_("Time in milliseconds of the average delay. Note average"), true )
84 add_float( "sweep-depth", 6, N_("Sweep Depth"),
85 N_("Time in milliseconds of the maximum sweep depth. Thus, the sweep "
86 "range will be delay-time +/- sweep-depth."), true )
87 add_float( "sweep-rate", 6, N_("Sweep Rate"),
88 N_("Rate of change of sweep depth in milliseconds shift per second "
89 "of play"), true )
90 add_float_with_range( "feedback-gain", 0.5, -0.9, 0.9,
91 N_("Feedback gain"), N_("Gain on Feedback loop"), true )
92 add_float_with_range( "wet-mix", 0.4, -0.999, 0.999,
93 N_("Wet mix"), N_("Level of delayed signal"), true )
94 add_float_with_range( "dry-mix", 0.4, -0.999, 0.999,
95 N_("Dry Mix"), N_("Level of input signal"), true )
96 set_capability( "audio filter", 0 )
97 set_callbacks( Open, Close )
98 vlc_module_end ()
101 * small_value: Helper function
102 * return high pass cutoff
104 static inline float small_value(void)
106 /* allows for 2^-24, should be enough for 24-bit DACs at least */
107 return 1.f / 16777216.f;
111 * Open: initialize and create stuff
112 * @param p_this
114 static int Open( vlc_object_t *p_this )
116 filter_t *p_filter = (filter_t*)p_this;
117 filter_sys_t *p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
118 if( !p_sys )
119 return VLC_ENOMEM;
121 p_sys->i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
122 p_sys->f_delayTime = var_CreateGetFloat( p_this, "delay-time" );
123 p_sys->f_sweepDepth = var_CreateGetFloat( p_this, "sweep-depth" );
124 p_sys->f_sweepRate = var_CreateGetFloat( p_this, "sweep-rate" );
125 p_sys->f_feedbackGain = var_CreateGetFloat( p_this, "feedback-gain" );
126 p_sys->f_dryLevel = var_CreateGetFloat( p_this, "dry-mix" );
127 p_sys->f_wetLevel = var_CreateGetFloat( p_this, "wet-mix" );
128 var_AddCallback( p_this, "delay-time", paramCallback, p_sys );
129 var_AddCallback( p_this, "sweep-depth", paramCallback, p_sys );
130 var_AddCallback( p_this, "sweep-rate", paramCallback, p_sys );
131 var_AddCallback( p_this, "feedback-gain", paramCallback, p_sys );
132 var_AddCallback( p_this, "dry-mix", paramCallback, p_sys );
133 var_AddCallback( p_this, "wet-mix", paramCallback, p_sys );
135 if( p_sys->f_delayTime < 0.f )
137 msg_Err( p_filter, "Delay Time is invalid" );
138 free(p_sys);
139 return VLC_EGENERIC;
142 if( p_sys->f_sweepDepth > p_sys->f_delayTime || p_sys->f_sweepDepth < 0.f )
144 msg_Err( p_filter, "Sweep Depth is invalid" );
145 free( p_sys );
146 return VLC_EGENERIC;
149 if( p_sys->f_sweepRate < 0.f )
151 msg_Err( p_filter, "Sweep Rate is invalid" );
152 free( p_sys );
153 return VLC_EGENERIC;
156 /* Max delay = delay + depth. Min = delay - depth */
157 p_sys->i_bufferLength = p_sys->i_channels * ( (int)( ( p_sys->f_delayTime
158 + p_sys->f_sweepDepth ) * p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
160 msg_Dbg( p_filter , "Buffer length:%d, Channels:%d, Sweep Depth:%f, Delay "
161 "time:%f, Sweep Rate:%f, Sample Rate: %d", p_sys->i_bufferLength,
162 p_sys->i_channels, (double) p_sys->f_sweepDepth,
163 (double) p_sys->f_delayTime, (double) p_sys->f_sweepRate,
164 p_filter->fmt_in.audio.i_rate );
165 if( p_sys->i_bufferLength <= 0 )
167 msg_Err( p_filter, "Delay-time, Sample rate or Channels was incorrect" );
168 free(p_sys);
169 return VLC_EGENERIC;
172 p_sys->p_delayLineStart = calloc( p_sys->i_bufferLength, sizeof( float ) );
173 if( !p_sys->p_delayLineStart )
175 free( p_sys );
176 return VLC_ENOMEM;
179 p_sys->i_cumulative = 0;
180 p_sys->i_step = p_sys->f_sweepRate > 0 ? 1 : 0;
181 p_sys->f_offset = 0;
182 p_sys->f_temp = 0;
184 p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
185 p_sys->p_write = p_sys->p_delayLineStart;
187 if( p_sys->f_sweepDepth < small_value() ||
188 p_filter->fmt_in.audio.i_rate < small_value() ) {
189 p_sys->f_sinMultiplier = 0.f;
191 else {
192 p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
193 ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
195 p_sys->i_sampleRate = p_filter->fmt_in.audio.i_rate;
197 p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
198 aout_FormatPrepare(&p_filter->fmt_in.audio);
199 p_filter->fmt_out.audio = p_filter->fmt_in.audio;
200 p_filter->pf_audio_filter = DoWork;
202 return VLC_SUCCESS;
206 * sanitize: Helper function to eliminate small amplitudes
207 * @param f_value pointer to value to clean
209 static inline void sanitize( float * f_value )
211 if ( fabsf( *f_value ) < small_value() )
212 *f_value = 0.f;
217 * DoWork : delays and finds the value of the current frame
218 * @param p_filter This filter object
219 * @param p_in_buf Input buffer
220 * @return Output buffer
222 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
224 struct filter_sys_t *p_sys = p_filter->p_sys;
225 int i_chan;
226 unsigned i_samples = p_in_buf->i_nb_samples; /* number of samples */
227 /* maximum number of samples to offset in buffer */
228 int i_maxOffset = floorf( p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000 );
229 float *p_out = (float*)p_in_buf->p_buffer;
230 float *p_in = (float*)p_in_buf->p_buffer;
232 float *p_ptr, f_temp = 0;/* f_diff = 0, f_frac = 0;*/
234 /* Process each sample */
235 for( unsigned i = 0; i < i_samples ; i++ )
237 /* Sine function as a oscillator wave to calculate sweep */
238 p_sys->i_cumulative += p_sys->i_step;
239 p_sys->f_offset = sinf( (p_sys->i_cumulative) * p_sys->f_sinMultiplier )
240 * floorf(p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000);
241 if( abs( p_sys->i_step ) > 0 )
243 if( p_sys->i_cumulative >= floorf( p_sys->f_sweepDepth *
244 p_sys->i_sampleRate / p_sys->f_sweepRate ))
246 p_sys->f_offset = i_maxOffset;
247 p_sys->i_step = -1 * ( p_sys->i_step );
249 if( p_sys->i_cumulative <= floorf( -1 * p_sys->f_sweepDepth *
250 p_sys->i_sampleRate / p_sys->f_sweepRate ) )
252 p_sys->f_offset = -i_maxOffset;
253 p_sys->i_step = -1 * ( p_sys->i_step );
256 /* Calculate position in delay */
257 int offset = floorf( p_sys->f_offset );
258 p_ptr = p_sys->p_write + ( i_maxOffset - offset ) * p_sys->i_channels;
260 /* Handle Overflow */
261 if( p_ptr < p_sys->p_delayLineStart )
263 p_ptr += p_sys->i_bufferLength - p_sys->i_channels;
265 if( p_ptr > p_sys->p_delayLineEnd - 2*p_sys->i_channels )
267 p_ptr -= p_sys->i_bufferLength - p_sys->i_channels;
269 /* For interpolation */
270 /* f_frac = ( p_sys->f_offset - (int)p_sys->f_offset );*/
271 for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
273 /* if( p_ptr <= p_sys->p_delayLineStart + p_sys->i_channels )
274 f_diff = *(p_sys->p_delayLineEnd + i_chan) - p_ptr[i_chan];
275 else
276 f_diff = *( p_ptr - p_sys->i_channels + i_chan )
277 - p_ptr[i_chan];*/
278 f_temp = ( *( p_ptr + i_chan ) );//+ f_diff * f_frac;
279 /*Linear Interpolation. FIXME. This creates LOTS of noise */
280 sanitize(&f_temp);
281 p_out[i_chan] = p_sys->f_dryLevel * p_in[i_chan] +
282 p_sys->f_wetLevel * f_temp;
283 *( p_sys->p_write + i_chan ) = p_in[i_chan] +
284 p_sys->f_feedbackGain * f_temp;
286 if( p_sys->p_write == p_sys->p_delayLineStart )
287 for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
288 *( p_sys->p_delayLineEnd - p_sys->i_channels + i_chan )
289 = *( p_sys->p_delayLineStart + i_chan );
291 p_in += p_sys->i_channels;
292 p_out += p_sys->i_channels;
293 p_sys->p_write += p_sys->i_channels;
294 if( p_sys->p_write == p_sys->p_delayLineEnd - p_sys->i_channels )
296 p_sys->p_write = p_sys->p_delayLineStart;
300 return p_in_buf;
304 * Close: Destructor
305 * @param p_this pointer to this filter object
307 static void Close( vlc_object_t *p_this )
309 filter_t *p_filter = ( filter_t* )p_this;
310 filter_sys_t *p_sys = p_filter->p_sys;
312 var_DelCallback( p_this, "delay-time", paramCallback, p_sys );
313 var_DelCallback( p_this, "sweep-depth", paramCallback, p_sys );
314 var_DelCallback( p_this, "sweep-rate", paramCallback, p_sys );
315 var_DelCallback( p_this, "feedback-gain", paramCallback, p_sys );
316 var_DelCallback( p_this, "wet-mix", paramCallback, p_sys );
317 var_DelCallback( p_this, "dry-mix", paramCallback, p_sys );
318 var_Destroy( p_this, "delay-time" );
319 var_Destroy( p_this, "sweep-depth" );
320 var_Destroy( p_this, "sweep-rate" );
321 var_Destroy( p_this, "feedback-gain" );
322 var_Destroy( p_this, "wet-mix" );
323 var_Destroy( p_this, "dry-mix" );
325 free( p_sys->p_delayLineStart );
326 free( p_sys );
329 /******************************************************************************
330 * Callback to update parameters on the fly
331 ******************************************************************************/
332 static int paramCallback( vlc_object_t *p_this, char const *psz_var,
333 vlc_value_t oldval, vlc_value_t newval, void *p_data )
335 filter_t *p_filter = (filter_t *)p_this;
336 filter_sys_t *p_sys = (filter_sys_t *) p_data;
338 if( !strncmp( psz_var, "delay-time", 10 ) )
340 /* if invalid value pretend everything is OK without updating value */
341 if( newval.f_float < 0 )
342 return VLC_SUCCESS;
343 p_sys->f_delayTime = newval.f_float;
344 if( !reallocate_buffer( p_filter, p_sys ) )
346 p_sys->f_delayTime = oldval.f_float;
347 p_sys->i_bufferLength = p_sys->i_channels * ( (int)
348 ( ( p_sys->f_delayTime + p_sys->f_sweepDepth ) *
349 p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
352 else if( !strncmp( psz_var, "sweep-depth", 11 ) )
354 if( newval.f_float < 0 || newval.f_float > p_sys->f_delayTime)
355 return VLC_SUCCESS;
356 p_sys->f_sweepDepth = newval.f_float;
357 if( !reallocate_buffer( p_filter, p_sys ) )
359 p_sys->f_sweepDepth = oldval.f_float;
360 p_sys->i_bufferLength = p_sys->i_channels * ( (int)
361 ( ( p_sys->f_delayTime + p_sys->f_sweepDepth ) *
362 p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
365 else if( !strncmp( psz_var, "sweep-rate", 10 ) )
367 if( newval.f_float > p_sys->f_sweepDepth )
368 return VLC_SUCCESS;
369 p_sys->f_sweepRate = newval.f_float;
370 /* Calculate new f_sinMultiplier */
371 if( p_sys->f_sweepDepth < small_value() ||
372 p_filter->fmt_in.audio.i_rate < small_value() ) {
373 p_sys->f_sinMultiplier = 0.0;
375 else {
376 p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
377 ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
380 else if( !strncmp( psz_var, "feedback-gain", 13 ) )
381 p_sys->f_feedbackGain = newval.f_float;
382 else if( !strncmp( psz_var, "wet-mix", 7 ) )
383 p_sys->f_wetLevel = newval.f_float;
384 else if( !strncmp( psz_var, "dry-mix", 7 ) )
385 p_sys->f_dryLevel = newval.f_float;
387 return VLC_SUCCESS;
390 static int reallocate_buffer( filter_t *p_filter, filter_sys_t *p_sys )
392 p_sys->i_bufferLength = p_sys->i_channels * ( (int)( ( p_sys->f_delayTime
393 + p_sys->f_sweepDepth ) * p_filter->fmt_in.audio.i_rate/1000 ) + 1 );
395 float *temp = realloc( p_sys->p_delayLineStart, p_sys->i_bufferLength );
396 if( unlikely( !temp ) )
398 msg_Err( p_filter, "Couldnt reallocate buffer for new delay." );
399 return 0;
401 p_sys->p_delayLineStart = temp;
402 p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
403 return 1;