skins2: the internal interval is not an mtime_t
[vlc.git] / lib / audio.c
blob4d35723023f1f9dadb5ed46fdc1ec5b7c436c150
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 if( config_GetType(varname) != VLC_VAR_STRING )
193 return NULL;
195 libvlc_audio_output_device_t *list = NULL, **pp = &list;
196 char **values, **texts;
197 ssize_t count = config_GetPszChoices( varname, &values, &texts );
198 for( ssize_t i = 0; i < count; i++ )
200 libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
201 if( unlikely(item == NULL) )
202 break;
204 *pp = item;
205 pp = &item->p_next;
206 item->psz_device = values[i];
207 item->psz_description = texts[i];
210 *pp = NULL;
211 free( texts );
212 free( values );
213 (void) p_instance;
214 return list;
217 void libvlc_audio_output_device_list_release( libvlc_audio_output_device_t *l )
219 while( l != NULL )
221 libvlc_audio_output_device_t *next = l->p_next;
223 free( l->psz_description );
224 free( l->psz_device );
225 free( l );
226 l = next;
230 /*****************************
231 * Set device for using
232 *****************************/
233 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
234 const char *module, const char *devid )
236 if( devid == NULL )
237 return;
239 if( module != NULL )
241 char *cfg_name;
243 if( asprintf( &cfg_name, "%s-audio-device", module ) == -1 )
244 return;
246 if( !var_Type( mp, cfg_name ) )
247 /* Don't recreate the same variable over and over and over... */
248 var_Create( mp, cfg_name, VLC_VAR_STRING );
249 var_SetString( mp, cfg_name, devid );
250 free( cfg_name );
251 return;
254 audio_output_t *aout = GetAOut( mp );
255 if( aout == NULL )
256 return;
258 aout_DeviceSet( aout, devid );
259 vlc_object_release( aout );
262 char *libvlc_audio_output_device_get( libvlc_media_player_t *mp )
264 audio_output_t *aout = GetAOut( mp );
265 if( aout == NULL )
266 return NULL;
268 char *devid = aout_DeviceGet( aout );
270 vlc_object_release( aout );
272 return devid;
275 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
277 int mute = libvlc_audio_get_mute( mp );
278 if( mute != -1 )
279 libvlc_audio_set_mute( mp, !mute );
282 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
284 int mute = -1;
286 audio_output_t *aout = GetAOut( mp );
287 if( aout != NULL )
289 mute = aout_MuteGet( aout );
290 vlc_object_release( aout );
292 return mute;
295 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
297 audio_output_t *aout = GetAOut( mp );
298 if( aout != NULL )
300 mute = aout_MuteSet( aout, mute );
301 vlc_object_release( aout );
305 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
307 int volume = -1;
309 audio_output_t *aout = GetAOut( mp );
310 if( aout != NULL )
312 float vol = aout_VolumeGet( aout );
313 vlc_object_release( aout );
314 volume = lroundf( vol * 100.f );
316 return volume;
319 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
321 float vol = volume / 100.f;
322 if (!isgreaterequal(vol, 0.f))
324 libvlc_printerr( "Volume out of range" );
325 return -1;
328 int ret = -1;
329 audio_output_t *aout = GetAOut( mp );
330 if( aout != NULL )
332 ret = aout_VolumeSet( aout, vol );
333 vlc_object_release( aout );
335 return ret;
338 /*****************************************************************************
339 * libvlc_audio_get_track_count : Get the number of available audio tracks
340 *****************************************************************************/
341 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
343 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
344 int i_track_count;
346 if( !p_input_thread )
347 return -1;
349 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
351 vlc_object_release( p_input_thread );
352 return i_track_count;
355 /*****************************************************************************
356 * libvlc_audio_get_track_description : Get the description of available audio tracks
357 *****************************************************************************/
358 libvlc_track_description_t *
359 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
361 return libvlc_get_track_description( p_mi, "audio-es" );
364 /*****************************************************************************
365 * libvlc_audio_get_track : Get the current audio track
366 *****************************************************************************/
367 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
369 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
370 if( !p_input_thread )
371 return -1;
373 int id = var_GetInteger( p_input_thread, "audio-es" );
374 vlc_object_release( p_input_thread );
375 return id;
378 /*****************************************************************************
379 * libvlc_audio_set_track : Set the current audio track
380 *****************************************************************************/
381 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
383 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
384 vlc_value_t *val_list;
385 size_t count;
386 int i_ret = -1;
388 if( !p_input_thread )
389 return -1;
391 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES,
392 &count, &val_list, (char ***)NULL );
393 for( size_t i = 0; i < count; i++ )
395 if( i_track == val_list[i].i_int )
397 if( var_SetInteger( p_input_thread, "audio-es", i_track ) < 0 )
398 break;
399 i_ret = 0;
400 goto end;
403 libvlc_printerr( "Track identifier not found" );
404 end:
405 free( val_list );
406 vlc_object_release( p_input_thread );
407 return i_ret;
410 /*****************************************************************************
411 * libvlc_audio_get_channel : Get the current audio channel
412 *****************************************************************************/
413 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
415 audio_output_t *p_aout = GetAOut( mp );
416 if( !p_aout )
417 return 0;
419 int val = var_GetInteger( p_aout, "stereo-mode" );
420 vlc_object_release( p_aout );
421 return val;
424 /*****************************************************************************
425 * libvlc_audio_set_channel : Set the current audio channel
426 *****************************************************************************/
427 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
429 audio_output_t *p_aout = GetAOut( mp );
430 int ret = 0;
432 if( !p_aout )
433 return -1;
435 if( var_SetInteger( p_aout, "stereo-mode", channel ) < 0 )
437 libvlc_printerr( "Audio channel out of range" );
438 ret = -1;
440 vlc_object_release( p_aout );
441 return ret;
444 /*****************************************************************************
445 * libvlc_audio_get_delay : Get the current audio delay
446 *****************************************************************************/
447 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
449 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
450 int64_t val = 0;
451 if( p_input_thread != NULL )
453 val = var_GetInteger( p_input_thread, "audio-delay" );
454 vlc_object_release( p_input_thread );
456 return val;
459 /*****************************************************************************
460 * libvlc_audio_set_delay : Set the current audio delay
461 *****************************************************************************/
462 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
464 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
465 int ret = 0;
466 if( p_input_thread != NULL )
468 var_SetInteger( p_input_thread, "audio-delay", i_delay );
469 vlc_object_release( p_input_thread );
471 else
473 ret = -1;
475 return ret;
478 /*****************************************************************************
479 * libvlc_audio_equalizer_get_preset_count : Get the number of equalizer presets
480 *****************************************************************************/
481 unsigned libvlc_audio_equalizer_get_preset_count( void )
483 return NB_PRESETS;
486 /*****************************************************************************
487 * libvlc_audio_equalizer_get_preset_name : Get the name for a preset
488 *****************************************************************************/
489 const char *libvlc_audio_equalizer_get_preset_name( unsigned u_index )
491 if ( u_index >= NB_PRESETS )
492 return NULL;
494 return preset_list_text[ u_index ];
497 /*****************************************************************************
498 * libvlc_audio_equalizer_get_band_count : Get the number of equalizer frequency bands
499 *****************************************************************************/
500 unsigned libvlc_audio_equalizer_get_band_count( void )
502 return EQZ_BANDS_MAX;
505 /*****************************************************************************
506 * libvlc_audio_equalizer_get_band_frequency : Get the frequency for a band
507 *****************************************************************************/
508 float libvlc_audio_equalizer_get_band_frequency( unsigned u_index )
510 if ( u_index >= EQZ_BANDS_MAX )
511 return -1.f;
513 return f_iso_frequency_table_10b[ u_index ];
516 /*****************************************************************************
517 * libvlc_audio_equalizer_new : Create a new audio equalizer with zeroed values
518 *****************************************************************************/
519 libvlc_equalizer_t *libvlc_audio_equalizer_new( void )
521 libvlc_equalizer_t *p_equalizer;
522 p_equalizer = malloc( sizeof( *p_equalizer ) );
523 if ( unlikely( p_equalizer == NULL ) )
524 return NULL;
526 p_equalizer->f_preamp = 0.f;
527 for ( unsigned i = 0; i < EQZ_BANDS_MAX; i++ )
528 p_equalizer->f_amp[ i ] = 0.f;
530 return p_equalizer;
533 /*****************************************************************************
534 * libvlc_audio_equalizer_new_from_preset : Create a new audio equalizer based on a preset
535 *****************************************************************************/
536 libvlc_equalizer_t *libvlc_audio_equalizer_new_from_preset( unsigned u_index )
538 libvlc_equalizer_t *p_equalizer;
540 if ( u_index >= NB_PRESETS )
541 return NULL;
543 p_equalizer = malloc( sizeof( *p_equalizer ) );
544 if ( unlikely( p_equalizer == NULL ) )
545 return NULL;
547 p_equalizer->f_preamp = eqz_preset_10b[ u_index ].f_preamp;
549 for ( unsigned i = 0; i < EQZ_BANDS_MAX; i++ )
550 p_equalizer->f_amp[ i ] = eqz_preset_10b[ u_index ].f_amp[ i ];
552 return p_equalizer;
555 /*****************************************************************************
556 * libvlc_audio_equalizer_release : Release a previously created equalizer
557 *****************************************************************************/
558 void libvlc_audio_equalizer_release( libvlc_equalizer_t *p_equalizer )
560 free( p_equalizer );
563 /*****************************************************************************
564 * libvlc_audio_equalizer_set_preamp : Set the preamp value for an equalizer
565 *****************************************************************************/
566 int libvlc_audio_equalizer_set_preamp( libvlc_equalizer_t *p_equalizer, float f_preamp )
568 if( isnan(f_preamp) )
569 return -1;
570 if( f_preamp < -20.f )
571 f_preamp = -20.f;
572 else if( f_preamp > 20.f )
573 f_preamp = 20.f;
575 p_equalizer->f_preamp = f_preamp;
576 return 0;
579 /*****************************************************************************
580 * libvlc_audio_equalizer_get_preamp : Get the preamp value for an equalizer
581 *****************************************************************************/
582 float libvlc_audio_equalizer_get_preamp( libvlc_equalizer_t *p_equalizer )
584 return p_equalizer->f_preamp;
587 /*****************************************************************************
588 * libvlc_audio_equalizer_set_amp_at_index : Set the amplification value for an equalizer band
589 *****************************************************************************/
590 int libvlc_audio_equalizer_set_amp_at_index( libvlc_equalizer_t *p_equalizer, float f_amp, unsigned u_band )
592 if( u_band >= EQZ_BANDS_MAX || isnan(f_amp) )
593 return -1;
596 if( f_amp < -20.f )
597 f_amp = -20.f;
598 else if( f_amp > 20.f )
599 f_amp = 20.f;
601 p_equalizer->f_amp[ u_band ] = f_amp;
602 return 0;
605 /*****************************************************************************
606 * libvlc_audio_equalizer_get_amp_at_index : Get the amplification value for an equalizer band
607 *****************************************************************************/
608 float libvlc_audio_equalizer_get_amp_at_index( libvlc_equalizer_t *p_equalizer, unsigned u_band )
610 if ( u_band >= EQZ_BANDS_MAX )
611 return nanf("");
613 return p_equalizer->f_amp[ u_band ];