merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / src / pulse / sample.h
1 #ifndef foosamplehfoo
2 #define foosamplehfoo
3
4 /* $Id$ */
5
6 /***
7   This file is part of PulseAudio.
8
9   Copyright 2004-2006 Lennart Poettering
10   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
11
12   PulseAudio is free software; you can redistribute it and/or modify
13   it under the terms of the GNU Lesser General Public License as published
14   by the Free Software Foundation; either version 2 of the License,
15   or (at your option) any later version.
16
17   PulseAudio is distributed in the hope that it will be useful, but
18   WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20   General Public License for more details.
21
22   You should have received a copy of the GNU Lesser General Public License
23   along with PulseAudio; if not, write to the Free Software
24   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25   USA.
26 ***/
27
28 #include <inttypes.h>
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <math.h>
32
33 #include <pulse/gccmacro.h>
34 #include <pulse/cdecl.h>
35
36 /** \page sample Sample Format Specifications
37  *
38  * \section overv_sec Overview
39  *
40  * PulseAudio is capable of handling a multitude of sample formats, rates
41  * and channels, transparently converting and mixing them as needed.
42  *
43  * \section format_sec Sample Format
44  *
45  * PulseAudio supports the following sample formats:
46  *
47  * \li PA_SAMPLE_U8 - Unsigned 8 bit integer PCM.
48  * \li PA_SAMPLE_S16LE - Signed 16 integer bit PCM, little endian.
49  * \li PA_SAMPLE_S16BE - Signed 16 integer bit PCM, big endian.
50  * \li PA_SAMPLE_FLOAT32LE - 32 bit IEEE floating point PCM, little endian.
51  * \li PA_SAMPLE_FLOAT32BE - 32 bit IEEE floating point PCM, big endian.
52  * \li PA_SAMPLE_ALAW - 8 bit a-Law.
53  * \li PA_SAMPLE_ULAW - 8 bit mu-Law.
54  * \li PA_SAMPLE_S32LE - Signed 32 bit integer PCM, little endian.
55  * \li PA_SAMPLE_S32BE - Signed 32 bit integer PCM, big endian.
56  *
57  * The floating point sample formats have the range from -1 to 1.
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 32
119
120 /** Maximum allowed sample rate */
121 #define PA_RATE_MAX (48000*4)
122
123 /** Sample format */
124 typedef enum pa_sample_format {
125     PA_SAMPLE_U8,              /**< Unsigned 8 Bit PCM */
126     PA_SAMPLE_ALAW,            /**< 8 Bit a-Law */
127     PA_SAMPLE_ULAW,            /**< 8 Bit mu-Law */
128     PA_SAMPLE_S16LE,           /**< Signed 16 Bit PCM, little endian (PC) */
129     PA_SAMPLE_S16BE,           /**< Signed 16 Bit PCM, big endian */
130     PA_SAMPLE_FLOAT32LE,       /**< 32 Bit IEEE floating point, little endian, range -1 to 1 */
131     PA_SAMPLE_FLOAT32BE,       /**< 32 Bit IEEE floating point, big endian, range -1 to 1 */
132     PA_SAMPLE_S32LE,           /**< Signed 32 Bit PCM, little endian (PC) */
133     PA_SAMPLE_S32BE,           /**< Signed 32 Bit PCM, big endian (PC) */
134     PA_SAMPLE_MAX,             /**< Upper limit of valid sample types */
135     PA_SAMPLE_INVALID = -1     /**< An invalid value */
136 } pa_sample_format_t;
137
138 #ifdef WORDS_BIGENDIAN
139 /** Signed 16 Bit PCM, native endian */
140 #define PA_SAMPLE_S16NE PA_SAMPLE_S16BE
141 /** 32 Bit IEEE floating point, native endian */
142 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32BE
143 /** Signed 32 Bit PCM, native endian */
144 #define PA_SAMPLE_S32NE PA_SAMPLE_S32BE
145 /** Signed 16 Bit PCM reverse endian */
146 #define PA_SAMPLE_S16RE PA_SAMPLE_S16LE
147 /** 32 Bit IEEE floating point, reverse endian */
148 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32LE
149 /** Signed 32 Bit PCM reverse endian */
150 #define PA_SAMPLE_S32RE PA_SAMPLE_S32LE
151 #else
152 /** Signed 16 Bit PCM, native endian */
153 #define PA_SAMPLE_S16NE PA_SAMPLE_S16LE
154 /** 32 Bit IEEE floating point, native endian */
155 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32LE
156 /** Signed 32 Bit PCM, native endian */
157 #define PA_SAMPLE_S32NE PA_SAMPLE_S32LE
158 /** Signed 16 Bit PCM reverse endian */
159 #define PA_SAMPLE_S16RE PA_SAMPLE_S16BE
160 /** 32 Bit IEEE floating point, reverse endian */
161 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32BE
162 /** Signed 32 Bit PCM reverse endian */
163 #define PA_SAMPLE_S32RE PA_SAMPLE_S32BE
164 #endif
165
166 /** A Shortcut for PA_SAMPLE_FLOAT32NE */
167 #define PA_SAMPLE_FLOAT32 PA_SAMPLE_FLOAT32NE
168
169 /** A sample format and attribute specification */
170 typedef struct pa_sample_spec {
171     pa_sample_format_t format;     /**< The sample format */
172     uint32_t rate;                 /**< The sample rate. (e.g. 44100) */
173     uint8_t channels;              /**< Audio channels. (1 for mono, 2 for stereo, ...) */
174 } pa_sample_spec;
175
176 /** Type for usec specifications (unsigned). Always 64 bit. */
177 typedef uint64_t pa_usec_t;
178
179 /** Return the amount of bytes playback of a second of audio with the specified sample type takes */
180 size_t pa_bytes_per_second(const pa_sample_spec *spec) PA_GCC_PURE;
181
182 /** Return the size of a frame with the specific sample type */
183 size_t pa_frame_size(const pa_sample_spec *spec) PA_GCC_PURE;
184
185 /** Return the size of a sample with the specific sample type */
186 size_t pa_sample_size(const pa_sample_spec *spec) PA_GCC_PURE;
187
188 /** Calculate the time the specified bytes take to play with the specified sample type */
189 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) PA_GCC_PURE;
190
191 /** Calculates the number of bytes that are required for the specified time. \since 0.9 */
192 size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) PA_GCC_PURE;
193
194 /** Return non-zero when the sample type specification is valid */
195 int pa_sample_spec_valid(const pa_sample_spec *spec) PA_GCC_PURE;
196
197 /** Return non-zero when the two sample type specifications match */
198 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) PA_GCC_PURE;
199
200 /** Return a descriptive string for the specified sample format. \since 0.8 */
201 const char *pa_sample_format_to_string(pa_sample_format_t f) PA_GCC_PURE;
202
203 /** Parse a sample format text. Inverse of pa_sample_format_to_string() */
204 pa_sample_format_t pa_parse_sample_format(const char *format) PA_GCC_PURE;
205
206 /** Maximum required string length for pa_sample_spec_snprint() */
207 #define PA_SAMPLE_SPEC_SNPRINT_MAX 32
208
209 /** Pretty print a sample type specification to a string */
210 char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec);
211
212 /** Pretty print a byte size value. (i.e. "2.5 MiB") */
213 char* pa_bytes_snprint(char *s, size_t l, unsigned v);
214
215 PA_C_DECL_END
216
217 #endif