input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / audio_filter / stereo_widen.c
blob69cb7e0e72fd19936934443ecb5c7279cb36629c
1 /*****************************************************************************
2 * stereo_widen.c : simple stereo widening effect
3 *****************************************************************************
4 * Copyright (C) 2012 VLC authors and VideoLAN
6 * Author : Sukrit Sangwan < sukritsangwan at gmail dot com >
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <vlc_common.h>
28 #include <vlc_aout.h>
29 #include <vlc_filter.h>
30 #include <vlc_plugin.h>
32 /*****************************************************************************
33 * Local prototypes
34 *****************************************************************************/
35 static int Open ( vlc_object_t * );
36 static void Close( vlc_object_t * );
38 static block_t *Filter ( filter_t *, block_t * );
39 static int paramCallback( vlc_object_t *, char const *, vlc_value_t ,
40 vlc_value_t , void * );
42 typedef struct
44 float *pf_ringbuf; /* circular buffer to store samples */
45 float *pf_write; /* where to write current sample */
46 size_t i_len; /* delay in number of samples */
47 float f_delay; /* delay in milliseconds */
48 float f_feedback;
49 float f_crossfeed;
50 float f_dry_mix;
51 } filter_sys_t;
53 #define HELP_TEXT N_("This filter enhances the stereo effect by "\
54 "suppressing mono (signal common to both channels) "\
55 "and by delaying the signal of left into right and vice versa, "\
56 "thereby widening the stereo effect.")
57 #define DELAY_TEXT N_("Delay time")
58 #define DELAY_LONGTEXT N_("Time in ms of the delay of left signal into right "\
59 "and vice versa.")
60 #define FEEDBACK_TEXT N_("Feedback gain")
61 #define FEEDBACK_LONGTEXT N_("Amount of gain in delayed left signal into "\
62 "right and vice versa. Gives a delay effect of left signal in "\
63 "right output and vice versa which gives widening effect.")
64 #define CROSSFEED_TEXT N_("Crossfeed")
65 #define CROSSFEED_LONGTEXT N_("Cross feed of left into right with inverted "\
66 "phase. This helps in suppressing the mono. If the value is 1 it "\
67 "will cancel all the signal common to both channels.")
68 #define DRYMIX_TEXT N_("Dry mix")
69 #define DRYMIX_LONGTEXT N_("Level of input signal of original channel.")
71 #define CONFIG_PREFIX "stereowiden-"
73 /*****************************************************************************
74 * Module descriptor
75 *****************************************************************************/
76 vlc_module_begin ()
77 set_shortname( N_("Stereo Enhancer") )
78 set_description( N_("Simple stereo widening effect") )
79 set_help( HELP_TEXT )
80 set_category( CAT_AUDIO )
81 set_subcategory( SUBCAT_AUDIO_AFILTER )
82 set_capability( "audio filter", 0 )
83 set_callbacks( Open, Close )
85 add_float_with_range( CONFIG_PREFIX "delay", 20, 1, 100,
86 DELAY_TEXT, DELAY_LONGTEXT, true )
87 add_float_with_range( CONFIG_PREFIX "feedback", 0.3, 0.0, 0.9,
88 FEEDBACK_TEXT, FEEDBACK_LONGTEXT, true )
89 add_float_with_range( CONFIG_PREFIX "crossfeed", 0.3, 0.0, 0.8,
90 CROSSFEED_TEXT, CROSSFEED_LONGTEXT, true )
91 add_float_with_range( CONFIG_PREFIX "dry-mix", 0.8, 0.0, 1.0,
92 DRYMIX_TEXT, DRYMIX_LONGTEXT, true )
93 vlc_module_end ()
95 /*****************************************************************************
96 * Open: Allocate buffer
97 *****************************************************************************/
98 static int MakeRingBuffer( float **pp_buffer, size_t *pi_buffer,
99 float **pp_write, float f_delay, unsigned i_rate )
101 const size_t i_size = (2 * (size_t)(1 + f_delay * i_rate / 1000));
103 if( unlikely(SIZE_MAX / sizeof(float) < i_size) )
104 return VLC_EGENERIC;
106 float *p_realloc = realloc( *pp_buffer, i_size * sizeof(float) );
107 if( !p_realloc )
108 return VLC_ENOMEM;
110 memset( p_realloc, 0, i_size * sizeof(float) );
111 *pp_write = *pp_buffer = p_realloc;
112 *pi_buffer = i_size;
114 return VLC_SUCCESS;
117 static int Open( vlc_object_t *obj )
119 filter_t *p_filter = (filter_t *)obj;
120 vlc_object_t *p_aout = p_filter->obj.parent;
121 filter_sys_t *p_sys;
123 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
124 !AOUT_FMTS_IDENTICAL( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio) )
125 return VLC_EGENERIC;
127 if( p_filter->fmt_in.audio.i_channels != 2 )
129 msg_Err ( p_filter, "stereo enhance requires stereo" );
130 return VLC_EGENERIC;
133 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
134 if( unlikely(!p_sys) )
135 return VLC_ENOMEM;
137 #define CREATE_VAR( stor, var ) \
138 p_sys->stor = var_CreateGetFloat( p_aout, var ); \
139 var_AddCallback( p_aout, var, paramCallback, p_sys );
141 CREATE_VAR( f_delay, CONFIG_PREFIX "delay" )
142 CREATE_VAR( f_feedback, CONFIG_PREFIX "feedback" )
143 CREATE_VAR( f_crossfeed, CONFIG_PREFIX "crossfeed" )
144 CREATE_VAR( f_dry_mix, CONFIG_PREFIX "dry-mix" )
146 /* Compute buffer length and allocate space */
147 p_sys->pf_ringbuf = NULL;
148 p_sys->i_len = 0;
149 if( MakeRingBuffer( &p_sys->pf_ringbuf, &p_sys->i_len, &p_sys->pf_write,
150 p_sys->f_delay, p_filter->fmt_in.audio.i_rate ) != VLC_SUCCESS )
152 Close( obj );
153 return VLC_ENOMEM;
156 p_filter->pf_audio_filter = Filter;
157 return VLC_SUCCESS;
160 /*****************************************************************************
161 * Filter: process each sample
162 *****************************************************************************/
163 static block_t *Filter( filter_t *p_filter, block_t *p_block )
165 filter_sys_t *p_sys = p_filter->p_sys;
166 float *p_out = (float *)p_block->p_buffer;
167 float *pf_read;
169 for (unsigned i = p_block->i_nb_samples; i > 0; i--)
171 pf_read = p_sys->pf_write + 2;
172 /* if at end of buffer put read ptr at begin */
173 if( pf_read >= p_sys->pf_ringbuf + p_sys->i_len )
174 pf_read = p_sys->pf_ringbuf;
176 float left = p_out[0];
177 float right = p_out[1];
179 *(p_out++) = p_sys->f_dry_mix * left - p_sys->f_crossfeed * right
180 - p_sys->f_feedback * pf_read[1];
181 *(p_out++) = p_sys->f_dry_mix * right - p_sys->f_crossfeed * left
182 - p_sys->f_feedback * pf_read[0];
183 *(p_sys->pf_write++) = left ;
184 *(p_sys->pf_write++) = right;
186 /* if at end of buffer place pf_write at begin */
187 if( p_sys->pf_write == p_sys->pf_ringbuf + p_sys->i_len )
188 p_sys->pf_write = p_sys->pf_ringbuf;
191 return p_block;
194 /*****************************************************************************
195 * Close: close the plugin
196 *****************************************************************************/
197 static void Close( vlc_object_t *obj )
199 filter_t *p_filter = (filter_t *)obj;
200 vlc_object_t *p_aout = p_filter->obj.parent;
201 filter_sys_t *p_sys = p_filter->p_sys;
203 #define DEL_VAR(var) \
204 var_DelCallback( p_aout, var, paramCallback, p_sys ); \
205 var_Destroy( p_aout, var );
207 DEL_VAR( CONFIG_PREFIX "feedback" );
208 DEL_VAR( CONFIG_PREFIX "crossfeed" );
209 DEL_VAR( CONFIG_PREFIX "dry-mix" );
210 DEL_VAR( CONFIG_PREFIX "delay" );
212 free( p_sys->pf_ringbuf );
213 free( p_sys );
217 /**********************************************************************
218 * Callback to update params on the fly
219 **********************************************************************/
220 static int paramCallback( vlc_object_t *p_this, char const *psz_var,
221 vlc_value_t oldval, vlc_value_t newval,
222 void *p_data )
224 filter_t *p_filter = (filter_t *)p_this;
225 filter_sys_t *p_sys = (filter_sys_t *) p_data;
227 VLC_UNUSED(oldval);
228 VLC_UNUSED(p_this);
230 if( !strcmp( psz_var, CONFIG_PREFIX "delay" ) )
232 if( MakeRingBuffer( &p_sys->pf_ringbuf, &p_sys->i_len, &p_sys->pf_write,
233 newval.f_float, p_filter->fmt_in.audio.i_rate ) != VLC_SUCCESS )
235 msg_Dbg( p_filter, "Couldnt allocate buffer for delay" );
237 else
239 p_sys->f_delay = newval.f_float;
242 else if( !strcmp( psz_var, CONFIG_PREFIX "feedback" ) )
243 p_sys->f_feedback = newval.f_float;
244 else if( !strcmp( psz_var, CONFIG_PREFIX "crossfeed" ) )
245 p_sys->f_feedback = newval.f_float;
246 else if( !strcmp( psz_var, CONFIG_PREFIX "dry-mix" ) )
247 p_sys->f_dry_mix = newval.f_float;
249 return VLC_SUCCESS;