]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/headphone.c
Allow on the fly change of equalizer config. Only restart aout when enabling/disablin...
[vlc] / modules / audio_filter / channel_mixer / headphone.c
1 /*****************************************************************************
2  * headphone.c : headphone virtual spatialization channel mixer module
3  *               -> gives the feeling of a real room with a simple headphone
4  *****************************************************************************
5  * Copyright (C) 2002 VideoLAN
6  * $Id$
7  *
8  * Authors: Boris Dorès <babal@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30 #include <math.h>                                        /* sqrt */
31
32 #include <vlc/vlc.h>
33 #include "audio_output.h"
34 #include "aout_internal.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
42 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
43                         aout_buffer_t * );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 #define MODULE_DESCRIPTION N_ ( \
49      "This effect gives you the feeling that you are standing in a room " \
50      "with a complete 5.1 speaker set when using only a headphone, " \
51      "providing a more realistic sound experience. It should also be " \
52      "more comfortable and less tiring when listening to music for " \
53      "long periods of time.\nIt works with any source format from mono " \
54      "to 5.1.")
55
56 #define HEADPHONE_DIM_TEXT N_("Characteristic dimension")
57 #define HEADPHONE_DIM_LONGTEXT N_( \
58      "Headphone virtual spatialization effect parameter: "\
59      "distance between front left speaker and listener in meters.")
60
61 vlc_module_begin();
62     set_description( N_("headphone channel mixer with virtual spatialization effect") );
63
64     add_integer( "headphone-dim", 10, NULL, HEADPHONE_DIM_TEXT,
65                  HEADPHONE_DIM_LONGTEXT, VLC_FALSE );
66
67     set_capability( "audio filter", 0 );
68     set_callbacks( Create, Destroy );
69     add_shortcut( "headphone" );
70 vlc_module_end();
71
72
73 /*****************************************************************************
74  * Internal data structures
75  *****************************************************************************/
76 struct atomic_operation_t
77 {
78     int i_source_channel_offset;
79     int i_dest_channel_offset;
80     unsigned int i_delay;/* in sample unit */
81     double d_amplitude_factor;
82 };
83
84 struct aout_filter_sys_t
85 {
86     size_t i_overflow_buffer_size;/* in bytes */
87     byte_t * p_overflow_buffer;
88     unsigned int i_nb_atomic_operations;
89     struct atomic_operation_t * p_atomic_operations;
90 };
91
92 /*****************************************************************************
93  * Init: initialize internal data structures
94  * and computes the needed atomic operations
95  *****************************************************************************/
96 /* x and z represent the coordinates of the virtual speaker
97  *  relatively to the center of the listener's head, measured in meters :
98  *
99  *  left              right
100  *Z
101  *-
102  *a          head
103  *x
104  *i
105  *s
106  *  rear left    rear right
107  *
108  *          x-axis
109  *  */
110 static void ComputeChannelOperations ( struct aout_filter_sys_t * p_data
111         , unsigned int i_rate , unsigned int i_next_atomic_operation
112         , int i_source_channel_offset , double d_x , double d_z
113         , double d_channel_amplitude_factor )
114 {
115     double d_c = 340; /*sound celerity (unit: m/s)*/
116
117     /* Left ear */
118     p_data->p_atomic_operations[i_next_atomic_operation]
119         .i_source_channel_offset = i_source_channel_offset;
120     p_data->p_atomic_operations[i_next_atomic_operation]
121         .i_dest_channel_offset = 0;/* left */
122     p_data->p_atomic_operations[i_next_atomic_operation]
123         .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
124                           / d_c * i_rate );
125     if ( d_x < 0 )
126     {
127         p_data->p_atomic_operations[i_next_atomic_operation]
128             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
129     }
130     else if ( d_x > 0 )
131     {
132         p_data->p_atomic_operations[i_next_atomic_operation]
133             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
134     }
135     else
136     {
137         p_data->p_atomic_operations[i_next_atomic_operation]
138             .d_amplitude_factor = d_channel_amplitude_factor / 2;
139     }
140
141     /* Right ear */
142     p_data->p_atomic_operations[i_next_atomic_operation + 1]
143         .i_source_channel_offset = i_source_channel_offset;
144     p_data->p_atomic_operations[i_next_atomic_operation + 1]
145         .i_dest_channel_offset = 1;/* right */
146     p_data->p_atomic_operations[i_next_atomic_operation + 1]
147         .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
148                           / d_c * i_rate );
149     if ( d_x < 0 )
150     {
151         p_data->p_atomic_operations[i_next_atomic_operation + 1]
152             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
153     }
154     else if ( d_x > 0 )
155     {
156         p_data->p_atomic_operations[i_next_atomic_operation + 1]
157             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
158     }
159     else
160     {
161         p_data->p_atomic_operations[i_next_atomic_operation + 1]
162             .d_amplitude_factor = d_channel_amplitude_factor / 2;
163     }
164 }
165
166 static int Init ( aout_filter_t * p_filter , struct aout_filter_sys_t * p_data
167         , unsigned int i_nb_channels , uint32_t i_physical_channels
168         , unsigned int i_rate )
169 {
170     double d_x = config_GetInt ( p_filter , "headphone-dim" );
171     double d_z = d_x;
172     double d_z_rear = -d_x/3;
173     unsigned int i_next_atomic_operation;
174     int i_source_channel_offset;
175     unsigned int i;
176
177     if ( p_data == NULL )
178     {
179         msg_Dbg ( p_filter, "passing a null pointer as argument" );
180         return 0;
181     }
182
183     /* Number of elementary operations */
184     p_data->i_nb_atomic_operations = i_nb_channels * 2;
185     p_data->p_atomic_operations = malloc ( sizeof(struct atomic_operation_t)
186             * p_data->i_nb_atomic_operations );
187     if ( p_data->p_atomic_operations == NULL )
188     {
189         msg_Err( p_filter, "out of memory" );
190         return -1;
191     }
192
193     /* For each virtual speaker, computes elementary wave propagation time
194      * to each ear */
195     i_next_atomic_operation = 0;
196     i_source_channel_offset = 0;
197     if ( i_physical_channels & AOUT_CHAN_LEFT )
198     {
199         ComputeChannelOperations ( p_data , i_rate
200                 , i_next_atomic_operation , i_source_channel_offset
201                 , -d_x , d_z , 2.0 / i_nb_channels );
202         i_next_atomic_operation += 2;
203         i_source_channel_offset++;
204     }
205     if ( i_physical_channels & AOUT_CHAN_RIGHT )
206     {
207         ComputeChannelOperations ( p_data , i_rate
208                 , i_next_atomic_operation , i_source_channel_offset
209                 , d_x , d_z , 2.0 / i_nb_channels );
210         i_next_atomic_operation += 2;
211         i_source_channel_offset++;
212     }
213     if ( i_physical_channels & AOUT_CHAN_REARLEFT )
214     {
215         ComputeChannelOperations ( p_data , i_rate
216                 , i_next_atomic_operation , i_source_channel_offset
217                 , -d_x , d_z_rear , 1.5 / i_nb_channels );
218         i_next_atomic_operation += 2;
219         i_source_channel_offset++;
220     }
221     if ( i_physical_channels & AOUT_CHAN_REARRIGHT )
222     {
223         ComputeChannelOperations ( p_data , i_rate
224                 , i_next_atomic_operation , i_source_channel_offset
225                 , d_x , d_z_rear , 1.5 / i_nb_channels );
226         i_next_atomic_operation += 2;
227         i_source_channel_offset++;
228     }
229     if ( i_physical_channels & AOUT_CHAN_REARCENTER )
230     {
231         ComputeChannelOperations ( p_data , i_rate
232                 , i_next_atomic_operation , i_source_channel_offset
233                 , 0 , -d_z , 1.5 / i_nb_channels );
234         i_next_atomic_operation += 2;
235         i_source_channel_offset++;
236     }
237     if ( i_physical_channels & AOUT_CHAN_CENTER )
238     {
239         ComputeChannelOperations ( p_data , i_rate
240                 , i_next_atomic_operation , i_source_channel_offset
241                 , 0 , d_z , 1.5 / i_nb_channels );
242         i_next_atomic_operation += 2;
243         i_source_channel_offset++;
244     }
245     if ( i_physical_channels & AOUT_CHAN_LFE )
246     {
247         ComputeChannelOperations ( p_data , i_rate
248                 , i_next_atomic_operation , i_source_channel_offset
249                 , 0 , d_z_rear , 5.0 / i_nb_channels );
250         i_next_atomic_operation += 2;
251         i_source_channel_offset++;
252     }
253     if ( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
254     {
255         ComputeChannelOperations ( p_data , i_rate
256                 , i_next_atomic_operation , i_source_channel_offset
257                 , -d_x , 0 , 1.5 / i_nb_channels );
258         i_next_atomic_operation += 2;
259         i_source_channel_offset++;
260     }
261     if ( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
262     {
263         ComputeChannelOperations ( p_data , i_rate
264                 , i_next_atomic_operation , i_source_channel_offset
265                 , d_x , 0 , 1.5 / i_nb_channels );
266         i_next_atomic_operation += 2;
267         i_source_channel_offset++;
268     }
269
270     /* Initialize the overflow buffer
271      * we need it because the process induce a delay in the samples */
272     p_data->i_overflow_buffer_size = 0;
273     for ( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
274     {
275         if ( p_data->i_overflow_buffer_size
276                 < p_data->p_atomic_operations[i].i_delay * i_nb_channels
277                 * sizeof (float) )
278         {
279             p_data->i_overflow_buffer_size
280                 = p_data->p_atomic_operations[i].i_delay * i_nb_channels
281                 * sizeof (float);
282         }
283     }
284     p_data->p_overflow_buffer = malloc ( p_data->i_overflow_buffer_size );
285     if ( p_data->p_atomic_operations == NULL )
286     {
287         msg_Err( p_filter, "out of memory" );
288         return -1;
289     }
290     memset ( p_data->p_overflow_buffer , 0 , p_data->i_overflow_buffer_size );
291
292     /* end */
293     return 0;
294 }
295
296 /*****************************************************************************
297  * Create: allocate headphone downmixer
298  *****************************************************************************/
299 static int Create( vlc_object_t *p_this )
300 {
301     aout_filter_t * p_filter = (aout_filter_t *)p_this;
302
303     if ( p_filter->output.i_physical_channels != ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
304           || p_filter->input.i_format != p_filter->output.i_format
305           || p_filter->input.i_rate != p_filter->output.i_rate
306           || (p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
307                && p_filter->input.i_format != VLC_FOURCC('f','i','3','2')) )
308     {
309         msg_Dbg( p_filter, "Filter discarded (invalid format)" );
310         return -1;
311     }
312
313     /* Allocate the memory needed to store the module's structure */
314     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
315     if ( p_filter->p_sys == NULL )
316     {
317         msg_Err( p_filter, "out of memory" );
318         return -1;
319     }
320     p_filter->p_sys->i_overflow_buffer_size = 0;
321     p_filter->p_sys->p_overflow_buffer = NULL;
322     p_filter->p_sys->i_nb_atomic_operations = 0;
323     p_filter->p_sys->p_atomic_operations = NULL;
324
325     if ( Init( p_filter , p_filter->p_sys
326                 , aout_FormatNbChannels ( &p_filter->input )
327                 , p_filter->input.i_physical_channels
328                 ,  p_filter->input.i_rate ) < 0 )
329     {
330         return -1;
331     }
332
333     p_filter->pf_do_work = DoWork;
334     p_filter->b_in_place = 0;
335
336     return 0;
337 }
338
339 /*****************************************************************************
340  * Destroy: deallocate resources associated with headphone downmixer
341  *****************************************************************************/
342 static void Destroy( vlc_object_t *p_this )
343 {
344     aout_filter_t * p_filter = (aout_filter_t *)p_this;
345
346     if ( p_filter->p_sys != NULL )
347     {
348         if ( p_filter->p_sys->p_overflow_buffer != NULL )
349         {
350             free ( p_filter->p_sys->p_overflow_buffer );
351         }
352         if ( p_filter->p_sys->p_atomic_operations != NULL )
353         {
354             free ( p_filter->p_sys->p_atomic_operations );
355         }
356         free ( p_filter->p_sys );
357         p_filter->p_sys = NULL;
358     }
359 }
360
361 /*****************************************************************************
362  * DoWork: convert a buffer
363  *****************************************************************************/
364 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
365                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
366 {
367     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
368     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
369
370     float * p_in = (float*) p_in_buf->p_buffer;
371     byte_t * p_out;
372     byte_t * p_overflow;
373     byte_t * p_slide;
374
375     size_t i_overflow_size;/* in bytes */
376     size_t i_out_size;/* in bytes */
377
378     unsigned int i, j;
379
380     int i_source_channel_offset;
381     int i_dest_channel_offset;
382     unsigned int i_delay;
383     double d_amplitude_factor;
384
385
386     /* out buffer characterisitcs */
387     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
388     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
389     p_out = p_out_buf->p_buffer;
390     i_out_size = p_out_buf->i_nb_bytes;
391
392     if ( p_filter->p_sys != NULL )
393     {
394         /* Slide the overflow buffer */
395         p_overflow = p_filter->p_sys->p_overflow_buffer;
396         i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
397
398         memset ( p_out , 0 , i_out_size );
399         if ( i_out_size > i_overflow_size )
400             memcpy ( p_out , p_overflow , i_overflow_size );
401         else
402             memcpy ( p_out , p_overflow , i_out_size );
403
404         p_slide = p_filter->p_sys->p_overflow_buffer;
405         while ( p_slide < p_overflow + i_overflow_size )
406         {
407             if ( p_slide + i_out_size < p_overflow + i_overflow_size )
408             {
409                 memset ( p_slide , 0 , i_out_size );
410                 if ( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
411                     memcpy ( p_slide , p_slide + i_out_size , i_out_size );
412                 else
413                     memcpy ( p_slide , p_slide + i_out_size
414                       , p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
415             }
416             else
417             {
418                 memset ( p_slide , 0 , p_overflow + i_overflow_size - p_slide );
419             }
420             p_slide += i_out_size;
421         }
422
423         /* apply the atomic operations */
424         for ( i = 0 ; i < p_filter->p_sys->i_nb_atomic_operations ; i++ )
425         {
426             /* shorter variable names */
427             i_source_channel_offset
428                 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
429             i_dest_channel_offset
430                 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
431             i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
432             d_amplitude_factor
433                 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
434
435             if ( p_out_buf->i_nb_samples > i_delay )
436             {
437                 /* current buffer coefficients */
438                 for ( j = 0 ; j < p_out_buf->i_nb_samples - i_delay ; j++ )
439                 {
440                     ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
441                         += p_in[ j * i_input_nb + i_source_channel_offset ]
442                            * d_amplitude_factor;
443                 }
444
445                 /* overflow buffer coefficients */
446                 for ( j = 0 ; j < i_delay ; j++ )
447                 {
448                     ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
449                         += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
450                            * i_input_nb + i_source_channel_offset ]
451                            * d_amplitude_factor;
452                 }
453             }
454             else
455             {
456                 /* overflow buffer coefficients only */
457                 for ( j = 0 ; j < p_out_buf->i_nb_samples ; j++ )
458                 {
459                     ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
460                         * i_output_nb + i_dest_channel_offset ]
461                         += p_in[ j * i_input_nb + i_source_channel_offset ]
462                            * d_amplitude_factor;
463                 }
464             }
465         }
466     }
467     else
468     {
469         memset ( p_out , 0 , i_out_size );
470     }
471 }