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
95 #define DECLARECB(fn) static int fn (vlc_object_t *,char const *, \
96 vlc_value_t, vlc_value_t, void *)
97 DECLARECB( RoomCallback );
98 DECLARECB( WetCallback );
99 DECLARECB( DryCallback );
100 DECLARECB( DampCallback );
101 DECLARECB( WidthCallback );
106 const char *psz_name;
107 int (*fp_callback)(vlc_object_t *,const char *,
108 vlc_value_t,vlc_value_t,void *);
109 void (revmodel::* fp_set)(float);
112 static const callback_s callbacks[] = {
113 { "spatializer-roomsize", RoomCallback, &revmodel::setroomsize },
114 { "spatializer-width", WidthCallback, &revmodel::setwidth },
115 { "spatializer-wet", WetCallback, &revmodel::setwet },
116 { "spatializer-dry", DryCallback, &revmodel::setdry },
117 { "spatializer-damp", DampCallback, &revmodel::setdamp }
119 enum { num_callbacks=sizeof(callbacks)/sizeof(callback_s) };
121 static void DoWork( aout_instance_t *, aout_filter_t *,
122 aout_buffer_t *, aout_buffer_t * );
124 /*****************************************************************************
126 *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
129 aout_filter_t *p_filter = (aout_filter_t *)p_this;
130 aout_filter_sys_t *p_sys;
131 aout_instance_t *p_aout = (aout_instance_t *)p_filter->p_parent;
133 msg_Dbg( p_this, "Opening filter spatializer" );
135 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
136 p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
139 p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
140 p_filter->fmt_out.audio.i_format = VLC_CODEC_FL32;
141 msg_Warn( p_filter, "bad input or output format" );
143 if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
146 memcpy( &p_filter->fmt_out.audio, &p_filter->fmt_in.audio,
147 sizeof(audio_sample_format_t) );
148 msg_Warn( p_filter, "input and output formats are not similar" );
156 p_filter->pf_do_work = DoWork;
157 p_filter->b_in_place = true;
159 /* Allocate structure */
160 p_sys = p_filter->p_sys = (aout_filter_sys_t*)malloc( sizeof( aout_filter_sys_t ) );
164 /* Force new to return 0 on failure instead of throwing, since we don't
165 want an exception to leak back to C code. Bad things would happen. */
166 p_sys->p_reverbm = new (nothrow) revmodel;
167 if( !p_sys->p_reverbm )
173 vlc_mutex_init( &p_sys->lock );
175 for(unsigned i=0;i<num_callbacks;++i)
177 /* NOTE: C++ pointer-to-member function call from table lookup. */
178 (p_sys->p_reverbm->*(callbacks[i].fp_set))
179 (var_CreateGetFloatCommand(p_aout,callbacks[i].psz_name));
180 var_AddCallback( p_aout, callbacks[i].psz_name,
181 callbacks[i].fp_callback, p_sys );
187 /*****************************************************************************
188 * Close: close the plugin
189 *****************************************************************************/
190 static void Close( vlc_object_t *p_this )
192 aout_filter_t *p_filter = (aout_filter_t *)p_this;
193 aout_filter_sys_t *p_sys = p_filter->p_sys;
194 aout_instance_t *p_aout = (aout_instance_t *)p_filter->p_parent;
196 /* Delete the callbacks */
197 for(unsigned i=0;i<num_callbacks;++i)
199 var_DelCallback( p_aout, callbacks[i].psz_name,
200 callbacks[i].fp_callback, p_sys );
203 delete p_sys->p_reverbm;
204 vlc_mutex_destroy( &p_sys->lock );
206 msg_Dbg( p_this, "Closing filter spatializer" );
209 /*****************************************************************************
210 * SpatFilter: process samples buffer
211 * DoWork: call SpatFilter
212 *****************************************************************************/
215 void SpatFilter( aout_instance_t *p_aout, aout_filter_t *p_filter,
216 float *out, float *in, int i_samples, int i_channels )
218 aout_filter_sys_t *p_sys = p_filter->p_sys;
219 vlc_mutex_locker locker( &p_sys->lock );
222 for( i = 0; i < i_samples; i++ )
224 for( ch = 0 ; ch < 2; ch++)
226 in[ch] = in[ch] * SPAT_AMP;
228 p_sys->p_reverbm->processreplace( in, out , 1, i_channels);
234 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
235 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
237 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
238 p_out_buf->i_buffer = p_in_buf->i_buffer;
240 SpatFilter( p_aout, p_filter, (float*)p_out_buf->p_buffer,
241 (float*)p_in_buf->p_buffer, p_in_buf->i_nb_samples,
242 aout_FormatNbChannels( &p_filter->fmt_in.audio ) );
246 /*****************************************************************************
247 * Variables callbacks
248 *****************************************************************************/
250 static int RoomCallback( vlc_object_t *p_this, char const *psz_cmd,
251 vlc_value_t oldval, vlc_value_t newval, void *p_data )
253 (void)psz_cmd; (void)oldval;
254 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
255 vlc_mutex_locker locker( &p_sys->lock );
257 p_sys->p_reverbm->setroomsize(newval.f_float);
258 msg_Dbg( p_this, "room size is now %3.1f", newval.f_float );
262 static int WidthCallback( vlc_object_t *p_this, char const *psz_cmd,
263 vlc_value_t oldval, vlc_value_t newval, void *p_data )
265 (void)psz_cmd; (void)oldval;
266 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
267 vlc_mutex_locker locker( &p_sys->lock );
269 p_sys->p_reverbm->setwidth(newval.f_float);
270 msg_Dbg( p_this, "width is now %3.1f", newval.f_float );
273 static int WetCallback( vlc_object_t *p_this, char const *psz_cmd,
274 vlc_value_t oldval, vlc_value_t newval, void *p_data )
276 (void)psz_cmd; (void)oldval;
277 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
278 vlc_mutex_locker locker( &p_sys->lock );
280 p_sys->p_reverbm->setwet(newval.f_float);
281 msg_Dbg( p_this, "'wet' value is now %3.1f", newval.f_float );
284 static int DryCallback( vlc_object_t *p_this, char const *psz_cmd,
285 vlc_value_t oldval, vlc_value_t newval, void *p_data )
287 (void)psz_cmd; (void)oldval;
288 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
289 vlc_mutex_locker locker( &p_sys->lock );
291 p_sys->p_reverbm->setdry(newval.f_float);
292 msg_Dbg( p_this, "'dry' value is now %3.1f", newval.f_float );
295 static int DampCallback( vlc_object_t *p_this, char const *psz_cmd,
296 vlc_value_t oldval, vlc_value_t newval, void *p_data )
298 (void)psz_cmd; (void)oldval;
299 aout_filter_sys_t *p_sys = (aout_filter_sys_t*)p_data;
300 vlc_mutex_locker locker( &p_sys->lock );
302 p_sys->p_reverbm->setdamp(newval.f_float);
303 msg_Dbg( p_this, "'damp' value is now %3.1f", newval.f_float );