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