]> git.sesse.net Git - vlc/blob - src/control/audio.c
Remember not to include anything before vlc/vlc.h
[vlc] / src / control / audio.c
1 /*****************************************************************************
2  * libvlc_audio.c: New libvlc audio control API
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Filippo Carone <filippo@carone.org>
8  *          Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <vlc/libvlc.h>
26
27 #include <vlc_input.h>
28 #include <vlc_aout.h>
29
30 #include "libvlc_internal.h"
31
32
33 /*
34  * Remember to release the returned aout_instance_t since it is locked at
35  * the end of this function.
36  */
37 static aout_instance_t *GetAOut( libvlc_instance_t *p_instance,
38                                  libvlc_exception_t *p_exception )
39 {
40     aout_instance_t * p_aout = NULL;
41
42     p_aout = vlc_object_find( p_instance->p_libvlc_int, VLC_OBJECT_AOUT, FIND_CHILD );
43     if( !p_aout )
44     {
45         libvlc_exception_raise( p_exception, "No active audio output" );
46         return NULL;
47     }
48
49     return p_aout;
50 }
51
52
53 /*****************************************************************************
54  * libvlc_audio_get_mute : Get the volume state, true if muted
55  *****************************************************************************/
56 void libvlc_audio_toggle_mute( libvlc_instance_t *p_instance,
57                                libvlc_exception_t *p_e )
58 {
59     aout_VolumeMute( p_instance->p_libvlc_int, NULL );
60 }
61
62 vlc_bool_t libvlc_audio_get_mute( libvlc_instance_t *p_instance,
63                                   libvlc_exception_t *p_e )
64 {
65     /*
66      * If the volume level is 0, then the channel is muted
67      */
68     audio_volume_t i_volume;
69
70     i_volume = libvlc_audio_get_volume(p_instance, p_e);
71     if ( i_volume == 0 )
72         return VLC_TRUE;
73     return VLC_FALSE;
74 }
75
76 void libvlc_audio_set_mute( libvlc_instance_t *p_instance, vlc_bool_t mute,
77                             libvlc_exception_t *p_e )
78 {
79     if ( mute ^ libvlc_audio_get_mute( p_instance, p_e ) )
80     {
81         aout_VolumeMute( p_instance->p_libvlc_int, NULL );
82     }
83 }
84
85 /*****************************************************************************
86  * libvlc_audio_get_volume : Get the current volume (range 0-200 %)
87  *****************************************************************************/
88 int libvlc_audio_get_volume( libvlc_instance_t *p_instance,
89                              libvlc_exception_t *p_e )
90 {
91     audio_volume_t i_volume;
92
93     aout_VolumeGet( p_instance->p_libvlc_int, &i_volume );
94
95     return (i_volume*200+AOUT_VOLUME_MAX/2)/AOUT_VOLUME_MAX;
96 }
97
98
99 /*****************************************************************************
100  * libvlc_audio_set_volume : Set the current volume
101  *****************************************************************************/
102 void libvlc_audio_set_volume( libvlc_instance_t *p_instance, int i_volume,
103                               libvlc_exception_t *p_e )
104 {
105     if( i_volume >= 0 && i_volume <= 200 )
106     {
107         i_volume = (i_volume * AOUT_VOLUME_MAX + 100) / 200;
108
109         aout_VolumeSet( p_instance->p_libvlc_int, i_volume );
110     }
111     else
112     {
113         libvlc_exception_raise( p_e, "Volume out of range" );
114     }
115 }
116
117 /*****************************************************************************
118  * libvlc_audio_get_track : Get the current audio track
119  *****************************************************************************/
120 int libvlc_audio_get_track( libvlc_media_instance_t *p_mi,
121                             libvlc_exception_t *p_e )
122 {
123     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
124     vlc_value_t val_list;
125     vlc_value_t val;
126     int i_track = -1;
127     int i_ret = -1;
128     int i;
129
130     if( !p_input_thread )
131         return -1;
132
133     i_ret = var_Get( p_input_thread, "audio-es", &val );
134     if( i_ret < 0 )
135     {
136         libvlc_exception_raise( p_e, "Getting Audio track information failed" );
137         vlc_object_release( p_input_thread );
138         return i_ret;
139     }
140
141     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
142     for( i = 0; i < val_list.p_list->i_count; i++ )
143     {
144         vlc_value_t track_val = val_list.p_list->p_values[i];
145         if( track_val.i_int == val.i_int )
146         {
147             i_track = i;
148             break;
149        }
150     }
151     vlc_object_release( p_input_thread );
152     return i_track;
153 }
154
155 /*****************************************************************************
156  * libvlc_audio_set_track : Set the current audio track
157  *****************************************************************************/
158 void libvlc_audio_set_track( libvlc_media_instance_t *p_mi, int i_track,
159                              libvlc_exception_t *p_e )
160 {
161     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
162     vlc_value_t val_list;
163     int i_ret = -1;
164     int i;
165
166     if( !p_input_thread )
167         return;
168
169     var_Change( p_input_thread, "audio-es", VLC_VAR_GETCHOICES, &val_list, NULL );
170     for( i = 0; i < val_list.p_list->i_count; i++ )
171     {
172         vlc_value_t val = val_list.p_list->p_values[i];
173         if( i_track == i )
174         {
175             i_ret = var_Set( p_input_thread, "audio-es", val );
176             if( i_ret < 0 )
177             {
178                 libvlc_exception_raise( p_e, "Setting audio track failed" );
179             }
180             vlc_object_release( p_input_thread );
181             return;
182         }
183     }
184     libvlc_exception_raise( p_e, "Audio track out of range" );
185     vlc_object_release( p_input_thread );
186 }
187
188 /*****************************************************************************
189  * libvlc_audio_get_channel : Get the current audio channel
190  *****************************************************************************/
191 int libvlc_audio_get_channel( libvlc_instance_t *p_instance,
192                                 libvlc_exception_t *p_e )
193 {
194     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
195     if( p_aout )
196     {
197         vlc_value_t val;
198
199         var_Get( p_aout, "audio-channels", &val );
200         vlc_object_release( p_aout );
201         return val.i_int;
202     }
203     return -1;
204 }
205
206 /*****************************************************************************
207  * libvlc_audio_set_channel : Set the current audio channel
208  *****************************************************************************/
209 void libvlc_audio_set_channel( libvlc_instance_t *p_instance, int i_channel,
210                                libvlc_exception_t *p_e )
211 {
212     aout_instance_t *p_aout = GetAOut( p_instance, p_e );
213     if( p_aout )
214     {
215         vlc_value_t val;
216         int i_ret = -1;
217
218         val.i_int = i_channel;
219         switch( i_channel )
220         {
221             case AOUT_VAR_CHAN_RSTEREO:
222             case AOUT_VAR_CHAN_STEREO:
223             case AOUT_VAR_CHAN_LEFT:
224             case AOUT_VAR_CHAN_RIGHT:
225             case AOUT_VAR_CHAN_DOLBYS:
226                 i_ret = var_Set( p_aout, "audio-channels", val );
227                 if( i_ret < 0 )
228                 {
229                     libvlc_exception_raise( p_e, "Failed setting audio channel" );
230                     vlc_object_release( p_aout );
231                     return;
232                 }
233                 vlc_object_release( p_aout );
234                 return; /* Found */
235             default:
236                 libvlc_exception_raise( p_e, "Audio channel out of range" );
237                 break;
238         }
239         vlc_object_release( p_aout );
240     }
241 }