]> code.delx.au - pulseaudio/blob - src/pulse/sample.h
Add support for 24bit samples encoded in the LSB of 32 bit words
[pulseaudio] / src / pulse / sample.h
1 #ifndef foosamplehfoo
2 #define foosamplehfoo
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 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 <sys/types.h>
28 #include <sys/param.h>
29 #include <math.h>
30
31 #include <pulse/gccmacro.h>
32 #include <pulse/cdecl.h>
33
34 /** \page sample Sample Format Specifications
35 *
36 * \section overv_sec Overview
37 *
38 * PulseAudio is capable of handling a multitude of sample formats, rates
39 * and channels, transparently converting and mixing them as needed.
40 *
41 * \section format_sec Sample Format
42 *
43 * PulseAudio supports the following sample formats:
44 *
45 * \li PA_SAMPLE_U8 - Unsigned 8 bit integer PCM.
46 * \li PA_SAMPLE_S16LE - Signed 16 integer bit PCM, little endian.
47 * \li PA_SAMPLE_S16BE - Signed 16 integer bit PCM, big endian.
48 * \li PA_SAMPLE_FLOAT32LE - 32 bit IEEE floating point PCM, little endian.
49 * \li PA_SAMPLE_FLOAT32BE - 32 bit IEEE floating point PCM, big endian.
50 * \li PA_SAMPLE_ALAW - 8 bit a-Law.
51 * \li PA_SAMPLE_ULAW - 8 bit mu-Law.
52 * \li PA_SAMPLE_S32LE - Signed 32 bit integer PCM, little endian.
53 * \li PA_SAMPLE_S32BE - Signed 32 bit integer PCM, big endian.
54 * \li PA_SAMPLE_S24LE - Signed 24 bit integer PCM packed, little endian.
55 * \li PA_SAMPLE_S24BE - Signed 24 bit integer PCM packed, big endian.
56 * \li PA_SAMPLE_S24_32LE - Signed 24 bit integer PCM in LSB of 32 bit words, little endian.
57 * \li PA_SAMPLE_S24_32BE - Signed 24 bit integer PCM in LSB of 32 bit words, big endian.
58 *
59 * The floating point sample formats have the range from -1.0 to 1.0.
60 *
61 * The sample formats that are sensitive to endianness have convenience
62 * macros for native endian (NE), and reverse endian (RE).
63 *
64 * \section rate_sec Sample Rates
65 *
66 * PulseAudio supports any sample rate between 1 Hz and 192000 Hz. There is no
67 * point trying to exceed the sample rate of the output device though as the
68 * signal will only get downsampled, consuming CPU on the machine running the
69 * server.
70 *
71 * \section chan_sec Channels
72 *
73 * PulseAudio supports up to 32 individiual channels. The order of the
74 * channels is up to the application, but they must be continous. To map
75 * channels to speakers, see \ref channelmap.
76 *
77 * \section calc_sec Calculations
78 *
79 * The PulseAudio library contains a number of convenience functions to do
80 * calculations on sample formats:
81 *
82 * \li pa_bytes_per_second() - The number of bytes one second of audio will
83 * take given a sample format.
84 * \li pa_frame_size() - The size, in bytes, of one frame (i.e. one set of
85 * samples, one for each channel).
86 * \li pa_sample_size() - The size, in bytes, of one sample.
87 * \li pa_bytes_to_usec() - Calculate the time it would take to play a buffer
88 * of a certain size.
89 *
90 * \section util_sec Convenience Functions
91 *
92 * The library also contains a couple of other convenience functions:
93 *
94 * \li pa_sample_spec_valid() - Tests if a sample format specification is
95 * valid.
96 * \li pa_sample_spec_equal() - Tests if the sample format specifications are
97 * identical.
98 * \li pa_sample_format_to_string() - Return a textual description of a
99 * sample format.
100 * \li pa_parse_sample_format() - Parse a text string into a sample format.
101 * \li pa_sample_spec_snprint() - Create a textual description of a complete
102 * sample format specification.
103 * \li pa_bytes_snprint() - Pretty print a byte value (e.g. 2.5 MiB).
104 */
105
106 /** \file
107 * Constants and routines for sample type handling */
108
109 PA_C_DECL_BEGIN
110
111 #if !defined(WORDS_BIGENDIAN)
112 #if defined(__BYTE_ORDER)
113 #if __BYTE_ORDER == __BIG_ENDIAN
114 #define WORDS_BIGENDIAN
115 #endif
116 #endif
117 #endif
118
119 /** Maximum number of allowed channels */
120 #define PA_CHANNELS_MAX 32U
121
122 /** Maximum allowed sample rate */
123 #define PA_RATE_MAX (48000U*4U)
124
125 /** Sample format */
126 typedef enum pa_sample_format {
127 PA_SAMPLE_U8,
128 /**< Unsigned 8 Bit PCM */
129
130 PA_SAMPLE_ALAW,
131 /**< 8 Bit a-Law */
132
133 PA_SAMPLE_ULAW,
134 /**< 8 Bit mu-Law */
135
136 PA_SAMPLE_S16LE,
137 /**< Signed 16 Bit PCM, little endian (PC) */
138
139 PA_SAMPLE_S16BE,
140 /**< Signed 16 Bit PCM, big endian */
141
142 PA_SAMPLE_FLOAT32LE,
143 /**< 32 Bit IEEE floating point, little endian (PC), range -1.0 to 1.0 */
144
145 PA_SAMPLE_FLOAT32BE,
146 /**< 32 Bit IEEE floating point, big endian, range -1.0 to 1.0 */
147
148 PA_SAMPLE_S32LE,
149 /**< Signed 32 Bit PCM, little endian (PC) */
150
151 PA_SAMPLE_S32BE,
152 /**< Signed 32 Bit PCM, big endian */
153
154 PA_SAMPLE_S24LE,
155 /**< Signed 24 Bit PCM packed, little endian (PC). \since 0.9.15 */
156
157 PA_SAMPLE_S24BE,
158 /**< Signed 24 Bit PCM packed, big endian. \since 0.9.15 */
159
160 PA_SAMPLE_S24_32LE,
161 /**< Signed 24 Bit PCM in LSB of 32 Bit words, little endian (PC). \since 0.9.15 */
162
163 PA_SAMPLE_S24_32BE,
164 /**< Signed 24 Bit PCM in LSB of 32 Bit words, big endian. \since 0.9.15 */
165
166 PA_SAMPLE_MAX,
167 /**< Upper limit of valid sample types */
168
169 PA_SAMPLE_INVALID = -1
170 /**< An invalid value */
171 } pa_sample_format_t;
172
173 #ifdef WORDS_BIGENDIAN
174 /** Signed 16 Bit PCM, native endian */
175 #define PA_SAMPLE_S16NE PA_SAMPLE_S16BE
176 /** 32 Bit IEEE floating point, native endian */
177 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32BE
178 /** Signed 32 Bit PCM, native endian */
179 #define PA_SAMPLE_S32NE PA_SAMPLE_S32BE
180 /** Signed 24 Bit PCM packed, native endian. \since 0.9.15 */
181 #define PA_SAMPLE_S24NE PA_SAMPLE_S24BE
182 /** Signed 24 Bit PCM in LSB of 32 Bit words, native endian. \since 0.9.15 */
183 #define PA_SAMPLE_S24_32NE PA_SAMPLE_S24_32BE
184
185 /** Signed 16 Bit PCM reverse endian */
186 #define PA_SAMPLE_S16RE PA_SAMPLE_S16LE
187 /** 32 Bit IEEE floating point, reverse endian */
188 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32LE
189 /** Signed 32 Bit PCM, reverse endian */
190 #define PA_SAMPLE_S32RE PA_SAMPLE_S32LE
191 /** Signed 24 Bit PCM, packed reverse endian. \since 0.9.15 */
192 #define PA_SAMPLE_S24RE PA_SAMPLE_S24LE
193 /** Signed 24 Bit PCM, in LSB of 32 Bit words, reverse endian. \since 0.9.15 */
194 #define PA_SAMPLE_S24_32RE PA_SAMPLE_S24_32LE
195 #else
196 /** Signed 16 Bit PCM, native endian */
197 #define PA_SAMPLE_S16NE PA_SAMPLE_S16LE
198 /** 32 Bit IEEE floating point, native endian */
199 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32LE
200 /** Signed 32 Bit PCM, native endian */
201 #define PA_SAMPLE_S32NE PA_SAMPLE_S32LE
202 /** Signed 24 Bit PCM packed, native endian. \since 0.9.15 */
203 #define PA_SAMPLE_S24NE PA_SAMPLE_S24LE
204 /** Signed 24 Bit PCM in LSB of 32 Bit words, native endian. \since 0.9.15 */
205 #define PA_SAMPLE_S24_32NE PA_SAMPLE_S24_32LE
206
207 /** Signed 16 Bit PCM, reverse endian */
208 #define PA_SAMPLE_S16RE PA_SAMPLE_S16BE
209 /** 32 Bit IEEE floating point, reverse endian */
210 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32BE
211 /** Signed 32 Bit PCM, reverse endian */
212 #define PA_SAMPLE_S32RE PA_SAMPLE_S32BE
213 /** Signed 24 Bit PCM, packed reverse endian. \since 0.9.15 */
214 #define PA_SAMPLE_S24RE PA_SAMPLE_S24BE
215 /** Signed 24 Bit PCM, in LSB of 32 Bit words, reverse endian. \since 0.9.15 */
216 #define PA_SAMPLE_S24_32RE PA_SAMPLE_S24_32BE
217 #endif
218
219 /** A Shortcut for PA_SAMPLE_FLOAT32NE */
220 #define PA_SAMPLE_FLOAT32 PA_SAMPLE_FLOAT32NE
221
222 /** \cond fulldocs */
223 /* Allow clients to check with #ifdef for thse sample formats */
224 #define PA_SAMPLE_U8 PA_SAMPLE_U8
225 #define PA_SAMPLE_ALAW PA_SAMPLE_ALAW
226 #define PA_SAMPLE_ULAW PA_SAMPLE_ULAW
227 #define PA_SAMPLE_S16LE PA_SAMPLE_S16LE
228 #define PA_SAMPLE_S16BE PA_SAMPLE_S16BE
229 #define PA_SAMPLE_FLOAT32LE PA_SAMPLE_FLOAT32LE
230 #define PA_SAMPLE_FLOAT32BE PA_SAMPLE_FLOAT32BE
231 #define PA_SAMPLE_S32LE PA_SAMPLE_S32LE
232 #define PA_SAMPLE_S32BE PA_SAMPLE_S32BE
233 #define PA_SAMPLE_S24LE PA_SAMPLE_S24LE
234 #define PA_SAMPLE_S24BE PA_SAMPLE_S24BE
235 #define PA_SAMPLE_S24_32LE PA_SAMPLE_S24_32LE
236 #define PA_SAMPLE_S24_32BE PA_SAMPLE_S24_32BE
237 /** \endcond */
238
239 /** A sample format and attribute specification */
240 typedef struct pa_sample_spec {
241 pa_sample_format_t format;
242 /**< The sample format */
243
244 uint32_t rate;
245 /**< The sample rate. (e.g. 44100) */
246
247 uint8_t channels;
248 /**< Audio channels. (1 for mono, 2 for stereo, ...) */
249 } pa_sample_spec;
250
251 /** Type for usec specifications (unsigned). Always 64 bit. */
252 typedef uint64_t pa_usec_t;
253
254 /** Return the amount of bytes playback of a second of audio with the specified sample type takes */
255 size_t pa_bytes_per_second(const pa_sample_spec *spec) PA_GCC_PURE;
256
257 /** Return the size of a frame with the specific sample type */
258 size_t pa_frame_size(const pa_sample_spec *spec) PA_GCC_PURE;
259
260 /** Return the size of a sample with the specific sample type */
261 size_t pa_sample_size(const pa_sample_spec *spec) PA_GCC_PURE;
262
263 /** Calculate the time the specified bytes take to play with the
264 * specified sample type. The return value will always be rounded
265 * down for non-integral return values. */
266 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) PA_GCC_PURE;
267
268 /** Calculates the number of bytes that are required for the specified
269 * time. The return value will always be rounded down for non-integral
270 * return values. \since 0.9 */
271 size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) PA_GCC_PURE;
272
273 /** Initialize the specified sample spec and return a pointer to
274 * it. The sample spec will have a defined state but
275 * pa_sample_spec_valid() will fail for it. \since 0.9.13 */
276 pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec);
277
278 /** Return non-zero when the sample type specification is valid */
279 int pa_sample_spec_valid(const pa_sample_spec *spec) PA_GCC_PURE;
280
281 /** Return non-zero when the two sample type specifications match */
282 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) PA_GCC_PURE;
283
284 /** Return a descriptive string for the specified sample format. \since 0.8 */
285 const char *pa_sample_format_to_string(pa_sample_format_t f) PA_GCC_PURE;
286
287 /** Parse a sample format text. Inverse of pa_sample_format_to_string() */
288 pa_sample_format_t pa_parse_sample_format(const char *format) PA_GCC_PURE;
289
290 /** Maximum required string length for
291 * pa_sample_spec_snprint(). Please note that this value can change
292 * with any release without warning and without being considered API
293 * or ABI breakage. You should not use this definition anywhere where
294 * it might become part of an ABI. */
295 #define PA_SAMPLE_SPEC_SNPRINT_MAX 32
296
297 /** Pretty print a sample type specification to a string */
298 char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec);
299
300 /** Pretty print a byte size value. (i.e. "2.5 MiB") */
301 char* pa_bytes_snprint(char *s, size_t l, unsigned v);
302
303 PA_C_DECL_END
304
305 #endif