1 /*****************************************************************************
2 * chorus_flanger: Basic chorus/flanger/delay audio filter
3 *****************************************************************************
4 * Copyright (C) 2009-12 VLC authors and VideoLAN
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 *****************************************************************************/
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(void)
104 /* allows for 2^-24, should be enough for 24-bit DACs at least */
105 return 1.f
/ 16777216.f
;
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.f
)
135 msg_Err( p_filter
, "Delay Time is invalid" );
140 if( p_sys
->f_sweepDepth
> p_sys
->f_delayTime
|| p_sys
->f_sweepDepth
< 0.f
)
142 msg_Err( p_filter
, "Sweep Depth is invalid" );
147 if( p_sys
->f_sweepRate
< 0.f
)
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
, (double) p_sys
->f_sweepDepth
,
161 (double) p_sys
->f_delayTime
, (double) p_sys
->f_sweepRate
,
162 p_filter
->fmt_in
.audio
.i_rate
);
163 if( p_sys
->i_bufferLength
<= 0 )
165 msg_Err( p_filter
, "Delay-time, Sample rate or Channels was incorrect" );
170 p_sys
->p_delayLineStart
= calloc( p_sys
->i_bufferLength
, sizeof( float ) );
171 if( !p_sys
->p_delayLineStart
)
177 p_sys
->i_cumulative
= 0;
178 p_sys
->i_step
= p_sys
->f_sweepRate
> 0 ? 1 : 0;
182 p_sys
->p_delayLineEnd
= p_sys
->p_delayLineStart
+ p_sys
->i_bufferLength
;
183 p_sys
->p_write
= p_sys
->p_delayLineStart
;
185 if( p_sys
->f_sweepDepth
< small_value() ||
186 p_filter
->fmt_in
.audio
.i_rate
< small_value() ) {
187 p_sys
->f_sinMultiplier
= 0.f
;
190 p_sys
->f_sinMultiplier
= 11 * p_sys
->f_sweepRate
/
191 ( 7 * p_sys
->f_sweepDepth
* p_filter
->fmt_in
.audio
.i_rate
) ;
193 p_sys
->i_sampleRate
= p_filter
->fmt_in
.audio
.i_rate
;
195 p_filter
->fmt_in
.audio
.i_format
= VLC_CODEC_FL32
;
196 aout_FormatPrepare(&p_filter
->fmt_in
.audio
);
197 p_filter
->fmt_out
.audio
= p_filter
->fmt_in
.audio
;
198 p_filter
->pf_audio_filter
= DoWork
;
204 * sanitize: Helper function to eliminate small amplitudes
205 * @param f_value pointer to value to clean
207 static inline void sanitize( float * f_value
)
209 if ( fabsf( *f_value
) < small_value() )
215 * DoWork : delays and finds the value of the current frame
216 * @param p_filter This filter object
217 * @param p_in_buf Input buffer
218 * @return Output buffer
220 static block_t
*DoWork( filter_t
*p_filter
, block_t
*p_in_buf
)
222 struct filter_sys_t
*p_sys
= p_filter
->p_sys
;
224 unsigned i_samples
= p_in_buf
->i_nb_samples
; /* number of samples */
225 /* maximum number of samples to offset in buffer */
226 int i_maxOffset
= floorf( p_sys
->f_sweepDepth
* p_sys
->i_sampleRate
/ 1000 );
227 float *p_out
= (float*)p_in_buf
->p_buffer
;
228 float *p_in
= (float*)p_in_buf
->p_buffer
;
230 float *p_ptr
, f_temp
= 0;/* f_diff = 0, f_frac = 0;*/
232 /* Process each sample */
233 for( unsigned i
= 0; i
< i_samples
; i
++ )
235 /* Sine function as a oscillator wave to calculate sweep */
236 p_sys
->i_cumulative
+= p_sys
->i_step
;
237 p_sys
->f_offset
= sinf( (p_sys
->i_cumulative
) * p_sys
->f_sinMultiplier
)
238 * floorf(p_sys
->f_sweepDepth
* p_sys
->i_sampleRate
/ 1000);
239 if( abs( p_sys
->i_step
) > 0 )
241 if( p_sys
->i_cumulative
>= floorf( p_sys
->f_sweepDepth
*
242 p_sys
->i_sampleRate
/ p_sys
->f_sweepRate
))
244 p_sys
->f_offset
= i_maxOffset
;
245 p_sys
->i_step
= -1 * ( p_sys
->i_step
);
247 if( p_sys
->i_cumulative
<= floorf( -1 * p_sys
->f_sweepDepth
*
248 p_sys
->i_sampleRate
/ p_sys
->f_sweepRate
) )
250 p_sys
->f_offset
= -i_maxOffset
;
251 p_sys
->i_step
= -1 * ( p_sys
->i_step
);
254 /* Calculate position in delay */
255 int offset
= floorf( p_sys
->f_offset
);
256 p_ptr
= p_sys
->p_write
+ ( i_maxOffset
- offset
) * p_sys
->i_channels
;
258 /* Handle Overflow */
259 if( p_ptr
< p_sys
->p_delayLineStart
)
261 p_ptr
+= p_sys
->i_bufferLength
- p_sys
->i_channels
;
263 if( p_ptr
> p_sys
->p_delayLineEnd
- 2*p_sys
->i_channels
)
265 p_ptr
-= p_sys
->i_bufferLength
- p_sys
->i_channels
;
267 /* For interpolation */
268 /* f_frac = ( p_sys->f_offset - (int)p_sys->f_offset );*/
269 for( i_chan
= 0; i_chan
< p_sys
->i_channels
; i_chan
++ )
271 /* if( p_ptr <= p_sys->p_delayLineStart + p_sys->i_channels )
272 f_diff = *(p_sys->p_delayLineEnd + i_chan) - p_ptr[i_chan];
274 f_diff = *( p_ptr - p_sys->i_channels + i_chan )
276 f_temp
= ( *( p_ptr
+ i_chan
) );//+ f_diff * f_frac;
277 /*Linear Interpolation. FIXME. This creates LOTS of noise */
279 p_out
[i_chan
] = p_sys
->f_dryLevel
* p_in
[i_chan
] +
280 p_sys
->f_wetLevel
* f_temp
;
281 *( p_sys
->p_write
+ i_chan
) = p_in
[i_chan
] +
282 p_sys
->f_feedbackGain
* f_temp
;
284 if( p_sys
->p_write
== p_sys
->p_delayLineStart
)
285 for( i_chan
= 0; i_chan
< p_sys
->i_channels
; i_chan
++ )
286 *( p_sys
->p_delayLineEnd
- p_sys
->i_channels
+ i_chan
)
287 = *( p_sys
->p_delayLineStart
+ i_chan
);
289 p_in
+= p_sys
->i_channels
;
290 p_out
+= p_sys
->i_channels
;
291 p_sys
->p_write
+= p_sys
->i_channels
;
292 if( p_sys
->p_write
== p_sys
->p_delayLineEnd
- p_sys
->i_channels
)
294 p_sys
->p_write
= p_sys
->p_delayLineStart
;
303 * @param p_this pointer to this filter object
305 static void Close( vlc_object_t
*p_this
)
307 filter_t
*p_filter
= ( filter_t
* )p_this
;
308 filter_sys_t
*p_sys
= p_filter
->p_sys
;
310 var_DelCallback( p_this
, "delay-time", paramCallback
, p_sys
);
311 var_DelCallback( p_this
, "sweep-depth", paramCallback
, p_sys
);
312 var_DelCallback( p_this
, "sweep-rate", paramCallback
, p_sys
);
313 var_DelCallback( p_this
, "feedback-gain", paramCallback
, p_sys
);
314 var_DelCallback( p_this
, "wet-mix", paramCallback
, p_sys
);
315 var_DelCallback( p_this
, "dry-mix", paramCallback
, p_sys
);
316 var_Destroy( p_this
, "delay-time" );
317 var_Destroy( p_this
, "sweep-depth" );
318 var_Destroy( p_this
, "sweep-rate" );
319 var_Destroy( p_this
, "feedback-gain" );
320 var_Destroy( p_this
, "wet-mix" );
321 var_Destroy( p_this
, "dry-mix" );
323 free( p_sys
->p_delayLineStart
);
327 /******************************************************************************
328 * Callback to update parameters on the fly
329 ******************************************************************************/
330 static int paramCallback( vlc_object_t
*p_this
, char const *psz_var
,
331 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
333 filter_t
*p_filter
= (filter_t
*)p_this
;
334 filter_sys_t
*p_sys
= (filter_sys_t
*) p_data
;
336 if( !strncmp( psz_var
, "delay-time", 10 ) )
338 /* if invalid value pretend everything is OK without updating value */
339 if( newval
.f_float
< 0 )
341 p_sys
->f_delayTime
= newval
.f_float
;
342 if( !reallocate_buffer( p_filter
, p_sys
) )
344 p_sys
->f_delayTime
= oldval
.f_float
;
345 p_sys
->i_bufferLength
= p_sys
->i_channels
* ( (int)
346 ( ( p_sys
->f_delayTime
+ p_sys
->f_sweepDepth
) *
347 p_filter
->fmt_in
.audio
.i_rate
/1000 ) + 1 );
350 else if( !strncmp( psz_var
, "sweep-depth", 11 ) )
352 if( newval
.f_float
< 0 || newval
.f_float
> p_sys
->f_delayTime
)
354 p_sys
->f_sweepDepth
= newval
.f_float
;
355 if( !reallocate_buffer( p_filter
, p_sys
) )
357 p_sys
->f_sweepDepth
= oldval
.f_float
;
358 p_sys
->i_bufferLength
= p_sys
->i_channels
* ( (int)
359 ( ( p_sys
->f_delayTime
+ p_sys
->f_sweepDepth
) *
360 p_filter
->fmt_in
.audio
.i_rate
/1000 ) + 1 );
363 else if( !strncmp( psz_var
, "sweep-rate", 10 ) )
365 if( newval
.f_float
> p_sys
->f_sweepDepth
)
367 p_sys
->f_sweepRate
= newval
.f_float
;
368 /* Calculate new f_sinMultiplier */
369 if( p_sys
->f_sweepDepth
< small_value() ||
370 p_filter
->fmt_in
.audio
.i_rate
< small_value() ) {
371 p_sys
->f_sinMultiplier
= 0.0;
374 p_sys
->f_sinMultiplier
= 11 * p_sys
->f_sweepRate
/
375 ( 7 * p_sys
->f_sweepDepth
* p_filter
->fmt_in
.audio
.i_rate
) ;
378 else if( !strncmp( psz_var
, "feedback-gain", 13 ) )
379 p_sys
->f_feedbackGain
= newval
.f_float
;
380 else if( !strncmp( psz_var
, "wet-mix", 7 ) )
381 p_sys
->f_wetLevel
= newval
.f_float
;
382 else if( !strncmp( psz_var
, "dry-mix", 7 ) )
383 p_sys
->f_dryLevel
= newval
.f_float
;
388 static int reallocate_buffer( filter_t
*p_filter
, filter_sys_t
*p_sys
)
390 p_sys
->i_bufferLength
= p_sys
->i_channels
* ( (int)( ( p_sys
->f_delayTime
391 + p_sys
->f_sweepDepth
) * p_filter
->fmt_in
.audio
.i_rate
/1000 ) + 1 );
393 float *temp
= realloc( p_sys
->p_delayLineStart
, p_sys
->i_bufferLength
);
394 if( unlikely( !temp
) )
396 msg_Err( p_filter
, "Couldnt reallocate buffer for new delay." );
399 p_sys
->p_delayLineStart
= temp
;
400 p_sys
->p_delayLineEnd
= p_sys
->p_delayLineStart
+ p_sys
->i_bufferLength
;