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
6 * $Id: headphone.c,v 1.1 2002/12/09 00:52:42 babal Exp $
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 *****************************************************************************/
49 set_description( _("headphone channel mixer with virtual spatialization effect") );
50 set_capability( "audio filter", 0 );
51 set_callbacks( Create, Destroy );
52 add_shortcut( "headphone" );
56 /*****************************************************************************
57 * Internal data structures
58 *****************************************************************************/
59 struct atomic_operation_t
61 int i_source_channel_offset;
62 int i_dest_channel_offset;
63 unsigned int i_delay;/* in sample unit */
64 double d_amplitude_factor;
67 struct aout_filter_sys_t
69 size_t i_overflow_buffer_size;/* in bytes */
70 byte_t * p_overflow_buffer;
71 unsigned int i_nb_atomic_operations;
72 struct atomic_operation_t * p_atomic_operations;
75 /*****************************************************************************
76 * Init: initialize internal data structures
77 * and computes the needed atomic operations
78 *****************************************************************************/
79 /* x and z represent the coordinates of the virtual speaker
80 * relatively to the center of the listener's head, measured in meters :
89 * rear left rear right
93 static void ComputeChannelOperations ( struct aout_filter_sys_t * p_data
94 , unsigned int i_rate , unsigned int i_next_atomic_operation
95 , int i_source_channel_offset , double d_x , double d_z
96 , double d_channel_amplitude_factor )
98 double d_c = 340; /*sound celerity (unit: m/s)*/
101 p_data->p_atomic_operations[i_next_atomic_operation]
102 .i_source_channel_offset = i_source_channel_offset;
103 p_data->p_atomic_operations[i_next_atomic_operation]
104 .i_dest_channel_offset = 0;/* left */
105 p_data->p_atomic_operations[i_next_atomic_operation]
106 .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
110 p_data->p_atomic_operations[i_next_atomic_operation]
111 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
115 p_data->p_atomic_operations[i_next_atomic_operation]
116 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
120 p_data->p_atomic_operations[i_next_atomic_operation]
121 .d_amplitude_factor = d_channel_amplitude_factor / 2;
125 p_data->p_atomic_operations[i_next_atomic_operation + 1]
126 .i_source_channel_offset = i_source_channel_offset;
127 p_data->p_atomic_operations[i_next_atomic_operation + 1]
128 .i_dest_channel_offset = 1;/* right */
129 p_data->p_atomic_operations[i_next_atomic_operation + 1]
130 .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
134 p_data->p_atomic_operations[i_next_atomic_operation + 1]
135 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
139 p_data->p_atomic_operations[i_next_atomic_operation + 1]
140 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
144 p_data->p_atomic_operations[i_next_atomic_operation + 1]
145 .d_amplitude_factor = d_channel_amplitude_factor / 2;
149 static int Init ( aout_filter_t * p_filter , struct aout_filter_sys_t * p_data
150 , unsigned int i_nb_channels , uint32_t i_physical_channels
151 , unsigned int i_rate )
153 double d_x = config_GetInt ( p_filter , "headphone-dim" );
155 double d_z_rear = -d_x/3;
156 unsigned int i_next_atomic_operation;
157 int i_source_channel_offset;
160 if ( p_data == NULL )
162 msg_Dbg ( p_filter, "passing a null pointer as argument" );
166 /* Number of elementary operations */
167 p_data->i_nb_atomic_operations = i_nb_channels * 2;
168 p_data->p_atomic_operations = malloc ( sizeof(struct atomic_operation_t)
169 * p_data->i_nb_atomic_operations );
170 if ( p_data->p_atomic_operations == NULL )
172 msg_Err( p_filter, "out of memory" );
176 /* For each virtual speaker, computes elementary wave propagation time
178 i_next_atomic_operation = 0;
179 i_source_channel_offset = 0;
180 if ( i_physical_channels & AOUT_CHAN_LEFT )
182 ComputeChannelOperations ( p_data , i_rate
183 , i_next_atomic_operation , i_source_channel_offset
184 , -d_x , d_z , 2.0 / i_nb_channels );
185 i_next_atomic_operation += 2;
186 i_source_channel_offset++;
188 if ( i_physical_channels & AOUT_CHAN_RIGHT )
190 ComputeChannelOperations ( p_data , i_rate
191 , i_next_atomic_operation , i_source_channel_offset
192 , d_x , d_z , 2.0 / i_nb_channels );
193 i_next_atomic_operation += 2;
194 i_source_channel_offset++;
196 if ( i_physical_channels & AOUT_CHAN_REARLEFT )
198 ComputeChannelOperations ( p_data , i_rate
199 , i_next_atomic_operation , i_source_channel_offset
200 , -d_x , d_z_rear , 1.5 / i_nb_channels );
201 i_next_atomic_operation += 2;
202 i_source_channel_offset++;
204 if ( i_physical_channels & AOUT_CHAN_REARRIGHT )
206 ComputeChannelOperations ( p_data , i_rate
207 , i_next_atomic_operation , i_source_channel_offset
208 , d_x , d_z_rear , 1.5 / i_nb_channels );
209 i_next_atomic_operation += 2;
210 i_source_channel_offset++;
212 if ( i_physical_channels & AOUT_CHAN_CENTER )
214 ComputeChannelOperations ( p_data , i_rate
215 , i_next_atomic_operation , i_source_channel_offset
216 , 0 , d_z , 1.5 / i_nb_channels );
217 i_next_atomic_operation += 2;
218 i_source_channel_offset++;
220 if ( i_physical_channels & AOUT_CHAN_LFE )
222 ComputeChannelOperations ( p_data , i_rate
223 , i_next_atomic_operation , i_source_channel_offset
224 , 0 , d_z_rear , 5.0 / i_nb_channels );
225 i_next_atomic_operation += 2;
226 i_source_channel_offset++;
229 /* Initialize the overflow buffer
230 * we need it because the process induce a delay in the samples */
231 p_data->i_overflow_buffer_size = 0;
232 for ( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
234 if ( p_data->i_overflow_buffer_size
235 < p_data->p_atomic_operations[i].i_delay * i_nb_channels
238 p_data->i_overflow_buffer_size
239 = p_data->p_atomic_operations[i].i_delay * i_nb_channels
243 p_data->p_overflow_buffer = malloc ( p_data->i_overflow_buffer_size );
244 if ( p_data->p_atomic_operations == NULL )
246 msg_Err( p_filter, "out of memory" );
249 memset ( p_data->p_overflow_buffer , 0 , p_data->i_overflow_buffer_size );
255 /*****************************************************************************
256 * Create: allocate headphone downmixer
257 *****************************************************************************/
258 static int Create( vlc_object_t *p_this )
260 aout_filter_t * p_filter = (aout_filter_t *)p_this;
262 if ( p_filter->output.i_physical_channels != ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
263 || p_filter->input.i_format != p_filter->output.i_format
264 || p_filter->input.i_rate != p_filter->output.i_rate
265 || (p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
266 && p_filter->input.i_format != VLC_FOURCC('f','i','3','2')) )
271 /* Allocate the memory needed to store the module's structure */
272 p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
273 if ( p_filter->p_sys == NULL )
275 msg_Err( p_filter, "out of memory" );
278 p_filter->p_sys->i_overflow_buffer_size = 0;
279 p_filter->p_sys->p_overflow_buffer = NULL;
280 p_filter->p_sys->i_nb_atomic_operations = 0;
281 p_filter->p_sys->p_atomic_operations = NULL;
283 if ( Init( p_filter , p_filter->p_sys
284 , aout_FormatNbChannels ( &p_filter->input )
285 , p_filter->input.i_physical_channels
286 , p_filter->input.i_rate ) < 0 )
291 p_filter->pf_do_work = DoWork;
292 p_filter->b_in_place = 0;
297 /*****************************************************************************
298 * Destroy: deallocate resources associated with headphone downmixer
299 *****************************************************************************/
300 static void Destroy( vlc_object_t *p_this )
302 aout_filter_t * p_filter = (aout_filter_t *)p_this;
304 if ( p_filter->p_sys != NULL )
306 if ( p_filter->p_sys->p_overflow_buffer != NULL )
308 free ( p_filter->p_sys->p_overflow_buffer );
310 if ( p_filter->p_sys->p_atomic_operations != NULL )
312 free ( p_filter->p_sys->p_atomic_operations );
314 free ( p_filter->p_sys );
315 p_filter->p_sys = NULL;
319 /*****************************************************************************
320 * DoWork: convert a buffer
321 *****************************************************************************/
322 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
323 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
325 int i_input_nb = aout_FormatNbChannels( &p_filter->input );
326 int i_output_nb = aout_FormatNbChannels( &p_filter->output );
328 float * p_in = (float*) p_in_buf->p_buffer;
333 size_t i_overflow_size;/* in bytes */
334 size_t i_out_size;/* in bytes */
338 int i_source_channel_offset;
339 int i_dest_channel_offset;
340 unsigned int i_delay;
341 double d_amplitude_factor;
344 /* out buffer characterisitcs */
345 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
346 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
347 p_out = p_out_buf->p_buffer;
348 i_out_size = p_out_buf->i_nb_bytes;
350 if ( p_filter->p_sys != NULL )
352 /* Slide the overflow buffer */
353 p_overflow = p_filter->p_sys->p_overflow_buffer;
354 i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
356 memset ( p_out , 0 , i_out_size );
357 if ( i_out_size > i_overflow_size )
358 memcpy ( p_out , p_overflow , i_overflow_size );
360 memcpy ( p_out , p_overflow , i_out_size );
362 p_slide = p_filter->p_sys->p_overflow_buffer;
363 while ( p_slide < p_overflow + i_overflow_size )
365 if ( p_slide + i_out_size < p_overflow + i_overflow_size )
367 memset ( p_slide , 0 , i_out_size );
368 if ( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
369 memcpy ( p_slide , p_slide + i_out_size , i_out_size );
371 memcpy ( p_slide , p_slide + i_out_size
372 , p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
376 memset ( p_slide , 0 , p_overflow + i_overflow_size - p_slide );
378 p_slide += i_out_size;
381 /* apply the atomic operations */
382 for ( i = 0 ; i < p_filter->p_sys->i_nb_atomic_operations ; i++ )
384 /* shorter variable names */
385 i_source_channel_offset
386 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
387 i_dest_channel_offset
388 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
389 i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
391 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
393 if ( p_out_buf->i_nb_samples > i_delay )
395 /* current buffer coefficients */
396 for ( j = 0 ; j < p_out_buf->i_nb_samples - i_delay ; j++ )
398 ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
399 += p_in[ j * i_input_nb + i_source_channel_offset ]
400 * d_amplitude_factor;
403 /* overflow buffer coefficients */
404 for ( j = 0 ; j < i_delay ; j++ )
406 ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
407 += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
408 * i_input_nb + i_source_channel_offset ]
409 * d_amplitude_factor;
414 /* overflow buffer coefficients only */
415 for ( j = 0 ; j < p_out_buf->i_nb_samples ; j++ )
417 ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
418 * i_output_nb + i_dest_channel_offset ]
419 += p_in[ j * i_input_nb + i_source_channel_offset ]
420 * d_amplitude_factor;
427 memset ( p_out , 0 , i_out_size );