]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/headphone.c
Audio strings, except parametric equalizer (I just don't understand anything) - Refs...
[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-2005 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 7.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 7.1.")
55
56 #define HEADPHONE_DIM_TEXT N_("Characteristic dimension")
57 #define HEADPHONE_DIM_LONGTEXT N_( \
58      "Distance between front left speaker and listener in meters.")
59
60 #define HEADPHONE_COMPENSATE_TEXT N_("Compensate delay")
61 #define HEADPHONE_COMPENSATE_LONGTEXT N_( \
62      "The delay which is introduced by the physical algorithm may "\
63      "sometimes be disturbing for the lipsync. In that case, turn "\
64      "this on to compensate.")
65
66 #define HEADPHONE_DOLBY_TEXT N_("No decoding of Dolby Surround")
67 #define HEADPHONE_DOLBY_LONGTEXT N_( \
68      "Dolby Surround encoded streams won't be decoded before being " \
69      "processed by this filter. Enabling this setting is not recommended.")
70
71 vlc_module_begin();
72     set_description( N_("Headphone virtual spatialization effect") );
73     set_shortname( _("Headphone effect") );
74     set_category( CAT_AUDIO );
75     set_subcategory( SUBCAT_AUDIO_AFILTER );
76
77     add_integer( "headphone-dim", 10, NULL, HEADPHONE_DIM_TEXT,
78                  HEADPHONE_DIM_LONGTEXT, VLC_FALSE );
79     add_bool( "headphone-compensate", 0, NULL, HEADPHONE_COMPENSATE_TEXT,
80               HEADPHONE_COMPENSATE_LONGTEXT, VLC_TRUE );
81     add_bool( "headphone-dolby", 0, NULL, HEADPHONE_DOLBY_TEXT,
82               HEADPHONE_DOLBY_LONGTEXT, VLC_TRUE );
83
84     set_capability( "audio filter", 0 );
85     set_callbacks( Create, Destroy );
86     add_shortcut( "headphone" );
87 vlc_module_end();
88
89
90 /*****************************************************************************
91  * Internal data structures
92  *****************************************************************************/
93 struct atomic_operation_t
94 {
95     int i_source_channel_offset;
96     int i_dest_channel_offset;
97     unsigned int i_delay;/* in sample unit */
98     double d_amplitude_factor;
99 };
100
101 struct aout_filter_sys_t
102 {
103     size_t i_overflow_buffer_size;/* in bytes */
104     byte_t * p_overflow_buffer;
105     unsigned int i_nb_atomic_operations;
106     struct atomic_operation_t * p_atomic_operations;
107 };
108
109 /*****************************************************************************
110  * Init: initialize internal data structures
111  * and computes the needed atomic operations
112  *****************************************************************************/
113 /* x and z represent the coordinates of the virtual speaker
114  *  relatively to the center of the listener's head, measured in meters :
115  *
116  *  left              right
117  *Z
118  *-
119  *a          head
120  *x
121  *i
122  *s
123  *  rear left    rear right
124  *
125  *          x-axis
126  *  */
127 static void ComputeChannelOperations ( struct aout_filter_sys_t * p_data
128         , unsigned int i_rate , unsigned int i_next_atomic_operation
129         , int i_source_channel_offset , double d_x , double d_z
130         , double d_compensation_length , double d_channel_amplitude_factor )
131 {
132     double d_c = 340; /*sound celerity (unit: m/s)*/
133     double d_compensation_delay = (d_compensation_length-0.1) / d_c * i_rate;
134
135     /* Left ear */
136     p_data->p_atomic_operations[i_next_atomic_operation]
137         .i_source_channel_offset = i_source_channel_offset;
138     p_data->p_atomic_operations[i_next_atomic_operation]
139         .i_dest_channel_offset = 0;/* left */
140     p_data->p_atomic_operations[i_next_atomic_operation]
141         .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
142                           / d_c * i_rate - d_compensation_delay );
143     if ( d_x < 0 )
144     {
145         p_data->p_atomic_operations[i_next_atomic_operation]
146             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
147     }
148     else if ( d_x > 0 )
149     {
150         p_data->p_atomic_operations[i_next_atomic_operation]
151             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
152     }
153     else
154     {
155         p_data->p_atomic_operations[i_next_atomic_operation]
156             .d_amplitude_factor = d_channel_amplitude_factor / 2;
157     }
158
159     /* Right ear */
160     p_data->p_atomic_operations[i_next_atomic_operation + 1]
161         .i_source_channel_offset = i_source_channel_offset;
162     p_data->p_atomic_operations[i_next_atomic_operation + 1]
163         .i_dest_channel_offset = 1;/* right */
164     p_data->p_atomic_operations[i_next_atomic_operation + 1]
165         .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
166                           / d_c * i_rate - d_compensation_delay );
167     if ( d_x < 0 )
168     {
169         p_data->p_atomic_operations[i_next_atomic_operation + 1]
170             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
171     }
172     else if ( d_x > 0 )
173     {
174         p_data->p_atomic_operations[i_next_atomic_operation + 1]
175             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
176     }
177     else
178     {
179         p_data->p_atomic_operations[i_next_atomic_operation + 1]
180             .d_amplitude_factor = d_channel_amplitude_factor / 2;
181     }
182 }
183
184 static int Init ( aout_filter_t * p_filter , struct aout_filter_sys_t * p_data
185         , unsigned int i_nb_channels , uint32_t i_physical_channels
186         , unsigned int i_rate )
187 {
188     double d_x = config_GetInt ( p_filter , "headphone-dim" );
189     double d_z = d_x;
190     double d_z_rear = -d_x/3;
191     double d_min = 0;
192     unsigned int i_next_atomic_operation;
193     int i_source_channel_offset;
194     unsigned int i;
195
196     if ( p_data == NULL )
197     {
198         msg_Dbg ( p_filter, "passing a null pointer as argument" );
199         return 0;
200     }
201
202     if ( config_GetInt ( p_filter , "headphone-compensate" ) )
203     {
204         /* minimal distance to any speaker */
205         if ( i_physical_channels & AOUT_CHAN_REARCENTER )
206         {
207             d_min = d_z_rear;
208         }
209         else
210         {
211             d_min = d_z;
212         }
213     }
214
215     /* Number of elementary operations */
216     p_data->i_nb_atomic_operations = i_nb_channels * 2;
217     if ( i_physical_channels & AOUT_CHAN_CENTER )
218     {
219         p_data->i_nb_atomic_operations += 2;
220     }
221     p_data->p_atomic_operations = malloc ( sizeof(struct atomic_operation_t)
222             * p_data->i_nb_atomic_operations );
223     if ( p_data->p_atomic_operations == NULL )
224     {
225         msg_Err( p_filter, "out of memory" );
226         return -1;
227     }
228
229     /* For each virtual speaker, computes elementary wave propagation time
230      * to each ear */
231     i_next_atomic_operation = 0;
232     i_source_channel_offset = 0;
233     if ( i_physical_channels & AOUT_CHAN_LEFT )
234     {
235         ComputeChannelOperations ( p_data , i_rate
236                 , i_next_atomic_operation , i_source_channel_offset
237                 , -d_x , d_z , d_min , 2.0 / i_nb_channels );
238         i_next_atomic_operation += 2;
239         i_source_channel_offset++;
240     }
241     if ( i_physical_channels & AOUT_CHAN_RIGHT )
242     {
243         ComputeChannelOperations ( p_data , i_rate
244                 , i_next_atomic_operation , i_source_channel_offset
245                 , d_x , d_z , d_min , 2.0 / i_nb_channels );
246         i_next_atomic_operation += 2;
247         i_source_channel_offset++;
248     }
249     if ( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
250     {
251         ComputeChannelOperations ( p_data , i_rate
252                 , i_next_atomic_operation , i_source_channel_offset
253                 , -d_x , 0 , d_min , 1.5 / i_nb_channels );
254         i_next_atomic_operation += 2;
255         i_source_channel_offset++;
256     }
257     if ( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
258     {
259         ComputeChannelOperations ( p_data , i_rate
260                 , i_next_atomic_operation , i_source_channel_offset
261                 , d_x , 0 , d_min , 1.5 / i_nb_channels );
262         i_next_atomic_operation += 2;
263         i_source_channel_offset++;
264     }
265     if ( i_physical_channels & AOUT_CHAN_REARLEFT )
266     {
267         ComputeChannelOperations ( p_data , i_rate
268                 , i_next_atomic_operation , i_source_channel_offset
269                 , -d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
270         i_next_atomic_operation += 2;
271         i_source_channel_offset++;
272     }
273     if ( i_physical_channels & AOUT_CHAN_REARRIGHT )
274     {
275         ComputeChannelOperations ( p_data , i_rate
276                 , i_next_atomic_operation , i_source_channel_offset
277                 , d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
278         i_next_atomic_operation += 2;
279         i_source_channel_offset++;
280     }
281     if ( i_physical_channels & AOUT_CHAN_REARCENTER )
282     {
283         ComputeChannelOperations ( p_data , i_rate
284                 , i_next_atomic_operation , i_source_channel_offset
285                 , 0 , -d_z , d_min , 1.5 / i_nb_channels );
286         i_next_atomic_operation += 2;
287         i_source_channel_offset++;
288     }
289     if ( i_physical_channels & AOUT_CHAN_CENTER )
290     {
291         /* having two center channels increases the spatialization effect */
292         ComputeChannelOperations ( p_data , i_rate
293                 , i_next_atomic_operation , i_source_channel_offset
294                 , d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
295         i_next_atomic_operation += 2;
296         ComputeChannelOperations ( p_data , i_rate
297                 , i_next_atomic_operation , i_source_channel_offset
298                 , -d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
299         i_next_atomic_operation += 2;
300         i_source_channel_offset++;
301     }
302     if ( i_physical_channels & AOUT_CHAN_LFE )
303     {
304         ComputeChannelOperations ( p_data , i_rate
305                 , i_next_atomic_operation , i_source_channel_offset
306                 , 0 , d_z_rear , d_min , 5.0 / i_nb_channels );
307         i_next_atomic_operation += 2;
308         i_source_channel_offset++;
309     }
310
311     /* Initialize the overflow buffer
312      * we need it because the process induce a delay in the samples */
313     p_data->i_overflow_buffer_size = 0;
314     for ( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
315     {
316         if ( p_data->i_overflow_buffer_size
317                 < p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float) )
318         {
319             p_data->i_overflow_buffer_size
320                 = p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float);
321         }
322     }
323     p_data->p_overflow_buffer = malloc ( p_data->i_overflow_buffer_size );
324     if ( p_data->p_atomic_operations == NULL )
325     {
326         msg_Err( p_filter, "out of memory" );
327         return -1;
328     }
329     memset ( p_data->p_overflow_buffer , 0 , p_data->i_overflow_buffer_size );
330
331     /* end */
332     return 0;
333 }
334
335 /*****************************************************************************
336  * Create: allocate headphone downmixer
337  *****************************************************************************/
338 static int Create( vlc_object_t *p_this )
339 {
340     aout_filter_t * p_filter = (aout_filter_t *)p_this;
341     vlc_bool_t b_fit = VLC_TRUE;
342
343     /* Activate this filter only with stereo devices */
344     if ( p_filter->output.i_physical_channels
345             != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
346     {
347         msg_Dbg( p_filter, "Filter discarded (incompatible format)" );
348         return VLC_EGENERIC;
349     }
350
351     /* Request a specific format if not already compatible */
352     if ( p_filter->input.i_original_channels
353             != p_filter->output.i_original_channels )
354     {
355         b_fit = VLC_FALSE;
356         p_filter->input.i_original_channels =
357                                         p_filter->output.i_original_channels;
358     }
359     if ( p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
360           || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
361     {
362         b_fit = VLC_FALSE;
363         p_filter->input.i_format = VLC_FOURCC('f','l','3','2');
364         p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
365     }
366     if ( p_filter->input.i_rate != p_filter->output.i_rate )
367     {
368         b_fit = VLC_FALSE;
369         p_filter->input.i_rate = p_filter->output.i_rate;
370     }
371     if ( p_filter->input.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
372           && ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
373           && ! config_GetInt ( p_filter , "headphone-dolby" ) )
374     {
375         b_fit = VLC_FALSE;
376         p_filter->input.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
377                                               AOUT_CHAN_CENTER |
378                                               AOUT_CHAN_REARLEFT |
379                                               AOUT_CHAN_REARRIGHT;
380     }
381     if ( ! b_fit )
382     {
383         msg_Dbg( p_filter, "Requesting specific format" );
384         return VLC_EGENERIC;
385     }
386
387     /* Allocate the memory needed to store the module's structure */
388     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
389     if ( p_filter->p_sys == NULL )
390     {
391         msg_Err( p_filter, "Out of memory" );
392         return VLC_EGENERIC;
393     }
394     p_filter->p_sys->i_overflow_buffer_size = 0;
395     p_filter->p_sys->p_overflow_buffer = NULL;
396     p_filter->p_sys->i_nb_atomic_operations = 0;
397     p_filter->p_sys->p_atomic_operations = NULL;
398
399     if ( Init( p_filter , p_filter->p_sys
400                 , aout_FormatNbChannels ( &p_filter->input )
401                 , p_filter->input.i_physical_channels
402                 , p_filter->input.i_rate ) < 0 )
403     {
404         return VLC_EGENERIC;
405     }
406
407     p_filter->pf_do_work = DoWork;
408     p_filter->b_in_place = 0;
409
410     return VLC_SUCCESS;
411 }
412
413 /*****************************************************************************
414  * Destroy: deallocate resources associated with headphone downmixer
415  *****************************************************************************/
416 static void Destroy( vlc_object_t *p_this )
417 {
418     aout_filter_t * p_filter = (aout_filter_t *)p_this;
419
420     if ( p_filter->p_sys != NULL )
421     {
422         if ( p_filter->p_sys->p_overflow_buffer != NULL )
423         {
424             free ( p_filter->p_sys->p_overflow_buffer );
425         }
426         if ( p_filter->p_sys->p_atomic_operations != NULL )
427         {
428             free ( p_filter->p_sys->p_atomic_operations );
429         }
430         free ( p_filter->p_sys );
431         p_filter->p_sys = NULL;
432     }
433 }
434
435 /*****************************************************************************
436  * DoWork: convert a buffer
437  *****************************************************************************/
438 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
439                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
440 {
441     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
442     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
443
444     float * p_in = (float*) p_in_buf->p_buffer;
445     byte_t * p_out;
446     byte_t * p_overflow;
447     byte_t * p_slide;
448
449     size_t i_overflow_size;/* in bytes */
450     size_t i_out_size;/* in bytes */
451
452     unsigned int i, j;
453
454     int i_source_channel_offset;
455     int i_dest_channel_offset;
456     unsigned int i_delay;
457     double d_amplitude_factor;
458
459
460     /* out buffer characterisitcs */
461     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
462     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
463     p_out = p_out_buf->p_buffer;
464     i_out_size = p_out_buf->i_nb_bytes;
465
466     if ( p_filter->p_sys != NULL )
467     {
468         /* Slide the overflow buffer */
469         p_overflow = p_filter->p_sys->p_overflow_buffer;
470         i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
471
472         memset ( p_out , 0 , i_out_size );
473         if ( i_out_size > i_overflow_size )
474             memcpy ( p_out , p_overflow , i_overflow_size );
475         else
476             memcpy ( p_out , p_overflow , i_out_size );
477
478         p_slide = p_filter->p_sys->p_overflow_buffer;
479         while ( p_slide < p_overflow + i_overflow_size )
480         {
481             if ( p_slide + i_out_size < p_overflow + i_overflow_size )
482             {
483                 memset ( p_slide , 0 , i_out_size );
484                 if ( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
485                     memcpy ( p_slide , p_slide + i_out_size , i_out_size );
486                 else
487                     memcpy ( p_slide , p_slide + i_out_size
488                       , p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
489             }
490             else
491             {
492                 memset ( p_slide , 0 , p_overflow + i_overflow_size - p_slide );
493             }
494             p_slide += i_out_size;
495         }
496
497         /* apply the atomic operations */
498         for ( i = 0 ; i < p_filter->p_sys->i_nb_atomic_operations ; i++ )
499         {
500             /* shorter variable names */
501             i_source_channel_offset
502                 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
503             i_dest_channel_offset
504                 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
505             i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
506             d_amplitude_factor
507                 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
508
509             if ( p_out_buf->i_nb_samples > i_delay )
510             {
511                 /* current buffer coefficients */
512                 for ( j = 0 ; j < p_out_buf->i_nb_samples - i_delay ; j++ )
513                 {
514                     ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
515                         += p_in[ j * i_input_nb + i_source_channel_offset ]
516                            * d_amplitude_factor;
517                 }
518
519                 /* overflow buffer coefficients */
520                 for ( j = 0 ; j < i_delay ; j++ )
521                 {
522                     ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
523                         += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
524                            * i_input_nb + i_source_channel_offset ]
525                            * d_amplitude_factor;
526                 }
527             }
528             else
529             {
530                 /* overflow buffer coefficients only */
531                 for ( j = 0 ; j < p_out_buf->i_nb_samples ; j++ )
532                 {
533                     ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
534                         * i_output_nb + i_dest_channel_offset ]
535                         += p_in[ j * i_input_nb + i_source_channel_offset ]
536                            * d_amplitude_factor;
537                 }
538             }
539         }
540     }
541     else
542     {
543         memset ( p_out , 0 , i_out_size );
544     }
545 }