coreaudio: fix play of uninitialized data (loud CRACK)
[vlc.git] / lib / audio.c
blobaab84cd47a9f9993bb5cd9825ddc368182dbe645
1 /*****************************************************************************
2 * libvlc_audio.c: New libvlc audio control API
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
6 * Authors: Filippo Carone <filippo@carone.org>
7 * Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
29 #include <math.h>
31 #include <vlc/libvlc.h>
32 #include <vlc/libvlc_renderer_discoverer.h>
33 #include <vlc/libvlc_picture.h>
34 #include <vlc/libvlc_media.h>
35 #include <vlc/libvlc_media_player.h>
37 #include <vlc_common.h>
38 #include <vlc_aout.h>
39 #include <vlc_modules.h>
41 #include "libvlc_internal.h"
42 #include "media_player_internal.h"
45 * Remember to release the returned audio_output_t since it is locked at
46 * the end of this function.
48 static audio_output_t *GetAOut( libvlc_media_player_t *mp )
50 assert( mp != NULL );
52 audio_output_t *p_aout = vlc_player_aout_Hold(mp->player);
53 if( p_aout == NULL )
54 libvlc_printerr( "No active audio output" );
55 return p_aout;
58 /*****************************************
59 * Get the list of available audio outputs
60 *****************************************/
61 libvlc_audio_output_t *
62 libvlc_audio_output_list_get( libvlc_instance_t *p_instance )
64 size_t count;
65 module_t **module_list = module_list_get( &count );
66 libvlc_audio_output_t *list = NULL;
68 for (size_t i = 0; i < count; i++)
70 module_t *module = module_list[i];
72 if( !module_provides( module, "audio output" ) )
73 continue;
75 libvlc_audio_output_t *item = malloc( sizeof( *item ) );
76 if( unlikely(item == NULL) )
78 error:
79 libvlc_printerr( "Not enough memory" );
80 libvlc_audio_output_list_release( list );
81 list = NULL;
82 break;
85 item->psz_name = strdup( module_get_object( module ) );
86 item->psz_description = strdup( module_get_name( module, true ) );
87 if( unlikely(item->psz_name == NULL || item->psz_description == NULL) )
89 free( item->psz_name );
90 free( item->psz_description );
91 free( item );
92 goto error;
94 item->p_next = list;
95 list = item;
97 module_list_free( module_list );
99 VLC_UNUSED( p_instance );
100 return list;
103 /********************************************
104 * Free the list of available audio outputs
105 ***********************************************/
106 void libvlc_audio_output_list_release( libvlc_audio_output_t *list )
108 while( list != NULL )
110 libvlc_audio_output_t *next = list->p_next;
112 free( list->psz_name );
113 free( list->psz_description );
114 free( list );
115 list = next;
120 /***********************
121 * Set the audio output.
122 ***********************/
123 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
125 char *value;
127 if( !module_exists( psz_name )
128 || asprintf( &value, "%s,none", psz_name ) == -1 )
129 return -1;
130 var_SetString( mp, "aout", value );
131 free( value );
133 return 0;
136 libvlc_audio_output_device_t *
137 libvlc_audio_output_device_enum( libvlc_media_player_t *mp )
139 audio_output_t *aout = GetAOut( mp );
140 if( aout == NULL )
141 return NULL;
143 libvlc_audio_output_device_t *list, **pp = &list;
144 char **values, **texts;
146 int n = aout_DevicesList( aout, &values, &texts );
147 aout_Release(aout);
148 if( n < 0 )
149 goto err;
151 for (int i = 0; i < n; i++)
153 libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
154 if( unlikely(item == NULL) )
156 free( texts[i] );
157 free( values[i] );
158 continue;
161 *pp = item;
162 pp = &item->p_next;
163 item->psz_device = values[i];
164 item->psz_description = texts[i];
167 free( texts );
168 free( values );
169 err:
170 *pp = NULL;
171 return list;
174 libvlc_audio_output_device_t *
175 libvlc_audio_output_device_list_get( libvlc_instance_t *p_instance,
176 const char *aout )
178 char varname[32];
179 if( (size_t)snprintf( varname, sizeof(varname), "%s-audio-device", aout )
180 >= sizeof(varname) )
181 return NULL;
183 if( config_GetType(varname) != VLC_VAR_STRING )
184 return NULL;
186 libvlc_audio_output_device_t *list = NULL, **pp = &list;
187 char **values, **texts;
188 ssize_t count = config_GetPszChoices( varname, &values, &texts );
189 for( ssize_t i = 0; i < count; i++ )
191 libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
192 if( unlikely(item == NULL) )
193 break;
195 *pp = item;
196 pp = &item->p_next;
197 item->psz_device = values[i];
198 item->psz_description = texts[i];
201 *pp = NULL;
202 free( texts );
203 free( values );
204 (void) p_instance;
205 return list;
208 void libvlc_audio_output_device_list_release( libvlc_audio_output_device_t *l )
210 while( l != NULL )
212 libvlc_audio_output_device_t *next = l->p_next;
214 free( l->psz_description );
215 free( l->psz_device );
216 free( l );
217 l = next;
221 /*****************************
222 * Set device for using
223 *****************************/
224 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
225 const char *module, const char *devid )
227 if( devid == NULL )
228 return;
230 if( module != NULL )
232 char *cfg_name;
234 if( asprintf( &cfg_name, "%s-audio-device", module ) == -1 )
235 return;
237 if( !var_Type( mp, cfg_name ) )
238 /* Don't recreate the same variable over and over and over... */
239 var_Create( mp, cfg_name, VLC_VAR_STRING );
240 var_SetString( mp, cfg_name, devid );
241 free( cfg_name );
242 return;
245 audio_output_t *aout = GetAOut( mp );
246 if( aout == NULL )
247 return;
249 aout_DeviceSet( aout, devid );
250 aout_Release(aout);
253 char *libvlc_audio_output_device_get( libvlc_media_player_t *mp )
255 audio_output_t *aout = GetAOut( mp );
256 if( aout == NULL )
257 return NULL;
259 char *devid = aout_DeviceGet( aout );
261 aout_Release(aout);
263 return devid;
266 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
268 int mute = libvlc_audio_get_mute( mp );
269 if( mute != -1 )
270 libvlc_audio_set_mute( mp, !mute );
273 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
275 int mute = -1;
277 audio_output_t *aout = GetAOut( mp );
278 if( aout != NULL )
280 mute = aout_MuteGet( aout );
281 aout_Release(aout);
283 return mute;
286 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
288 audio_output_t *aout = GetAOut( mp );
289 if( aout != NULL )
291 mute = aout_MuteSet( aout, mute );
292 aout_Release(aout);
296 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
298 int volume = -1;
300 audio_output_t *aout = GetAOut( mp );
301 if( aout != NULL )
303 float vol = aout_VolumeGet( aout );
304 aout_Release(aout);
305 volume = lroundf( vol * 100.f );
307 return volume;
310 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
312 float vol = volume / 100.f;
313 if (!isgreaterequal(vol, 0.f))
315 libvlc_printerr( "Volume out of range" );
316 return -1;
319 int ret = -1;
320 audio_output_t *aout = GetAOut( mp );
321 if( aout != NULL )
323 ret = aout_VolumeSet( aout, vol );
324 aout_Release(aout);
326 return ret;
329 /*****************************************************************************
330 * libvlc_audio_get_track_count : Get the number of available audio tracks
331 *****************************************************************************/
332 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
334 vlc_player_t *player = p_mi->player;
335 vlc_player_Lock(player);
337 int ret = vlc_player_GetTrackCount(p_mi->player, AUDIO_ES);
339 vlc_player_Unlock(player);
340 return ret;
343 /*****************************************************************************
344 * libvlc_audio_get_track_description : Get the description of available audio tracks
345 *****************************************************************************/
346 libvlc_track_description_t *
347 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
349 return libvlc_get_track_description( p_mi, AUDIO_ES );
352 /*****************************************************************************
353 * libvlc_audio_get_track : Get the current audio track
354 *****************************************************************************/
355 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
357 vlc_player_t *player = p_mi->player;
358 vlc_player_Lock(player);
360 const struct vlc_player_track *track =
361 vlc_player_GetSelectedTrack(player, AUDIO_ES);
362 int id = track ? vlc_es_id_GetInputId(track->es_id) : -1;
364 vlc_player_Unlock(player);
365 return id;
368 /*****************************************************************************
369 * libvlc_audio_set_track : Set the current audio track
370 *****************************************************************************/
371 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
373 int i_ret = -1;
375 vlc_player_t *player = p_mi->player;
376 vlc_player_Lock(player);
378 size_t count = vlc_player_GetAudioTrackCount(player);
379 for( size_t i = 0; i < count; i++ )
381 const struct vlc_player_track *track =
382 vlc_player_GetAudioTrackAt(player, i);
383 if (i_track == vlc_es_id_GetInputId(track->es_id))
385 /* found */
386 vlc_player_SelectTrack(player, track, VLC_PLAYER_SELECT_EXCLUSIVE);
387 i_ret = 0;
388 goto end;
391 libvlc_printerr( "Track identifier not found" );
392 end:
393 vlc_player_Unlock(player);
394 return i_ret;
397 /*****************************************************************************
398 * libvlc_audio_get_channel : Get the current audio channel
399 *****************************************************************************/
400 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
402 audio_output_t *p_aout = GetAOut( mp );
403 if( !p_aout )
404 return 0;
406 int val = var_GetInteger( p_aout, "stereo-mode" );
407 aout_Release(p_aout);
408 return val;
411 /*****************************************************************************
412 * libvlc_audio_set_channel : Set the current audio channel
413 *****************************************************************************/
414 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
416 audio_output_t *p_aout = GetAOut( mp );
417 int ret = 0;
419 if( !p_aout )
420 return -1;
422 if( var_SetInteger( p_aout, "stereo-mode", channel ) < 0 )
424 libvlc_printerr( "Audio channel out of range" );
425 ret = -1;
427 aout_Release(p_aout);
428 return ret;
431 /*****************************************************************************
432 * libvlc_audio_get_delay : Get the current audio delay
433 *****************************************************************************/
434 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
436 vlc_player_t *player = p_mi->player;
437 vlc_player_Lock(player);
439 vlc_tick_t delay = vlc_player_GetAudioDelay(player);
441 vlc_player_Unlock(player);
443 return US_FROM_VLC_TICK(delay);
446 /*****************************************************************************
447 * libvlc_audio_set_delay : Set the current audio delay
448 *****************************************************************************/
449 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
451 vlc_player_t *player = p_mi->player;
452 vlc_player_Lock(player);
454 vlc_player_SetAudioDelay(player, VLC_TICK_FROM_US(i_delay),
455 VLC_PLAYER_WHENCE_ABSOLUTE);
457 vlc_player_Unlock(player);
458 /* may not fail anymore, keep int not to break the API */
459 return 0;
462 /*****************************************************************************
463 * libvlc_audio_equalizer_get_preset_count : Get the number of equalizer presets
464 *****************************************************************************/
465 unsigned libvlc_audio_equalizer_get_preset_count( void )
467 return NB_PRESETS;
470 /*****************************************************************************
471 * libvlc_audio_equalizer_get_preset_name : Get the name for a preset
472 *****************************************************************************/
473 const char *libvlc_audio_equalizer_get_preset_name( unsigned u_index )
475 if ( u_index >= NB_PRESETS )
476 return NULL;
478 return preset_list_text[ u_index ];
481 /*****************************************************************************
482 * libvlc_audio_equalizer_get_band_count : Get the number of equalizer frequency bands
483 *****************************************************************************/
484 unsigned libvlc_audio_equalizer_get_band_count( void )
486 return EQZ_BANDS_MAX;
489 /*****************************************************************************
490 * libvlc_audio_equalizer_get_band_frequency : Get the frequency for a band
491 *****************************************************************************/
492 float libvlc_audio_equalizer_get_band_frequency( unsigned u_index )
494 if ( u_index >= EQZ_BANDS_MAX )
495 return -1.f;
497 return f_iso_frequency_table_10b[ u_index ];
500 /*****************************************************************************
501 * libvlc_audio_equalizer_new : Create a new audio equalizer with zeroed values
502 *****************************************************************************/
503 libvlc_equalizer_t *libvlc_audio_equalizer_new( void )
505 libvlc_equalizer_t *p_equalizer;
506 p_equalizer = malloc( sizeof( *p_equalizer ) );
507 if ( unlikely( p_equalizer == NULL ) )
508 return NULL;
510 p_equalizer->f_preamp = 0.f;
511 for ( unsigned i = 0; i < EQZ_BANDS_MAX; i++ )
512 p_equalizer->f_amp[ i ] = 0.f;
514 return p_equalizer;
517 /*****************************************************************************
518 * libvlc_audio_equalizer_new_from_preset : Create a new audio equalizer based on a preset
519 *****************************************************************************/
520 libvlc_equalizer_t *libvlc_audio_equalizer_new_from_preset( unsigned u_index )
522 libvlc_equalizer_t *p_equalizer;
524 if ( u_index >= NB_PRESETS )
525 return NULL;
527 p_equalizer = malloc( sizeof( *p_equalizer ) );
528 if ( unlikely( p_equalizer == NULL ) )
529 return NULL;
531 p_equalizer->f_preamp = eqz_preset_10b[ u_index ].f_preamp;
533 for ( unsigned i = 0; i < EQZ_BANDS_MAX; i++ )
534 p_equalizer->f_amp[ i ] = eqz_preset_10b[ u_index ].f_amp[ i ];
536 return p_equalizer;
539 /*****************************************************************************
540 * libvlc_audio_equalizer_release : Release a previously created equalizer
541 *****************************************************************************/
542 void libvlc_audio_equalizer_release( libvlc_equalizer_t *p_equalizer )
544 free( p_equalizer );
547 /*****************************************************************************
548 * libvlc_audio_equalizer_set_preamp : Set the preamp value for an equalizer
549 *****************************************************************************/
550 int libvlc_audio_equalizer_set_preamp( libvlc_equalizer_t *p_equalizer, float f_preamp )
552 if( isnan(f_preamp) )
553 return -1;
554 if( f_preamp < -20.f )
555 f_preamp = -20.f;
556 else if( f_preamp > 20.f )
557 f_preamp = 20.f;
559 p_equalizer->f_preamp = f_preamp;
560 return 0;
563 /*****************************************************************************
564 * libvlc_audio_equalizer_get_preamp : Get the preamp value for an equalizer
565 *****************************************************************************/
566 float libvlc_audio_equalizer_get_preamp( libvlc_equalizer_t *p_equalizer )
568 return p_equalizer->f_preamp;
571 /*****************************************************************************
572 * libvlc_audio_equalizer_set_amp_at_index : Set the amplification value for an equalizer band
573 *****************************************************************************/
574 int libvlc_audio_equalizer_set_amp_at_index( libvlc_equalizer_t *p_equalizer, float f_amp, unsigned u_band )
576 if( u_band >= EQZ_BANDS_MAX || isnan(f_amp) )
577 return -1;
580 if( f_amp < -20.f )
581 f_amp = -20.f;
582 else if( f_amp > 20.f )
583 f_amp = 20.f;
585 p_equalizer->f_amp[ u_band ] = f_amp;
586 return 0;
589 /*****************************************************************************
590 * libvlc_audio_equalizer_get_amp_at_index : Get the amplification value for an equalizer band
591 *****************************************************************************/
592 float libvlc_audio_equalizer_get_amp_at_index( libvlc_equalizer_t *p_equalizer, unsigned u_band )
594 if ( u_band >= EQZ_BANDS_MAX )
595 return nanf("");
597 return p_equalizer->f_amp[ u_band ];