]> code.delx.au - pulseaudio/blob - src/pulse/sample.h
Merge branch 'master' of git://git.0pointer.de/pulseaudio
[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 *
55 * The floating point sample formats have the range from -1.0 to 1.0.
56 *
57 * The sample formats that are sensitive to endianness have convenience
58 * macros for native endian (NE), and reverse endian (RE).
59 *
60 * \section rate_sec Sample Rates
61 *
62 * PulseAudio supports any sample rate between 1 Hz and 4 GHz. There is no
63 * point trying to exceed the sample rate of the output device though as the
64 * signal will only get downsampled, consuming CPU on the machine running the
65 * server.
66 *
67 * \section chan_sec Channels
68 *
69 * PulseAudio supports up to 16 individiual channels. The order of the
70 * channels is up to the application, but they must be continous. To map
71 * channels to speakers, see \ref channelmap.
72 *
73 * \section calc_sec Calculations
74 *
75 * The PulseAudio library contains a number of convenience functions to do
76 * calculations on sample formats:
77 *
78 * \li pa_bytes_per_second() - The number of bytes one second of audio will
79 * take given a sample format.
80 * \li pa_frame_size() - The size, in bytes, of one frame (i.e. one set of
81 * samples, one for each channel).
82 * \li pa_sample_size() - The size, in bytes, of one sample.
83 * \li pa_bytes_to_usec() - Calculate the time it would take to play a buffer
84 * of a certain size.
85 *
86 * \section util_sec Convenience Functions
87 *
88 * The library also contains a couple of other convenience functions:
89 *
90 * \li pa_sample_spec_valid() - Tests if a sample format specification is
91 * valid.
92 * \li pa_sample_spec_equal() - Tests if the sample format specifications are
93 * identical.
94 * \li pa_sample_format_to_string() - Return a textual description of a
95 * sample format.
96 * \li pa_parse_sample_format() - Parse a text string into a sample format.
97 * \li pa_sample_spec_snprint() - Create a textual description of a complete
98 * sample format specification.
99 * \li pa_bytes_snprint() - Pretty print a byte value (e.g. 2.5 MiB).
100 */
101
102 /** \file
103 * Constants and routines for sample type handling */
104
105 PA_C_DECL_BEGIN
106
107 #if !defined(WORDS_BIGENDIAN)
108 #if defined(__BYTE_ORDER)
109 #if __BYTE_ORDER == __BIG_ENDIAN
110 #define WORDS_BIGENDIAN
111 #endif
112 #endif
113 #endif
114
115 /** Maximum number of allowed channels */
116 #define PA_CHANNELS_MAX 32U
117
118 /** Maximum allowed sample rate */
119 #define PA_RATE_MAX (48000U*4U)
120
121 /** Sample format */
122 typedef enum pa_sample_format {
123 PA_SAMPLE_U8, /**< Unsigned 8 Bit PCM */
124 PA_SAMPLE_ALAW, /**< 8 Bit a-Law */
125 PA_SAMPLE_ULAW, /**< 8 Bit mu-Law */
126 PA_SAMPLE_S16LE, /**< Signed 16 Bit PCM, little endian (PC) */
127 PA_SAMPLE_S16BE, /**< Signed 16 Bit PCM, big endian */
128 PA_SAMPLE_FLOAT32LE, /**< 32 Bit IEEE floating point, little endian, range -1 to 1 */
129 PA_SAMPLE_FLOAT32BE, /**< 32 Bit IEEE floating point, big endian, range -1 to 1 */
130 PA_SAMPLE_S32LE, /**< Signed 32 Bit PCM, little endian (PC) */
131 PA_SAMPLE_S32BE, /**< Signed 32 Bit PCM, big endian (PC) */
132 PA_SAMPLE_MAX, /**< Upper limit of valid sample types */
133 PA_SAMPLE_INVALID = -1 /**< An invalid value */
134 } pa_sample_format_t;
135
136 #ifdef WORDS_BIGENDIAN
137 /** Signed 16 Bit PCM, native endian */
138 #define PA_SAMPLE_S16NE PA_SAMPLE_S16BE
139 /** 32 Bit IEEE floating point, native endian */
140 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32BE
141 /** Signed 32 Bit PCM, native endian */
142 #define PA_SAMPLE_S32NE PA_SAMPLE_S32BE
143 /** Signed 16 Bit PCM reverse endian */
144 #define PA_SAMPLE_S16RE PA_SAMPLE_S16LE
145 /** 32 Bit IEEE floating point, reverse endian */
146 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32LE
147 /** Signed 32 Bit PCM reverse endian */
148 #define PA_SAMPLE_S32RE PA_SAMPLE_S32LE
149 #else
150 /** Signed 16 Bit PCM, native endian */
151 #define PA_SAMPLE_S16NE PA_SAMPLE_S16LE
152 /** 32 Bit IEEE floating point, native endian */
153 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32LE
154 /** Signed 32 Bit PCM, native endian */
155 #define PA_SAMPLE_S32NE PA_SAMPLE_S32LE
156 /** Signed 16 Bit PCM reverse endian */
157 #define PA_SAMPLE_S16RE PA_SAMPLE_S16BE
158 /** 32 Bit IEEE floating point, reverse endian */
159 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32BE
160 /** Signed 32 Bit PCM reverse endian */
161 #define PA_SAMPLE_S32RE PA_SAMPLE_S32BE
162 #endif
163
164 /** A Shortcut for PA_SAMPLE_FLOAT32NE */
165 #define PA_SAMPLE_FLOAT32 PA_SAMPLE_FLOAT32NE
166
167 /** A sample format and attribute specification */
168 typedef struct pa_sample_spec {
169 pa_sample_format_t format; /**< The sample format */
170 uint32_t rate; /**< The sample rate. (e.g. 44100) */
171 uint8_t channels; /**< Audio channels. (1 for mono, 2 for stereo, ...) */
172 } pa_sample_spec;
173
174 /** Type for usec specifications (unsigned). Always 64 bit. */
175 typedef uint64_t pa_usec_t;
176
177 /** Return the amount of bytes playback of a second of audio with the specified sample type takes */
178 size_t pa_bytes_per_second(const pa_sample_spec *spec) PA_GCC_PURE;
179
180 /** Return the size of a frame with the specific sample type */
181 size_t pa_frame_size(const pa_sample_spec *spec) PA_GCC_PURE;
182
183 /** Return the size of a sample with the specific sample type */
184 size_t pa_sample_size(const pa_sample_spec *spec) PA_GCC_PURE;
185
186 /** Calculate the time the specified bytes take to play with the specified sample type */
187 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) PA_GCC_PURE;
188
189 /** Calculates the number of bytes that are required for the specified time. \since 0.9 */
190 size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) PA_GCC_PURE;
191
192 /** Return non-zero when the sample type specification is valid */
193 int pa_sample_spec_valid(const pa_sample_spec *spec) PA_GCC_PURE;
194
195 /** Return non-zero when the two sample type specifications match */
196 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) PA_GCC_PURE;
197
198 /** Return a descriptive string for the specified sample format. \since 0.8 */
199 const char *pa_sample_format_to_string(pa_sample_format_t f) PA_GCC_PURE;
200
201 /** Parse a sample format text. Inverse of pa_sample_format_to_string() */
202 pa_sample_format_t pa_parse_sample_format(const char *format) PA_GCC_PURE;
203
204 /** Maximum required string length for pa_sample_spec_snprint() */
205 #define PA_SAMPLE_SPEC_SNPRINT_MAX 32
206
207 /** Pretty print a sample type specification to a string */
208 char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec);
209
210 /** Pretty print a byte size value. (i.e. "2.5 MiB") */
211 char* pa_bytes_snprint(char *s, size_t l, unsigned v);
212
213 PA_C_DECL_END
214
215 #endif