1 /*****************************************************************************
2 * intf.c : audio output API towards the interface modules
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: intf.c,v 1.1 2002/09/16 20:46:38 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
32 #include "audio_output.h"
33 #include "aout_internal.h"
38 * The hardware volume cannot be set if the output module gets deleted, so
39 * we must take the mixer lock. The software volume cannot be set while the
40 * mixer is running, so we need the mixer lock (too).
42 * Here is a schematic of the i_volume range :
44 * |------------------+------------------------------------+--------------|
45 * 0 pi_low_soft pi_high_soft 1024
47 * Between pi_low_soft and pi_high_soft, the volume is done in hardware
48 * by the output module. Outside, the output module will change
49 * p_aout->mixer.i_multiplier (done in software). This scaling may result
50 * in cropping errors and should be avoided as much as possible.
52 * It is legal to have *pi_low_soft == *pi_high_soft, and do everything in
53 * software. In that case, it is recommended to use *pi_low_soft == 256,
54 * along with the utility functions provided in this file.
56 * It is also legal to have *pi_low_soft == 0 and *pi_high_soft == 1024, and
57 * completely avoid software scaling. However, some streams (esp. A/52)
58 * are encoded with a very low volume and users may complain.
61 /*****************************************************************************
62 * aout_VolumeGet : get the volume of the output device
63 *****************************************************************************/
64 int aout_VolumeGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
68 vlc_mutex_lock( &p_aout->mixer_lock );
70 if ( p_aout->i_nb_inputs == 0 )
72 /* The output module is destroyed. */
73 vlc_mutex_unlock( &p_aout->mixer_lock );
74 msg_Err( p_aout, "VolumeGet called without output module" );
78 i_result = p_aout->output.pf_volume_get( p_aout, pi_volume );
80 vlc_mutex_lock( &p_aout->mixer_lock );
84 /*****************************************************************************
85 * aout_VolumeSet : set the volume of the output device
86 *****************************************************************************/
87 int aout_VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
91 vlc_mutex_lock( &p_aout->mixer_lock );
93 if ( p_aout->i_nb_inputs == 0 )
95 /* The output module is destroyed. */
96 vlc_mutex_unlock( &p_aout->mixer_lock );
97 msg_Err( p_aout, "VolumeSet called without output module" );
101 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
103 vlc_mutex_lock( &p_aout->mixer_lock );
107 /*****************************************************************************
108 * aout_VolumeInfos : get the boundaries pi_low_soft and pi_high_soft
109 *****************************************************************************/
110 int aout_VolumeInfos( aout_instance_t * p_aout, audio_volume_t * pi_low_soft,
111 audio_volume_t * pi_high_soft )
115 vlc_mutex_lock( &p_aout->mixer_lock );
117 if ( p_aout->i_nb_inputs == 0 )
119 /* The output module is destroyed. */
120 vlc_mutex_unlock( &p_aout->mixer_lock );
121 msg_Err( p_aout, "VolumeInfos called without output module" );
125 i_result = p_aout->output.pf_volume_infos( p_aout, pi_low_soft,
128 vlc_mutex_lock( &p_aout->mixer_lock );
132 /*****************************************************************************
133 * aout_VolumeUp : raise the output volume
134 *****************************************************************************
135 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
137 *****************************************************************************/
138 int aout_VolumeUp( aout_instance_t * p_aout, int i_nb_steps,
139 audio_volume_t * pi_volume )
142 audio_volume_t i_volume;
144 vlc_mutex_lock( &p_aout->mixer_lock );
146 if ( p_aout->i_nb_inputs == 0 )
148 /* The output module is destroyed. */
149 vlc_mutex_unlock( &p_aout->mixer_lock );
150 msg_Err( p_aout, "VolumeUp called without output module" );
154 if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
156 vlc_mutex_unlock( &p_aout->mixer_lock );
160 i_volume += AOUT_VOLUME_STEP * i_nb_steps;
161 if ( i_volume > 1024 ) i_volume = 1024;
163 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
165 vlc_mutex_lock( &p_aout->mixer_lock );
167 if ( pi_volume != NULL ) *pi_volume = i_volume;
171 /*****************************************************************************
172 * aout_VolumeDown : lower the output volume
173 *****************************************************************************
174 * If pi_volume != NULL, *pi_volume will contain the volume at the end of the
176 *****************************************************************************/
177 int aout_VolumeDown( aout_instance_t * p_aout, int i_nb_steps,
178 audio_volume_t * pi_volume )
181 audio_volume_t i_volume;
183 vlc_mutex_lock( &p_aout->mixer_lock );
185 if ( p_aout->i_nb_inputs == 0 )
187 /* The output module is destroyed. */
188 vlc_mutex_unlock( &p_aout->mixer_lock );
189 msg_Err( p_aout, "VolumeUp called without output module" );
193 if ( p_aout->output.pf_volume_get( p_aout, &i_volume ) )
195 vlc_mutex_unlock( &p_aout->mixer_lock );
199 if ( i_volume < AOUT_VOLUME_STEP * i_nb_steps )
202 i_volume -= AOUT_VOLUME_STEP * i_nb_steps;
204 i_result = p_aout->output.pf_volume_set( p_aout, i_volume );
206 vlc_mutex_lock( &p_aout->mixer_lock );
208 if ( pi_volume != NULL ) *pi_volume = i_volume;
213 * The next functions are not supposed to be called by the interface, but
214 * are placeholders for software-only scaling.
217 /* Meant to be called by the output plug-in's Open(). */
218 void aout_VolumeSoftInit( aout_instance_t * p_aout )
220 p_aout->output.i_volume = AOUT_VOLUME_DEFAULT;
223 /* Placeholder for pf_volume_infos(). */
224 int aout_VolumeSoftInfos( aout_instance_t * p_aout,
225 audio_volume_t * pi_low_soft,
226 audio_volume_t * pi_high_soft )
228 *pi_low_soft = *pi_high_soft = AOUT_VOLUME_DEFAULT;
232 /* Placeholder for pf_volume_get(). */
233 int aout_VolumeSoftGet( aout_instance_t * p_aout,
234 audio_volume_t * pi_volume )
236 *pi_volume = p_aout->output.i_volume;
241 /* Placeholder for pf_volume_set(). */
242 int aout_VolumeSoftSet( aout_instance_t * p_aout,
243 audio_volume_t i_volume )
245 aout_MixerMultiplierSet( p_aout, (float)(i_volume / AOUT_VOLUME_DEFAULT) );