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;
115 filter_sys_t *p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
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 );
133 if( p_sys->f_delayTime < 0.0)
135 msg_Err( p_filter, "Delay Time is invalid" );
140 if( p_sys->f_sweepDepth > p_sys->f_delayTime || p_sys->f_sweepDepth < 0.0 )
142 msg_Err( p_filter, "Sweep Depth is invalid" );
147 if( p_sys->f_sweepRate < 0.0 )
149 msg_Err( p_filter, "Sweep Rate is invalid" );
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 );
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 )
164 msg_Err( p_filter, "Delay-time, Sample rate or Channels was incorrect" );
169 p_sys->p_delayLineStart = calloc( p_sys->i_bufferLength, sizeof( float ) );
170 if( !p_sys->p_delayLineStart )
176 p_sys->i_cumulative = 0;
177 p_sys->i_step = p_sys->f_sweepRate > 0 ? 1 : 0;
181 p_sys->p_delayLineEnd = p_sys->p_delayLineStart + p_sys->i_bufferLength;
182 p_sys->p_write = p_sys->p_delayLineStart;
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;
189 p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
190 ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
192 p_sys->i_sampleRate = p_filter->fmt_in.audio.i_rate;
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;
202 * sanitize: Helper function to eliminate small amplitudes
203 * @param f_value pointer to value to clean
205 static inline void sanitize( float * f_value )
207 if ( fabs( *f_value ) < small_value() )
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
218 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
220 struct filter_sys_t *p_sys = p_filter->p_sys;
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;
228 float *p_ptr, f_temp = 0;/* f_diff = 0, f_frac = 0;*/
230 /* Process each sample */
231 for( unsigned i = 0; i < i_samples ; i++ )
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 )
239 if( p_sys->i_cumulative >= floor( p_sys->f_sweepDepth *
240 p_sys->i_sampleRate / p_sys->f_sweepRate ))
242 p_sys->f_offset = i_maxOffset;
243 p_sys->i_step = -1 * ( p_sys->i_step );
245 if( p_sys->i_cumulative <= floor( -1 * p_sys->f_sweepDepth *
246 p_sys->i_sampleRate / p_sys->f_sweepRate ) )
248 p_sys->f_offset = -i_maxOffset;
249 p_sys->i_step = -1 * ( p_sys->i_step );
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;
256 /* Handle Overflow */
257 if( p_ptr < p_sys->p_delayLineStart )
259 p_ptr += p_sys->i_bufferLength - p_sys->i_channels;
261 if( p_ptr > p_sys->p_delayLineEnd - 2*p_sys->i_channels )
263 p_ptr -= p_sys->i_bufferLength - p_sys->i_channels;
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++ )
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];
272 f_diff = *( p_ptr - p_sys->i_channels + i_chan )
274 f_temp = ( *( p_ptr + i_chan ) );//+ f_diff * f_frac;
275 /*Linear Interpolation. FIXME. This creates LOTS of noise */
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;
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 );
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 )
292 p_sys->p_write = p_sys->p_delayLineStart;
301 * @param p_this pointer to this filter object
303 static void Close( vlc_object_t *p_this )
305 filter_t *p_filter = ( filter_t* )p_this;
306 filter_sys_t *p_sys = p_filter->p_sys;
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" );
321 free( p_sys->p_delayLineStart );
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 )
331 filter_t *p_filter = (filter_t *)p_this;
332 filter_sys_t *p_sys = (filter_sys_t *) p_data;
334 if( !strncmp( psz_var, "delay-time", 10 ) )
336 /* if invalid value pretend everything is OK without updating value */
337 if( newval.f_float < 0 )
339 p_sys->f_delayTime = newval.f_float;
340 if( !reallocate_buffer( p_filter, p_sys ) )
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 );
348 else if( !strncmp( psz_var, "sweep-depth", 11 ) )
350 if( newval.f_float < 0 || newval.f_float > p_sys->f_delayTime)
352 p_sys->f_sweepDepth = newval.f_float;
353 if( !reallocate_buffer( p_filter, p_sys ) )
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 );
361 else if( !strncmp( psz_var, "sweep-rate", 10 ) )
363 if( newval.f_float > p_sys->f_sweepDepth )
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;
372 p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
373 ( 7 * p_sys->f_sweepDepth * p_filter->fmt_in.audio.i_rate ) ;
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;
386 static int reallocate_buffer( filter_t *p_filter, filter_sys_t *p_sys )
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 );
391 float *temp = realloc( p_sys->p_delayLineStart, p_sys->i_bufferLength );
392 if( unlikely( !temp ) )
394 msg_Err( p_filter, "Couldnt reallocate buffer for new delay." );
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;