Path: blob/master/thirdparty/linuxbsd_headers/pulse/volume.h
9905 views
#ifndef foovolumehfoo1#define foovolumehfoo23/***4This file is part of PulseAudio.56Copyright 2004-2006 Lennart Poettering7Copyright 2006 Pierre Ossman <[email protected]> for Cendio AB89PulseAudio is free software; you can redistribute it and/or modify10it under the terms of the GNU Lesser General Public License as published11by the Free Software Foundation; either version 2.1 of the License,12or (at your option) any later version.1314PulseAudio is distributed in the hope that it will be useful, but15WITHOUT ANY WARRANTY; without even the implied warranty of16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU17General Public License for more details.1819You should have received a copy of the GNU Lesser General Public License20along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.21***/2223#include <inttypes.h>24#include <limits.h>2526#include <pulse/cdecl.h>27#include <pulse/gccmacro.h>28#include <pulse/sample.h>29#include <pulse/channelmap.h>30#include <pulse/version.h>3132/** \page volume Volume Control33*34* \section overv_sec Overview35*36* Sinks, sources, sink inputs and samples can all have their own volumes.37* To deal with these, The PulseAudio library contains a number of functions38* that ease handling.39*40* The basic volume type in PulseAudio is the \ref pa_volume_t type. Most of41* the time, applications will use the aggregated pa_cvolume structure that42* can store the volume of all channels at once.43*44* Volumes commonly span between muted (0%), and normal (100%). It is possible45* 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 a48* sink input. In fact, it depends on the server configuration. With flat49* volumes enabled (the default in most Linux distributions), it means the50* maximum volume that the sound hardware is capable of, which is usually so51* high that you absolutely must not set sink input volume to 100% unless the52* the user explicitly requests that (note that usually you shouldn't set the53* volume anyway if the user doesn't explicitly request it, instead, let54* PulseAudio decide the volume for the sink input). With flat volumes disabled55* (the default in Ubuntu), the sink input volume is relative to the sink56* volume, so 100% sink input volume means that the sink input is played at the57* current sink volume level. In this case 100% is often a good default volume58* for a sink input, although you still should let PulseAudio decide the59* default volume. It is possible to figure out whether flat volume mode is in60* effect for a given sink by calling pa_context_get_sink_info_by_name().61*62* \section calc_sec Calculations63*64* The volumes in PulseAudio are logarithmic in nature and applications65* shouldn't perform calculations with them directly. Instead, they should66* be converted to and from either dB or a linear scale:67*68* \li dB - pa_sw_volume_from_dB() / pa_sw_volume_to_dB()69* \li Linear - pa_sw_volume_from_linear() / pa_sw_volume_to_linear()70*71* For simple multiplication, pa_sw_volume_multiply() and72* pa_sw_cvolume_multiply() can be used.73*74* Calculations can only be reliably performed on software volumes75* as it is commonly unknown what scale hardware volumes relate to.76*77* The functions described above are only valid when used with78* software volumes. Hence it is usually a better idea to treat all79* volume values as opaque with a range from PA_VOLUME_MUTED (0%) to80* PA_VOLUME_NORM (100%) and to refrain from any calculations with81* them.82*83* \section conv_sec Convenience Functions84*85* To handle the pa_cvolume structure, the PulseAudio library provides a86* number of convenience functions:87*88* \li pa_cvolume_valid() - Tests if a pa_cvolume structure is valid.89* \li pa_cvolume_equal() - Tests if two pa_cvolume structures are identical.90* \li pa_cvolume_channels_equal_to() - Tests if all channels of a pa_cvolume91* structure have a given volume.92* \li pa_cvolume_is_muted() - Tests if all channels of a pa_cvolume93* structure are muted.94* \li pa_cvolume_is_norm() - Tests if all channels of a pa_cvolume structure95* are at a normal volume.96* \li pa_cvolume_set() - Set the first n channels of a pa_cvolume structure to97* a certain volume.98* \li pa_cvolume_reset() - Set the first n channels of a pa_cvolume structure99* to a normal volume.100* \li pa_cvolume_mute() - Set the first n channels of a pa_cvolume structure101* to a muted volume.102* \li pa_cvolume_avg() - Return the average volume of all channels.103* \li pa_cvolume_snprint() - Pretty print a pa_cvolume structure.104*/105106/** \file107* Constants and routines for volume handling108*109* See also \subpage volume110*/111112PA_C_DECL_BEGIN113114/** Volume specification:115* PA_VOLUME_MUTED: silence;116* < PA_VOLUME_NORM: decreased volume;117* PA_VOLUME_NORM: normal volume;118* > PA_VOLUME_NORM: increased volume */119typedef uint32_t pa_volume_t;120121/** Normal volume (100%, 0 dB) */122#define PA_VOLUME_NORM ((pa_volume_t) 0x10000U)123124/** Muted (minimal valid) volume (0%, -inf dB) */125#define PA_VOLUME_MUTED ((pa_volume_t) 0U)126127/** Maximum valid volume we can store. \since 0.9.15 */128#define PA_VOLUME_MAX ((pa_volume_t) UINT32_MAX/2)129130/** Recommended maximum volume to show in user facing UIs.131* Note: UIs should deal gracefully with volumes greater than this value132* and not cause feedback loops etc. - i.e. if the volume is more than133* this, the UI should not limit it and push the limited value back to134* the server. \since 0.9.23 */135#define PA_VOLUME_UI_MAX (pa_sw_volume_from_dB(+11.0))136137/** Special 'invalid' volume. \since 0.9.16 */138#define PA_VOLUME_INVALID ((pa_volume_t) UINT32_MAX)139140/** Check if volume is valid. \since 1.0 */141#define PA_VOLUME_IS_VALID(v) ((v) <= PA_VOLUME_MAX)142143/** Clamp volume to the permitted range. \since 1.0 */144#define PA_CLAMP_VOLUME(v) (PA_CLAMP_UNLIKELY((v), PA_VOLUME_MUTED, PA_VOLUME_MAX))145146/** A structure encapsulating a per-channel volume */147typedef struct pa_cvolume {148uint8_t channels; /**< Number of channels */149pa_volume_t values[PA_CHANNELS_MAX]; /**< Per-channel volume */150} pa_cvolume;151152/** Return non-zero when *a == *b */153int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) PA_GCC_PURE;154155/** Initialize the specified volume and return a pointer to156* it. The sample spec will have a defined state but157* pa_cvolume_valid() will fail for it. \since 0.9.13 */158pa_cvolume* pa_cvolume_init(pa_cvolume *a);159160/** Set the volume of the first n channels to PA_VOLUME_NORM */161#define pa_cvolume_reset(a, n) pa_cvolume_set((a), (n), PA_VOLUME_NORM)162163/** Set the volume of the first n channels to PA_VOLUME_MUTED */164#define pa_cvolume_mute(a, n) pa_cvolume_set((a), (n), PA_VOLUME_MUTED)165166/** Set the volume of the specified number of channels to the volume v */167pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v);168169/** Maximum length of the strings returned by170* pa_cvolume_snprint(). Please note that this value can change with171* any release without warning and without being considered API or ABI172* breakage. You should not use this definition anywhere where it173* might become part of an ABI.*/174#define PA_CVOLUME_SNPRINT_MAX 320175176/** Pretty print a volume structure */177char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c);178179/** Maximum length of the strings returned by180* pa_sw_cvolume_snprint_dB(). Please note that this value can change with181* any release without warning and without being considered API or ABI182* breakage. You should not use this definition anywhere where it183* might become part of an ABI. \since 0.9.13 */184#define PA_SW_CVOLUME_SNPRINT_DB_MAX 448185186/** Pretty print a volume structure but show dB values. \since 0.9.13 */187char *pa_sw_cvolume_snprint_dB(char *s, size_t l, const pa_cvolume *c);188189/** Maximum length of the strings returned by pa_cvolume_snprint_verbose().190* Please note that this value can change with any release without warning and191* without being considered API or ABI breakage. You should not use this192* definition anywhere where it might become part of an ABI. \since 5.0 */193#define PA_CVOLUME_SNPRINT_VERBOSE_MAX 1984194195/** Pretty print a volume structure in a verbose way. The volume for each196* channel is printed in several formats: the raw pa_volume_t value,197* percentage, and if print_dB is non-zero, also the dB value. If map is not198* NULL, the channel names will be printed. \since 5.0 */199char *pa_cvolume_snprint_verbose(char *s, size_t l, const pa_cvolume *c, const pa_channel_map *map, int print_dB);200201/** Maximum length of the strings returned by202* pa_volume_snprint(). Please note that this value can change with203* any release without warning and without being considered API or ABI204* breakage. You should not use this definition anywhere where it205* might become part of an ABI. \since 0.9.15 */206#define PA_VOLUME_SNPRINT_MAX 10207208/** Pretty print a volume \since 0.9.15 */209char *pa_volume_snprint(char *s, size_t l, pa_volume_t v);210211/** Maximum length of the strings returned by212* pa_sw_volume_snprint_dB(). Please note that this value can change with213* any release without warning and without being considered API or ABI214* breakage. You should not use this definition anywhere where it215* might become part of an ABI. \since 0.9.15 */216#define PA_SW_VOLUME_SNPRINT_DB_MAX 11217218/** Pretty print a volume but show dB values. \since 0.9.15 */219char *pa_sw_volume_snprint_dB(char *s, size_t l, pa_volume_t v);220221/** Maximum length of the strings returned by pa_volume_snprint_verbose().222* Please note that this value can change with any release without warning and223* withou being considered API or ABI breakage. You should not use this224* definition anywhere where it might become part of an ABI. \since 5.0 */225#define PA_VOLUME_SNPRINT_VERBOSE_MAX 35226227/** Pretty print a volume in a verbose way. The volume is printed in several228* formats: the raw pa_volume_t value, percentage, and if print_dB is non-zero,229* also the dB value. \since 5.0 */230char *pa_volume_snprint_verbose(char *s, size_t l, pa_volume_t v, int print_dB);231232/** Return the average volume of all channels */233pa_volume_t pa_cvolume_avg(const pa_cvolume *a) PA_GCC_PURE;234235/** Return the average volume of all channels that are included in the236* specified channel map with the specified channel position mask. If237* cm is NULL this call is identical to pa_cvolume_avg(). If no238* channel is selected the returned value will be239* PA_VOLUME_MUTED. \since 0.9.16 */240pa_volume_t pa_cvolume_avg_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) PA_GCC_PURE;241242/** Return the maximum volume of all channels. \since 0.9.12 */243pa_volume_t pa_cvolume_max(const pa_cvolume *a) PA_GCC_PURE;244245/** Return the maximum volume of all channels that are included in the246* specified channel map with the specified channel position mask. If247* cm is NULL this call is identical to pa_cvolume_max(). If no248* channel is selected the returned value will be PA_VOLUME_MUTED.249* \since 0.9.16 */250pa_volume_t pa_cvolume_max_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) PA_GCC_PURE;251252/** Return the minimum volume of all channels. \since 0.9.16 */253pa_volume_t pa_cvolume_min(const pa_cvolume *a) PA_GCC_PURE;254255/** Return the minimum volume of all channels that are included in the256* specified channel map with the specified channel position mask. If257* cm is NULL this call is identical to pa_cvolume_min(). If no258* channel is selected the returned value will be PA_VOLUME_MUTED.259* \since 0.9.16 */260pa_volume_t pa_cvolume_min_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) PA_GCC_PURE;261262/** Return non-zero when the passed cvolume structure is valid */263int pa_cvolume_valid(const pa_cvolume *v) PA_GCC_PURE;264265/** Return non-zero if the volume of all channels is equal to the specified value */266int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) PA_GCC_PURE;267268/** Return 1 if the specified volume has all channels muted */269#define pa_cvolume_is_muted(a) pa_cvolume_channels_equal_to((a), PA_VOLUME_MUTED)270271/** Return 1 if the specified volume has all channels on normal level */272#define pa_cvolume_is_norm(a) pa_cvolume_channels_equal_to((a), PA_VOLUME_NORM)273274/** Multiply two volume specifications, return the result. This uses275* PA_VOLUME_NORM as neutral element of multiplication. This is only276* valid for software volumes! */277pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) PA_GCC_CONST;278279/** Multiply two per-channel volumes and return the result in280* *dest. This is only valid for software volumes! a, b and dest may281* point to the same structure. */282pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b);283284/** Multiply a per-channel volume with a scalar volume and return the285* result in *dest. This is only valid for software volumes! a286* and dest may point to the same structure. \since287* 0.9.16 */288pa_cvolume *pa_sw_cvolume_multiply_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b);289290/** Divide two volume specifications, return the result. This uses291* PA_VOLUME_NORM as neutral element of division. This is only valid292* for software volumes! If a division by zero is tried the result293* will be 0. \since 0.9.13 */294pa_volume_t pa_sw_volume_divide(pa_volume_t a, pa_volume_t b) PA_GCC_CONST;295296/** Divide two per-channel volumes and return the result in297* *dest. This is only valid for software volumes! a, b298* and dest may point to the same structure. \since 0.9.13 */299pa_cvolume *pa_sw_cvolume_divide(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b);300301/** Divide a per-channel volume by a scalar volume and return the302* result in *dest. This is only valid for software volumes! a303* and dest may point to the same structure. \since304* 0.9.16 */305pa_cvolume *pa_sw_cvolume_divide_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b);306307/** Convert a decibel value to a volume (amplitude, not power). This is only valid for software volumes! */308pa_volume_t pa_sw_volume_from_dB(double f) PA_GCC_CONST;309310/** Convert a volume to a decibel value (amplitude, not power). This is only valid for software volumes! */311double pa_sw_volume_to_dB(pa_volume_t v) PA_GCC_CONST;312313/** Convert a linear factor to a volume. 0.0 and less is muted while314* 1.0 is PA_VOLUME_NORM. This is only valid for software volumes! */315pa_volume_t pa_sw_volume_from_linear(double v) PA_GCC_CONST;316317/** Convert a volume to a linear factor. This is only valid for software volumes! */318double pa_sw_volume_to_linear(pa_volume_t v) PA_GCC_CONST;319320#ifdef INFINITY321#define PA_DECIBEL_MININFTY ((double) -INFINITY)322#else323/** This floor value is used as minus infinity when using pa_sw_volume_to_dB() / pa_sw_volume_from_dB(). */324#define PA_DECIBEL_MININFTY ((double) -200.0)325#endif326327/** Remap a volume from one channel mapping to a different channel mapping. \since 0.9.12 */328pa_cvolume *pa_cvolume_remap(pa_cvolume *v, const pa_channel_map *from, const pa_channel_map *to);329330/** Return non-zero if the specified volume is compatible with the331* specified sample spec. \since 0.9.13 */332int pa_cvolume_compatible(const pa_cvolume *v, const pa_sample_spec *ss) PA_GCC_PURE;333334/** Return non-zero if the specified volume is compatible with the335* specified sample spec. \since 0.9.15 */336int pa_cvolume_compatible_with_channel_map(const pa_cvolume *v, const pa_channel_map *cm) PA_GCC_PURE;337338/** Calculate a 'balance' value for the specified volume with the339* specified channel map. The return value will range from -1.0f340* (left) to +1.0f (right). If no balance value is applicable to this341* channel map the return value will always be 0.0f. See342* pa_channel_map_can_balance(). \since 0.9.15 */343float pa_cvolume_get_balance(const pa_cvolume *v, const pa_channel_map *map) PA_GCC_PURE;344345/** Adjust the 'balance' value for the specified volume with the346* specified channel map. v will be modified in place and347* returned. The balance is a value between -1.0f and +1.0f. This348* operation might not be reversible! Also, after this call349* pa_cvolume_get_balance() is not guaranteed to actually return the350* requested balance value (e.g. when the input volume was zero anyway for351* all channels). If no balance value is applicable to352* this channel map the volume will not be modified. See353* pa_channel_map_can_balance(). \since 0.9.15 */354pa_cvolume* pa_cvolume_set_balance(pa_cvolume *v, const pa_channel_map *map, float new_balance);355356/** Calculate a 'fade' value (i.e.\ 'balance' between front and rear)357* for the specified volume with the specified channel map. The return358* value will range from -1.0f (rear) to +1.0f (left). If no fade359* value is applicable to this channel map the return value will360* always be 0.0f. See pa_channel_map_can_fade(). \since 0.9.15 */361float pa_cvolume_get_fade(const pa_cvolume *v, const pa_channel_map *map) PA_GCC_PURE;362363/** Adjust the 'fade' value (i.e.\ 'balance' between front and rear)364* for the specified volume with the specified channel map. v will be365* modified in place and returned. The balance is a value between366* -1.0f and +1.0f. This operation might not be reversible! Also,367* after this call pa_cvolume_get_fade() is not guaranteed to actually368* return the requested fade value (e.g. when the input volume was369* zero anyway for all channels). If no fade value is applicable to370* this channel map the volume will not be modified. See371* pa_channel_map_can_fade(). \since 0.9.15 */372pa_cvolume* pa_cvolume_set_fade(pa_cvolume *v, const pa_channel_map *map, float new_fade);373374/** Calculate a 'lfe balance' value for the specified volume with375* the specified channel map. The return value will range from376* -1.0f (no lfe) to +1.0f (only lfe), where 0.0f is balanced.377* If no value is applicable to this channel map the return value378* will always be 0.0f. See pa_channel_map_can_lfe_balance(). \since 8.0 */379float pa_cvolume_get_lfe_balance(const pa_cvolume *v, const pa_channel_map *map) PA_GCC_PURE;380381/** Adjust the 'lfe balance' value for the specified volume with382* the specified channel map. v will be modified in place and returned.383* The balance is a value between -1.0f (no lfe) and +1.0f (only lfe).384* This operation might not be reversible! Also, after this call385* pa_cvolume_get_lfe_balance() is not guaranteed to actually386* return the requested value (e.g. when the input volume was387* zero anyway for all channels). If no lfe balance value is applicable to388* this channel map the volume will not be modified. See389* pa_channel_map_can_lfe_balance(). \since 8.0 */390pa_cvolume* pa_cvolume_set_lfe_balance(pa_cvolume *v, const pa_channel_map *map, float new_balance);391392/** Scale the passed pa_cvolume structure so that the maximum volume393* of all channels equals max. The proportions between the channel394* volumes are kept. \since 0.9.15 */395pa_cvolume* pa_cvolume_scale(pa_cvolume *v, pa_volume_t max);396397/** Scale the passed pa_cvolume structure so that the maximum volume398* of all channels selected via cm/mask equals max. This also modifies399* the volume of those channels that are unmasked. The proportions400* between the channel volumes are kept. \since 0.9.16 */401pa_cvolume* pa_cvolume_scale_mask(pa_cvolume *v, pa_volume_t max, pa_channel_map *cm, pa_channel_position_mask_t mask);402403/** Set the passed volume to all channels at the specified channel404* position. Will return the updated volume struct, or NULL if there405* is no channel at the position specified. You can check if a channel406* map includes a specific position by calling407* pa_channel_map_has_position(). \since 0.9.16 */408pa_cvolume* pa_cvolume_set_position(pa_cvolume *cv, const pa_channel_map *map, pa_channel_position_t t, pa_volume_t v);409410/** Get the maximum volume of all channels at the specified channel411* position. Will return 0 if there is no channel at the position412* specified. You can check if a channel map includes a specific413* position by calling pa_channel_map_has_position(). \since 0.9.16 */414pa_volume_t pa_cvolume_get_position(pa_cvolume *cv, const pa_channel_map *map, pa_channel_position_t t) PA_GCC_PURE;415416/** This goes through all channels in a and b and sets the417* corresponding channel in dest to the greater volume of both. a, b418* and dest may point to the same structure. \since 0.9.16 */419pa_cvolume* pa_cvolume_merge(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b);420421/** Increase the volume passed in by 'inc', but not exceeding 'limit'.422* The proportions between the channels are kept. \since 0.9.19 */423pa_cvolume* pa_cvolume_inc_clamp(pa_cvolume *v, pa_volume_t inc, pa_volume_t limit);424425/** Increase the volume passed in by 'inc'. The proportions between426* the channels are kept. \since 0.9.16 */427pa_cvolume* pa_cvolume_inc(pa_cvolume *v, pa_volume_t inc);428429/** Decrease the volume passed in by 'dec'. The proportions between430* the channels are kept. \since 0.9.16 */431pa_cvolume* pa_cvolume_dec(pa_cvolume *v, pa_volume_t dec);432433PA_C_DECL_END434435#endif436437438