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") );
62 set_category( CAT_AUDIO );
63 set_subcategory( SUBCAT_AUDIO_AFILTER );
65 add_integer( "headphone-dim", 10, NULL, HEADPHONE_DIM_TEXT,
66 HEADPHONE_DIM_LONGTEXT, VLC_FALSE );
68 set_capability( "audio filter", 0 );
69 set_callbacks( Create, Destroy );
70 add_shortcut( "headphone" );
74 /*****************************************************************************
75 * Internal data structures
76 *****************************************************************************/
77 struct atomic_operation_t
79 int i_source_channel_offset;
80 int i_dest_channel_offset;
81 unsigned int i_delay;/* in sample unit */
82 double d_amplitude_factor;
85 struct aout_filter_sys_t
87 size_t i_overflow_buffer_size;/* in bytes */
88 byte_t * p_overflow_buffer;
89 unsigned int i_nb_atomic_operations;
90 struct atomic_operation_t * p_atomic_operations;
93 /*****************************************************************************
94 * Init: initialize internal data structures
95 * and computes the needed atomic operations
96 *****************************************************************************/
97 /* x and z represent the coordinates of the virtual speaker
98 * relatively to the center of the listener's head, measured in meters :
107 * rear left rear right
111 static void ComputeChannelOperations ( struct aout_filter_sys_t * p_data
112 , unsigned int i_rate , unsigned int i_next_atomic_operation
113 , int i_source_channel_offset , double d_x , double d_z
114 , double d_channel_amplitude_factor )
116 double d_c = 340; /*sound celerity (unit: m/s)*/
119 p_data->p_atomic_operations[i_next_atomic_operation]
120 .i_source_channel_offset = i_source_channel_offset;
121 p_data->p_atomic_operations[i_next_atomic_operation]
122 .i_dest_channel_offset = 0;/* left */
123 p_data->p_atomic_operations[i_next_atomic_operation]
124 .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
128 p_data->p_atomic_operations[i_next_atomic_operation]
129 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
133 p_data->p_atomic_operations[i_next_atomic_operation]
134 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
138 p_data->p_atomic_operations[i_next_atomic_operation]
139 .d_amplitude_factor = d_channel_amplitude_factor / 2;
143 p_data->p_atomic_operations[i_next_atomic_operation + 1]
144 .i_source_channel_offset = i_source_channel_offset;
145 p_data->p_atomic_operations[i_next_atomic_operation + 1]
146 .i_dest_channel_offset = 1;/* right */
147 p_data->p_atomic_operations[i_next_atomic_operation + 1]
148 .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
152 p_data->p_atomic_operations[i_next_atomic_operation + 1]
153 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
157 p_data->p_atomic_operations[i_next_atomic_operation + 1]
158 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
162 p_data->p_atomic_operations[i_next_atomic_operation + 1]
163 .d_amplitude_factor = d_channel_amplitude_factor / 2;
167 static int Init ( aout_filter_t * p_filter , struct aout_filter_sys_t * p_data
168 , unsigned int i_nb_channels , uint32_t i_physical_channels
169 , unsigned int i_rate )
171 double d_x = config_GetInt ( p_filter , "headphone-dim" );
173 double d_z_rear = -d_x/3;
174 unsigned int i_next_atomic_operation;
175 int i_source_channel_offset;
178 if ( p_data == NULL )
180 msg_Dbg ( p_filter, "passing a null pointer as argument" );
184 /* Number of elementary operations */
185 p_data->i_nb_atomic_operations = i_nb_channels * 2;
186 p_data->p_atomic_operations = malloc ( sizeof(struct atomic_operation_t)
187 * p_data->i_nb_atomic_operations );
188 if ( p_data->p_atomic_operations == NULL )
190 msg_Err( p_filter, "out of memory" );
194 /* For each virtual speaker, computes elementary wave propagation time
196 i_next_atomic_operation = 0;
197 i_source_channel_offset = 0;
198 if ( i_physical_channels & AOUT_CHAN_LEFT )
200 ComputeChannelOperations ( p_data , i_rate
201 , i_next_atomic_operation , i_source_channel_offset
202 , -d_x , d_z , 2.0 / i_nb_channels );
203 i_next_atomic_operation += 2;
204 i_source_channel_offset++;
206 if ( i_physical_channels & AOUT_CHAN_RIGHT )
208 ComputeChannelOperations ( p_data , i_rate
209 , i_next_atomic_operation , i_source_channel_offset
210 , d_x , d_z , 2.0 / i_nb_channels );
211 i_next_atomic_operation += 2;
212 i_source_channel_offset++;
214 if ( i_physical_channels & AOUT_CHAN_REARLEFT )
216 ComputeChannelOperations ( p_data , i_rate
217 , i_next_atomic_operation , i_source_channel_offset
218 , -d_x , d_z_rear , 1.5 / i_nb_channels );
219 i_next_atomic_operation += 2;
220 i_source_channel_offset++;
222 if ( i_physical_channels & AOUT_CHAN_REARRIGHT )
224 ComputeChannelOperations ( p_data , i_rate
225 , i_next_atomic_operation , i_source_channel_offset
226 , d_x , d_z_rear , 1.5 / i_nb_channels );
227 i_next_atomic_operation += 2;
228 i_source_channel_offset++;
230 if ( i_physical_channels & AOUT_CHAN_REARCENTER )
232 ComputeChannelOperations ( p_data , i_rate
233 , i_next_atomic_operation , i_source_channel_offset
234 , 0 , -d_z , 1.5 / i_nb_channels );
235 i_next_atomic_operation += 2;
236 i_source_channel_offset++;
238 if ( i_physical_channels & AOUT_CHAN_CENTER )
240 ComputeChannelOperations ( p_data , i_rate
241 , i_next_atomic_operation , i_source_channel_offset
242 , 0 , d_z , 1.5 / i_nb_channels );
243 i_next_atomic_operation += 2;
244 i_source_channel_offset++;
246 if ( i_physical_channels & AOUT_CHAN_LFE )
248 ComputeChannelOperations ( p_data , i_rate
249 , i_next_atomic_operation , i_source_channel_offset
250 , 0 , d_z_rear , 5.0 / i_nb_channels );
251 i_next_atomic_operation += 2;
252 i_source_channel_offset++;
254 if ( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
256 ComputeChannelOperations ( p_data , i_rate
257 , i_next_atomic_operation , i_source_channel_offset
258 , -d_x , 0 , 1.5 / i_nb_channels );
259 i_next_atomic_operation += 2;
260 i_source_channel_offset++;
262 if ( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
264 ComputeChannelOperations ( p_data , i_rate
265 , i_next_atomic_operation , i_source_channel_offset
266 , d_x , 0 , 1.5 / i_nb_channels );
267 i_next_atomic_operation += 2;
268 i_source_channel_offset++;
271 /* Initialize the overflow buffer
272 * we need it because the process induce a delay in the samples */
273 p_data->i_overflow_buffer_size = 0;
274 for ( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
276 if ( p_data->i_overflow_buffer_size
277 < p_data->p_atomic_operations[i].i_delay * i_nb_channels
280 p_data->i_overflow_buffer_size
281 = p_data->p_atomic_operations[i].i_delay * i_nb_channels
285 p_data->p_overflow_buffer = malloc ( p_data->i_overflow_buffer_size );
286 if ( p_data->p_atomic_operations == NULL )
288 msg_Err( p_filter, "out of memory" );
291 memset ( p_data->p_overflow_buffer , 0 , p_data->i_overflow_buffer_size );
297 /*****************************************************************************
298 * Create: allocate headphone downmixer
299 *****************************************************************************/
300 static int Create( vlc_object_t *p_this )
302 aout_filter_t * p_filter = (aout_filter_t *)p_this;
304 if ( p_filter->output.i_physical_channels != ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
305 || p_filter->input.i_format != p_filter->output.i_format
306 || p_filter->input.i_rate != p_filter->output.i_rate
307 || (p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
308 && p_filter->input.i_format != VLC_FOURCC('f','i','3','2')) )
310 msg_Dbg( p_filter, "Filter discarded (invalid format)" );
314 /* Allocate the memory needed to store the module's structure */
315 p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
316 if ( p_filter->p_sys == NULL )
318 msg_Err( p_filter, "out of memory" );
321 p_filter->p_sys->i_overflow_buffer_size = 0;
322 p_filter->p_sys->p_overflow_buffer = NULL;
323 p_filter->p_sys->i_nb_atomic_operations = 0;
324 p_filter->p_sys->p_atomic_operations = NULL;
326 if ( Init( p_filter , p_filter->p_sys
327 , aout_FormatNbChannels ( &p_filter->input )
328 , p_filter->input.i_physical_channels
329 , p_filter->input.i_rate ) < 0 )
334 p_filter->pf_do_work = DoWork;
335 p_filter->b_in_place = 0;
340 /*****************************************************************************
341 * Destroy: deallocate resources associated with headphone downmixer
342 *****************************************************************************/
343 static void Destroy( vlc_object_t *p_this )
345 aout_filter_t * p_filter = (aout_filter_t *)p_this;
347 if ( p_filter->p_sys != NULL )
349 if ( p_filter->p_sys->p_overflow_buffer != NULL )
351 free ( p_filter->p_sys->p_overflow_buffer );
353 if ( p_filter->p_sys->p_atomic_operations != NULL )
355 free ( p_filter->p_sys->p_atomic_operations );
357 free ( p_filter->p_sys );
358 p_filter->p_sys = NULL;
362 /*****************************************************************************
363 * DoWork: convert a buffer
364 *****************************************************************************/
365 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
366 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
368 int i_input_nb = aout_FormatNbChannels( &p_filter->input );
369 int i_output_nb = aout_FormatNbChannels( &p_filter->output );
371 float * p_in = (float*) p_in_buf->p_buffer;
376 size_t i_overflow_size;/* in bytes */
377 size_t i_out_size;/* in bytes */
381 int i_source_channel_offset;
382 int i_dest_channel_offset;
383 unsigned int i_delay;
384 double d_amplitude_factor;
387 /* out buffer characterisitcs */
388 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
389 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
390 p_out = p_out_buf->p_buffer;
391 i_out_size = p_out_buf->i_nb_bytes;
393 if ( p_filter->p_sys != NULL )
395 /* Slide the overflow buffer */
396 p_overflow = p_filter->p_sys->p_overflow_buffer;
397 i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
399 memset ( p_out , 0 , i_out_size );
400 if ( i_out_size > i_overflow_size )
401 memcpy ( p_out , p_overflow , i_overflow_size );
403 memcpy ( p_out , p_overflow , i_out_size );
405 p_slide = p_filter->p_sys->p_overflow_buffer;
406 while ( p_slide < p_overflow + i_overflow_size )
408 if ( p_slide + i_out_size < p_overflow + i_overflow_size )
410 memset ( p_slide , 0 , i_out_size );
411 if ( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
412 memcpy ( p_slide , p_slide + i_out_size , i_out_size );
414 memcpy ( p_slide , p_slide + i_out_size
415 , p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
419 memset ( p_slide , 0 , p_overflow + i_overflow_size - p_slide );
421 p_slide += i_out_size;
424 /* apply the atomic operations */
425 for ( i = 0 ; i < p_filter->p_sys->i_nb_atomic_operations ; i++ )
427 /* shorter variable names */
428 i_source_channel_offset
429 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
430 i_dest_channel_offset
431 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
432 i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
434 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
436 if ( p_out_buf->i_nb_samples > i_delay )
438 /* current buffer coefficients */
439 for ( j = 0 ; j < p_out_buf->i_nb_samples - i_delay ; j++ )
441 ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
442 += p_in[ j * i_input_nb + i_source_channel_offset ]
443 * d_amplitude_factor;
446 /* overflow buffer coefficients */
447 for ( j = 0 ; j < i_delay ; j++ )
449 ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
450 += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
451 * i_input_nb + i_source_channel_offset ]
452 * d_amplitude_factor;
457 /* overflow buffer coefficients only */
458 for ( j = 0 ; j < p_out_buf->i_nb_samples ; j++ )
460 ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
461 * i_output_nb + i_dest_channel_offset ]
462 += p_in[ j * i_input_nb + i_source_channel_offset ]
463 * d_amplitude_factor;
470 memset ( p_out , 0 , i_out_size );