]> git.sesse.net Git - vlc/blob - modules/audio_filter/spatializer/spatializer.cpp
aout_filter_t.(in|out)put -> aout_filter_t.fmt_(in|out).audio
[vlc] / modules / audio_filter / spatializer / spatializer.cpp
1 /*****************************************************************************
2  * spatializer.cpp: sound reverberation
3  *****************************************************************************
4  * Copyright (C) 2004, 2006, 2007 the VideoLAN team
5  *
6  * Google Summer of Code 2007
7  *
8  * Authors: Biodun Osunkunle <biodun@videolan.org>
9  *
10  * Mentor : Jean-Baptiste Kempf <jb@videolan.org>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <math.h>
36
37 #include <new>
38 using std::nothrow;
39
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_aout.h>
43
44 #include "revmodel.hpp"
45 #define SPAT_AMP 0.3
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 static int  Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
52
53 #define ROOMSIZE_TEXT N_("Room size")
54 #define ROOMSIZE_LONGTEXT N_("Defines the virtual surface of the room" \
55                              " emulated by the filter." )
56
57 #define WIDTH_TEXT N_("Room width")
58 #define WIDTH_LONGTEXT N_("Width of the virtual room")
59
60 #define WET_TEXT N_("Wet")
61 #define WET_LONGTEXT NULL
62
63 #define DRY_TEXT N_("Dry")
64 #define DRY_LONGTEXT NULL
65
66 #define DAMP_TEXT N_("Damp")
67 #define DAMP_LONGTEXT NULL
68
69 vlc_module_begin ()
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 )
75
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 )
84 vlc_module_end ()
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89 struct aout_filter_sys_t
90 {
91     vlc_mutex_t lock;
92     revmodel *p_reverbm;
93 };
94
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 );
102
103 #undef  DECLARECB
104
105 struct callback_s {
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);
110 };
111
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 }
118 };
119 enum { num_callbacks=sizeof(callbacks)/sizeof(callback_s) };
120
121 static void DoWork( aout_instance_t *, aout_filter_t *,
122                     aout_buffer_t *, aout_buffer_t * );
123
124 /*****************************************************************************
125  * Open:
126  *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
128 {
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;
132     bool               b_fit = true;
133     msg_Dbg( p_this, "Opening filter spatializer" );
134
135     if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
136         p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
137     {
138         b_fit = false;
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" );
142     }
143     if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
144     {
145         b_fit = false;
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" );
149     }
150
151     if ( ! b_fit )
152     {
153         return VLC_EGENERIC;
154     }
155
156     p_filter->pf_do_work = DoWork;
157     p_filter->b_in_place = true;
158
159      /* Allocate structure */
160     p_sys = p_filter->p_sys = (aout_filter_sys_t*)malloc( sizeof( aout_filter_sys_t ) );
161     if( !p_sys )
162         return VLC_ENOMEM;
163
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 )
168     {
169         free( p_sys );
170         return VLC_ENOMEM;
171     }
172
173     vlc_mutex_init( &p_sys->lock );
174
175     for(unsigned i=0;i<num_callbacks;++i)
176     {
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 );
182     }
183
184     return VLC_SUCCESS;
185 }
186
187 /*****************************************************************************
188  * Close: close the plugin
189  *****************************************************************************/
190 static void Close( vlc_object_t *p_this )
191 {
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;
195
196     /* Delete the callbacks */
197     for(unsigned i=0;i<num_callbacks;++i)
198     {
199         var_DelCallback( p_aout, callbacks[i].psz_name,
200                          callbacks[i].fp_callback, p_sys );
201     }
202
203     delete p_sys->p_reverbm;
204     vlc_mutex_destroy( &p_sys->lock );
205     free( p_sys );
206     msg_Dbg( p_this, "Closing filter spatializer" );
207 }
208
209 /*****************************************************************************
210  * SpatFilter: process samples buffer
211  * DoWork: call SpatFilter
212  *****************************************************************************/
213
214 static inline
215 void SpatFilter( aout_instance_t *p_aout, aout_filter_t *p_filter,
216                  float *out, float *in, int i_samples, int i_channels )
217 {
218     aout_filter_sys_t *p_sys = p_filter->p_sys;
219     vlc_mutex_locker locker( &p_sys->lock );
220
221     int i, ch;
222     for( i = 0; i < i_samples; i++ )
223     {
224         for( ch = 0 ; ch < 2; ch++)
225         {
226             in[ch] = in[ch] * SPAT_AMP;
227         }
228         p_sys->p_reverbm->processreplace( in, out , 1, i_channels);
229         in  += i_channels;
230         out += i_channels;
231     }
232 }
233
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 )
236 {
237     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
238     p_out_buf->i_buffer = p_in_buf->i_buffer;
239
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 ) );
243 }
244
245
246 /*****************************************************************************
247  * Variables callbacks
248  *****************************************************************************/
249
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 )
252 {
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 );
256
257     p_sys->p_reverbm->setroomsize(newval.f_float);
258     msg_Dbg( p_this, "room size is now %3.1f", newval.f_float );
259     return VLC_SUCCESS;
260 }
261
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 )
264 {
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 );
268
269     p_sys->p_reverbm->setwidth(newval.f_float);
270     msg_Dbg( p_this, "width is now %3.1f", newval.f_float );
271     return VLC_SUCCESS;
272 }
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 )
275 {
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 );
279
280     p_sys->p_reverbm->setwet(newval.f_float);
281     msg_Dbg( p_this, "'wet' value is now %3.1f", newval.f_float );
282     return VLC_SUCCESS;
283 }
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 )
286 {
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 );
290
291     p_sys->p_reverbm->setdry(newval.f_float);
292     msg_Dbg( p_this, "'dry' value is now %3.1f", newval.f_float );
293     return VLC_SUCCESS;
294 }
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 )
297 {
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 );
301
302     p_sys->p_reverbm->setdamp(newval.f_float);
303     msg_Dbg( p_this, "'damp' value is now %3.1f", newval.f_float );
304     return VLC_SUCCESS;
305 }
306