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