5 This file is part of PulseAudio.
7 Copyright 2004-2006 Lennart Poettering
8 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
10 PulseAudio is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published
12 by the Free Software Foundation; either version 2.1 of the License,
13 or (at your option) any later version.
15 PulseAudio is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with PulseAudio; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/cdecl.h>
30 #include <pulse/gccmacro.h>
31 #include <pulse/sample.h>
32 #include <pulse/channelmap.h>
33 #include <pulse/version.h>
35 /** \page volume Volume Control
37 * \section overv_sec Overview
39 * Sinks, sources, sink inputs and samples can all have their own volumes.
40 * To deal with these, The PulseAudio library contains a number of functions
43 * The basic volume type in PulseAudio is the \ref pa_volume_t type. Most of
44 * the time, applications will use the aggregated pa_cvolume structure that
45 * can store the volume of all channels at once.
47 * Volumes commonly span between muted (0%), and normal (100%). It is possible
48 * to set volumes to higher than 100%, but clipping might occur.
50 * There is no single well-defined meaning attached to the 100% volume for a
51 * sink input. In fact, it depends on the server configuration. With flat
52 * volumes enabled (the default in most Linux distributions), it means the
53 * maximum volume that the sound hardware is capable of, which is usually so
54 * high that you absolutely must not set sink input volume to 100% unless the
55 * the user explicitly requests that (note that usually you shouldn't set the
56 * volume anyway if the user doesn't explicitly request it, instead, let
57 * PulseAudio decide the volume for the sink input). With flat volumes disabled
58 * (the default in Ubuntu), the sink input volume is relative to the sink
59 * volume, so 100% sink input volume means that the sink input is played at the
60 * current sink volume level. In this case 100% is often a good default volume
61 * for a sink input, although you still should let PulseAudio decide the
62 * default volume. It is possible to figure out whether flat volume mode is in
63 * effect for a given sink by calling pa_context_get_sink_info_by_name().
65 * \section calc_sec Calculations
67 * The volumes in PulseAudio are logarithmic in nature and applications
68 * shouldn't perform calculations with them directly. Instead, they should
69 * be converted to and from either dB or a linear scale:
71 * \li dB - pa_sw_volume_from_dB() / pa_sw_volume_to_dB()
72 * \li Linear - pa_sw_volume_from_linear() / pa_sw_volume_to_linear()
74 * For simple multiplication, pa_sw_volume_multiply() and
75 * pa_sw_cvolume_multiply() can be used.
77 * Calculations can only be reliably performed on software volumes
78 * as it is commonly unknown what scale hardware volumes relate to.
80 * The functions described above are only valid when used with
81 * software volumes. Hence it is usually a better idea to treat all
82 * volume values as opaque with a range from PA_VOLUME_MUTED (0%) to
83 * PA_VOLUME_NORM (100%) and to refrain from any calculations with
86 * \section conv_sec Convenience Functions
88 * To handle the pa_cvolume structure, the PulseAudio library provides a
89 * number of convenience functions:
91 * \li pa_cvolume_valid() - Tests if a pa_cvolume structure is valid.
92 * \li pa_cvolume_equal() - Tests if two pa_cvolume structures are identical.
93 * \li pa_cvolume_channels_equal_to() - Tests if all channels of a pa_cvolume
94 * structure have a given volume.
95 * \li pa_cvolume_is_muted() - Tests if all channels of a pa_cvolume
96 * structure are muted.
97 * \li pa_cvolume_is_norm() - Tests if all channels of a pa_cvolume structure
98 * are at a normal volume.
99 * \li pa_cvolume_set() - Set the first n channels of a pa_cvolume structure to
101 * \li pa_cvolume_reset() - Set the first n channels of a pa_cvolume structure
102 * to a normal volume.
103 * \li pa_cvolume_mute() - Set the first n channels of a pa_cvolume structure
105 * \li pa_cvolume_avg() - Return the average volume of all channels.
106 * \li pa_cvolume_snprint() - Pretty print a pa_cvolume structure.
110 * Constants and routines for volume handling
112 * See also \subpage volume
117 /** Volume specification:
118 * PA_VOLUME_MUTED: silence;
119 * < PA_VOLUME_NORM: decreased volume;
120 * PA_VOLUME_NORM: normal volume;
121 * > PA_VOLUME_NORM: increased volume */
122 typedef uint32_t pa_volume_t
;
124 /** Normal volume (100%, 0 dB) */
125 #define PA_VOLUME_NORM ((pa_volume_t) 0x10000U)
127 /** Muted (minimal valid) volume (0%, -inf dB) */
128 #define PA_VOLUME_MUTED ((pa_volume_t) 0U)
130 /** Maximum valid volume we can store. \since 0.9.15 */
131 #define PA_VOLUME_MAX ((pa_volume_t) UINT32_MAX/2)
133 /** Recommended maximum volume to show in user facing UIs.
134 * Note: UIs should deal gracefully with volumes greater than this value
135 * and not cause feedback loops etc. - i.e. if the volume is more than
136 * this, the UI should not limit it and push the limited value back to
137 * the server. \since 0.9.23 */
138 #define PA_VOLUME_UI_MAX (pa_sw_volume_from_dB(+11.0))
140 /** Special 'invalid' volume. \since 0.9.16 */
141 #define PA_VOLUME_INVALID ((pa_volume_t) UINT32_MAX)
143 /** Check if volume is valid. \since 1.0 */
144 #define PA_VOLUME_IS_VALID(v) ((v) <= PA_VOLUME_MAX)
146 /** Clamp volume to the permitted range. \since 1.0 */
147 #define PA_CLAMP_VOLUME(v) (PA_CLAMP_UNLIKELY((v), PA_VOLUME_MUTED, PA_VOLUME_MAX))
149 /** A structure encapsulating a per-channel volume */
150 typedef struct pa_cvolume
{
151 uint8_t channels
; /**< Number of channels */
152 pa_volume_t values
[PA_CHANNELS_MAX
]; /**< Per-channel volume */
155 /** Return non-zero when *a == *b */
156 int pa_cvolume_equal(const pa_cvolume
*a
, const pa_cvolume
*b
) PA_GCC_PURE
;
158 /** Initialize the specified volume and return a pointer to
159 * it. The sample spec will have a defined state but
160 * pa_cvolume_valid() will fail for it. \since 0.9.13 */
161 pa_cvolume
* pa_cvolume_init(pa_cvolume
*a
);
163 /** Set the volume of the first n channels to PA_VOLUME_NORM */
164 #define pa_cvolume_reset(a, n) pa_cvolume_set((a), (n), PA_VOLUME_NORM)
166 /** Set the volume of the first n channels to PA_VOLUME_MUTED */
167 #define pa_cvolume_mute(a, n) pa_cvolume_set((a), (n), PA_VOLUME_MUTED)
169 /** Set the volume of the specified number of channels to the volume v */
170 pa_cvolume
* pa_cvolume_set(pa_cvolume
*a
, unsigned channels
, pa_volume_t v
);
172 /** Maximum length of the strings returned by
173 * pa_cvolume_snprint(). Please note that this value can change with
174 * any release without warning and without being considered API or ABI
175 * breakage. You should not use this definition anywhere where it
176 * might become part of an ABI.*/
177 #define PA_CVOLUME_SNPRINT_MAX 320
179 /** Pretty print a volume structure */
180 char *pa_cvolume_snprint(char *s
, size_t l
, const pa_cvolume
*c
);
182 /** Maximum length of the strings returned by
183 * pa_sw_cvolume_snprint_dB(). Please note that this value can change with
184 * any release without warning and without being considered API or ABI
185 * breakage. You should not use this definition anywhere where it
186 * might become part of an ABI. \since 0.9.13 */
187 #define PA_SW_CVOLUME_SNPRINT_DB_MAX 448
189 /** Pretty print a volume structure but show dB values. \since 0.9.13 */
190 char *pa_sw_cvolume_snprint_dB(char *s
, size_t l
, const pa_cvolume
*c
);
192 /** Maximum length of the strings returned by pa_cvolume_snprint_verbose().
193 * Please note that this value can change with any release without warning and
194 * without being considered API or ABI breakage. You should not use this
195 * definition anywhere where it might become part of an ABI. \since 5.0 */
196 #define PA_CVOLUME_SNPRINT_VERBOSE_MAX 1984
198 /** Pretty print a volume structure in a verbose way. The volume for each
199 * channel is printed in several formats: the raw pa_volume_t value,
200 * percentage, and if print_dB is non-zero, also the dB value. If map is not
201 * NULL, the channel names will be printed. \since 5.0 */
202 char *pa_cvolume_snprint_verbose(char *s
, size_t l
, const pa_cvolume
*c
, const pa_channel_map
*map
, int print_dB
);
204 /** Maximum length of the strings returned by
205 * pa_volume_snprint(). Please note that this value can change with
206 * any release without warning and without being considered API or ABI
207 * breakage. You should not use this definition anywhere where it
208 * might become part of an ABI. \since 0.9.15 */
209 #define PA_VOLUME_SNPRINT_MAX 10
211 /** Pretty print a volume \since 0.9.15 */
212 char *pa_volume_snprint(char *s
, size_t l
, pa_volume_t v
);
214 /** Maximum length of the strings returned by
215 * pa_sw_volume_snprint_dB(). Please note that this value can change with
216 * any release without warning and without being considered API or ABI
217 * breakage. You should not use this definition anywhere where it
218 * might become part of an ABI. \since 0.9.15 */
219 #define PA_SW_VOLUME_SNPRINT_DB_MAX 11
221 /** Pretty print a volume but show dB values. \since 0.9.15 */
222 char *pa_sw_volume_snprint_dB(char *s
, size_t l
, pa_volume_t v
);
224 /** Maximum length of the strings returned by pa_volume_snprint_verbose().
225 * Please note that this value can change with any release without warning and
226 * withou being considered API or ABI breakage. You should not use this
227 * definition anywhere where it might become part of an ABI. \since 5.0 */
228 #define PA_VOLUME_SNPRINT_VERBOSE_MAX 35
230 /** Pretty print a volume in a verbose way. The volume is printed in several
231 * formats: the raw pa_volume_t value, percentage, and if print_dB is non-zero,
232 * also the dB value. \since 5.0 */
233 char *pa_volume_snprint_verbose(char *s
, size_t l
, pa_volume_t v
, int print_dB
);
235 /** Return the average volume of all channels */
236 pa_volume_t
pa_cvolume_avg(const pa_cvolume
*a
) PA_GCC_PURE
;
238 /** Return the average volume of all channels that are included in the
239 * specified channel map with the specified channel position mask. If
240 * cm is NULL this call is identical to pa_cvolume_avg(). If no
241 * channel is selected the returned value will be
242 * PA_VOLUME_MUTED. \since 0.9.16 */
243 pa_volume_t
pa_cvolume_avg_mask(const pa_cvolume
*a
, const pa_channel_map
*cm
, pa_channel_position_mask_t mask
) PA_GCC_PURE
;
245 /** Return the maximum volume of all channels. \since 0.9.12 */
246 pa_volume_t
pa_cvolume_max(const pa_cvolume
*a
) PA_GCC_PURE
;
248 /** Return the maximum volume of all channels that are included in the
249 * specified channel map with the specified channel position mask. If
250 * cm is NULL this call is identical to pa_cvolume_max(). If no
251 * channel is selected the returned value will be PA_VOLUME_MUTED.
253 pa_volume_t
pa_cvolume_max_mask(const pa_cvolume
*a
, const pa_channel_map
*cm
, pa_channel_position_mask_t mask
) PA_GCC_PURE
;
255 /** Return the minimum volume of all channels. \since 0.9.16 */
256 pa_volume_t
pa_cvolume_min(const pa_cvolume
*a
) PA_GCC_PURE
;
258 /** Return the minimum volume of all channels that are included in the
259 * specified channel map with the specified channel position mask. If
260 * cm is NULL this call is identical to pa_cvolume_min(). If no
261 * channel is selected the returned value will be PA_VOLUME_MUTED.
263 pa_volume_t
pa_cvolume_min_mask(const pa_cvolume
*a
, const pa_channel_map
*cm
, pa_channel_position_mask_t mask
) PA_GCC_PURE
;
265 /** Return non-zero when the passed cvolume structure is valid */
266 int pa_cvolume_valid(const pa_cvolume
*v
) PA_GCC_PURE
;
268 /** Return non-zero if the volume of all channels is equal to the specified value */
269 int pa_cvolume_channels_equal_to(const pa_cvolume
*a
, pa_volume_t v
) PA_GCC_PURE
;
271 /** Return 1 if the specified volume has all channels muted */
272 #define pa_cvolume_is_muted(a) pa_cvolume_channels_equal_to((a), PA_VOLUME_MUTED)
274 /** Return 1 if the specified volume has all channels on normal level */
275 #define pa_cvolume_is_norm(a) pa_cvolume_channels_equal_to((a), PA_VOLUME_NORM)
277 /** Multiply two volume specifications, return the result. This uses
278 * PA_VOLUME_NORM as neutral element of multiplication. This is only
279 * valid for software volumes! */
280 pa_volume_t
pa_sw_volume_multiply(pa_volume_t a
, pa_volume_t b
) PA_GCC_CONST
;
282 /** Multiply two per-channel volumes and return the result in
283 * *dest. This is only valid for software volumes! a, b and dest may
284 * point to the same structure. */
285 pa_cvolume
*pa_sw_cvolume_multiply(pa_cvolume
*dest
, const pa_cvolume
*a
, const pa_cvolume
*b
);
287 /** Multiply a per-channel volume with a scalar volume and return the
288 * result in *dest. This is only valid for software volumes! a
289 * and dest may point to the same structure. \since
291 pa_cvolume
*pa_sw_cvolume_multiply_scalar(pa_cvolume
*dest
, const pa_cvolume
*a
, pa_volume_t b
);
293 /** Divide two volume specifications, return the result. This uses
294 * PA_VOLUME_NORM as neutral element of division. This is only valid
295 * for software volumes! If a division by zero is tried the result
296 * will be 0. \since 0.9.13 */
297 pa_volume_t
pa_sw_volume_divide(pa_volume_t a
, pa_volume_t b
) PA_GCC_CONST
;
299 /** Divide two per-channel volumes and return the result in
300 * *dest. This is only valid for software volumes! a, b
301 * and dest may point to the same structure. \since 0.9.13 */
302 pa_cvolume
*pa_sw_cvolume_divide(pa_cvolume
*dest
, const pa_cvolume
*a
, const pa_cvolume
*b
);
304 /** Divide a per-channel volume by a scalar volume and return the
305 * result in *dest. This is only valid for software volumes! a
306 * and dest may point to the same structure. \since
308 pa_cvolume
*pa_sw_cvolume_divide_scalar(pa_cvolume
*dest
, const pa_cvolume
*a
, pa_volume_t b
);
310 /** Convert a decibel value to a volume (amplitude, not power). This is only valid for software volumes! */
311 pa_volume_t
pa_sw_volume_from_dB(double f
) PA_GCC_CONST
;
313 /** Convert a volume to a decibel value (amplitude, not power). This is only valid for software volumes! */
314 double pa_sw_volume_to_dB(pa_volume_t v
) PA_GCC_CONST
;
316 /** Convert a linear factor to a volume. 0.0 and less is muted while
317 * 1.0 is PA_VOLUME_NORM. This is only valid for software volumes! */
318 pa_volume_t
pa_sw_volume_from_linear(double v
) PA_GCC_CONST
;
320 /** Convert a volume to a linear factor. This is only valid for software volumes! */
321 double pa_sw_volume_to_linear(pa_volume_t v
) PA_GCC_CONST
;
324 #define PA_DECIBEL_MININFTY ((double) -INFINITY)
326 /** This floor value is used as minus infinity when using pa_sw_volume_to_dB() / pa_sw_volume_from_dB(). */
327 #define PA_DECIBEL_MININFTY ((double) -200.0)
330 /** Remap a volume from one channel mapping to a different channel mapping. \since 0.9.12 */
331 pa_cvolume
*pa_cvolume_remap(pa_cvolume
*v
, const pa_channel_map
*from
, const pa_channel_map
*to
);
333 /** Return non-zero if the specified volume is compatible with the
334 * specified sample spec. \since 0.9.13 */
335 int pa_cvolume_compatible(const pa_cvolume
*v
, const pa_sample_spec
*ss
) PA_GCC_PURE
;
337 /** Return non-zero if the specified volume is compatible with the
338 * specified sample spec. \since 0.9.15 */
339 int pa_cvolume_compatible_with_channel_map(const pa_cvolume
*v
, const pa_channel_map
*cm
) PA_GCC_PURE
;
341 /** Calculate a 'balance' value for the specified volume with the
342 * specified channel map. The return value will range from -1.0f
343 * (left) to +1.0f (right). If no balance value is applicable to this
344 * channel map the return value will always be 0.0f. See
345 * pa_channel_map_can_balance(). \since 0.9.15 */
346 float pa_cvolume_get_balance(const pa_cvolume
*v
, const pa_channel_map
*map
) PA_GCC_PURE
;
348 /** Adjust the 'balance' value for the specified volume with the
349 * specified channel map. v will be modified in place and
350 * returned. The balance is a value between -1.0f and +1.0f. This
351 * operation might not be reversible! Also, after this call
352 * pa_cvolume_get_balance() is not guaranteed to actually return the
353 * requested balance value (e.g. when the input volume was zero anyway for
354 * all channels). If no balance value is applicable to
355 * this channel map the volume will not be modified. See
356 * pa_channel_map_can_balance(). \since 0.9.15 */
357 pa_cvolume
* pa_cvolume_set_balance(pa_cvolume
*v
, const pa_channel_map
*map
, float new_balance
);
359 /** Calculate a 'fade' value (i.e.\ 'balance' between front and rear)
360 * for the specified volume with the specified channel map. The return
361 * value will range from -1.0f (rear) to +1.0f (left). If no fade
362 * value is applicable to this channel map the return value will
363 * always be 0.0f. See pa_channel_map_can_fade(). \since 0.9.15 */
364 float pa_cvolume_get_fade(const pa_cvolume
*v
, const pa_channel_map
*map
) PA_GCC_PURE
;
366 /** Adjust the 'fade' value (i.e.\ 'balance' between front and rear)
367 * for the specified volume with the specified channel map. v will be
368 * modified in place and returned. The balance is a value between
369 * -1.0f and +1.0f. This operation might not be reversible! Also,
370 * after this call pa_cvolume_get_fade() is not guaranteed to actually
371 * return the requested fade value (e.g. when the input volume was
372 * zero anyway for all channels). If no fade value is applicable to
373 * this channel map the volume will not be modified. See
374 * pa_channel_map_can_fade(). \since 0.9.15 */
375 pa_cvolume
* pa_cvolume_set_fade(pa_cvolume
*v
, const pa_channel_map
*map
, float new_fade
);
377 /** Scale the passed pa_cvolume structure so that the maximum volume
378 * of all channels equals max. The proportions between the channel
379 * volumes are kept. \since 0.9.15 */
380 pa_cvolume
* pa_cvolume_scale(pa_cvolume
*v
, pa_volume_t max
);
382 /** Scale the passed pa_cvolume structure so that the maximum volume
383 * of all channels selected via cm/mask equals max. This also modifies
384 * the volume of those channels that are unmasked. The proportions
385 * between the channel volumes are kept. \since 0.9.16 */
386 pa_cvolume
* pa_cvolume_scale_mask(pa_cvolume
*v
, pa_volume_t max
, pa_channel_map
*cm
, pa_channel_position_mask_t mask
);
388 /** Set the passed volume to all channels at the specified channel
389 * position. Will return the updated volume struct, or NULL if there
390 * is no channel at the position specified. You can check if a channel
391 * map includes a specific position by calling
392 * pa_channel_map_has_position(). \since 0.9.16 */
393 pa_cvolume
* pa_cvolume_set_position(pa_cvolume
*cv
, const pa_channel_map
*map
, pa_channel_position_t t
, pa_volume_t v
);
395 /** Get the maximum volume of all channels at the specified channel
396 * position. Will return 0 if there is no channel at the position
397 * specified. You can check if a channel map includes a specific
398 * position by calling pa_channel_map_has_position(). \since 0.9.16 */
399 pa_volume_t
pa_cvolume_get_position(pa_cvolume
*cv
, const pa_channel_map
*map
, pa_channel_position_t t
) PA_GCC_PURE
;
401 /** This goes through all channels in a and b and sets the
402 * corresponding channel in dest to the greater volume of both. a, b
403 * and dest may point to the same structure. \since 0.9.16 */
404 pa_cvolume
* pa_cvolume_merge(pa_cvolume
*dest
, const pa_cvolume
*a
, const pa_cvolume
*b
);
406 /** Increase the volume passed in by 'inc', but not exceeding 'limit'.
407 * The proportions between the channels are kept. \since 0.9.19 */
408 pa_cvolume
* pa_cvolume_inc_clamp(pa_cvolume
*v
, pa_volume_t inc
, pa_volume_t limit
);
410 /** Increase the volume passed in by 'inc'. The proportions between
411 * the channels are kept. \since 0.9.16 */
412 pa_cvolume
* pa_cvolume_inc(pa_cvolume
*v
, pa_volume_t inc
);
414 /** Decrease the volume passed in by 'dec'. The proportions between
415 * the channels are kept. \since 0.9.16 */
416 pa_cvolume
* pa_cvolume_dec(pa_cvolume
*v
, pa_volume_t dec
);