]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/headphone.c
d6b07afc46353f91173615e06706999e4fcdff33
[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 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 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      "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     p_data->p_atomic_operations = malloc ( sizeof(struct atomic_operation_t)
188             * p_data->i_nb_atomic_operations );
189     if ( p_data->p_atomic_operations == NULL )
190     {
191         msg_Err( p_filter, "out of memory" );
192         return -1;
193     }
194
195     /* For each virtual speaker, computes elementary wave propagation time
196      * to each ear */
197     i_next_atomic_operation = 0;
198     i_source_channel_offset = 0;
199     if ( i_physical_channels & AOUT_CHAN_LEFT )
200     {
201         ComputeChannelOperations ( p_data , i_rate
202                 , i_next_atomic_operation , i_source_channel_offset
203                 , -d_x , d_z , 2.0 / i_nb_channels );
204         i_next_atomic_operation += 2;
205         i_source_channel_offset++;
206     }
207     if ( i_physical_channels & AOUT_CHAN_RIGHT )
208     {
209         ComputeChannelOperations ( p_data , i_rate
210                 , i_next_atomic_operation , i_source_channel_offset
211                 , d_x , d_z , 2.0 / i_nb_channels );
212         i_next_atomic_operation += 2;
213         i_source_channel_offset++;
214     }
215     if ( i_physical_channels & AOUT_CHAN_REARLEFT )
216     {
217         ComputeChannelOperations ( p_data , i_rate
218                 , i_next_atomic_operation , i_source_channel_offset
219                 , -d_x , d_z_rear , 1.5 / i_nb_channels );
220         i_next_atomic_operation += 2;
221         i_source_channel_offset++;
222     }
223     if ( i_physical_channels & AOUT_CHAN_REARRIGHT )
224     {
225         ComputeChannelOperations ( p_data , i_rate
226                 , i_next_atomic_operation , i_source_channel_offset
227                 , d_x , d_z_rear , 1.5 / i_nb_channels );
228         i_next_atomic_operation += 2;
229         i_source_channel_offset++;
230     }
231     if ( i_physical_channels & AOUT_CHAN_REARCENTER )
232     {
233         ComputeChannelOperations ( p_data , i_rate
234                 , i_next_atomic_operation , i_source_channel_offset
235                 , 0 , -d_z , 1.5 / i_nb_channels );
236         i_next_atomic_operation += 2;
237         i_source_channel_offset++;
238     }
239     if ( i_physical_channels & AOUT_CHAN_CENTER )
240     {
241         ComputeChannelOperations ( p_data , i_rate
242                 , i_next_atomic_operation , i_source_channel_offset
243                 , 0 , d_z , 1.5 / i_nb_channels );
244         i_next_atomic_operation += 2;
245         i_source_channel_offset++;
246     }
247     if ( i_physical_channels & AOUT_CHAN_LFE )
248     {
249         ComputeChannelOperations ( p_data , i_rate
250                 , i_next_atomic_operation , i_source_channel_offset
251                 , 0 , d_z_rear , 5.0 / i_nb_channels );
252         i_next_atomic_operation += 2;
253         i_source_channel_offset++;
254     }
255     if ( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
256     {
257         ComputeChannelOperations ( p_data , i_rate
258                 , i_next_atomic_operation , i_source_channel_offset
259                 , -d_x , 0 , 1.5 / i_nb_channels );
260         i_next_atomic_operation += 2;
261         i_source_channel_offset++;
262     }
263     if ( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
264     {
265         ComputeChannelOperations ( p_data , i_rate
266                 , i_next_atomic_operation , i_source_channel_offset
267                 , d_x , 0 , 1.5 / i_nb_channels );
268         i_next_atomic_operation += 2;
269         i_source_channel_offset++;
270     }
271
272     /* Initialize the overflow buffer
273      * we need it because the process induce a delay in the samples */
274     p_data->i_overflow_buffer_size = 0;
275     for ( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
276     {
277         if ( p_data->i_overflow_buffer_size
278                 < p_data->p_atomic_operations[i].i_delay * i_nb_channels
279                 * sizeof (float) )
280         {
281             p_data->i_overflow_buffer_size
282                 = p_data->p_atomic_operations[i].i_delay * i_nb_channels
283                 * sizeof (float);
284         }
285     }
286     p_data->p_overflow_buffer = malloc ( p_data->i_overflow_buffer_size );
287     if ( p_data->p_atomic_operations == NULL )
288     {
289         msg_Err( p_filter, "out of memory" );
290         return -1;
291     }
292     memset ( p_data->p_overflow_buffer , 0 , p_data->i_overflow_buffer_size );
293
294     /* end */
295     return 0;
296 }
297
298 /*****************************************************************************
299  * Create: allocate headphone downmixer
300  *****************************************************************************/
301 static int Create( vlc_object_t *p_this )
302 {
303     aout_filter_t * p_filter = (aout_filter_t *)p_this;
304
305     if ( p_filter->output.i_physical_channels != ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
306           || p_filter->input.i_format != p_filter->output.i_format
307           || p_filter->input.i_rate != p_filter->output.i_rate
308           || (p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
309                && p_filter->input.i_format != VLC_FOURCC('f','i','3','2')) )
310     {
311         msg_Dbg( p_filter, "Filter discarded (invalid format)" );
312         return -1;
313     }
314
315     /* Allocate the memory needed to store the module's structure */
316     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
317     if ( p_filter->p_sys == NULL )
318     {
319         msg_Err( p_filter, "out of memory" );
320         return -1;
321     }
322     p_filter->p_sys->i_overflow_buffer_size = 0;
323     p_filter->p_sys->p_overflow_buffer = NULL;
324     p_filter->p_sys->i_nb_atomic_operations = 0;
325     p_filter->p_sys->p_atomic_operations = NULL;
326
327     if ( Init( p_filter , p_filter->p_sys
328                 , aout_FormatNbChannels ( &p_filter->input )
329                 , p_filter->input.i_physical_channels
330                 ,  p_filter->input.i_rate ) < 0 )
331     {
332         return -1;
333     }
334
335     p_filter->pf_do_work = DoWork;
336     p_filter->b_in_place = 0;
337
338     return 0;
339 }
340
341 /*****************************************************************************
342  * Destroy: deallocate resources associated with headphone downmixer
343  *****************************************************************************/
344 static void Destroy( vlc_object_t *p_this )
345 {
346     aout_filter_t * p_filter = (aout_filter_t *)p_this;
347
348     if ( p_filter->p_sys != NULL )
349     {
350         if ( p_filter->p_sys->p_overflow_buffer != NULL )
351         {
352             free ( p_filter->p_sys->p_overflow_buffer );
353         }
354         if ( p_filter->p_sys->p_atomic_operations != NULL )
355         {
356             free ( p_filter->p_sys->p_atomic_operations );
357         }
358         free ( p_filter->p_sys );
359         p_filter->p_sys = NULL;
360     }
361 }
362
363 /*****************************************************************************
364  * DoWork: convert a buffer
365  *****************************************************************************/
366 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
367                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
368 {
369     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
370     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
371
372     float * p_in = (float*) p_in_buf->p_buffer;
373     byte_t * p_out;
374     byte_t * p_overflow;
375     byte_t * p_slide;
376
377     size_t i_overflow_size;/* in bytes */
378     size_t i_out_size;/* in bytes */
379
380     unsigned int i, j;
381
382     int i_source_channel_offset;
383     int i_dest_channel_offset;
384     unsigned int i_delay;
385     double d_amplitude_factor;
386
387
388     /* out buffer characterisitcs */
389     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
390     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
391     p_out = p_out_buf->p_buffer;
392     i_out_size = p_out_buf->i_nb_bytes;
393
394     if ( p_filter->p_sys != NULL )
395     {
396         /* Slide the overflow buffer */
397         p_overflow = p_filter->p_sys->p_overflow_buffer;
398         i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
399
400         memset ( p_out , 0 , i_out_size );
401         if ( i_out_size > i_overflow_size )
402             memcpy ( p_out , p_overflow , i_overflow_size );
403         else
404             memcpy ( p_out , p_overflow , i_out_size );
405
406         p_slide = p_filter->p_sys->p_overflow_buffer;
407         while ( p_slide < p_overflow + i_overflow_size )
408         {
409             if ( p_slide + i_out_size < p_overflow + i_overflow_size )
410             {
411                 memset ( p_slide , 0 , i_out_size );
412                 if ( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
413                     memcpy ( p_slide , p_slide + i_out_size , i_out_size );
414                 else
415                     memcpy ( p_slide , p_slide + i_out_size
416                       , p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
417             }
418             else
419             {
420                 memset ( p_slide , 0 , p_overflow + i_overflow_size - p_slide );
421             }
422             p_slide += i_out_size;
423         }
424
425         /* apply the atomic operations */
426         for ( i = 0 ; i < p_filter->p_sys->i_nb_atomic_operations ; i++ )
427         {
428             /* shorter variable names */
429             i_source_channel_offset
430                 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
431             i_dest_channel_offset
432                 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
433             i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
434             d_amplitude_factor
435                 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
436
437             if ( p_out_buf->i_nb_samples > i_delay )
438             {
439                 /* current buffer coefficients */
440                 for ( j = 0 ; j < p_out_buf->i_nb_samples - i_delay ; j++ )
441                 {
442                     ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
443                         += p_in[ j * i_input_nb + i_source_channel_offset ]
444                            * d_amplitude_factor;
445                 }
446
447                 /* overflow buffer coefficients */
448                 for ( j = 0 ; j < i_delay ; j++ )
449                 {
450                     ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
451                         += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
452                            * i_input_nb + i_source_channel_offset ]
453                            * d_amplitude_factor;
454                 }
455             }
456             else
457             {
458                 /* overflow buffer coefficients only */
459                 for ( j = 0 ; j < p_out_buf->i_nb_samples ; j++ )
460                 {
461                     ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
462                         * i_output_nb + i_dest_channel_offset ]
463                         += p_in[ j * i_input_nb + i_source_channel_offset ]
464                            * d_amplitude_factor;
465                 }
466             }
467         }
468     }
469     else
470     {
471         memset ( p_out , 0 , i_out_size );
472     }
473 }