]> code.delx.au - pulseaudio/blob - src/polyp/volume.h
181784f405f1c8ae3e78db419419086d0b815449
[pulseaudio] / src / polyp / volume.h
1 #ifndef foovolumehfoo
2 #define foovolumehfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of polypaudio.
8
9 polypaudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 polypaudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with polypaudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #include <inttypes.h>
26 #include <polyp/cdecl.h>
27 #include <polyp/sample.h>
28
29 /** \page volume Volume control
30 *
31 * \section overv_sec Overview
32 *
33 * Sinks, sources, sink inputs and samples can all have their own volumes.
34 * To deal with these, The Polypaudio libray contains a number of functions
35 * that ease handling.
36 *
37 * The basic volume type in Polypaudio is the \ref pa_volume_t type. Most of
38 * the time, applications will use the aggregated pa_cvolume structure that
39 * can store the volume of all channels at once.
40 *
41 * Volumes commonly span between muted (0%), and normal (100%). It is possible
42 * to set volumes to higher than 100%, but clipping might occur.
43 *
44 * \section calc_sec Calculations
45 *
46 * The volumes in Polypaudio are logarithmic in nature and applications
47 * shouldn't perform calculations with them directly. Instead, they should
48 * be converted to and from either dB or a linear scale:
49 *
50 * \li dB - pa_sw_volume_from_dB() / pa_sw_volume_to_dB()
51 * \li Linear - pa_sw_volume_from_linear() / pa_sw_volume_to_linear()
52 *
53 * For simple multiplication, pa_sw_volume_multiply() and
54 * pa_sw_cvolume_multiply() can be used.
55 *
56 * Calculations can only be reliably be performed on software volumes as
57 * it is commonly unknown what scale hardware volumes use.
58 *
59 * \section conv_sec Convenience functions
60 *
61 * To handle the pa_cvolume structure, the Polypaudio library provides a
62 * number of convenienc functions:
63 *
64 * \li pa_cvolume_valid() - Tests if a pa_cvolume structure is valid.
65 * \li pa_cvolume_equal() - Tests if two pa_cvolume structures are identical.
66 * \li pa_cvolume_channels_equal_to() - Tests if all channels of a pa_cvolume
67 * structure have a given volume.
68 * \li pa_cvolume_is_muted() - Tests if all channels of a pa_cvolume
69 * structure are muted.
70 * \li pa_cvolume_is_norm() - Tests if all channels of a pa_cvolume structure
71 * are at a normal volume.
72 * \li pa_cvolume_set() - Set all channels of a pa_cvolume structure to a
73 * certain volume.
74 * \li pa_cvolume_reset() - Set all channels of a pa_cvolume structure to a
75 * normal volume.
76 * \li pa_cvolume_mute() - Set all channels of a pa_cvolume structure to a
77 * muted volume.
78 * \li pa_cvolume_avg() - Return the average volume of all channels.
79 * \li pa_cvolume_snprint() - Pretty print a pa_cvolume structure.
80 */
81
82 /** \file
83 * Constants and routines for volume handling */
84
85 PA_C_DECL_BEGIN
86
87 /** Volume specification:
88 * PA_VOLUME_MUTED: silence;
89 * < PA_VOLUME_NORM: decreased volume;
90 * PA_VOLUME_NORM: normal volume;
91 * > PA_VOLUME_NORM: increased volume */
92 typedef uint32_t pa_volume_t;
93
94 /** Normal volume (100%) */
95 #define PA_VOLUME_NORM (0x10000)
96
97 /** Muted volume (0%) */
98 #define PA_VOLUME_MUTED (0)
99
100 /** A structure encapsulating a per-channel volume */
101 typedef struct pa_cvolume {
102 uint8_t channels; /**< Number of channels */
103 pa_volume_t values[PA_CHANNELS_MAX]; /**< Per-channel volume */
104 } pa_cvolume;
105
106 /** Return non-zero when *a == *b */
107 int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b);
108
109 /** Set the volume of all channels to PA_VOLUME_NORM */
110 #define pa_cvolume_reset(a, n) pa_cvolume_set((a), (n), PA_VOLUME_NORM)
111
112 /** Set the volume of all channels to PA_VOLUME_MUTED */
113 #define pa_cvolume_mute(a, n) pa_cvolume_set((a), (n), PA_VOLUME_MUTED)
114
115 /** Set the volume of all channels to the specified parameter */
116 pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v);
117
118 /** Maximum length of the strings returned by pa_cvolume_snprint() */
119 #define PA_CVOLUME_SNPRINT_MAX 64
120
121 /** Pretty print a volume structure */
122 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c);
123
124 /** Return the average volume of all channels */
125 pa_volume_t pa_cvolume_avg(const pa_cvolume *a);
126
127 /** Return TRUE when the passed cvolume structure is valid, FALSE otherwise */
128 int pa_cvolume_valid(const pa_cvolume *v);
129
130 /** Return non-zero if the volume of all channels is equal to the specified value */
131 int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v);
132
133 /** Return 1 if the specified volume has all channels muted */
134 #define pa_cvolume_is_muted(a) pa_cvolume_channels_equal_to((a), PA_VOLUME_MUTED)
135
136 /** Return 1 if the specified volume has all channels on normal level */
137 #define pa_cvolume_is_norm(a) pa_cvolume_channels_equal_to((a), PA_VOLUME_NORM)
138
139 /** Multiply two volumes specifications, return the result. This uses PA_VOLUME_NORM as neutral element of multiplication. This is only valid for software volumes! */
140 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b);
141
142 /** Multiply to per-channel volumes and return the result in *dest. This is only valid for software volumes! */
143 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b);
144
145 /** Convert a decibel value to a volume. This is only valid for software volumes! \since 0.4 */
146 pa_volume_t pa_sw_volume_from_dB(double f);
147
148 /** Convert a volume to a decibel value. This is only valid for software volumes! \since 0.4 */
149 double pa_sw_volume_to_dB(pa_volume_t v);
150
151 /** Convert a linear factor to a volume. This is only valid for software volumes! \since 0.8 */
152 pa_volume_t pa_sw_volume_from_linear(double v);
153
154 /** Convert a volume to a linear factor. This is only valid for software volumes! \since 0.8 */
155 double pa_sw_volume_to_linear(pa_volume_t v);
156
157 #ifdef INFINITY
158 #define PA_DECIBEL_MININFTY (-INFINITY)
159 #else
160 /** This value is used as minus infinity when using pa_volume_{to,from}_dB(). \since 0.4 */
161 #define PA_DECIBEL_MININFTY (-200)
162 #endif
163
164 PA_C_DECL_END
165
166 #endif