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 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 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_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 p_data->p_atomic_operations = malloc ( sizeof(struct atomic_operation_t)
188 * p_data->i_nb_atomic_operations );
189 if ( p_data->p_atomic_operations == NULL )
191 msg_Err( p_filter, "out of memory" );
195 /* For each virtual speaker, computes elementary wave propagation time
197 i_next_atomic_operation = 0;
198 i_source_channel_offset = 0;
199 if ( i_physical_channels & AOUT_CHAN_LEFT )
201 ComputeChannelOperations ( p_data , i_rate
202 , i_next_atomic_operation , i_source_channel_offset
203 , -d_x , d_z , 2.0 / i_nb_channels );
204 i_next_atomic_operation += 2;
205 i_source_channel_offset++;
207 if ( i_physical_channels & AOUT_CHAN_RIGHT )
209 ComputeChannelOperations ( p_data , i_rate
210 , i_next_atomic_operation , i_source_channel_offset
211 , d_x , d_z , 2.0 / i_nb_channels );
212 i_next_atomic_operation += 2;
213 i_source_channel_offset++;
215 if ( i_physical_channels & AOUT_CHAN_REARLEFT )
217 ComputeChannelOperations ( p_data , i_rate
218 , i_next_atomic_operation , i_source_channel_offset
219 , -d_x , d_z_rear , 1.5 / i_nb_channels );
220 i_next_atomic_operation += 2;
221 i_source_channel_offset++;
223 if ( i_physical_channels & AOUT_CHAN_REARRIGHT )
225 ComputeChannelOperations ( p_data , i_rate
226 , i_next_atomic_operation , i_source_channel_offset
227 , d_x , d_z_rear , 1.5 / i_nb_channels );
228 i_next_atomic_operation += 2;
229 i_source_channel_offset++;
231 if ( i_physical_channels & AOUT_CHAN_REARCENTER )
233 ComputeChannelOperations ( p_data , i_rate
234 , i_next_atomic_operation , i_source_channel_offset
235 , 0 , -d_z , 1.5 / i_nb_channels );
236 i_next_atomic_operation += 2;
237 i_source_channel_offset++;
239 if ( i_physical_channels & AOUT_CHAN_CENTER )
241 ComputeChannelOperations ( p_data , i_rate
242 , i_next_atomic_operation , i_source_channel_offset
243 , 0 , d_z , 1.5 / i_nb_channels );
244 i_next_atomic_operation += 2;
245 i_source_channel_offset++;
247 if ( i_physical_channels & AOUT_CHAN_LFE )
249 ComputeChannelOperations ( p_data , i_rate
250 , i_next_atomic_operation , i_source_channel_offset
251 , 0 , d_z_rear , 5.0 / i_nb_channels );
252 i_next_atomic_operation += 2;
253 i_source_channel_offset++;
255 if ( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
257 ComputeChannelOperations ( p_data , i_rate
258 , i_next_atomic_operation , i_source_channel_offset
259 , -d_x , 0 , 1.5 / i_nb_channels );
260 i_next_atomic_operation += 2;
261 i_source_channel_offset++;
263 if ( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
265 ComputeChannelOperations ( p_data , i_rate
266 , i_next_atomic_operation , i_source_channel_offset
267 , d_x , 0 , 1.5 / i_nb_channels );
268 i_next_atomic_operation += 2;
269 i_source_channel_offset++;
272 /* Initialize the overflow buffer
273 * we need it because the process induce a delay in the samples */
274 p_data->i_overflow_buffer_size = 0;
275 for ( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
277 if ( p_data->i_overflow_buffer_size
278 < p_data->p_atomic_operations[i].i_delay * i_nb_channels
281 p_data->i_overflow_buffer_size
282 = p_data->p_atomic_operations[i].i_delay * i_nb_channels
286 p_data->p_overflow_buffer = malloc ( p_data->i_overflow_buffer_size );
287 if ( p_data->p_atomic_operations == NULL )
289 msg_Err( p_filter, "out of memory" );
292 memset ( p_data->p_overflow_buffer , 0 , p_data->i_overflow_buffer_size );
298 /*****************************************************************************
299 * Create: allocate headphone downmixer
300 *****************************************************************************/
301 static int Create( vlc_object_t *p_this )
303 aout_filter_t * p_filter = (aout_filter_t *)p_this;
305 if ( p_filter->output.i_physical_channels != ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
306 || p_filter->input.i_format != p_filter->output.i_format
307 || p_filter->input.i_rate != p_filter->output.i_rate
308 || (p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
309 && p_filter->input.i_format != VLC_FOURCC('f','i','3','2')) )
311 msg_Dbg( p_filter, "Filter discarded (invalid format)" );
315 /* Allocate the memory needed to store the module's structure */
316 p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
317 if ( p_filter->p_sys == NULL )
319 msg_Err( p_filter, "out of memory" );
322 p_filter->p_sys->i_overflow_buffer_size = 0;
323 p_filter->p_sys->p_overflow_buffer = NULL;
324 p_filter->p_sys->i_nb_atomic_operations = 0;
325 p_filter->p_sys->p_atomic_operations = NULL;
327 if ( Init( p_filter , p_filter->p_sys
328 , aout_FormatNbChannels ( &p_filter->input )
329 , p_filter->input.i_physical_channels
330 , p_filter->input.i_rate ) < 0 )
335 p_filter->pf_do_work = DoWork;
336 p_filter->b_in_place = 0;
341 /*****************************************************************************
342 * Destroy: deallocate resources associated with headphone downmixer
343 *****************************************************************************/
344 static void Destroy( vlc_object_t *p_this )
346 aout_filter_t * p_filter = (aout_filter_t *)p_this;
348 if ( p_filter->p_sys != NULL )
350 if ( p_filter->p_sys->p_overflow_buffer != NULL )
352 free ( p_filter->p_sys->p_overflow_buffer );
354 if ( p_filter->p_sys->p_atomic_operations != NULL )
356 free ( p_filter->p_sys->p_atomic_operations );
358 free ( p_filter->p_sys );
359 p_filter->p_sys = NULL;
363 /*****************************************************************************
364 * DoWork: convert a buffer
365 *****************************************************************************/
366 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
367 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
369 int i_input_nb = aout_FormatNbChannels( &p_filter->input );
370 int i_output_nb = aout_FormatNbChannels( &p_filter->output );
372 float * p_in = (float*) p_in_buf->p_buffer;
377 size_t i_overflow_size;/* in bytes */
378 size_t i_out_size;/* in bytes */
382 int i_source_channel_offset;
383 int i_dest_channel_offset;
384 unsigned int i_delay;
385 double d_amplitude_factor;
388 /* out buffer characterisitcs */
389 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
390 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
391 p_out = p_out_buf->p_buffer;
392 i_out_size = p_out_buf->i_nb_bytes;
394 if ( p_filter->p_sys != NULL )
396 /* Slide the overflow buffer */
397 p_overflow = p_filter->p_sys->p_overflow_buffer;
398 i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
400 memset ( p_out , 0 , i_out_size );
401 if ( i_out_size > i_overflow_size )
402 memcpy ( p_out , p_overflow , i_overflow_size );
404 memcpy ( p_out , p_overflow , i_out_size );
406 p_slide = p_filter->p_sys->p_overflow_buffer;
407 while ( p_slide < p_overflow + i_overflow_size )
409 if ( p_slide + i_out_size < p_overflow + i_overflow_size )
411 memset ( p_slide , 0 , i_out_size );
412 if ( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
413 memcpy ( p_slide , p_slide + i_out_size , i_out_size );
415 memcpy ( p_slide , p_slide + i_out_size
416 , p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
420 memset ( p_slide , 0 , p_overflow + i_overflow_size - p_slide );
422 p_slide += i_out_size;
425 /* apply the atomic operations */
426 for ( i = 0 ; i < p_filter->p_sys->i_nb_atomic_operations ; i++ )
428 /* shorter variable names */
429 i_source_channel_offset
430 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
431 i_dest_channel_offset
432 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
433 i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
435 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
437 if ( p_out_buf->i_nb_samples > i_delay )
439 /* current buffer coefficients */
440 for ( j = 0 ; j < p_out_buf->i_nb_samples - i_delay ; j++ )
442 ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
443 += p_in[ j * i_input_nb + i_source_channel_offset ]
444 * d_amplitude_factor;
447 /* overflow buffer coefficients */
448 for ( j = 0 ; j < i_delay ; j++ )
450 ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
451 += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
452 * i_input_nb + i_source_channel_offset ]
453 * d_amplitude_factor;
458 /* overflow buffer coefficients only */
459 for ( j = 0 ; j < p_out_buf->i_nb_samples ; j++ )
461 ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
462 * i_output_nb + i_dest_channel_offset ]
463 += p_in[ j * i_input_nb + i_source_channel_offset ]
464 * d_amplitude_factor;
471 memset ( p_out , 0 , i_out_size );