volume ramp: additions to the low level infra
[platform/upstream/pulseaudio.git] / src / pulse / volume.h
1 #ifndef foovolumehfoo
2 #define foovolumehfoo
3
4 /***
5   This file is part of PulseAudio.
6
7   Copyright 2004-2006 Lennart Poettering
8   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
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.
14
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.
19
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
23   USA.
24 ***/
25
26 #include <inttypes.h>
27 #include <limits.h>
28
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>
34
35 /** \page volume Volume Control
36  *
37  * \section overv_sec Overview
38  *
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
41  * that ease handling.
42  *
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.
46  *
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.
49  *
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().
64  *
65  * \section calc_sec Calculations
66  *
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:
70  *
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()
73  *
74  * For simple multiplication, pa_sw_volume_multiply() and
75  * pa_sw_cvolume_multiply() can be used.
76  *
77  * Calculations can only be reliably performed on software volumes
78  * as it is commonly unknown what scale hardware volumes relate to.
79  *
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
84  * them.
85  *
86  * \section conv_sec Convenience Functions
87  *
88  * To handle the pa_cvolume structure, the PulseAudio library provides a
89  * number of convenience functions:
90  *
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
100  *                        a certain volume.
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
104  *                         to a muted volume.
105  * \li pa_cvolume_avg() - Return the average volume of all channels.
106  * \li pa_cvolume_snprint() - Pretty print a pa_cvolume structure.
107  */
108
109 /** \file
110  * Constants and routines for volume handling
111  *
112  * See also \subpage volume
113  */
114
115 PA_C_DECL_BEGIN
116
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;
123
124 /** Normal volume (100%, 0 dB) */
125 #define PA_VOLUME_NORM ((pa_volume_t) 0x10000U)
126
127 /** Muted (minimal valid) volume (0%, -inf dB) */
128 #define PA_VOLUME_MUTED ((pa_volume_t) 0U)
129
130 /** Maximum valid volume we can store. \since 0.9.15 */
131 #define PA_VOLUME_MAX ((pa_volume_t) UINT32_MAX/2)
132
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))
139
140 /** Special 'invalid' volume. \since 0.9.16 */
141 #define PA_VOLUME_INVALID ((pa_volume_t) UINT32_MAX)
142
143 /** Check if volume is valid. \since 1.0 */
144 #define PA_VOLUME_IS_VALID(v) ((v) <= PA_VOLUME_MAX)
145
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))
148
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 */
153 } pa_cvolume;
154
155 /** Return non-zero when *a == *b */
156 int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) PA_GCC_PURE;
157
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);
162
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)
165
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)
168
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);
171
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
178
179 /** Pretty print a volume structure */
180 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c);
181
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
188
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);
191
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
197
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);
203
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
210
211 /** Pretty print a volume \since 0.9.15 */
212 char *pa_volume_snprint(char *s, size_t l, pa_volume_t v);
213
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 10
220
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);
223
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
229
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);
234
235 /** Return the average volume of all channels */
236 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) PA_GCC_PURE;
237
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;
244
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;
247
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.
252  * \since 0.9.16 */
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;
254
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;
257
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.
262  * \since 0.9.16 */
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;
264
265 /** Return non-zero when the passed cvolume structure is valid */
266 int pa_cvolume_valid(const pa_cvolume *v) PA_GCC_PURE;
267
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;
270
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)
273
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)
276
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;
281
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);
286
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
290  * 0.9.16 */
291 pa_cvolume *pa_sw_cvolume_multiply_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b);
292
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;
298
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);
303
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
307  * 0.9.16 */
308 pa_cvolume *pa_sw_cvolume_divide_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b);
309
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;
312
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;
315
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;
319
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;
322
323 #ifdef INFINITY
324 #define PA_DECIBEL_MININFTY ((double) -INFINITY)
325 #else
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)
328 #endif
329
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);
332
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;
336
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;
340
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;
347
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);
358
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;
365
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);
376
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);
381
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);
387
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);
394
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;
400
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);
405
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);
409
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);
413
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);
417
418 /** Volume ramp type
419 */
420 typedef enum pa_volume_ramp_type {
421     PA_VOLUME_RAMP_TYPE_LINEAR = 0,        /**< linear */
422     PA_VOLUME_RAMP_TYPE_LOGARITHMIC = 1,   /**< logarithmic */
423     PA_VOLUME_RAMP_TYPE_CUBIC = 2,
424 } pa_volume_ramp_type_t;
425
426 /** A structure encapsulating a volume ramp */
427 typedef struct pa_volume_ramp_t {
428     pa_volume_ramp_type_t type;
429     long length;
430     pa_volume_t target;
431 } pa_volume_ramp_t;
432
433 /** A structure encapsulating a multichannel volume ramp */
434 typedef struct pa_cvolume_ramp {
435     uint8_t channels;
436     pa_volume_ramp_t ramps[PA_CHANNELS_MAX];
437 } pa_cvolume_ramp;
438
439 /** Return non-zero when *a == *b */
440 int pa_cvolume_ramp_equal(const pa_cvolume_ramp *a, const pa_cvolume_ramp *b);
441
442 /** Init volume ramp struct */
443 pa_cvolume_ramp* pa_cvolume_ramp_init(pa_cvolume_ramp *ramp);
444
445 /** Set first n channels of ramp struct to certain value */
446 pa_cvolume_ramp* pa_cvolume_ramp_set(pa_cvolume_ramp *ramp, unsigned channel, pa_volume_ramp_type_t type, long time, pa_volume_t vol);
447
448 /** Set individual channel in the channel struct */
449 pa_cvolume_ramp* pa_cvolume_ramp_channel_ramp_set(pa_cvolume_ramp *ramp, unsigned channel, pa_volume_ramp_type_t type, long time, pa_volume_t vol);
450
451 PA_C_DECL_END
452
453 #endif