]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/headphone.c
- new headphone channel mixer with virtual spatialization effect : This
[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: headphone.c,v 1.1 2002/12/09 00:52:42 babal Exp $
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 vlc_module_begin();
49     set_description( _("headphone channel mixer with virtual spatialization effect") );
50     set_capability( "audio filter", 0 );
51     set_callbacks( Create, Destroy );
52     add_shortcut( "headphone" );
53 vlc_module_end();
54
55
56 /*****************************************************************************
57  * Internal data structures
58  *****************************************************************************/
59 struct atomic_operation_t
60 {
61     int i_source_channel_offset;
62     int i_dest_channel_offset;
63     unsigned int i_delay;/* in sample unit */
64     double d_amplitude_factor;
65 };
66
67 struct aout_filter_sys_t
68 {
69     size_t i_overflow_buffer_size;/* in bytes */
70     byte_t * p_overflow_buffer;
71     unsigned int i_nb_atomic_operations;
72     struct atomic_operation_t * p_atomic_operations;
73 };
74
75 /*****************************************************************************
76  * Init: initialize internal data structures
77  * and computes the needed atomic operations
78  *****************************************************************************/
79 /* x and z represent the coordinates of the virtual speaker
80  *  relatively to the center of the listener's head, measured in meters :
81  *
82  *  left              right
83  *Z
84  *-
85  *a          head
86  *x
87  *i
88  *s
89  *  rear left    rear right
90  *
91  *          x-axis
92  *  */
93 static void ComputeChannelOperations ( struct aout_filter_sys_t * p_data
94         , unsigned int i_rate , unsigned int i_next_atomic_operation
95         , int i_source_channel_offset , double d_x , double d_z
96         , double d_channel_amplitude_factor )
97 {
98     double d_c = 340; /*sound celerity (unit: m/s)*/
99
100     /* Left ear */
101     p_data->p_atomic_operations[i_next_atomic_operation]
102         .i_source_channel_offset = i_source_channel_offset;
103     p_data->p_atomic_operations[i_next_atomic_operation]
104         .i_dest_channel_offset = 0;/* left */
105     p_data->p_atomic_operations[i_next_atomic_operation]
106         .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
107                           / d_c * i_rate );
108     if ( d_x < 0 )
109     {
110         p_data->p_atomic_operations[i_next_atomic_operation]
111             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
112     }
113     else if ( d_x > 0 )
114     {
115         p_data->p_atomic_operations[i_next_atomic_operation]
116             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
117     }
118     else
119     {
120         p_data->p_atomic_operations[i_next_atomic_operation]
121             .d_amplitude_factor = d_channel_amplitude_factor / 2;
122     }
123
124     /* Right ear */
125     p_data->p_atomic_operations[i_next_atomic_operation + 1]
126         .i_source_channel_offset = i_source_channel_offset;
127     p_data->p_atomic_operations[i_next_atomic_operation + 1]
128         .i_dest_channel_offset = 1;/* right */
129     p_data->p_atomic_operations[i_next_atomic_operation + 1]
130         .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
131                           / d_c * i_rate );
132     if ( d_x < 0 )
133     {
134         p_data->p_atomic_operations[i_next_atomic_operation + 1]
135             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
136     }
137     else if ( d_x > 0 )
138     {
139         p_data->p_atomic_operations[i_next_atomic_operation + 1]
140             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
141     }
142     else
143     {
144         p_data->p_atomic_operations[i_next_atomic_operation + 1]
145             .d_amplitude_factor = d_channel_amplitude_factor / 2;
146     }
147 }
148
149 static int Init ( aout_filter_t * p_filter , struct aout_filter_sys_t * p_data
150         , unsigned int i_nb_channels , uint32_t i_physical_channels
151         , unsigned int i_rate )
152 {
153     double d_x = config_GetInt ( p_filter , "headphone-dim" );
154     double d_z = d_x;
155     double d_z_rear = -d_x/3;
156     unsigned int i_next_atomic_operation;
157     int i_source_channel_offset;
158     unsigned int i;
159
160     if ( p_data == NULL )
161     {
162         msg_Dbg ( p_filter, "passing a null pointer as argument" );
163         return 0;
164     }
165
166     /* Number of elementary operations */
167     p_data->i_nb_atomic_operations = i_nb_channels * 2;
168     p_data->p_atomic_operations = malloc ( sizeof(struct atomic_operation_t)
169             * p_data->i_nb_atomic_operations );
170     if ( p_data->p_atomic_operations == NULL )
171     {
172         msg_Err( p_filter, "out of memory" );
173         return -1;
174     }
175
176     /* For each virtual speaker, computes elementary wave propagation time
177      * to each ear */
178     i_next_atomic_operation = 0;
179     i_source_channel_offset = 0;
180     if ( i_physical_channels & AOUT_CHAN_LEFT )
181     {
182         ComputeChannelOperations ( p_data , i_rate
183                 , i_next_atomic_operation , i_source_channel_offset
184                 , -d_x , d_z , 2.0 / i_nb_channels );
185         i_next_atomic_operation += 2;
186         i_source_channel_offset++;
187     }
188     if ( i_physical_channels & AOUT_CHAN_RIGHT )
189     {
190         ComputeChannelOperations ( p_data , i_rate
191                 , i_next_atomic_operation , i_source_channel_offset
192                 , d_x , d_z , 2.0 / i_nb_channels );
193         i_next_atomic_operation += 2;
194         i_source_channel_offset++;
195     }
196     if ( i_physical_channels & AOUT_CHAN_REARLEFT )
197     {
198         ComputeChannelOperations ( p_data , i_rate
199                 , i_next_atomic_operation , i_source_channel_offset
200                 , -d_x , d_z_rear , 1.5 / i_nb_channels );
201         i_next_atomic_operation += 2;
202         i_source_channel_offset++;
203     }
204     if ( i_physical_channels & AOUT_CHAN_REARRIGHT )
205     {
206         ComputeChannelOperations ( p_data , i_rate
207                 , i_next_atomic_operation , i_source_channel_offset
208                 , d_x , d_z_rear , 1.5 / i_nb_channels );
209         i_next_atomic_operation += 2;
210         i_source_channel_offset++;
211     }
212     if ( i_physical_channels & AOUT_CHAN_CENTER )
213     {
214         ComputeChannelOperations ( p_data , i_rate
215                 , i_next_atomic_operation , i_source_channel_offset
216                 , 0 , d_z , 1.5 / i_nb_channels );
217         i_next_atomic_operation += 2;
218         i_source_channel_offset++;
219     }
220     if ( i_physical_channels & AOUT_CHAN_LFE )
221     {
222         ComputeChannelOperations ( p_data , i_rate
223                 , i_next_atomic_operation , i_source_channel_offset
224                 , 0 , d_z_rear , 5.0 / i_nb_channels );
225         i_next_atomic_operation += 2;
226         i_source_channel_offset++;
227     }
228
229     /* Initialize the overflow buffer
230      * we need it because the process induce a delay in the samples */
231     p_data->i_overflow_buffer_size = 0;
232     for ( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
233     {
234         if ( p_data->i_overflow_buffer_size
235                 < p_data->p_atomic_operations[i].i_delay * i_nb_channels
236                 * sizeof (float) )
237         {
238             p_data->i_overflow_buffer_size
239                 = p_data->p_atomic_operations[i].i_delay * i_nb_channels
240                 * sizeof (float);
241         }
242     }
243     p_data->p_overflow_buffer = malloc ( p_data->i_overflow_buffer_size );
244     if ( p_data->p_atomic_operations == NULL )
245     {
246         msg_Err( p_filter, "out of memory" );
247         return -1;
248     }
249     memset ( p_data->p_overflow_buffer , 0 , p_data->i_overflow_buffer_size );
250
251     /* end */
252     return 0;
253 }
254
255 /*****************************************************************************
256  * Create: allocate headphone downmixer
257  *****************************************************************************/
258 static int Create( vlc_object_t *p_this )
259 {
260     aout_filter_t * p_filter = (aout_filter_t *)p_this;
261
262     if ( p_filter->output.i_physical_channels != ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
263           || p_filter->input.i_format != p_filter->output.i_format
264           || p_filter->input.i_rate != p_filter->output.i_rate
265           || (p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
266                && p_filter->input.i_format != VLC_FOURCC('f','i','3','2')) )
267     {
268         return -1;
269     }
270
271     /* Allocate the memory needed to store the module's structure */
272     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
273     if ( p_filter->p_sys == NULL )
274     {
275         msg_Err( p_filter, "out of memory" );
276         return -1;
277     }
278     p_filter->p_sys->i_overflow_buffer_size = 0;
279     p_filter->p_sys->p_overflow_buffer = NULL;
280     p_filter->p_sys->i_nb_atomic_operations = 0;
281     p_filter->p_sys->p_atomic_operations = NULL;
282
283     if ( Init( p_filter , p_filter->p_sys
284                 , aout_FormatNbChannels ( &p_filter->input )
285                 , p_filter->input.i_physical_channels
286                 ,  p_filter->input.i_rate ) < 0 )
287     {
288         return -1;
289     }
290
291     p_filter->pf_do_work = DoWork;
292     p_filter->b_in_place = 0;
293
294     return 0;
295 }
296
297 /*****************************************************************************
298  * Destroy: deallocate resources associated with headphone downmixer
299  *****************************************************************************/
300 static void Destroy( vlc_object_t *p_this )
301 {
302     aout_filter_t * p_filter = (aout_filter_t *)p_this;
303
304     if ( p_filter->p_sys != NULL )
305     {
306         if ( p_filter->p_sys->p_overflow_buffer != NULL )
307         {
308             free ( p_filter->p_sys->p_overflow_buffer );
309         }
310         if ( p_filter->p_sys->p_atomic_operations != NULL )
311         {
312             free ( p_filter->p_sys->p_atomic_operations );
313         }
314         free ( p_filter->p_sys );
315         p_filter->p_sys = NULL;
316     }
317 }
318
319 /*****************************************************************************
320  * DoWork: convert a buffer
321  *****************************************************************************/
322 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
323                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
324 {
325     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
326     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
327
328     float * p_in = (float*) p_in_buf->p_buffer;
329     byte_t * p_out;
330     byte_t * p_overflow;
331     byte_t * p_slide;
332
333     size_t i_overflow_size;/* in bytes */
334     size_t i_out_size;/* in bytes */
335
336     unsigned int i, j;
337
338     int i_source_channel_offset;
339     int i_dest_channel_offset;
340     unsigned int i_delay;
341     double d_amplitude_factor;
342
343
344     /* out buffer characterisitcs */
345     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
346     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
347     p_out = p_out_buf->p_buffer;
348     i_out_size = p_out_buf->i_nb_bytes;
349
350     if ( p_filter->p_sys != NULL )
351     {
352         /* Slide the overflow buffer */
353         p_overflow = p_filter->p_sys->p_overflow_buffer;
354         i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
355
356         memset ( p_out , 0 , i_out_size );
357         if ( i_out_size > i_overflow_size )
358             memcpy ( p_out , p_overflow , i_overflow_size );
359         else
360             memcpy ( p_out , p_overflow , i_out_size );
361
362         p_slide = p_filter->p_sys->p_overflow_buffer;
363         while ( p_slide < p_overflow + i_overflow_size )
364         {
365             if ( p_slide + i_out_size < p_overflow + i_overflow_size )
366             {
367                 memset ( p_slide , 0 , i_out_size );
368                 if ( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
369                     memcpy ( p_slide , p_slide + i_out_size , i_out_size );
370                 else
371                     memcpy ( p_slide , p_slide + i_out_size
372                       , p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
373             }
374             else
375             {
376                 memset ( p_slide , 0 , p_overflow + i_overflow_size - p_slide );
377             }
378             p_slide += i_out_size;
379         }
380
381         /* apply the atomic operations */
382         for ( i = 0 ; i < p_filter->p_sys->i_nb_atomic_operations ; i++ )
383         {
384             /* shorter variable names */
385             i_source_channel_offset
386                 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
387             i_dest_channel_offset
388                 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
389             i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
390             d_amplitude_factor
391                 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
392
393             if ( p_out_buf->i_nb_samples > i_delay )
394             {
395                 /* current buffer coefficients */
396                 for ( j = 0 ; j < p_out_buf->i_nb_samples - i_delay ; j++ )
397                 {
398                     ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
399                         += p_in[ j * i_input_nb + i_source_channel_offset ]
400                            * d_amplitude_factor;
401                 }
402
403                 /* overflow buffer coefficients */
404                 for ( j = 0 ; j < i_delay ; j++ )
405                 {
406                     ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
407                         += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
408                            * i_input_nb + i_source_channel_offset ]
409                            * d_amplitude_factor;
410                 }
411             }
412             else
413             {
414                 /* overflow buffer coefficients only */
415                 for ( j = 0 ; j < p_out_buf->i_nb_samples ; j++ )
416                 {
417                     ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
418                         * i_output_nb + i_dest_channel_offset ]
419                         += p_in[ j * i_input_nb + i_source_channel_offset ]
420                            * d_amplitude_factor;
421                 }
422             }
423         }
424     }
425     else
426     {
427         memset ( p_out , 0 , i_out_size );
428     }
429 }