1 /*****************************************************************************
2 * chorus_flanger: Basic chorus/flanger/delay audio filter
3 *****************************************************************************
4 * Copyright (C) 2009-12 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
37 /*****************************************************************************
39 *****************************************************************************/
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 * );
50 /* TODO: Cleanup and optimise */
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;
60 float f_sinMultiplier;
62 /* This data is for the the circular queue which stores the samples. */
64 float * p_delayLineStart, * p_delayLineEnd;
68 /*****************************************************************************
70 *****************************************************************************/
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 "
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 )
99 * small_value: Helper function
100 * return high pass cutoff
102 static inline float small_value()
104 /* allows for 2^-24, should be enough for 24-bit DACs at least */
105 return ( 1.0 / 16777216.0 );
109 * Open: initialize and create stuff
112 static int Open( vlc_object_t *p_this )
114 filter_t *p_filter = (filter_t*)p_this;
117 if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
119 msg_Err( p_filter, "input and output formats are not similar" );
123 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
124 p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
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" );
131 p_filter->pf_audio_filter = DoWork;
133 p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
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 );
151 if( p_sys->f_delayTime < 0.0)
153 msg_Err( p_filter, "Delay Time is invalid" );
158 if( p_sys->f_sweepDepth > p_sys->f_delayTime || p_sys->f_sweepDepth < 0.0 )
160 msg_Err( p_filter, "Sweep Depth is invalid" );
165 if( p_sys->f_sweepRate < 0.0 )
167 msg_Err( p_filter, "Sweep Rate is invalid" );
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 );
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 )
182 msg_Err( p_filter, "Delay-time, Sample rate or Channels was incorrect" );
187 p_sys->p_delayLineStart = calloc( p_sys->i_bufferLength, sizeof( float ) );
188 if( !p_sys->p_delayLineStart )
194 p_sys->i_cumulative = 0;
195 p_sys->i_step = p_sys->f_sweepRate > 0 ? 1 : 0;
199 p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
200 p_sys->p_write = p_sys->p_delayLineStart;
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;
207 p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
208 ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
210 p_sys->i_sampleRate = p_filter->fmt_in.audio.i_rate;
216 * sanitize: Helper function to eliminate small amplitudes
217 * @param f_value pointer to value to clean
219 static inline void sanitize( float * f_value )
221 if ( fabs( *f_value ) < small_value() )
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
232 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
234 struct filter_sys_t *p_sys = p_filter->p_sys;
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;
242 float *p_ptr, f_temp = 0;/* f_diff = 0, f_frac = 0;*/
244 /* Process each sample */
245 for( unsigned i = 0; i < i_samples ; i++ )
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 )
253 if( p_sys->i_cumulative >= floor( p_sys->f_sweepDepth *
254 p_sys->i_sampleRate / p_sys->f_sweepRate ))
256 p_sys->f_offset = i_maxOffset;
257 p_sys->i_step = -1 * ( p_sys->i_step );
259 if( p_sys->i_cumulative <= floor( -1 * p_sys->f_sweepDepth *
260 p_sys->i_sampleRate / p_sys->f_sweepRate ) )
262 p_sys->f_offset = -i_maxOffset;
263 p_sys->i_step = -1 * ( p_sys->i_step );
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;
270 /* Handle Overflow */
271 if( p_ptr < p_sys->p_delayLineStart )
273 p_ptr += p_sys->i_bufferLength - p_sys->i_channels;
275 if( p_ptr > p_sys->p_delayLineEnd - 2*p_sys->i_channels )
277 p_ptr -= p_sys->i_bufferLength - p_sys->i_channels;
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++ )
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];
286 f_diff = *( p_ptr - p_sys->i_channels + i_chan )
288 f_temp = ( *( p_ptr + i_chan ) );//+ f_diff * f_frac;
289 /*Linear Interpolation. FIXME. This creates LOTS of noise */
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;
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 );
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 )
306 p_sys->p_write = p_sys->p_delayLineStart;
315 * @param p_this pointer to this filter object
317 static void Close( vlc_object_t *p_this )
319 filter_t *p_filter = ( filter_t* )p_this;
320 filter_sys_t *p_sys = p_filter->p_sys;
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" );
335 free( p_sys->p_delayLineStart );
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 )
345 filter_t *p_filter = (filter_t *)p_this;
346 filter_sys_t *p_sys = (filter_sys_t *) p_data;
348 if( !strncmp( psz_var, "delay-time", 10 ) )
350 /* if invalid value pretend everything is OK without updating value */
351 if( newval.f_float < 0 )
353 p_sys->f_delayTime = newval.f_float;
354 if( !reallocate_buffer( p_filter, p_sys ) )
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 );
362 else if( !strncmp( psz_var, "sweep-depth", 11 ) )
364 if( newval.f_float < 0 || newval.f_float > p_sys->f_delayTime)
366 p_sys->f_sweepDepth = newval.f_float;
367 if( !reallocate_buffer( p_filter, p_sys ) )
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 );
375 else if( !strncmp( psz_var, "sweep-rate", 10 ) )
377 if( newval.f_float > p_sys->f_sweepDepth )
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;
386 p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
387 ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
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;
400 static int reallocate_buffer( filter_t *p_filter, filter_sys_t *p_sys )
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 );
405 float *temp = realloc( p_sys->p_delayLineStart, p_sys->i_bufferLength );
406 if( unlikely( !temp ) )
408 msg_Err( p_filter, "Couldnt reallocate buffer for new delay." );
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;