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
8 * Authors: Boris Dorès <babal@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
30 #include <math.h> /* sqrt */
33 #include "audio_output.h"
34 #include "aout_internal.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Create ( vlc_object_t * );
40 static void Destroy ( vlc_object_t * );
42 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
45 /*****************************************************************************
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 " \
56 #define HEADPHONE_DIM_TEXT N_("Characteristic dimension")
57 #define HEADPHONE_DIM_LONGTEXT N_( \
58 "Distance between front left speaker and listener in meters.")
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 );
66 add_integer( "headphone-dim", 10, NULL, HEADPHONE_DIM_TEXT,
67 HEADPHONE_DIM_LONGTEXT, VLC_FALSE );
69 set_capability( "audio filter", 0 );
70 set_callbacks( Create, Destroy );
71 add_shortcut( "headphone" );
75 /*****************************************************************************
76 * Internal data structures
77 *****************************************************************************/
78 struct atomic_operation_t
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;
86 struct aout_filter_sys_t
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;
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 :
108 * rear left rear right
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 )
117 double d_c = 340; /*sound celerity (unit: m/s)*/
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) )
129 p_data->p_atomic_operations[i_next_atomic_operation]
130 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
134 p_data->p_atomic_operations[i_next_atomic_operation]
135 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
139 p_data->p_atomic_operations[i_next_atomic_operation]
140 .d_amplitude_factor = d_channel_amplitude_factor / 2;
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) )
153 p_data->p_atomic_operations[i_next_atomic_operation + 1]
154 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
158 p_data->p_atomic_operations[i_next_atomic_operation + 1]
159 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
163 p_data->p_atomic_operations[i_next_atomic_operation + 1]
164 .d_amplitude_factor = d_channel_amplitude_factor / 2;
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 )
172 double d_x = config_GetInt ( p_filter , "headphone-dim" );
174 double d_z_rear = -d_x/3;
175 unsigned int i_next_atomic_operation;
176 int i_source_channel_offset;
179 if ( p_data == NULL )
181 msg_Dbg ( p_filter, "passing a null pointer as argument" );
185 /* Number of elementary operations */
186 p_data->i_nb_atomic_operations = i_nb_channels * 2;
187 if ( i_physical_channels & AOUT_CHAN_CENTER )
189 p_data->i_nb_atomic_operations += 2;
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 )
195 msg_Err( p_filter, "out of memory" );
199 /* For each virtual speaker, computes elementary wave propagation time
201 i_next_atomic_operation = 0;
202 i_source_channel_offset = 0;
203 if ( i_physical_channels & AOUT_CHAN_LEFT )
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++;
211 if ( i_physical_channels & AOUT_CHAN_RIGHT )
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++;
219 if ( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
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++;
227 if ( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
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++;
235 if ( i_physical_channels & AOUT_CHAN_REARLEFT )
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++;
243 if ( i_physical_channels & AOUT_CHAN_REARRIGHT )
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++;
251 if ( i_physical_channels & AOUT_CHAN_REARCENTER )
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++;
259 if ( i_physical_channels & AOUT_CHAN_CENTER )
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++;
272 if ( i_physical_channels & AOUT_CHAN_LFE )
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++;
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++ )
286 if ( p_data->i_overflow_buffer_size
287 < p_data->p_atomic_operations[i].i_delay * i_nb_channels
290 p_data->i_overflow_buffer_size
291 = p_data->p_atomic_operations[i].i_delay * i_nb_channels
295 p_data->p_overflow_buffer = malloc ( p_data->i_overflow_buffer_size );
296 if ( p_data->p_atomic_operations == NULL )
298 msg_Err( p_filter, "out of memory" );
301 memset ( p_data->p_overflow_buffer , 0 , p_data->i_overflow_buffer_size );
307 /*****************************************************************************
308 * Create: allocate headphone downmixer
309 *****************************************************************************/
310 static int Create( vlc_object_t *p_this )
312 aout_filter_t * p_filter = (aout_filter_t *)p_this;
313 vlc_bool_t b_fit = VLC_TRUE;
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 ) )
322 msg_Dbg( p_filter, "Filter discarded (incompatible format)" );
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') )
331 p_filter->input.i_format = VLC_FOURCC('f','l','3','2');
332 p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
334 if ( p_filter->input.i_rate != p_filter->output.i_rate )
337 p_filter->input.i_rate = p_filter->output.i_rate;
339 if ( p_filter->input.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
340 && ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO ) )
343 p_filter->input.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
347 p_filter->input.i_original_channels = p_filter->input.i_physical_channels;
351 msg_Dbg( p_filter, "Requesting specific format" );
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 )
359 msg_Err( p_filter, "Out of memory" );
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;
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 )
375 p_filter->pf_do_work = DoWork;
376 p_filter->b_in_place = 0;
381 /*****************************************************************************
382 * Destroy: deallocate resources associated with headphone downmixer
383 *****************************************************************************/
384 static void Destroy( vlc_object_t *p_this )
386 aout_filter_t * p_filter = (aout_filter_t *)p_this;
388 if ( p_filter->p_sys != NULL )
390 if ( p_filter->p_sys->p_overflow_buffer != NULL )
392 free ( p_filter->p_sys->p_overflow_buffer );
394 if ( p_filter->p_sys->p_atomic_operations != NULL )
396 free ( p_filter->p_sys->p_atomic_operations );
398 free ( p_filter->p_sys );
399 p_filter->p_sys = NULL;
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 )
409 int i_input_nb = aout_FormatNbChannels( &p_filter->input );
410 int i_output_nb = aout_FormatNbChannels( &p_filter->output );
412 float * p_in = (float*) p_in_buf->p_buffer;
417 size_t i_overflow_size;/* in bytes */
418 size_t i_out_size;/* in bytes */
422 int i_source_channel_offset;
423 int i_dest_channel_offset;
424 unsigned int i_delay;
425 double d_amplitude_factor;
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;
434 if ( p_filter->p_sys != NULL )
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;
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 );
444 memcpy ( p_out , p_overflow , i_out_size );
446 p_slide = p_filter->p_sys->p_overflow_buffer;
447 while ( p_slide < p_overflow + i_overflow_size )
449 if ( p_slide + i_out_size < p_overflow + i_overflow_size )
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 );
455 memcpy ( p_slide , p_slide + i_out_size
456 , p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
460 memset ( p_slide , 0 , p_overflow + i_overflow_size - p_slide );
462 p_slide += i_out_size;
465 /* apply the atomic operations */
466 for ( i = 0 ; i < p_filter->p_sys->i_nb_atomic_operations ; i++ )
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;
475 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
477 if ( p_out_buf->i_nb_samples > i_delay )
479 /* current buffer coefficients */
480 for ( j = 0 ; j < p_out_buf->i_nb_samples - i_delay ; j++ )
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;
487 /* overflow buffer coefficients */
488 for ( j = 0 ; j < i_delay ; j++ )
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;
498 /* overflow buffer coefficients only */
499 for ( j = 0 ; j < p_out_buf->i_nb_samples ; j++ )
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;
511 memset ( p_out , 0 , i_out_size );