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