Revert "NPAPI-win32: use PKG_CONFIG_LIBDIR to detect libVLC"
[vlc/gmpfix.git] / lib / audio.c
blob51d3ff315c70addab6c4d8d817550ea2e4370d1a
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_media.h>
34 #include <vlc/libvlc_media_player.h>
36 #include <vlc_common.h>
37 #include <vlc_input.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 = input_resource_HoldAout( mp->input.p_resource );
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 );
90 goto error;
92 item->p_next = list;
93 list = item;
95 module_list_free( module_list );
97 VLC_UNUSED( p_instance );
98 return list;
101 /********************************************
102 * Free the list of available audio outputs
103 ***********************************************/
104 void libvlc_audio_output_list_release( libvlc_audio_output_t *list )
106 while( list != NULL )
108 libvlc_audio_output_t *next = list->p_next;
110 free( list->psz_name );
111 free( list->psz_description );
112 free( list );
113 list = next;
118 /***********************
119 * Set the audio output.
120 ***********************/
121 int libvlc_audio_output_set( libvlc_media_player_t *mp, const char *psz_name )
123 char *value;
125 if( !module_exists( psz_name )
126 || asprintf( &value, "%s,none", psz_name ) == -1 )
127 return -1;
128 var_SetString( mp, "aout", value );
129 free( value );
131 /* Forget the existing audio output */
132 input_resource_ResetAout(mp->input.p_resource);
134 /* Create a new audio output */
135 audio_output_t *aout = input_resource_GetAout(mp->input.p_resource);
136 if( aout != NULL )
137 input_resource_PutAout(mp->input.p_resource, aout);
139 return 0;
142 libvlc_audio_output_device_t *
143 libvlc_audio_output_device_list_get( libvlc_instance_t *p_instance,
144 const char *aout )
146 char varname[32];
147 if( (size_t)snprintf( varname, sizeof(varname), "%s-audio-device", aout )
148 >= sizeof(varname) )
149 return NULL;
151 libvlc_audio_output_device_t *list = NULL, **pp = &list;
152 char **values, **texts;
153 ssize_t count = config_GetPszChoices( VLC_OBJECT(p_instance->p_libvlc_int),
154 varname, &values, &texts );
155 for( ssize_t i = 0; i < count; i++ )
157 libvlc_audio_output_device_t *item = malloc( sizeof(*item) );
158 if( unlikely(item == NULL) )
159 break;
161 *pp = item;
162 pp = &item->p_next;
163 item->psz_device = values[i];
164 item->psz_description = texts[i];
167 *pp = NULL;
168 free( texts );
169 free( values );
170 (void) p_instance;
171 return list;
174 void libvlc_audio_output_device_list_release( libvlc_audio_output_device_t *l )
176 while( l != NULL )
178 libvlc_audio_output_device_t *next = l->p_next;
180 free( l->psz_description );
181 free( l->psz_device );
182 free( l );
183 l = next;
187 int libvlc_audio_output_device_count( libvlc_instance_t *p_instance,
188 const char *psz_audio_output )
190 (void) p_instance; (void) psz_audio_output;
191 return 0;
194 char *libvlc_audio_output_device_longname( libvlc_instance_t *p_instance,
195 const char *psz_audio_output,
196 int i_device )
198 (void) p_instance; (void) psz_audio_output; (void) i_device;
199 return NULL;
202 char *libvlc_audio_output_device_id( libvlc_instance_t *p_instance,
203 const char *psz_audio_output,
204 int i_device )
206 (void) p_instance; (void) psz_audio_output; (void) i_device;
207 return NULL;
210 /*****************************
211 * Set device for using
212 *****************************/
213 void libvlc_audio_output_device_set( libvlc_media_player_t *mp,
214 const char *psz_audio_output,
215 const char *psz_device_id )
217 char *psz_config_name;
218 if( !psz_audio_output || !psz_device_id )
219 return;
220 if( asprintf( &psz_config_name, "%s-audio-device", psz_audio_output ) == -1 )
221 return;
222 if( !var_Type( mp, psz_config_name ) )
223 /* Don't recreate the same variable over and over and over... */
224 var_Create( mp, psz_config_name, VLC_VAR_STRING );
225 var_SetString( mp, psz_config_name, psz_device_id );
226 free( psz_config_name );
229 int libvlc_audio_output_get_device_type( libvlc_media_player_t *mp )
231 (void) mp;
232 return libvlc_AudioOutputDevice_Error;
235 void libvlc_audio_output_set_device_type( libvlc_media_player_t *mp,
236 int device_type )
238 (void) mp; (void) device_type;
241 void libvlc_audio_toggle_mute( libvlc_media_player_t *mp )
243 int mute = libvlc_audio_get_mute( mp );
244 if( mute != -1 )
245 libvlc_audio_set_mute( mp, !mute );
248 int libvlc_audio_get_mute( libvlc_media_player_t *mp )
250 int mute = -1;
252 audio_output_t *aout = GetAOut( mp );
253 if( aout != NULL )
255 mute = aout_MuteGet( aout );
256 vlc_object_release( aout );
258 return mute;
261 void libvlc_audio_set_mute( libvlc_media_player_t *mp, int mute )
263 audio_output_t *aout = GetAOut( mp );
264 if( aout != NULL )
266 mute = aout_MuteSet( aout, mute );
267 vlc_object_release( aout );
271 int libvlc_audio_get_volume( libvlc_media_player_t *mp )
273 int volume = -1;
275 audio_output_t *aout = GetAOut( mp );
276 if( aout != NULL )
278 float vol = aout_VolumeGet( aout );
279 vlc_object_release( aout );
280 volume = lroundf( vol * 100.f );
282 return volume;
285 int libvlc_audio_set_volume( libvlc_media_player_t *mp, int volume )
287 float vol = volume / 100.f;
288 if (vol < 0.f)
290 libvlc_printerr( "Volume out of range" );
291 return -1;
294 int ret = -1;
295 audio_output_t *aout = GetAOut( mp );
296 if( aout != NULL )
298 ret = aout_VolumeSet( aout, vol );
299 vlc_object_release( aout );
301 return ret;
304 /*****************************************************************************
305 * libvlc_audio_get_track_count : Get the number of available audio tracks
306 *****************************************************************************/
307 int libvlc_audio_get_track_count( libvlc_media_player_t *p_mi )
309 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
310 int i_track_count;
312 if( !p_input_thread )
313 return -1;
315 i_track_count = var_CountChoices( p_input_thread, "audio-es" );
317 vlc_object_release( p_input_thread );
318 return i_track_count;
321 /*****************************************************************************
322 * libvlc_audio_get_track_description : Get the description of available audio tracks
323 *****************************************************************************/
324 libvlc_track_description_t *
325 libvlc_audio_get_track_description( libvlc_media_player_t *p_mi )
327 return libvlc_get_track_description( p_mi, "audio-es" );
330 /*****************************************************************************
331 * libvlc_audio_get_track : Get the current audio track
332 *****************************************************************************/
333 int libvlc_audio_get_track( libvlc_media_player_t *p_mi )
335 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
336 if( !p_input_thread )
337 return -1;
339 int id = var_GetInteger( p_input_thread, "audio-es" );
340 vlc_object_release( p_input_thread );
341 return id;
344 /*****************************************************************************
345 * libvlc_audio_set_track : Set the current audio track
346 *****************************************************************************/
347 int libvlc_audio_set_track( libvlc_media_player_t *p_mi, int i_track )
349 input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi );
350 vlc_value_t val_list;
351 int i_ret = -1;
353 if( !p_input_thread )
354 return -1;
356 var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
357 for( int i = 0; i < val_list.p_list->i_count; i++ )
359 if( i_track == val_list.p_list->p_values[i].i_int )
361 if( var_SetInteger( p_input_thread, "audio-es", i_track ) < 0 )
362 break;
363 i_ret = 0;
364 goto end;
367 libvlc_printerr( "Track identifier not found" );
368 end:
369 var_FreeList( &val_list, NULL );
370 vlc_object_release( p_input_thread );
371 return i_ret;
374 /*****************************************************************************
375 * libvlc_audio_get_channel : Get the current audio channel
376 *****************************************************************************/
377 int libvlc_audio_get_channel( libvlc_media_player_t *mp )
379 audio_output_t *p_aout = GetAOut( mp );
380 if( !p_aout )
381 return 0;
383 int val = var_GetInteger( p_aout, "stereo-mode" );
384 vlc_object_release( p_aout );
385 return val;
388 /*****************************************************************************
389 * libvlc_audio_set_channel : Set the current audio channel
390 *****************************************************************************/
391 int libvlc_audio_set_channel( libvlc_media_player_t *mp, int channel )
393 audio_output_t *p_aout = GetAOut( mp );
394 int ret = 0;
396 if( !p_aout )
397 return -1;
399 if( var_SetInteger( p_aout, "stereo-mode", channel ) < 0 )
401 libvlc_printerr( "Audio channel out of range" );
402 ret = -1;
404 vlc_object_release( p_aout );
405 return ret;
408 /*****************************************************************************
409 * libvlc_audio_get_delay : Get the current audio delay
410 *****************************************************************************/
411 int64_t libvlc_audio_get_delay( libvlc_media_player_t *p_mi )
413 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
414 int64_t val = 0;
415 if( p_input_thread != NULL )
417 val = var_GetTime( p_input_thread, "audio-delay" );
418 vlc_object_release( p_input_thread );
420 return val;
423 /*****************************************************************************
424 * libvlc_audio_set_delay : Set the current audio delay
425 *****************************************************************************/
426 int libvlc_audio_set_delay( libvlc_media_player_t *p_mi, int64_t i_delay )
428 input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi );
429 int ret = 0;
430 if( p_input_thread != NULL )
432 var_SetTime( p_input_thread, "audio-delay", i_delay );
433 vlc_object_release( p_input_thread );
435 else
437 ret = -1;
439 return ret;
442 /*****************************************************************************
443 * libvlc_audio_equalizer_get_preset_count : Get the number of equalizer presets
444 *****************************************************************************/
445 unsigned libvlc_audio_equalizer_get_preset_count( void )
447 return NB_PRESETS;
450 /*****************************************************************************
451 * libvlc_audio_equalizer_get_preset_name : Get the name for a preset
452 *****************************************************************************/
453 const char *libvlc_audio_equalizer_get_preset_name( unsigned u_index )
455 if ( u_index >= NB_PRESETS )
456 return NULL;
458 return preset_list_text[ u_index ];
461 /*****************************************************************************
462 * libvlc_audio_equalizer_get_band_count : Get the number of equalizer frequency bands
463 *****************************************************************************/
464 unsigned libvlc_audio_equalizer_get_band_count( void )
466 return EQZ_BANDS_MAX;
469 /*****************************************************************************
470 * libvlc_audio_equalizer_get_band_frequency : Get the frequency for a band
471 *****************************************************************************/
472 float libvlc_audio_equalizer_get_band_frequency( unsigned u_index )
474 if ( u_index >= EQZ_BANDS_MAX )
475 return -1.f;
477 return f_iso_frequency_table_10b[ u_index ];
480 /*****************************************************************************
481 * libvlc_audio_equalizer_new : Create a new audio equalizer with zeroed values
482 *****************************************************************************/
483 libvlc_equalizer_t *libvlc_audio_equalizer_new( void )
485 libvlc_equalizer_t *p_equalizer;
486 p_equalizer = malloc( sizeof( *p_equalizer ) );
487 if ( unlikely( p_equalizer == NULL ) )
488 return NULL;
490 p_equalizer->f_preamp = 0.f;
491 for ( unsigned i = 0; i < EQZ_BANDS_MAX; i++ )
492 p_equalizer->f_amp[ i ] = 0.f;
494 return p_equalizer;
497 /*****************************************************************************
498 * libvlc_audio_equalizer_new_from_preset : Create a new audio equalizer based on a preset
499 *****************************************************************************/
500 libvlc_equalizer_t *libvlc_audio_equalizer_new_from_preset( unsigned u_index )
502 libvlc_equalizer_t *p_equalizer;
504 if ( u_index >= NB_PRESETS )
505 return NULL;
507 p_equalizer = malloc( sizeof( *p_equalizer ) );
508 if ( unlikely( p_equalizer == NULL ) )
509 return NULL;
511 p_equalizer->f_preamp = eqz_preset_10b[ u_index ].f_preamp;
513 for ( unsigned i = 0; i < EQZ_BANDS_MAX; i++ )
514 p_equalizer->f_amp[ i ] = eqz_preset_10b[ u_index ].f_amp[ i ];
516 return p_equalizer;
519 /*****************************************************************************
520 * libvlc_audio_equalizer_release : Release a previously created equalizer
521 *****************************************************************************/
522 void libvlc_audio_equalizer_release( libvlc_equalizer_t *p_equalizer )
524 free( p_equalizer );
527 /*****************************************************************************
528 * libvlc_audio_equalizer_set_preamp : Set the preamp value for an equalizer
529 *****************************************************************************/
530 int libvlc_audio_equalizer_set_preamp( libvlc_equalizer_t *p_equalizer, float f_preamp )
532 if ( f_preamp < -20.0f )
533 f_preamp = -20.0f;
534 else if ( f_preamp > 20.0f )
535 f_preamp = 20.0f;
537 p_equalizer->f_preamp = f_preamp;
538 return 0;
541 /*****************************************************************************
542 * libvlc_audio_equalizer_get_preamp : Get the preamp value for an equalizer
543 *****************************************************************************/
544 float libvlc_audio_equalizer_get_preamp( libvlc_equalizer_t *p_equalizer )
546 return p_equalizer->f_preamp;
549 /*****************************************************************************
550 * libvlc_audio_equalizer_set_amp_at_index : Set the amplification value for an equalizer band
551 *****************************************************************************/
552 int libvlc_audio_equalizer_set_amp_at_index( libvlc_equalizer_t *p_equalizer, float f_amp, unsigned u_band )
554 if ( u_band >= EQZ_BANDS_MAX )
555 return -1;
557 if ( f_amp < -20.0f )
558 f_amp = -20.0f;
559 else if ( f_amp > 20.0f )
560 f_amp = 20.0f;
562 p_equalizer->f_amp[ u_band ] = f_amp;
563 return 0;
566 /*****************************************************************************
567 * libvlc_audio_equalizer_get_amp_at_index : Get the amplification value for an equalizer band
568 *****************************************************************************/
569 float libvlc_audio_equalizer_get_amp_at_index( libvlc_equalizer_t *p_equalizer, unsigned u_band )
571 if ( u_band >= EQZ_BANDS_MAX )
572 return 0.f;
574 return p_equalizer->f_amp[ u_band ];