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
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 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 " \
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") );
63 add_integer( "headphone-dim", 10, NULL, HEADPHONE_DIM_TEXT,
64 HEADPHONE_DIM_LONGTEXT, VLC_FALSE );
66 set_capability( "audio filter", 0 );
67 set_callbacks( Create, Destroy );
68 add_shortcut( "headphone" );
72 /*****************************************************************************
73 * Internal data structures
74 *****************************************************************************/
75 struct atomic_operation_t
77 int i_source_channel_offset;
78 int i_dest_channel_offset;
79 unsigned int i_delay;/* in sample unit */
80 double d_amplitude_factor;
83 struct aout_filter_sys_t
85 size_t i_overflow_buffer_size;/* in bytes */
86 byte_t * p_overflow_buffer;
87 unsigned int i_nb_atomic_operations;
88 struct atomic_operation_t * p_atomic_operations;
91 /*****************************************************************************
92 * Init: initialize internal data structures
93 * and computes the needed atomic operations
94 *****************************************************************************/
95 /* x and z represent the coordinates of the virtual speaker
96 * relatively to the center of the listener's head, measured in meters :
105 * rear left rear right
109 static void ComputeChannelOperations ( struct aout_filter_sys_t * p_data
110 , unsigned int i_rate , unsigned int i_next_atomic_operation
111 , int i_source_channel_offset , double d_x , double d_z
112 , double d_channel_amplitude_factor )
114 double d_c = 340; /*sound celerity (unit: m/s)*/
117 p_data->p_atomic_operations[i_next_atomic_operation]
118 .i_source_channel_offset = i_source_channel_offset;
119 p_data->p_atomic_operations[i_next_atomic_operation]
120 .i_dest_channel_offset = 0;/* left */
121 p_data->p_atomic_operations[i_next_atomic_operation]
122 .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
126 p_data->p_atomic_operations[i_next_atomic_operation]
127 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
131 p_data->p_atomic_operations[i_next_atomic_operation]
132 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
136 p_data->p_atomic_operations[i_next_atomic_operation]
137 .d_amplitude_factor = d_channel_amplitude_factor / 2;
141 p_data->p_atomic_operations[i_next_atomic_operation + 1]
142 .i_source_channel_offset = i_source_channel_offset;
143 p_data->p_atomic_operations[i_next_atomic_operation + 1]
144 .i_dest_channel_offset = 1;/* right */
145 p_data->p_atomic_operations[i_next_atomic_operation + 1]
146 .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
150 p_data->p_atomic_operations[i_next_atomic_operation + 1]
151 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
155 p_data->p_atomic_operations[i_next_atomic_operation + 1]
156 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
160 p_data->p_atomic_operations[i_next_atomic_operation + 1]
161 .d_amplitude_factor = d_channel_amplitude_factor / 2;
165 static int Init ( aout_filter_t * p_filter , struct aout_filter_sys_t * p_data
166 , unsigned int i_nb_channels , uint32_t i_physical_channels
167 , unsigned int i_rate )
169 double d_x = config_GetInt ( p_filter , "headphone-dim" );
171 double d_z_rear = -d_x/3;
172 unsigned int i_next_atomic_operation;
173 int i_source_channel_offset;
176 if ( p_data == NULL )
178 msg_Dbg ( p_filter, "passing a null pointer as argument" );
182 /* Number of elementary operations */
183 p_data->i_nb_atomic_operations = i_nb_channels * 2;
184 p_data->p_atomic_operations = malloc ( sizeof(struct atomic_operation_t)
185 * p_data->i_nb_atomic_operations );
186 if ( p_data->p_atomic_operations == NULL )
188 msg_Err( p_filter, "out of memory" );
192 /* For each virtual speaker, computes elementary wave propagation time
194 i_next_atomic_operation = 0;
195 i_source_channel_offset = 0;
196 if ( i_physical_channels & AOUT_CHAN_LEFT )
198 ComputeChannelOperations ( p_data , i_rate
199 , i_next_atomic_operation , i_source_channel_offset
200 , -d_x , d_z , 2.0 / i_nb_channels );
201 i_next_atomic_operation += 2;
202 i_source_channel_offset++;
204 if ( i_physical_channels & AOUT_CHAN_RIGHT )
206 ComputeChannelOperations ( p_data , i_rate
207 , i_next_atomic_operation , i_source_channel_offset
208 , d_x , d_z , 2.0 / i_nb_channels );
209 i_next_atomic_operation += 2;
210 i_source_channel_offset++;
212 if ( i_physical_channels & AOUT_CHAN_REARLEFT )
214 ComputeChannelOperations ( p_data , i_rate
215 , i_next_atomic_operation , i_source_channel_offset
216 , -d_x , d_z_rear , 1.5 / i_nb_channels );
217 i_next_atomic_operation += 2;
218 i_source_channel_offset++;
220 if ( i_physical_channels & AOUT_CHAN_REARRIGHT )
222 ComputeChannelOperations ( p_data , i_rate
223 , i_next_atomic_operation , i_source_channel_offset
224 , d_x , d_z_rear , 1.5 / i_nb_channels );
225 i_next_atomic_operation += 2;
226 i_source_channel_offset++;
228 if ( i_physical_channels & AOUT_CHAN_REARCENTER )
230 ComputeChannelOperations ( p_data , i_rate
231 , i_next_atomic_operation , i_source_channel_offset
232 , 0 , -d_z , 1.5 / i_nb_channels );
233 i_next_atomic_operation += 2;
234 i_source_channel_offset++;
236 if ( i_physical_channels & AOUT_CHAN_CENTER )
238 ComputeChannelOperations ( p_data , i_rate
239 , i_next_atomic_operation , i_source_channel_offset
240 , 0 , d_z , 1.5 / i_nb_channels );
241 i_next_atomic_operation += 2;
242 i_source_channel_offset++;
244 if ( i_physical_channels & AOUT_CHAN_LFE )
246 ComputeChannelOperations ( p_data , i_rate
247 , i_next_atomic_operation , i_source_channel_offset
248 , 0 , d_z_rear , 5.0 / i_nb_channels );
249 i_next_atomic_operation += 2;
250 i_source_channel_offset++;
252 if ( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
254 ComputeChannelOperations ( p_data , i_rate
255 , i_next_atomic_operation , i_source_channel_offset
256 , -d_x , 0 , 1.5 / i_nb_channels );
257 i_next_atomic_operation += 2;
258 i_source_channel_offset++;
260 if ( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
262 ComputeChannelOperations ( p_data , i_rate
263 , i_next_atomic_operation , i_source_channel_offset
264 , d_x , 0 , 1.5 / i_nb_channels );
265 i_next_atomic_operation += 2;
266 i_source_channel_offset++;
269 /* Initialize the overflow buffer
270 * we need it because the process induce a delay in the samples */
271 p_data->i_overflow_buffer_size = 0;
272 for ( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
274 if ( p_data->i_overflow_buffer_size
275 < p_data->p_atomic_operations[i].i_delay * i_nb_channels
278 p_data->i_overflow_buffer_size
279 = p_data->p_atomic_operations[i].i_delay * i_nb_channels
283 p_data->p_overflow_buffer = malloc ( p_data->i_overflow_buffer_size );
284 if ( p_data->p_atomic_operations == NULL )
286 msg_Err( p_filter, "out of memory" );
289 memset ( p_data->p_overflow_buffer , 0 , p_data->i_overflow_buffer_size );
295 /*****************************************************************************
296 * Create: allocate headphone downmixer
297 *****************************************************************************/
298 static int Create( vlc_object_t *p_this )
300 aout_filter_t * p_filter = (aout_filter_t *)p_this;
302 if ( p_filter->output.i_physical_channels != ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
303 || p_filter->input.i_format != p_filter->output.i_format
304 || p_filter->input.i_rate != p_filter->output.i_rate
305 || (p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
306 && p_filter->input.i_format != VLC_FOURCC('f','i','3','2')) )
308 msg_Dbg( p_filter, "Filter discarded (invalid format)" );
312 /* Allocate the memory needed to store the module's structure */
313 p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
314 if ( p_filter->p_sys == NULL )
316 msg_Err( p_filter, "out of memory" );
319 p_filter->p_sys->i_overflow_buffer_size = 0;
320 p_filter->p_sys->p_overflow_buffer = NULL;
321 p_filter->p_sys->i_nb_atomic_operations = 0;
322 p_filter->p_sys->p_atomic_operations = NULL;
324 if ( Init( p_filter , p_filter->p_sys
325 , aout_FormatNbChannels ( &p_filter->input )
326 , p_filter->input.i_physical_channels
327 , p_filter->input.i_rate ) < 0 )
332 p_filter->pf_do_work = DoWork;
333 p_filter->b_in_place = 0;
338 /*****************************************************************************
339 * Destroy: deallocate resources associated with headphone downmixer
340 *****************************************************************************/
341 static void Destroy( vlc_object_t *p_this )
343 aout_filter_t * p_filter = (aout_filter_t *)p_this;
345 if ( p_filter->p_sys != NULL )
347 if ( p_filter->p_sys->p_overflow_buffer != NULL )
349 free ( p_filter->p_sys->p_overflow_buffer );
351 if ( p_filter->p_sys->p_atomic_operations != NULL )
353 free ( p_filter->p_sys->p_atomic_operations );
355 free ( p_filter->p_sys );
356 p_filter->p_sys = NULL;
360 /*****************************************************************************
361 * DoWork: convert a buffer
362 *****************************************************************************/
363 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
364 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
366 int i_input_nb = aout_FormatNbChannels( &p_filter->input );
367 int i_output_nb = aout_FormatNbChannels( &p_filter->output );
369 float * p_in = (float*) p_in_buf->p_buffer;
374 size_t i_overflow_size;/* in bytes */
375 size_t i_out_size;/* in bytes */
379 int i_source_channel_offset;
380 int i_dest_channel_offset;
381 unsigned int i_delay;
382 double d_amplitude_factor;
385 /* out buffer characterisitcs */
386 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
387 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
388 p_out = p_out_buf->p_buffer;
389 i_out_size = p_out_buf->i_nb_bytes;
391 if ( p_filter->p_sys != NULL )
393 /* Slide the overflow buffer */
394 p_overflow = p_filter->p_sys->p_overflow_buffer;
395 i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
397 memset ( p_out , 0 , i_out_size );
398 if ( i_out_size > i_overflow_size )
399 memcpy ( p_out , p_overflow , i_overflow_size );
401 memcpy ( p_out , p_overflow , i_out_size );
403 p_slide = p_filter->p_sys->p_overflow_buffer;
404 while ( p_slide < p_overflow + i_overflow_size )
406 if ( p_slide + i_out_size < p_overflow + i_overflow_size )
408 memset ( p_slide , 0 , i_out_size );
409 if ( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
410 memcpy ( p_slide , p_slide + i_out_size , i_out_size );
412 memcpy ( p_slide , p_slide + i_out_size
413 , p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
417 memset ( p_slide , 0 , p_overflow + i_overflow_size - p_slide );
419 p_slide += i_out_size;
422 /* apply the atomic operations */
423 for ( i = 0 ; i < p_filter->p_sys->i_nb_atomic_operations ; i++ )
425 /* shorter variable names */
426 i_source_channel_offset
427 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
428 i_dest_channel_offset
429 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
430 i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
432 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
434 if ( p_out_buf->i_nb_samples > i_delay )
436 /* current buffer coefficients */
437 for ( j = 0 ; j < p_out_buf->i_nb_samples - i_delay ; j++ )
439 ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
440 += p_in[ j * i_input_nb + i_source_channel_offset ]
441 * d_amplitude_factor;
444 /* overflow buffer coefficients */
445 for ( j = 0 ; j < i_delay ; j++ )
447 ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
448 += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
449 * i_input_nb + i_source_channel_offset ]
450 * d_amplitude_factor;
455 /* overflow buffer coefficients only */
456 for ( j = 0 ; j < p_out_buf->i_nb_samples ; j++ )
458 ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
459 * i_output_nb + i_dest_channel_offset ]
460 += p_in[ j * i_input_nb + i_source_channel_offset ]
461 * d_amplitude_factor;
468 memset ( p_out , 0 , i_out_size );