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 *****************************************************************************/
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>
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
)
52 audio_output_t
*p_aout
= vlc_player_aout_Hold(mp
->player
);
54 libvlc_printerr( "No active audio output" );
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
)
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" ) )
75 libvlc_audio_output_t
*item
= malloc( sizeof( *item
) );
76 if( unlikely(item
== NULL
) )
79 libvlc_printerr( "Not enough memory" );
80 libvlc_audio_output_list_release( list
);
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
);
97 module_list_free( module_list
);
99 VLC_UNUSED( p_instance
);
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
);
120 /***********************
121 * Set the audio output.
122 ***********************/
123 int libvlc_audio_output_set( libvlc_media_player_t
*mp
, const char *psz_name
)
127 if( !module_exists( psz_name
)
128 || asprintf( &value
, "%s,none", psz_name
) == -1 )
130 var_SetString( mp
, "aout", value
);
136 libvlc_audio_output_device_t
*
137 libvlc_audio_output_device_enum( libvlc_media_player_t
*mp
)
139 audio_output_t
*aout
= GetAOut( mp
);
143 libvlc_audio_output_device_t
*list
, **pp
= &list
;
144 char **values
, **texts
;
146 int n
= aout_DevicesList( aout
, &values
, &texts
);
151 for (int i
= 0; i
< n
; i
++)
153 libvlc_audio_output_device_t
*item
= malloc( sizeof(*item
) );
154 if( unlikely(item
== NULL
) )
163 item
->psz_device
= values
[i
];
164 item
->psz_description
= texts
[i
];
174 libvlc_audio_output_device_t
*
175 libvlc_audio_output_device_list_get( libvlc_instance_t
*p_instance
,
179 if( (size_t)snprintf( varname
, sizeof(varname
), "%s-audio-device", aout
)
183 if( config_GetType(varname
) != VLC_VAR_STRING
)
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
) )
197 item
->psz_device
= values
[i
];
198 item
->psz_description
= texts
[i
];
208 void libvlc_audio_output_device_list_release( libvlc_audio_output_device_t
*l
)
212 libvlc_audio_output_device_t
*next
= l
->p_next
;
214 free( l
->psz_description
);
215 free( l
->psz_device
);
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
)
234 if( asprintf( &cfg_name
, "%s-audio-device", module
) == -1 )
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
);
245 audio_output_t
*aout
= GetAOut( mp
);
249 aout_DeviceSet( aout
, devid
);
253 char *libvlc_audio_output_device_get( libvlc_media_player_t
*mp
)
255 audio_output_t
*aout
= GetAOut( mp
);
259 char *devid
= aout_DeviceGet( aout
);
266 void libvlc_audio_toggle_mute( libvlc_media_player_t
*mp
)
268 int mute
= libvlc_audio_get_mute( mp
);
270 libvlc_audio_set_mute( mp
, !mute
);
273 int libvlc_audio_get_mute( libvlc_media_player_t
*mp
)
277 audio_output_t
*aout
= GetAOut( mp
);
280 mute
= aout_MuteGet( aout
);
286 void libvlc_audio_set_mute( libvlc_media_player_t
*mp
, int mute
)
288 audio_output_t
*aout
= GetAOut( mp
);
291 mute
= aout_MuteSet( aout
, mute
);
296 int libvlc_audio_get_volume( libvlc_media_player_t
*mp
)
300 audio_output_t
*aout
= GetAOut( mp
);
303 float vol
= aout_VolumeGet( aout
);
305 volume
= lroundf( vol
* 100.f
);
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" );
320 audio_output_t
*aout
= GetAOut( mp
);
323 ret
= aout_VolumeSet( aout
, vol
);
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
);
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
);
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
)
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
))
386 vlc_player_SelectTrack(player
, track
, VLC_PLAYER_SELECT_EXCLUSIVE
);
391 libvlc_printerr( "Track identifier not found" );
393 vlc_player_Unlock(player
);
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
);
406 int val
= var_GetInteger( p_aout
, "stereo-mode" );
407 aout_Release(p_aout
);
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
);
422 if( var_SetInteger( p_aout
, "stereo-mode", channel
) < 0 )
424 libvlc_printerr( "Audio channel out of range" );
427 aout_Release(p_aout
);
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 */
462 /*****************************************************************************
463 * libvlc_audio_equalizer_get_preset_count : Get the number of equalizer presets
464 *****************************************************************************/
465 unsigned libvlc_audio_equalizer_get_preset_count( void )
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
)
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
)
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
) )
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
;
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
)
527 p_equalizer
= malloc( sizeof( *p_equalizer
) );
528 if ( unlikely( p_equalizer
== 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
];
539 /*****************************************************************************
540 * libvlc_audio_equalizer_release : Release a previously created equalizer
541 *****************************************************************************/
542 void libvlc_audio_equalizer_release( libvlc_equalizer_t
*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
) )
554 if( f_preamp
< -20.f
)
556 else if( f_preamp
> 20.f
)
559 p_equalizer
->f_preamp
= f_preamp
;
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
) )
582 else if( f_amp
> 20.f
)
585 p_equalizer
->f_amp
[ u_band
] = f_amp
;
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
)
597 return p_equalizer
->f_amp
[ u_band
];