contrib: update libogg to 1.3.3
[vlc.git] / lib / audio.c
blob3aed6f5ff3735e018ce293ead3fc55eac182757d
1 /*****************************************************************************
2 * libvlc_audio.c: New libvlc audio control API
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Filippo Carone <filippo@carone.org>
8 * Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <assert.h>
30 #include <math.h>
32 #include <vlc/libvlc.h>
33 #include <vlc/libvlc_renderer_discoverer.h>
34 #include <vlc/libvlc_media.h>
35 #include <vlc/libvlc_media_player.h>
37 #include <vlc_common.h>
38 #include <vlc_input.h>
39 #include <vlc_aout.h>
40 #include <vlc_modules.h>
42 #include "libvlc_internal.h"
43 #include "media_player_internal.h"
46 * Remember to release the returned audio_output_t since it is locked at
47 * the end of this function.
49 static audio_output_t *GetAOut( libvlc_media_player_t *mp )
51 assert( mp != NULL );
53 audio_output_t *p_aout = input_resource_HoldAout( mp->input.p_resource );
54 if( p_aout == NULL )
55 libvlc_printerr( "No active audio output" );
56 return p_aout;
59 /*****************************************
60 * Get the list of available audio outputs
61 *****************************************/
62 libvlc_audio_output_t *
63 libvlc_audio_output_list_get( libvlc_instance_t *p_instance )
65 size_t count;
66 module_t **module_list = module_list_get( &count );
67 libvlc_audio_output_t *list = NULL;
69 for (size_t i = 0; i < count; i++)
71 module_t *module = module_list[i];
73 if( !module_provides( module, "audio output" ) )
74 continue;
76 libvlc_audio_output_t *item = malloc( sizeof( *item ) );
77 if( unlikely(item == NULL) )
79 error:
80 libvlc_printerr( "Not enough memory" );
81 libvlc_audio_output_list_release( list );
82 list = NULL;
83 break;
86 item->psz_name = strdup( module_get_object( module ) );
87 item->psz_description = strdup( module_get_name( module, true ) );
88 if( unlikely(item->psz_name == NULL || item->psz_description == NULL) )
90 free( item->psz_name );
91 free( item->psz_description );
92 free( item );
93 goto error;
95 item->p_next = list;
96 list = item;
98 module_list_free( module_list );
100 VLC_UNUSED( p_instance );
101 return list;
104 /********************************************
105 * Free the list of available audio outputs
106 ***********************************************/
107 void libvlc_audio_output_list_release( libvlc_audio_output_t *list )
109 while( list != NULL )
111 libvlc_audio_output_t *next = list->p_next;
113 free( list->psz_name );
114 free( list->psz_description );
115 free( list );
116 list = next;
121 /***********************
122 * Set the audio output.
123 ***********************/
124 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
126 char *value;
128 if( !module_exists( psz_name )
129 || asprintf( &value, "%s,none", psz_name ) == -1 )
130 return -1;
131 var_SetString( mp, "aout", value );
132 free( value );
134 /* Forget the existing audio output */
135 input_resource_ResetAout(mp->input.p_resource);
137 /* Create a new audio output */
138 audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
139 if( aout != NULL )
140 input_resource_PutAout(mp->input.p_resource, aout);
142 return 0;
145 libvlc_audio_output_device_t *
146 libvlc_audio_output_device_enum( libvlc_media_player_t *mp )
148 audio_output_t *aout = GetAOut( mp );
149 if( aout == NULL )
150 return NULL;
152 libvlc_audio_output_device_t *list, **pp = &list;
153 char **values, **texts;
155 int n = aout_DevicesList( aout, &values, &texts );
156 vlc_object_release( aout );
157 if( n < 0 )
158 goto err;
160 for (int i = 0; i < n; i++)
162 libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
163 if( unlikely(item == NULL) )
165 free( texts[i] );
166 free( values[i] );
167 continue;
170 *pp = item;
171 pp = &item->p_next;
172 item->psz_device = values[i];
173 item->psz_description = texts[i];
176 free( texts );
177 free( values );
178 err:
179 *pp = NULL;
180 return list;
183 libvlc_audio_output_device_t *
184 libvlc_audio_output_device_list_get( libvlc_instance_t *p_instance,
185 const char *aout )
187 char varname[32];
188 if( (size_t)snprintf( varname, sizeof(varname), "%s-audio-device", aout )
189 >= sizeof(varname) )
190 return NULL;
192 libvlc_audio_output_device_t *list = NULL, **pp = &list;
193 char **values, **texts;
194 ssize_t count = config_GetPszChoices( VLC_OBJECT(p_instance->p_libvlc_int),
195 varname, &values, &texts );
196 for( ssize_t i = 0; i < count; i++ )
198 libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
199 if( unlikely(item == NULL) )
200 break;
202 *pp = item;
203 pp = &item->p_next;
204 item->psz_device = values[i];
205 item->psz_description = texts[i];
208 *pp = NULL;
209 free( texts );
210 free( values );
211 (void) p_instance;
212 return list;
215 void libvlc_audio_output_device_list_release( libvlc_audio_output_device_t *l )
217 while( l != NULL )
219 libvlc_audio_output_device_t *next = l->p_next;
221 free( l->psz_description );
222 free( l->psz_device );
223 free( l );
224 l = next;
228 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
229 const char *psz_audio_output )
231 (void) p_instance; (void) psz_audio_output;
232 return 0;
235 char *libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
236 const char *psz_audio_output,
237 int i_device )
239 (void) p_instance; (void) psz_audio_output; (void) i_device;
240 return NULL;
243 char *libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
244 const char *psz_audio_output,
245 int i_device )
247 (void) p_instance; (void) psz_audio_output; (void) i_device;
248 return NULL;
251 /*****************************
252 * Set device for using
253 *****************************/
254 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
255 const char *module, const char *devid )
257 if( devid == NULL )
258 return;
260 if( module != NULL )
262 char *cfg_name;
264 if( asprintf( &cfg_name, "%s-audio-device", module ) == -1 )
265 return;
267 if( !var_Type( mp, cfg_name ) )
268 /* Don't recreate the same variable over and over and over... */
269 var_Create( mp, cfg_name, VLC_VAR_STRING );
270 var_SetString( mp, cfg_name, devid );
271 free( cfg_name );
272 return;
275 audio_output_t *aout = GetAOut( mp );
276 if( aout == NULL )
277 return;
279 aout_DeviceSet( aout, devid );
280 vlc_object_release( aout );
283 char *libvlc_audio_output_device_get( libvlc_media_player_t *mp )
285 audio_output_t *aout = GetAOut( mp );
286 if( aout == NULL )
287 return NULL;
289 char *devid = aout_DeviceGet( aout );
291 vlc_object_release( aout );
293 return devid;
296 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
298 (void) mp;
299 return libvlc_AudioOutputDevice_Error;
302 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
303 int device_type )
305 (void) mp; (void) device_type;
308 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
310 int mute = libvlc_audio_get_mute( mp );
311 if( mute != -1 )
312 libvlc_audio_set_mute( mp, !mute );
315 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
317 int mute = -1;
319 audio_output_t *aout = GetAOut( mp );
320 if( aout != NULL )
322 mute = aout_MuteGet( aout );
323 vlc_object_release( aout );
325 return mute;
328 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
330 audio_output_t *aout = GetAOut( mp );
331 if( aout != NULL )
333 mute = aout_MuteSet( aout, mute );
334 vlc_object_release( aout );
338 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
340 int volume = -1;
342 audio_output_t *aout = GetAOut( mp );
343 if( aout != NULL )
345 float vol = aout_VolumeGet( aout );
346 vlc_object_release( aout );
347 volume = lroundf( vol * 100.f );
349 return volume;
352 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
354 float vol = volume / 100.f;
355 if (!isgreaterequal(vol, 0.f))
357 libvlc_printerr( "Volume out of range" );
358 return -1;
361 int ret = -1;
362 audio_output_t *aout = GetAOut( mp );
363 if( aout != NULL )
365 ret = aout_VolumeSet( aout, vol );
366 vlc_object_release( aout );
368 return ret;
371 /*****************************************************************************
372 * libvlc_audio_get_track_count : Get the number of available audio tracks
373 *****************************************************************************/
374 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
376 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
377 int i_track_count;
379 if( !p_input_thread )
380 return -1;
382 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
384 vlc_object_release( p_input_thread );
385 return i_track_count;
388 /*****************************************************************************
389 * libvlc_audio_get_track_description : Get the description of available audio tracks
390 *****************************************************************************/
391 libvlc_track_description_t *
392 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
394 return libvlc_get_track_description( p_mi, "audio-es" );
397 /*****************************************************************************
398 * libvlc_audio_get_track : Get the current audio track
399 *****************************************************************************/
400 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
402 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
403 if( !p_input_thread )
404 return -1;
406 int id = var_GetInteger( p_input_thread, "audio-es" );
407 vlc_object_release( p_input_thread );
408 return id;
411 /*****************************************************************************
412 * libvlc_audio_set_track : Set the current audio track
413 *****************************************************************************/
414 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
416 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
417 vlc_value_t val_list;
418 int i_ret = -1;
420 if( !p_input_thread )
421 return -1;
423 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
424 for( int i = 0; i < val_list.p_list->i_count; i++ )
426 if( i_track == val_list.p_list->p_values[i].i_int )
428 if( var_SetInteger( p_input_thread, "audio-es", i_track ) < 0 )
429 break;
430 i_ret = 0;
431 goto end;
434 libvlc_printerr( "Track identifier not found" );
435 end:
436 var_FreeList( &val_list, NULL );
437 vlc_object_release( p_input_thread );
438 return i_ret;
441 /*****************************************************************************
442 * libvlc_audio_get_channel : Get the current audio channel
443 *****************************************************************************/
444 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
446 audio_output_t *p_aout = GetAOut( mp );
447 if( !p_aout )
448 return 0;
450 int val = var_GetInteger( p_aout, "stereo-mode" );
451 vlc_object_release( p_aout );
452 return val;
455 /*****************************************************************************
456 * libvlc_audio_set_channel : Set the current audio channel
457 *****************************************************************************/
458 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
460 audio_output_t *p_aout = GetAOut( mp );
461 int ret = 0;
463 if( !p_aout )
464 return -1;
466 if( var_SetInteger( p_aout, "stereo-mode", channel ) < 0 )
468 libvlc_printerr( "Audio channel out of range" );
469 ret = -1;
471 vlc_object_release( p_aout );
472 return ret;
475 /*****************************************************************************
476 * libvlc_audio_get_delay : Get the current audio delay
477 *****************************************************************************/
478 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
480 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
481 int64_t val = 0;
482 if( p_input_thread != NULL )
484 val = var_GetInteger( p_input_thread, "audio-delay" );
485 vlc_object_release( p_input_thread );
487 return val;
490 /*****************************************************************************
491 * libvlc_audio_set_delay : Set the current audio delay
492 *****************************************************************************/
493 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
495 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
496 int ret = 0;
497 if( p_input_thread != NULL )
499 var_SetInteger( p_input_thread, "audio-delay", i_delay );
500 vlc_object_release( p_input_thread );
502 else
504 ret = -1;
506 return ret;
509 /*****************************************************************************
510 * libvlc_audio_equalizer_get_preset_count : Get the number of equalizer presets
511 *****************************************************************************/
512 unsigned libvlc_audio_equalizer_get_preset_count( void )
514 return NB_PRESETS;
517 /*****************************************************************************
518 * libvlc_audio_equalizer_get_preset_name : Get the name for a preset
519 *****************************************************************************/
520 const char *libvlc_audio_equalizer_get_preset_name( unsigned u_index )
522 if ( u_index >= NB_PRESETS )
523 return NULL;
525 return preset_list_text[ u_index ];
528 /*****************************************************************************
529 * libvlc_audio_equalizer_get_band_count : Get the number of equalizer frequency bands
530 *****************************************************************************/
531 unsigned libvlc_audio_equalizer_get_band_count( void )
533 return EQZ_BANDS_MAX;
536 /*****************************************************************************
537 * libvlc_audio_equalizer_get_band_frequency : Get the frequency for a band
538 *****************************************************************************/
539 float libvlc_audio_equalizer_get_band_frequency( unsigned u_index )
541 if ( u_index >= EQZ_BANDS_MAX )
542 return -1.f;
544 return f_iso_frequency_table_10b[ u_index ];
547 /*****************************************************************************
548 * libvlc_audio_equalizer_new : Create a new audio equalizer with zeroed values
549 *****************************************************************************/
550 libvlc_equalizer_t *libvlc_audio_equalizer_new( void )
552 libvlc_equalizer_t *p_equalizer;
553 p_equalizer = malloc( sizeof( *p_equalizer ) );
554 if ( unlikely( p_equalizer == NULL ) )
555 return NULL;
557 p_equalizer->f_preamp = 0.f;
558 for ( unsigned i = 0; i < EQZ_BANDS_MAX; i++ )
559 p_equalizer->f_amp[ i ] = 0.f;
561 return p_equalizer;
564 /*****************************************************************************
565 * libvlc_audio_equalizer_new_from_preset : Create a new audio equalizer based on a preset
566 *****************************************************************************/
567 libvlc_equalizer_t *libvlc_audio_equalizer_new_from_preset( unsigned u_index )
569 libvlc_equalizer_t *p_equalizer;
571 if ( u_index >= NB_PRESETS )
572 return NULL;
574 p_equalizer = malloc( sizeof( *p_equalizer ) );
575 if ( unlikely( p_equalizer == NULL ) )
576 return NULL;
578 p_equalizer->f_preamp = eqz_preset_10b[ u_index ].f_preamp;
580 for ( unsigned i = 0; i < EQZ_BANDS_MAX; i++ )
581 p_equalizer->f_amp[ i ] = eqz_preset_10b[ u_index ].f_amp[ i ];
583 return p_equalizer;
586 /*****************************************************************************
587 * libvlc_audio_equalizer_release : Release a previously created equalizer
588 *****************************************************************************/
589 void libvlc_audio_equalizer_release( libvlc_equalizer_t *p_equalizer )
591 free( p_equalizer );
594 /*****************************************************************************
595 * libvlc_audio_equalizer_set_preamp : Set the preamp value for an equalizer
596 *****************************************************************************/
597 int libvlc_audio_equalizer_set_preamp( libvlc_equalizer_t *p_equalizer, float f_preamp )
599 if( isnan(f_preamp) )
600 return -1;
601 if( f_preamp < -20.f )
602 f_preamp = -20.f;
603 else if( f_preamp > 20.f )
604 f_preamp = 20.f;
606 p_equalizer->f_preamp = f_preamp;
607 return 0;
610 /*****************************************************************************
611 * libvlc_audio_equalizer_get_preamp : Get the preamp value for an equalizer
612 *****************************************************************************/
613 float libvlc_audio_equalizer_get_preamp( libvlc_equalizer_t *p_equalizer )
615 return p_equalizer->f_preamp;
618 /*****************************************************************************
619 * libvlc_audio_equalizer_set_amp_at_index : Set the amplification value for an equalizer band
620 *****************************************************************************/
621 int libvlc_audio_equalizer_set_amp_at_index( libvlc_equalizer_t *p_equalizer, float f_amp, unsigned u_band )
623 if( u_band >= EQZ_BANDS_MAX || isnan(f_amp) )
624 return -1;
627 if( f_amp < -20.f )
628 f_amp = -20.f;
629 else if( f_amp > 20.f )
630 f_amp = 20.f;
632 p_equalizer->f_amp[ u_band ] = f_amp;
633 return 0;
636 /*****************************************************************************
637 * libvlc_audio_equalizer_get_amp_at_index : Get the amplification value for an equalizer band
638 *****************************************************************************/
639 float libvlc_audio_equalizer_get_amp_at_index( libvlc_equalizer_t *p_equalizer, unsigned u_band )
641 if ( u_band >= EQZ_BANDS_MAX )
642 return nanf("");
644 return p_equalizer->f_amp[ u_band ];