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