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 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
28 #include <pulse/cdecl.h>
29 #include <pulse/gccmacro.h>
30 #include <pulse/sample.h>
31 #include <pulse/channelmap.h>
33 /** \page volume Volume Control
35 * \section overv_sec Overview
37 * Sinks, sources, sink inputs and samples can all have their own volumes.
38 * To deal with these, The PulseAudio libray contains a number of functions
41 * The basic volume type in PulseAudio is the \ref pa_volume_t type. Most of
42 * the time, applications will use the aggregated pa_cvolume structure that
43 * can store the volume of all channels at once.
45 * Volumes commonly span between muted (0%), and normal (100%). It is possible
46 * to set volumes to higher than 100%, but clipping might occur.
48 * \section calc_sec Calculations
50 * The volumes in PulseAudio are logarithmic in nature and applications
51 * shouldn't perform calculations with them directly. Instead, they should
52 * be converted to and from either dB or a linear scale:
54 * \li dB - pa_sw_volume_from_dB() / pa_sw_volume_to_dB()
55 * \li Linear - pa_sw_volume_from_linear() / pa_sw_volume_to_linear()
57 * For simple multiplication, pa_sw_volume_multiply() and
58 * pa_sw_cvolume_multiply() can be used.
60 * Calculations can only be reliably performed on software volumes
61 * as it is commonly unknown what scale hardware volumes relate to.
63 * The functions described above are only valid when used with
64 * software volumes. Hence it is usually a better idea to treat all
65 * volume values as opaque with a range from PA_VOLUME_MUTED (0%) to
66 * PA_VOLUME_NORM (100%) and to refrain from any calculations with
69 * \section conv_sec Convenience Functions
71 * To handle the pa_cvolume structure, the PulseAudio library provides a
72 * number of convenienc functions:
74 * \li pa_cvolume_valid() - Tests if a pa_cvolume structure is valid.
75 * \li pa_cvolume_equal() - Tests if two pa_cvolume structures are identical.
76 * \li pa_cvolume_channels_equal_to() - Tests if all channels of a pa_cvolume
77 * structure have a given volume.
78 * \li pa_cvolume_is_muted() - Tests if all channels of a pa_cvolume
79 * structure are muted.
80 * \li pa_cvolume_is_norm() - Tests if all channels of a pa_cvolume structure
81 * are at a normal volume.
82 * \li pa_cvolume_set() - Set all channels of a pa_cvolume structure to a
84 * \li pa_cvolume_reset() - Set all channels of a pa_cvolume structure to a
86 * \li pa_cvolume_mute() - Set all channels of a pa_cvolume structure to a
88 * \li pa_cvolume_avg() - Return the average volume of all channels.
89 * \li pa_cvolume_snprint() - Pretty print a pa_cvolume structure.
93 * Constants and routines for volume handling */
97 /** Volume specification:
98 * PA_VOLUME_MUTED: silence;
99 * < PA_VOLUME_NORM: decreased volume;
100 * PA_VOLUME_NORM: normal volume;
101 * > PA_VOLUME_NORM: increased volume */
102 typedef uint32_t pa_volume_t;
104 /** Normal volume (100%) */
105 #define PA_VOLUME_NORM ((pa_volume_t) 0x10000U)
107 /** Muted volume (0%) */
108 #define PA_VOLUME_MUTED ((pa_volume_t) 0U)
110 /** A structure encapsulating a per-channel volume */
111 typedef struct pa_cvolume {
112 uint8_t channels; /**< Number of channels */
113 pa_volume_t values[PA_CHANNELS_MAX]; /**< Per-channel volume */
116 /** Return non-zero when *a == *b */
117 int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) PA_GCC_PURE;
119 /** Initialize the specified volume and return a pointer to
120 * it. The sample spec will have a defined state but
121 * pa_cvolume_valid() will fail for it. \since 0.9.13 */
122 pa_cvolume* pa_cvolume_init(pa_cvolume *a);
124 /** Set the volume of all channels to PA_VOLUME_NORM */
125 #define pa_cvolume_reset(a, n) pa_cvolume_set((a), (n), PA_VOLUME_NORM)
127 /** Set the volume of all channels to PA_VOLUME_MUTED */
128 #define pa_cvolume_mute(a, n) pa_cvolume_set((a), (n), PA_VOLUME_MUTED)
130 /** Set the volume of all channels to the specified parameter */
131 pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v);
133 /** Maximum length of the strings returned by
134 * pa_cvolume_snprint(). Please note that this value can change with
135 * any release without warning and without being considered API or ABI
136 * breakage. You should not use this definition anywhere where it
137 * might become part of an ABI.*/
138 #define PA_CVOLUME_SNPRINT_MAX 320
140 /** Pretty print a volume structure */
141 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c);
143 /** Maximum length of the strings returned by
144 * pa_cvolume_snprint_dB(). Please note that this value can change with
145 * any release without warning and without being considered API or ABI
146 * breakage. You should not use this definition anywhere where it
147 * might become part of an ABI. \since 0.9.13 */
148 #define PA_SW_CVOLUME_SNPRINT_DB_MAX 448
150 /** Pretty print a volume structure but show dB values. \since 0.9.13 */
151 char *pa_sw_cvolume_snprint_dB(char *s, size_t l, const pa_cvolume *c);
153 /** Maximum length of the strings returned by
154 * pa_volume_snprint(). Please note that this value can change with
155 * any release without warning and without being considered API or ABI
156 * breakage. You should not use this definition anywhere where it
157 * might become part of an ABI. \since 0.9.14 */
158 #define PA_VOLUME_SNPRINT_MAX 10
160 /** Pretty print a volume \since 0.9.14 */
161 char *pa_volume_snprint(char *s, size_t l, pa_volume_t v);
163 /** Maximum length of the strings returned by
164 * pa_volume_snprint_dB(). Please note that this value can change with
165 * any release without warning and without being considered API or ABI
166 * breakage. You should not use this definition anywhere where it
167 * might become part of an ABI. \since 0.9.14 */
168 #define PA_SW_VOLUME_SNPRINT_DB_MAX 10
170 /** Pretty print a volume but show dB values. \since 0.9.14 */
171 char *pa_sw_volume_snprint_dB(char *s, size_t l, pa_volume_t v);
173 /** Return the average volume of all channels */
174 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) PA_GCC_PURE;
176 /** Return the maximum volume of all channels. \since 0.9.12 */
177 pa_volume_t pa_cvolume_max(const pa_cvolume *a) PA_GCC_PURE;
179 /** Return TRUE when the passed cvolume structure is valid, FALSE otherwise */
180 int pa_cvolume_valid(const pa_cvolume *v) PA_GCC_PURE;
182 /** Return non-zero if the volume of all channels is equal to the specified value */
183 int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) PA_GCC_PURE;
185 /** Return 1 if the specified volume has all channels muted */
186 #define pa_cvolume_is_muted(a) pa_cvolume_channels_equal_to((a), PA_VOLUME_MUTED)
188 /** Return 1 if the specified volume has all channels on normal level */
189 #define pa_cvolume_is_norm(a) pa_cvolume_channels_equal_to((a), PA_VOLUME_NORM)
191 /** Multiply two volume specifications, return the result. This uses
192 * PA_VOLUME_NORM as neutral element of multiplication. This is only
193 * valid for software volumes! */
194 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) PA_GCC_CONST;
196 /** Multiply two per-channel volumes and return the result in
197 * *dest. This is only valid for software volumes! */
198 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b);
200 /** Divide two volume specifications, return the result. This uses
201 * PA_VOLUME_NORM as neutral element of division. This is only valid
202 * for software volumes! If a division by zero is tried the result
203 * will be 0. \since 0.9.13 */
204 pa_volume_t pa_sw_volume_divide(pa_volume_t a, pa_volume_t b) PA_GCC_CONST;
206 /** Multiply to per-channel volumes and return the result in
207 * *dest. This is only valid for software volumes! \since 0.9.13 */
208 pa_cvolume *pa_sw_cvolume_divide(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b);
210 /** Convert a decibel value to a volume. This is only valid for software volumes! */
211 pa_volume_t pa_sw_volume_from_dB(double f) PA_GCC_CONST;
213 /** Convert a volume to a decibel value. This is only valid for software volumes! */
214 double pa_sw_volume_to_dB(pa_volume_t v) PA_GCC_CONST;
216 /** Convert a linear factor to a volume. This is only valid for software volumes! */
217 pa_volume_t pa_sw_volume_from_linear(double v) PA_GCC_CONST;
219 /** Convert a volume to a linear factor. This is only valid for software volumes! */
220 double pa_sw_volume_to_linear(pa_volume_t v) PA_GCC_CONST;
223 #define PA_DECIBEL_MININFTY ((double) -INFINITY)
225 /** This value is used as minus infinity when using pa_volume_{to,from}_dB(). */
226 #define PA_DECIBEL_MININFTY ((double) -200.0)
229 /** Remap a volume from one channel mapping to a different channel mapping. \since 0.9.12 */
230 pa_cvolume *pa_cvolume_remap(pa_cvolume *v, pa_channel_map *from, pa_channel_map *to);
232 /** Return non-zero if the specified volume is compatible with
233 * the specified sample spec. \since 0.9.13 */
234 int pa_cvolume_compatible(const pa_cvolume *v, const pa_sample_spec *ss) PA_GCC_PURE;