]> git.sesse.net Git - vlc/blob - modules/audio_filter/chorus_flanger.c
Chorus/Flanger audio filter Based on basic variable delay filter
[vlc] / modules / audio_filter / chorus_flanger.c
1 /*****************************************************************************
2  * chorus_flanger.c
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Author: Srikanth Raju < srikiraju at gmail dot com >
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * Basic chorus/flanger/delay audio filter
26  * This implements a variable delay filter for VLC. It has some issues with
27  * interpolation and sounding 'correct'.
28  */
29
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <math.h>
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39
40 #include <vlc_aout.h>
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45
46 static int  Open     ( vlc_object_t * );
47 static void Close    ( vlc_object_t * );
48 static void DoWork   ( aout_instance_t * , aout_filter_t *,
49                        aout_buffer_t * , aout_buffer_t * );
50
51 struct aout_filter_sys_t
52 {
53     /* TODO: Cleanup and optimise */
54     int i_cumulative;
55     int i_channels, i_sampleRate;
56     float f_delayTime, f_feedbackGain;  /* delayTime is in milliseconds */
57     float f_wetLevel, f_dryLevel;
58     float f_sweepDepth, f_sweepRate;
59
60     float f_step,f_offset;
61     int i_step,i_offset;
62     float f_temp;
63     float f_sinMultiplier;
64
65     /* This data is for the the circular queue which stores the samples. */
66     int i_bufferLength;
67     float * pf_delayLineStart, * pf_delayLineEnd;
68     float * pf_write;
69 };
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74
75
76 vlc_module_begin ()
77     set_description( N_("Sound Delay") )
78     set_shortname( N_("delay") )
79     set_category( CAT_AUDIO )
80     set_subcategory( SUBCAT_AUDIO_AFILTER )
81     add_shortcut( "delay" )
82     add_float( "delay-time", 40, NULL, N_("Delay time"),
83         N_("Time in milliseconds of the average delay. Note average"), true )
84     add_float( "sweep-depth", 6, NULL, 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, NULL, 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, NULL,
91         N_("Feedback Gain"), N_("Gain on Feedback loop"), true )
92     add_float_with_range( "wet-mix", 0.4, -0.999, 0.999, NULL,
93         N_("Wet mix"), N_("Level of delayed signal"), true )
94     add_float_with_range( "dry-mix", 0.4, -0.999, 0.999, NULL,
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 ()
99
100 /**
101  * small_value: Helper function
102  * return high pass cutoff
103  */
104 static inline float small_value()
105 {
106     /* allows for 2^-24, should be enough for 24-bit DACs at least */
107     return ( 1.0 / 16777216.0 );
108 }
109
110 /**
111  * Open: initialize and create stuff
112  * @param p_this
113  */
114 static int Open( vlc_object_t *p_this )
115 {
116     aout_filter_t *p_filter = (aout_filter_t*)p_this;
117     aout_filter_sys_t *p_sys;
118
119     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
120     {
121         msg_Err( p_filter, "input and output formats are not similar" );
122         return VLC_EGENERIC;
123     }
124
125     if( p_filter->input.i_format != VLC_CODEC_FL32 ||
126         p_filter->output.i_format != VLC_CODEC_FL32 )
127     {
128         p_filter->input.i_format = VLC_CODEC_FL32;
129         p_filter->output.i_format = VLC_CODEC_FL32;
130         msg_Warn( p_filter, "bad input or output format" );
131     }
132
133     p_filter->pf_do_work = DoWork;
134     p_filter->b_in_place = true;
135
136     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
137     if( !p_sys )
138         return VLC_ENOMEM;
139
140     p_sys->i_channels       = aout_FormatNbChannels( &p_filter->input );
141     p_sys->f_delayTime      = var_CreateGetFloat( p_this, "delay-time" );
142     p_sys->f_sweepDepth     = var_CreateGetFloat( p_this, "sweep-depth" );
143     p_sys->f_sweepRate      = var_CreateGetFloat( p_this, "sweep-rate" );
144     p_sys->f_feedbackGain   = var_CreateGetFloat( p_this, "feedback-gain" );
145     p_sys->f_dryLevel       = var_CreateGetFloat( p_this, "dry-mix" );
146     p_sys->f_wetLevel       = var_CreateGetFloat( p_this, "wet-mix" );
147
148     if( p_sys->f_delayTime < 0.0)
149     {
150         msg_Err( p_filter, "Delay Time is invalid" );
151         free(p_sys);
152         return VLC_EGENERIC;
153     }
154
155     if( p_sys->f_sweepDepth > p_sys->f_delayTime || p_sys->f_sweepDepth < 0.0 )
156     {
157         msg_Err( p_filter, "Sweep Depth is invalid" );
158         free( p_sys );
159         return VLC_EGENERIC;
160     }
161
162     if( p_sys->f_sweepRate < 0.0 )
163     {
164         msg_Err( p_filter, "Sweep Rate is invalid" );
165         free( p_sys );
166         return VLC_EGENERIC;
167     }
168
169     /* Max delay = delay + depth. Min = delay - depth */
170     p_sys->i_bufferLength = p_sys->i_channels * ( (int)( ( p_sys->f_delayTime
171                 + p_sys->f_sweepDepth ) * p_filter->input.i_rate/1000 ) + 1 );
172
173     msg_Dbg( p_filter , "Buffer length:%d, Channels:%d, Sweep Depth:%f, Delay "
174             "time:%f, Sweep Rate:%f, Sample Rate: %d", p_sys->i_bufferLength,
175             p_sys->i_channels, p_sys->f_sweepDepth, p_sys->f_delayTime,
176             p_sys->f_sweepRate, p_filter->input.i_rate );
177     if( p_sys->i_bufferLength <= 0 )
178     {
179         msg_Err( p_filter, "Delay-time, Sampl rate or Channels was incorrect" );
180         free(p_sys);
181         return VLC_EGENERIC;
182     }
183
184     p_sys->pf_delayLineStart = calloc( p_sys->i_bufferLength, sizeof( float ) );
185     if( !p_sys->pf_delayLineStart )
186     {
187         free( p_sys );
188         return VLC_ENOMEM;
189     }
190
191     p_sys->i_cumulative = 0;
192     p_sys->f_step = p_sys->f_sweepRate / 1000.0;
193     p_sys->i_step = p_sys->f_sweepRate > 0 ? 1 : 0;
194     p_sys->f_offset = 0;
195     p_sys->i_offset = 0;
196     p_sys->f_temp = 0;
197
198     p_sys->pf_delayLineEnd = p_sys->pf_delayLineStart + p_sys->i_bufferLength;
199     p_sys->pf_write = p_sys->pf_delayLineStart;
200
201     if( p_sys->f_sweepDepth < small_value() ||
202             p_filter->input.i_rate < small_value() ) {
203         p_sys->f_sinMultiplier = 0.0;
204     }
205     else {
206         p_sys->f_sinMultiplier = 11 * p_sys->f_sweepRate /
207             ( 7 * p_sys->f_sweepDepth * p_filter->input.i_rate ) ;
208     }
209     p_sys->i_sampleRate = p_filter->input.i_rate;
210
211     return VLC_SUCCESS;
212 }
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_aout Audio output object
229  * @param p_filter This filter object
230  * @param p_in_buf Input buffer
231  * @param p_out_buf Output buffer
232  */
233 static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
234                     aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
235 {
236     struct aout_filter_sys_t *p_sys = p_filter->p_sys;
237     int i, i_chan;
238     int i_samples = p_in_buf->i_nb_samples; /* Gives the number of samples */
239     int i_maxOffset = (int)floor( p_sys->f_sweepDepth * p_sys->i_sampleRate /
240             1000 ); /*maximum number of samples to offset in buffer */
241     float *p_out = (float*)p_out_buf->p_buffer;
242     float *p_in =  (float*)p_in_buf->p_buffer;
243
244     float *pf_ptr, f_diff = 0, f_frac = 0, f_temp = 0 ;
245
246     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
247     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
248
249     /* Process each sample */
250     for( i = 0; i < i_samples ; i++ )
251     {
252         /* Use a sine function as a oscillator wave. TODO */
253         /* f_offset = sinf( ( p_sys->i_cumulative ) * p_sys->f_sinMultiplier ) *
254          * (int)floor(p_sys->f_sweepDepth * p_sys->i_sampleRate / 1000);
255          */
256
257         /* Triangle oscillator. Step using ints, because floats give rounding */
258         p_sys->i_offset+=p_sys->i_step;
259         p_sys->f_offset = p_sys->i_offset * p_sys->f_step;
260         if( abs( p_sys->i_step ) > 0 )
261         {
262             if( p_sys->i_offset >=  floor( p_sys->f_sweepDepth *
263                         p_sys->i_sampleRate / p_sys->f_sweepRate ))
264             {
265                 p_sys->f_offset = i_maxOffset;
266                 p_sys->i_step = -1 * ( p_sys->i_step );
267             }
268             if( p_sys->i_offset <= floor( -1 * p_sys->f_sweepDepth *
269                         p_sys->i_sampleRate / p_sys->f_sweepRate ) )
270             {
271                 p_sys->f_offset = -i_maxOffset;
272                 p_sys->i_step = -1 * ( p_sys->i_step );
273             }
274         }
275         /* Calculate position in delay */
276         pf_ptr = p_sys->pf_write + i_maxOffset * p_sys->i_channels +
277             (int)( floor( p_sys->f_offset ) ) * p_sys->i_channels;
278
279         /* Handle Overflow */
280         if( pf_ptr < p_sys->pf_delayLineStart )
281         {
282             pf_ptr += p_sys->i_bufferLength - p_sys->i_channels;
283         }
284         if( pf_ptr > p_sys->pf_delayLineEnd - 2*p_sys->i_channels )
285         {
286             pf_ptr -= p_sys->i_bufferLength - p_sys->i_channels;
287         }
288         /* For interpolation */
289         f_frac = ( p_sys->f_offset - (int)p_sys->f_offset );
290         for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
291         {
292             f_diff =  *( pf_ptr + p_sys->i_channels + i_chan )
293                         - *( pf_ptr + i_chan );
294             f_temp = ( *( pf_ptr + i_chan ) );//+ f_diff * f_frac);
295             /*Linear Interpolation. FIXME. This creates LOTS of noise */
296             sanitize(&f_temp);
297             p_out[i_chan] = p_sys->f_dryLevel * p_in[i_chan] +
298                 p_sys->f_wetLevel * f_temp;
299             *( p_sys->pf_write + i_chan ) = p_in[i_chan] +
300                 p_sys->f_feedbackGain * f_temp;
301         }
302         if( p_sys->pf_write == p_sys->pf_delayLineStart )
303             for( i_chan = 0; i_chan < p_sys->i_channels; i_chan++ )
304                 *( p_sys->pf_delayLineEnd - p_sys->i_channels + i_chan )
305                     = *( p_sys->pf_delayLineStart + i_chan );
306
307         p_in += p_sys->i_channels;
308         p_out += p_sys->i_channels;
309         p_sys->pf_write += p_sys->i_channels;
310         if( p_sys->pf_write == p_sys->pf_delayLineEnd - p_sys->i_channels )
311         {
312             p_sys->pf_write = p_sys->pf_delayLineStart;
313         }
314
315     }
316     return;
317 }
318
319 /**
320  * Close: Destructor
321  * @param p_this pointer to this filter object
322  */
323 static void Close( vlc_object_t *p_this )
324 {
325     aout_filter_t *p_filter = ( aout_filter_t* )p_this;
326     aout_filter_sys_t *p_sys = p_filter->p_sys;
327
328     free( p_sys->pf_delayLineStart );
329     free( p_sys );
330 }