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