1 /*****************************************************************************
2 * spatializer.cpp: sound reverberation
3 *****************************************************************************
4 * Copyright (C) 2004, 2006, 2007 the VideoLAN team
6 * Google Summer of Code 2007
8 * Authors: Biodun Osunkunle <biodun@videolan.org>
10 * Mentor : Jean-Baptiste Kempf <jb@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <stdlib.h> /* malloc(), free() */
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
44 #include "revmodel.hpp"
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
53 #define ROOMSIZE_TEXT N_("Room size")
54 #define ROOMSIZE_LONGTEXT N_("Defines the virtual surface of the room" \
55 " emulated by the filter." )
57 #define WIDTH_TEXT N_("Room width")
58 #define WIDTH_LONGTEXT N_("Width of the virtual room")
60 #define WET_TEXT N_("Wet")
61 #define WET_LONGTEXT NULL
63 #define DRY_TEXT N_("Dry")
64 #define DRY_LONGTEXT NULL
66 #define DAMP_TEXT N_("Damp")
67 #define DAMP_LONGTEXT NULL
70 set_description( N_("Audio Spatializer") )
71 set_shortname( N_("Spatializer" ) )
72 set_capability( "audio filter", 0 )
73 set_category( CAT_AUDIO )
74 set_subcategory( SUBCAT_AUDIO_AFILTER )
76 set_callbacks( Open, Close )
77 add_shortcut( "spatializer" )
78 add_float( "spatializer-roomsize", 1.05, NULL, ROOMSIZE_TEXT,
79 ROOMSIZE_LONGTEXT, true )
80 add_float( "spatializer-width", 10., NULL, WIDTH_TEXT,WIDTH_LONGTEXT, true )
81 add_float( "spatializer-wet", 3., NULL, WET_TEXT,WET_LONGTEXT, true )
82 add_float( "spatializer-dry", 2., NULL, DRY_TEXT,DRY_LONGTEXT, true )
83 add_float( "spatializer-damp", 1., NULL, DAMP_TEXT,DAMP_LONGTEXT, true )
86 /*****************************************************************************
88 *****************************************************************************/
89 struct aout_filter_sys_t
98 CLocker( vlc_mutex_t *p_lock_to_manage ) : p_lock(p_lock_to_manage)
100 vlc_mutex_lock( p_lock );
104 vlc_mutex_unlock( p_lock );
110 #define DECLARECB(fn) static int fn (vlc_object_t *,char const *, \
111 vlc_value_t, vlc_value_t, void *)
112 DECLARECB( RoomCallback );
113 DECLARECB( WetCallback );
114 DECLARECB( DryCallback );
115 DECLARECB( DampCallback );
116 DECLARECB( WidthCallback );
121 const char *psz_name;
122 int (*fp_callback)(vlc_object_t *,const char *,
123 vlc_value_t,vlc_value_t,void *);
124 void (revmodel::* fp_set)(float);
127 static const callback_s callbacks[] = {
128 { "spatializer-roomsize", RoomCallback, &revmodel::setroomsize },
129 { "spatializer-width", WidthCallback, &revmodel::setwidth },
130 { "spatializer-wet", WetCallback, &revmodel::setwet },
131 { "spatializer-dry", DryCallback, &revmodel::setdry },
132 { "spatializer-damp", DampCallback, &revmodel::setdamp }
134 enum { num_callbacks=sizeof(callbacks)/sizeof(callback_s) };
136 static void DoWork( aout_instance_t *, aout_filter_t *,
137 aout_buffer_t *, aout_buffer_t * );
139 /*****************************************************************************
141 *****************************************************************************/
142 static int Open( vlc_object_t *p_this )
144 aout_filter_t *p_filter = (aout_filter_t *)p_this;
145 aout_filter_sys_t *p_sys;
146 aout_instance_t *p_aout = (aout_instance_t *)p_filter->p_parent;
148 msg_Dbg( p_this, "Opening filter spatializer" );
150 if( p_filter->input.i_format != VLC_CODEC_FL32 ||
151 p_filter->output.i_format != VLC_CODEC_FL32 )
154 p_filter->input.i_format = VLC_CODEC_FL32;
155 p_filter->output.i_format = VLC_CODEC_FL32;
156 msg_Warn( p_filter, "bad input or output format" );
158 if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
161 memcpy( &p_filter->output, &p_filter->input,
162 sizeof(audio_sample_format_t) );
163 msg_Warn( p_filter, "input and output formats are not similar" );
171 p_filter->pf_do_work = DoWork;
172 p_filter->b_in_place = true;
174 /* Allocate structure */
175 p_sys = p_filter->p_sys = (aout_filter_sys_t*)malloc( sizeof( aout_filter_sys_t ) );
179 /* Force new to return 0 on failure instead of throwing, since we don't
180 want an exception to leak back to C code. Bad things would happen. */
181 p_sys->p_reverbm = new (nothrow) revmodel;
182 if( !p_sys->p_reverbm )
188 vlc_mutex_init( &p_sys->lock );
190 for(unsigned i=0;i<num_callbacks;++i)
192 /* NOTE: C++ pointer-to-member function call from table lookup. */
193 (p_sys->p_reverbm->*(callbacks[i].fp_set))
194 (var_CreateGetFloatCommand(p_aout,callbacks[i].psz_name));
195 var_AddCallback( p_aout, callbacks[i].psz_name,
196 callbacks[i].fp_callback, p_sys );
202 /*****************************************************************************
203 * Close: close the plugin
204 *****************************************************************************/
205 static void Close( vlc_object_t *p_this )
207 aout_filter_t *p_filter = (aout_filter_t *)p_this;
208 aout_filter_sys_t *p_sys = p_filter->p_sys;
209 aout_instance_t *p_aout = (aout_instance_t *)p_filter->p_parent;
211 /* Delete the callbacks */
212 for(unsigned i=0;i<num_callbacks;++i)
214 var_DelCallback( p_aout, callbacks[i].psz_name,
215 callbacks[i].fp_callback, p_sys );
218 delete p_sys->p_reverbm;
219 vlc_mutex_destroy( &p_sys->lock );
221 msg_Dbg( p_this, "Closing filter spatializer" );
224 /*****************************************************************************
225 * SpatFilter: process samples buffer
226 * DoWork: call SpatFilter
227 *****************************************************************************/
230 void SpatFilter( aout_instance_t *p_aout, aout_filter_t *p_filter,
231 float *out, float *in, int i_samples, int i_channels )
233 aout_filter_sys_t *p_sys = p_filter->p_sys;
234 CLocker locker( &p_sys->lock );
237 for( i = 0; i < i_samples; i++ )
239 for( ch = 0 ; ch < 2; ch++)
241 in[ch] = in[ch] * SPAT_AMP;
243 p_sys->p_reverbm->processreplace( in, out , 1, i_channels);
249 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
250 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
252 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
253 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
255 SpatFilter( p_aout, p_filter, (float*)p_out_buf->p_buffer,
256 (float*)p_in_buf->p_buffer, p_in_buf->i_nb_samples,
257 aout_FormatNbChannels( &p_filter->input ) );
261 /*****************************************************************************
262 * Variables callbacks
263 *****************************************************************************/
265 static int RoomCallback( vlc_object_t *p_this, char const *psz_cmd,
266 vlc_value_t oldval, vlc_value_t newval, void *p_data )
268 (void)psz_cmd; (void)oldval;
269 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
270 CLocker locker( &p_sys->lock );
272 p_sys->p_reverbm->setroomsize(newval.f_float);
273 msg_Dbg( p_this, "room size is now %3.1f", newval.f_float );
277 static int WidthCallback( vlc_object_t *p_this, char const *psz_cmd,
278 vlc_value_t oldval, vlc_value_t newval, void *p_data )
280 (void)psz_cmd; (void)oldval;
281 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
282 CLocker locker( &p_sys->lock );
284 p_sys->p_reverbm->setwidth(newval.f_float);
285 msg_Dbg( p_this, "width is now %3.1f", newval.f_float );
288 static int WetCallback( vlc_object_t *p_this, char const *psz_cmd,
289 vlc_value_t oldval, vlc_value_t newval, void *p_data )
291 (void)psz_cmd; (void)oldval;
292 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
293 CLocker locker( &p_sys->lock );
295 p_sys->p_reverbm->setwet(newval.f_float);
296 msg_Dbg( p_this, "'wet' value is now %3.1f", newval.f_float );
299 static int DryCallback( vlc_object_t *p_this, char const *psz_cmd,
300 vlc_value_t oldval, vlc_value_t newval, void *p_data )
302 (void)psz_cmd; (void)oldval;
303 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
304 CLocker locker( &p_sys->lock );
306 p_sys->p_reverbm->setdry(newval.f_float);
307 msg_Dbg( p_this, "'dry' value is now %3.1f", newval.f_float );
310 static int DampCallback( vlc_object_t *p_this, char const *psz_cmd,
311 vlc_value_t oldval, vlc_value_t newval, void *p_data )
313 (void)psz_cmd; (void)oldval;
314 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
315 CLocker locker( &p_sys->lock );
317 p_sys->p_reverbm->setdamp(newval.f_float);
318 msg_Dbg( p_this, "'damp' value is now %3.1f", newval.f_float );