]> code.delx.au - pulseaudio/blob - src/polyp/sample.h
0706be5ae666f0215ebb5d59161b747fc6241648
[pulseaudio] / src / polyp / sample.h
1 #ifndef foosamplehfoo
2 #define foosamplehfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of polypaudio.
8
9 polypaudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 polypaudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with polypaudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #include <inttypes.h>
26 #include <sys/types.h>
27 #include <math.h>
28
29 #include <polyp/cdecl.h>
30
31 /** \page sample Sample format specifications
32 *
33 * \section overv_sec Overview
34 *
35 * Polypaudio is capable of handling a multitude of sample formats, rates
36 * and channels, transparently converting and mixing them as needed.
37 *
38 * \section format_sec Sample format
39 *
40 * Polypaudio supports the following sample formats:
41 *
42 * \li PA_SAMPLE_U8 - Unsigned 8 bit PCM.
43 * \li PA_SAMPLE_S16LE - Signed 16 bit PCM, little endian.
44 * \li PA_SAMPLE_S16BE - Signed 16 bit PCM, big endian.
45 * \li PA_SAMPLE_FLOAT32LE - 32 bit IEEE floating point PCM, little endian.
46 * \li PA_SAMPLE_FLOAT32BE - 32 bit IEEE floating point PCM, big endian.
47 * \li PA_SAMPLE_ALAW - 8 bit a-Law.
48 * \li PA_SAMPLE_ULAW - 8 bit mu-Law.
49 *
50 * The floating point sample formats have the range from -1 to 1.
51 *
52 * The sample formats that are sensitive to endianness have convenience
53 * macros for native endian (NE), and reverse endian (RE).
54 *
55 * \section rate_sec Sample rates
56 *
57 * Polypaudio supports any sample rate between 1 Hz and 4 GHz. There is no
58 * point trying to exceed the sample rate of the output device though as the
59 * signal will only get downsampled, consuming CPU on the machine running the
60 * server.
61 *
62 * \section chan_sec Channels
63 *
64 * Polypaudio supports up to 16 individiual channels. The order of the
65 * channels is up to the application, but they must be continous. To map
66 * channels to speakers, see \ref channelmap.
67 *
68 * \section calc_sec Calculations
69 *
70 * The Polypaudio library contains a number of convenience functions to do
71 * calculations on sample formats:
72 *
73 * \li pa_bytes_per_second() - The number of bytes one second of audio will
74 * take given a sample format.
75 * \li pa_frame_size() - The size, in bytes, of one frame (i.e. one set of
76 * samples, one for each channel).
77 * \li pa_sample_size() - The size, in bytes, of one sample.
78 * \li pa_bytes_to_usec() - Calculate the time it would take to play a buffer
79 * of a certain size.
80 *
81 * \section util_sec Convenience functions
82 *
83 * The library also contains a couple of other convenience functions:
84 *
85 * \li pa_sample_spec_valid() - Tests if a sample format specification is
86 * valid.
87 * \li pa_sample_spec_equal() - Tests if the sample format specifications are
88 * identical.
89 * \li pa_sample_format_to_string() - Return a textual description of a
90 * sample format.
91 * \li pa_parse_sample_format() - Parse a text string into a sample format.
92 * \li pa_sample_spec_snprint() - Create a textual description of a complete
93 * sample format specification.
94 * \li pa_bytes_snprint() - Pretty print a byte value (e.g. 2.5 MiB).
95 */
96
97 /** \file
98 * Constants and routines for sample type handling */
99
100 PA_C_DECL_BEGIN
101
102 /** Maximum number of allowed channels */
103 #define PA_CHANNELS_MAX 16
104
105 /** Sample format */
106 typedef enum pa_sample_format {
107 PA_SAMPLE_U8, /**< Unsigned 8 Bit PCM */
108 PA_SAMPLE_ALAW, /**< 8 Bit a-Law */
109 PA_SAMPLE_ULAW, /**< 8 Bit mu-Law */
110 PA_SAMPLE_S16LE, /**< Signed 16 Bit PCM, little endian (PC) */
111 PA_SAMPLE_S16BE, /**< Signed 16 Bit PCM, big endian */
112 PA_SAMPLE_FLOAT32LE, /**< 32 Bit IEEE floating point, little endian, range -1 to 1 */
113 PA_SAMPLE_FLOAT32BE, /**< 32 Bit IEEE floating point, big endian, range -1 to 1 */
114 PA_SAMPLE_MAX, /**< Upper limit of valid sample types */
115 PA_SAMPLE_INVALID = -1 /**< An invalid value */
116 } pa_sample_format_t;
117
118 #ifdef WORDS_BIGENDIAN
119 /** Signed 16 Bit PCM, native endian */
120 #define PA_SAMPLE_S16NE PA_SAMPLE_S16BE
121 /** 32 Bit IEEE floating point, native endian */
122 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32BE
123 /** Signed 16 Bit PCM reverse endian */
124 #define PA_SAMPLE_S16RE PA_SAMPLE_S16LE
125 /** 32 Bit IEEE floating point, reverse endian */
126 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32LE
127 #else
128 /** Signed 16 Bit PCM, native endian */
129 #define PA_SAMPLE_S16NE PA_SAMPLE_S16LE
130 /** 32 Bit IEEE floating point, native endian */
131 #define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32LE
132 /** Signed 16 Bit PCM reverse endian */
133 #define PA_SAMPLE_S16RE PA_SAMPLE_S16BE
134 /** 32 Bit IEEE floating point, reverse endian */
135 #define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32BE
136 #endif
137
138 /** A Shortcut for PA_SAMPLE_FLOAT32NE */
139 #define PA_SAMPLE_FLOAT32 PA_SAMPLE_FLOAT32NE
140
141 /** A sample format and attribute specification */
142 typedef struct pa_sample_spec {
143 pa_sample_format_t format; /**< The sample format */
144 uint32_t rate; /**< The sample rate. (e.g. 44100) */
145 uint8_t channels; /**< Audio channels. (1 for mono, 2 for stereo, ...) */
146 } pa_sample_spec;
147
148 /** Type for usec specifications (unsigned). May be either 32 or 64 bit, depending on the architecture */
149 typedef uint64_t pa_usec_t;
150
151 /** Return the amount of bytes playback of a second of audio with the specified sample type takes */
152 size_t pa_bytes_per_second(const pa_sample_spec *spec);
153
154 /** Return the size of a frame with the specific sample type */
155 size_t pa_frame_size(const pa_sample_spec *spec);
156
157 /** Return the size of a sample with the specific sample type */
158 size_t pa_sample_size(const pa_sample_spec *spec);
159
160 /** Calculate the time the specified bytes take to play with the specified sample type */
161 pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec);
162
163 /** Return non-zero when the sample type specification is valid */
164 int pa_sample_spec_valid(const pa_sample_spec *spec);
165
166 /** Return non-zero when the two sample type specifications match */
167 int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b);
168
169 /** Return a descriptive string for the specified sample format. \since 0.8 */
170 const char *pa_sample_format_to_string(pa_sample_format_t f);
171
172 /** Parse a sample format text. Inverse of pa_sample_format_to_string() */
173 pa_sample_format_t pa_parse_sample_format(const char *format);
174
175 /** Maximum required string length for pa_sample_spec_snprint() */
176 #define PA_SAMPLE_SPEC_SNPRINT_MAX 32
177
178 /** Pretty print a sample type specification to a string */
179 char* pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec);
180
181 /** Pretty print a byte size value. (i.e. "2.5 MiB") */
182 void pa_bytes_snprint(char *s, size_t l, unsigned v);
183
184 PA_C_DECL_END
185
186 #endif