1 module pulse.volume;
2 
3 import core.stdc.stdint;
4 
5 import pulse.channelmap;
6 import pulse.sample;
7 
8 extern (C):
9 
10 /***
11   This file is part of PulseAudio.
12 
13   Copyright 2004-2006 Lennart Poettering
14   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
15 
16   PulseAudio is free software; you can redistribute it and/or modify
17   it under the terms of the GNU Lesser General Public License as published
18   by the Free Software Foundation; either version 2.1 of the License,
19   or (at your option) any later version.
20 
21   PulseAudio is distributed in the hope that it will be useful, but
22   WITHOUT ANY WARRANTY; without even the implied warranty of
23   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24   General Public License for more details.
25 
26   You should have received a copy of the GNU Lesser General Public License
27   along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
28 ***/
29 
30 /** \page volume Volume Control
31  *
32  * \section overv_sec Overview
33  *
34  * Sinks, sources, sink inputs, source outputs and samples can all have their
35  * own volumes. To deal with these, The PulseAudio library contains a number of
36  * functions that ease handling.
37  *
38  * The basic volume type in PulseAudio is the \ref pa_volume_t type. Most of
39  * the time, applications will use the aggregated pa_cvolume structure that
40  * can store the volume of all channels at once.
41  *
42  * Volumes commonly span between muted (0%), and normal (100%). It is possible
43  * to set volumes to higher than 100%, but clipping might occur.
44  *
45  * There is no single well-defined meaning attached to the 100% volume for a
46  * sink input. In fact, it depends on the server configuration. With flat
47  * volumes enabled, it means the maximum volume that the sound hardware is
48  * capable of, which is usually so high that you absolutely must not set sink
49  * input volume to 100% unless the the user explicitly requests that (note that
50  * usually you shouldn't set the volume anyway if the user doesn't explicitly
51  * request it, instead, let PulseAudio decide the volume for the sink input).
52  * With flat volumes disabled the sink input volume is relative to the sink
53  * volume, so 100% sink input volume means that the sink input is played at the
54  * current sink volume level. In this case 100% is often a good default volume
55  * for a sink input, although you still should let PulseAudio decide the
56  * default volume. It is possible to figure out whether flat volume mode is in
57  * effect for a given sink by calling pa_context_get_sink_info_by_name().
58  *
59  * \section calc_sec Calculations
60  *
61  * The volumes in PulseAudio are cubic in nature and applications shouldn't
62  * perform calculations with them directly. Instead, they should be converted
63  * to and from either dB or a linear scale:
64  *
65  * \li dB - pa_sw_volume_from_dB() / pa_sw_volume_to_dB()
66  * \li Linear - pa_sw_volume_from_linear() / pa_sw_volume_to_linear()
67  *
68  * For simple multiplication, pa_sw_volume_multiply() and
69  * pa_sw_cvolume_multiply() can be used.
70  *
71  * It's often unknown what scale hardware volumes relate to. Don't use the
72  * above functions on sink and source volumes, unless the sink or source in
73  * question has the PA_SINK_DECIBEL_VOLUME or PA_SOURCE_DECIBEL_VOLUME flag
74  * set. The conversion functions are rarely needed anyway, most of the time
75  * it's sufficient to treat all volumes as opaque with a range from
76  * PA_VOLUME_MUTED (0%) to PA_VOLUME_NORM (100%).
77  *
78  * \section conv_sec Convenience Functions
79  *
80  * To handle the pa_cvolume structure, the PulseAudio library provides a
81  * number of convenience functions:
82  *
83  * \li pa_cvolume_valid() - Tests if a pa_cvolume structure is valid.
84  * \li pa_cvolume_equal() - Tests if two pa_cvolume structures are identical.
85  * \li pa_cvolume_channels_equal_to() - Tests if all channels of a pa_cvolume
86  *                             structure have a given volume.
87  * \li pa_cvolume_is_muted() - Tests if all channels of a pa_cvolume
88  *                             structure are muted.
89  * \li pa_cvolume_is_norm() - Tests if all channels of a pa_cvolume structure
90  *                            are at a normal volume.
91  * \li pa_cvolume_set() - Set the first n channels of a pa_cvolume structure to
92  *                        a certain volume.
93  * \li pa_cvolume_reset() - Set the first n channels of a pa_cvolume structure
94  *                          to a normal volume.
95  * \li pa_cvolume_mute() - Set the first n channels of a pa_cvolume structure
96  *                         to a muted volume.
97  * \li pa_cvolume_avg() - Return the average volume of all channels.
98  * \li pa_cvolume_snprint() - Pretty print a pa_cvolume structure.
99  */
100 
101 /** \file
102  * Constants and routines for volume handling
103  *
104  * See also \subpage volume
105  */
106 
107 /** Volume specification:
108  *  PA_VOLUME_MUTED: silence;
109  * < PA_VOLUME_NORM: decreased volume;
110  *   PA_VOLUME_NORM: normal volume;
111  * > PA_VOLUME_NORM: increased volume */
112 alias pa_volume_t = uint;
113 
114 /** Normal volume (100%, 0 dB) */
115 enum PA_VOLUME_NORM = cast(pa_volume_t) 0x10000U;
116 
117 /** Muted (minimal valid) volume (0%, -inf dB) */
118 enum PA_VOLUME_MUTED = cast(pa_volume_t) 0U;
119 
120 /** Maximum valid volume we can store. \since 0.9.15 */
121 enum PA_VOLUME_MAX = cast(pa_volume_t) UINT32_MAX / 2;
122 
123 /** Recommended maximum volume to show in user facing UIs.
124  * Note: UIs should deal gracefully with volumes greater than this value
125  * and not cause feedback loops etc. - i.e. if the volume is more than
126  * this, the UI should not limit it and push the limited value back to
127  * the server. \since 0.9.23 */
128 //pa_volume_t PA_VOLUME_UI_MAX = pa_sw_volume_from_dB(+11.0);
129 
130 /** Special 'invalid' volume. \since 0.9.16 */
131 enum PA_VOLUME_INVALID = cast(pa_volume_t) UINT32_MAX;
132 
133 /** Check if volume is valid. \since 1.0 */
134 extern (D) auto PA_VOLUME_IS_VALID(T)(auto ref T v)
135 {
136     return v <= PA_VOLUME_MAX;
137 }
138 
139 /** Clamp volume to the permitted range. \since 1.0 */
140 extern (D) auto PA_CLAMP_VOLUME(T)(auto ref T v)
141 {
142     return PA_CLAMP_UNLIKELY(v, PA_VOLUME_MUTED, PA_VOLUME_MAX);
143 }
144 
145 /** A structure encapsulating a per-channel volume */
146 struct pa_cvolume
147 {
148     ubyte channels; /**< Number of channels */
149     pa_volume_t[PA_CHANNELS_MAX] values; /**< Per-channel volume */
150 }
151 
152 /** Return non-zero when *a == *b, checking that both a and b
153  * have the same number of channels and that the volumes of
154  * channels in a equal those in b. */
155 int pa_cvolume_equal (const(pa_cvolume)* a, const(pa_cvolume)* b);
156 
157 /** Initialize the specified volume and return a pointer to
158  * it. The sample spec will have a defined state but
159  * pa_cvolume_valid() will fail for it. \since 0.9.13 */
160 pa_cvolume* pa_cvolume_init (pa_cvolume* a);
161 
162 /** Set the volume of the first n channels to PA_VOLUME_NORM */
163 extern (D) auto pa_cvolume_reset(T0, T1)(auto ref T0 a, auto ref T1 n)
164 {
165     return pa_cvolume_set(a, n, PA_VOLUME_NORM);
166 }
167 
168 /** Set the volume of the first n channels to PA_VOLUME_MUTED */
169 extern (D) auto pa_cvolume_mute(T0, T1)(auto ref T0 a, auto ref T1 n)
170 {
171     return pa_cvolume_set(a, n, PA_VOLUME_MUTED);
172 }
173 
174 /** Set the volume of the specified number of channels to the volume v */
175 pa_cvolume* pa_cvolume_set (pa_cvolume* a, uint channels, pa_volume_t v);
176 
177 /** Maximum length of the strings returned by
178  * pa_cvolume_snprint(). Please note that this value can change with
179  * any release without warning and without being considered API or ABI
180  * breakage. You should not use this definition anywhere where it
181  * might become part of an ABI.*/
182 enum PA_CVOLUME_SNPRINT_MAX = 320;
183 
184 /** Pretty print a volume structure. Returns \a s. */
185 char* pa_cvolume_snprint (char* s, size_t l, const(pa_cvolume)* c);
186 
187 /** Maximum length of the strings returned by
188  * pa_sw_cvolume_snprint_dB(). Please note that this value can change with
189  * any release without warning and without being considered API or ABI
190  * breakage. You should not use this definition anywhere where it
191  * might become part of an ABI. \since 0.9.13 */
192 enum PA_SW_CVOLUME_SNPRINT_DB_MAX = 448;
193 
194 /** Pretty print a volume structure, showing dB values. Returns \a s. \since 0.9.13 */
195 char* pa_sw_cvolume_snprint_dB (char* s, size_t l, const(pa_cvolume)* c);
196 
197 /** Maximum length of the strings returned by pa_cvolume_snprint_verbose().
198  * Please note that this value can change with any release without warning and
199  * without being considered API or ABI breakage. You should not use this
200  * definition anywhere where it might become part of an ABI. \since 5.0 */
201 enum PA_CVOLUME_SNPRINT_VERBOSE_MAX = 1984;
202 
203 /** Pretty print a volume structure in a verbose way. The volume for each
204  * channel is printed in several formats: the raw pa_volume_t value,
205  * percentage, and if print_dB is non-zero, also the dB value. If map is not
206  * NULL, the channel names will be printed. Returns \a s. \since 5.0 */
207 char* pa_cvolume_snprint_verbose (char* s, size_t l, const(pa_cvolume)* c, const(pa_channel_map)* map, int print_dB);
208 
209 /** Maximum length of the strings returned by
210  * pa_volume_snprint(). Please note that this value can change with
211  * any release without warning and without being considered API or ABI
212  * breakage. You should not use this definition anywhere where it
213  * might become part of an ABI. \since 0.9.15 */
214 enum PA_VOLUME_SNPRINT_MAX = 10;
215 
216 /** Pretty print a volume. Returns \a s. \since 0.9.15 */
217 char* pa_volume_snprint (char* s, size_t l, pa_volume_t v);
218 
219 /** Maximum length of the strings returned by
220  * pa_sw_volume_snprint_dB(). Please note that this value can change with
221  * any release without warning and without being considered API or ABI
222  * breakage. You should not use this definition anywhere where it
223  * might become part of an ABI. \since 0.9.15 */
224 enum PA_SW_VOLUME_SNPRINT_DB_MAX = 11;
225 
226 /** Pretty print a volume but show dB values. Returns \a s. \since 0.9.15 */
227 char* pa_sw_volume_snprint_dB (char* s, size_t l, pa_volume_t v);
228 
229 /** Maximum length of the strings returned by pa_volume_snprint_verbose().
230  * Please note that this value can change with any release without warning and
231  * withou being considered API or ABI breakage. You should not use this
232  * definition anywhere where it might become part of an ABI. \since 5.0 */
233 enum PA_VOLUME_SNPRINT_VERBOSE_MAX = 35;
234 
235 /** Pretty print a volume in a verbose way. The volume is printed in several
236  * formats: the raw pa_volume_t value, percentage, and if print_dB is non-zero,
237  * also the dB value. Returns \a s. \since 5.0 */
238 char* pa_volume_snprint_verbose (char* s, size_t l, pa_volume_t v, int print_dB);
239 
240 /** Return the average volume of all channels */
241 pa_volume_t pa_cvolume_avg (const(pa_cvolume)* a);
242 
243 /** Return the average volume of all channels that are included in the
244  * specified channel map with the specified channel position mask. If
245  * cm is NULL this call is identical to pa_cvolume_avg(). If no
246  * channel is selected the returned value will be
247  * PA_VOLUME_MUTED. \since 0.9.16 */
248 pa_volume_t pa_cvolume_avg_mask (const(pa_cvolume)* a, const(pa_channel_map)* cm, pa_channel_position_mask_t mask);
249 
250 /** Return the maximum volume of all channels. \since 0.9.12 */
251 pa_volume_t pa_cvolume_max (const(pa_cvolume)* a);
252 
253 /** Return the maximum volume of all channels that are included in the
254  * specified channel map with the specified channel position mask. If
255  * cm is NULL this call is identical to pa_cvolume_max(). If no
256  * channel is selected the returned value will be PA_VOLUME_MUTED.
257  * \since 0.9.16 */
258 pa_volume_t pa_cvolume_max_mask (const(pa_cvolume)* a, const(pa_channel_map)* cm, pa_channel_position_mask_t mask);
259 
260 /** Return the minimum volume of all channels. \since 0.9.16 */
261 pa_volume_t pa_cvolume_min (const(pa_cvolume)* a);
262 
263 /** Return the minimum volume of all channels that are included in the
264  * specified channel map with the specified channel position mask. If
265  * cm is NULL this call is identical to pa_cvolume_min(). If no
266  * channel is selected the returned value will be PA_VOLUME_MUTED.
267  * \since 0.9.16 */
268 pa_volume_t pa_cvolume_min_mask (const(pa_cvolume)* a, const(pa_channel_map)* cm, pa_channel_position_mask_t mask);
269 
270 /** Return non-zero when the passed cvolume structure is valid */
271 int pa_cvolume_valid (const(pa_cvolume)* v);
272 
273 /** Return non-zero if the volume of all channels is equal to the specified value */
274 int pa_cvolume_channels_equal_to (const(pa_cvolume)* a, pa_volume_t v);
275 
276 /** Return 1 if the specified volume has all channels muted */
277 extern (D) auto pa_cvolume_is_muted(T)(auto ref T a)
278 {
279     return pa_cvolume_channels_equal_to(a, PA_VOLUME_MUTED);
280 }
281 
282 /** Return 1 if the specified volume has all channels on normal level */
283 extern (D) auto pa_cvolume_is_norm(T)(auto ref T a)
284 {
285     return pa_cvolume_channels_equal_to(a, PA_VOLUME_NORM);
286 }
287 
288 /** Multiply two volume specifications, return the result. This uses
289  * PA_VOLUME_NORM as neutral element of multiplication. This is only
290  * valid for software volumes! */
291 pa_volume_t pa_sw_volume_multiply (pa_volume_t a, pa_volume_t b);
292 
293 /** Multiply two per-channel volumes and return the result in
294  * *dest. This is only valid for software volumes! a, b and dest may
295  * point to the same structure. Returns dest, or NULL on error. */
296 pa_cvolume* pa_sw_cvolume_multiply (pa_cvolume* dest, const(pa_cvolume)* a, const(pa_cvolume)* b);
297 
298 /** Multiply a per-channel volume with a scalar volume and return the
299  * result in *dest. This is only valid for software volumes! a
300  * and dest may point to the same structure. Returns dest, or NULL on error.
301  * \since 0.9.16 */
302 pa_cvolume* pa_sw_cvolume_multiply_scalar (pa_cvolume* dest, const(pa_cvolume)* a, pa_volume_t b);
303 
304 /** Divide two volume specifications, return the result. This uses
305  * PA_VOLUME_NORM as neutral element of division. This is only valid
306  * for software volumes! If a division by zero is tried the result
307  * will be 0. \since 0.9.13 */
308 pa_volume_t pa_sw_volume_divide (pa_volume_t a, pa_volume_t b);
309 
310 /** Divide two per-channel volumes and return the result in
311  * *dest. This is only valid for software volumes! a, b
312  * and dest may point to the same structure. Returns dest,
313  * or NULL on error. \since 0.9.13 */
314 pa_cvolume* pa_sw_cvolume_divide (pa_cvolume* dest, const(pa_cvolume)* a, const(pa_cvolume)* b);
315 
316 /** Divide a per-channel volume by a scalar volume and return the
317  * result in *dest. This is only valid for software volumes! a
318  * and dest may point to the same structure. Returns dest,
319  * or NULL on error. \since 0.9.16 */
320 pa_cvolume* pa_sw_cvolume_divide_scalar (pa_cvolume* dest, const(pa_cvolume)* a, pa_volume_t b);
321 
322 /** Convert a decibel value to a volume (amplitude, not power). This is only valid for software volumes! */
323 pa_volume_t pa_sw_volume_from_dB (double f);
324 
325 /** Convert a volume to a decibel value (amplitude, not power). This is only valid for software volumes! */
326 double pa_sw_volume_to_dB (pa_volume_t v);
327 
328 /** Convert a linear factor to a volume. 0.0 and less is muted while
329  * 1.0 is PA_VOLUME_NORM. This is only valid for software volumes! */
330 pa_volume_t pa_sw_volume_from_linear (double v);
331 
332 /** Convert a volume to a linear factor. This is only valid for software volumes! */
333 double pa_sw_volume_to_linear (pa_volume_t v);
334 
335 /** This floor value is used as minus infinity when using pa_sw_volume_to_dB() / pa_sw_volume_from_dB(). */
336 enum PA_DECIBEL_MININFTY = cast(double) -200.0;
337 
338 /** Remap a volume from one channel mapping to a different channel mapping.
339  * Returns \a v. \since 0.9.12 */
340 pa_cvolume* pa_cvolume_remap (pa_cvolume* v, const(pa_channel_map)* from, const(pa_channel_map)* to);
341 
342 /** Return non-zero if the specified volume is compatible with the
343  * specified sample spec. \since 0.9.13 */
344 int pa_cvolume_compatible (const(pa_cvolume)* v, const(pa_sample_spec)* ss);
345 
346 /** Return non-zero if the specified volume is compatible with the
347  * specified sample spec. \since 0.9.15 */
348 int pa_cvolume_compatible_with_channel_map (const(pa_cvolume)* v, const(pa_channel_map)* cm);
349 
350 /** Calculate a 'balance' value for the specified volume with the
351  * specified channel map. The return value will range from -1.0f
352  * (left) to +1.0f (right). If no balance value is applicable to this
353  * channel map the return value will always be 0.0f. See
354  * pa_channel_map_can_balance(). \since 0.9.15 */
355 float pa_cvolume_get_balance (const(pa_cvolume)* v, const(pa_channel_map)* map);
356 
357 /** Adjust the 'balance' value for the specified volume with the
358  * specified channel map. v will be modified in place and
359  * returned. The balance is a value between -1.0f and +1.0f. This
360  * operation might not be reversible! Also, after this call
361  * pa_cvolume_get_balance() is not guaranteed to actually return the
362  * requested balance value (e.g. when the input volume was zero anyway for
363  * all channels). If no balance value is applicable to
364  * this channel map the volume will not be modified. See
365  * pa_channel_map_can_balance(). Will return NULL on error. \since 0.9.15 */
366 pa_cvolume* pa_cvolume_set_balance (pa_cvolume* v, const(pa_channel_map)* map, float new_balance);
367 
368 /** Calculate a 'fade' value (i.e.\ 'balance' between front and rear)
369  * for the specified volume with the specified channel map. The return
370  * value will range from -1.0f (rear) to +1.0f (left). If no fade
371  * value is applicable to this channel map the return value will
372  * always be 0.0f. See pa_channel_map_can_fade(). \since 0.9.15 */
373 float pa_cvolume_get_fade (const(pa_cvolume)* v, const(pa_channel_map)* map);
374 
375 /** Adjust the 'fade' value (i.e.\ 'balance' between front and rear)
376  * for the specified volume with the specified channel map. v will be
377  * modified in place and returned. The balance is a value between
378  * -1.0f and +1.0f. This operation might not be reversible! Also,
379  * after this call pa_cvolume_get_fade() is not guaranteed to actually
380  * return the requested fade value (e.g. when the input volume was
381  * zero anyway for all channels). If no fade value is applicable to
382  * this channel map the volume will not be modified. See
383  * pa_channel_map_can_fade(). Will return NULL on error. \since 0.9.15 */
384 pa_cvolume* pa_cvolume_set_fade (pa_cvolume* v, const(pa_channel_map)* map, float new_fade);
385 
386 /** Calculate a 'lfe balance' value for the specified volume with
387  * the specified channel map. The return value will range from
388  * -1.0f (no lfe) to +1.0f (only lfe), where 0.0f is balanced.
389  * If no value is applicable to this channel map the return value
390  * will always be 0.0f. See pa_channel_map_can_lfe_balance(). \since 8.0 */
391 float pa_cvolume_get_lfe_balance (const(pa_cvolume)* v, const(pa_channel_map)* map);
392 
393 /** Adjust the 'lfe balance' value for the specified volume with
394  * the specified channel map. v will be modified in place and returned.
395  * The balance is a value between -1.0f (no lfe) and +1.0f (only lfe).
396  * This operation might not be reversible! Also, after this call
397  * pa_cvolume_get_lfe_balance() is not guaranteed to actually
398  * return the requested value (e.g. when the input volume was
399  * zero anyway for all channels). If no lfe balance value is applicable to
400  * this channel map the volume will not be modified. See
401  * pa_channel_map_can_lfe_balance(). Will return NULL on error. \since 8.0 */
402 pa_cvolume* pa_cvolume_set_lfe_balance (pa_cvolume* v, const(pa_channel_map)* map, float new_balance);
403 
404 /** Scale the passed pa_cvolume structure so that the maximum volume
405  * of all channels equals max. The proportions between the channel
406  * volumes are kept. Returns \a v, or NULL on error. \since 0.9.15 */
407 pa_cvolume* pa_cvolume_scale (pa_cvolume* v, pa_volume_t max);
408 
409 /** Scale the passed pa_cvolume structure so that the maximum volume
410  * of all channels selected via cm/mask equals max. This also modifies
411  * the volume of those channels that are unmasked. The proportions
412  * between the channel volumes are kept. If cm is NULL this call is
413  * identical to pa_cvolume_scale(). Returns \a v, or NULL on error.
414  * \since 0.9.16 */
415 pa_cvolume* pa_cvolume_scale_mask (pa_cvolume* v, pa_volume_t max, const(pa_channel_map)* cm, pa_channel_position_mask_t mask);
416 
417 /** Set the passed volume to all channels at the specified channel
418  * position. Will return the updated volume struct, or NULL if there
419  * is no channel at the position specified. You can check if a channel
420  * map includes a specific position by calling
421  * pa_channel_map_has_position(). Returns \a cv, or NULL on error.
422  * \since 0.9.16 */
423 pa_cvolume* pa_cvolume_set_position (pa_cvolume* cv, const(pa_channel_map)* map, pa_channel_position_t t, pa_volume_t v);
424 
425 /** Get the maximum volume of all channels at the specified channel
426  * position. Will return 0 if there is no channel at the position
427  * specified. You can check if a channel map includes a specific
428  * position by calling pa_channel_map_has_position(). \since 0.9.16 */
429 pa_volume_t pa_cvolume_get_position (const(pa_cvolume)* cv, const(pa_channel_map)* map, pa_channel_position_t t);
430 
431 /** This goes through all channels in a and b and sets the
432  * corresponding channel in dest to the greater volume of both. a, b
433  * and dest may point to the same structure. Returns \a dest, or NULL
434  * on error. \since 0.9.16 */
435 pa_cvolume* pa_cvolume_merge (pa_cvolume* dest, const(pa_cvolume)* a, const(pa_cvolume)* b);
436 
437 /** Increase the volume passed in by 'inc', but not exceeding 'limit'.
438  * The proportions between the channels are kept.
439  * Returns \a v, or NULL on error. \since 0.9.19 */
440 pa_cvolume* pa_cvolume_inc_clamp (pa_cvolume* v, pa_volume_t inc, pa_volume_t limit);
441 
442 /** Increase the volume passed in by 'inc'. The proportions between
443  * the channels are kept. Returns \a v, or NULL on error. \since 0.9.16 */
444 pa_cvolume* pa_cvolume_inc (pa_cvolume* v, pa_volume_t inc);
445 
446 /** Decrease the volume passed in by 'dec'. The proportions between
447  * the channels are kept. Returns \a v, or NULL on error. \since 0.9.16 */
448 pa_cvolume* pa_cvolume_dec (pa_cvolume* v, pa_volume_t dec);
449