1 | /*
|
---|
2 | * Copyright (c) 2012 Jan Vesely
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup audio
|
---|
30 | * @brief HelenOS sound server
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <byteorder.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <macros.h>
|
---|
40 | #include <stdio.h>
|
---|
41 |
|
---|
42 | #include "format.h"
|
---|
43 |
|
---|
44 | #define uint8_t_le2host(x) (x)
|
---|
45 | #define host2uint8_t_le(x) (x)
|
---|
46 | #define uint8_t_be2host(x) (x)
|
---|
47 | #define host2uint8_t_be(x) (x)
|
---|
48 |
|
---|
49 | #define int8_t_le2host(x) (x)
|
---|
50 | #define host2int8_t_le(x) (x)
|
---|
51 |
|
---|
52 | #define int16_t_le2host(x) uint16_t_le2host(x)
|
---|
53 | #define host2int16_t_le(x) host2uint16_t_le(x)
|
---|
54 |
|
---|
55 | #define int32_t_le2host(x) uint32_t_le2host(x)
|
---|
56 | #define host2int32_t_le(x) host2uint32_t_le(x)
|
---|
57 |
|
---|
58 | #define int8_t_be2host(x) (x)
|
---|
59 | #define host2int8_t_be(x) (x)
|
---|
60 |
|
---|
61 | #define int16_t_be2host(x) uint16_t_be2host(x)
|
---|
62 | #define host2int16_t_be(x) host2uint16_t_be(x)
|
---|
63 |
|
---|
64 | #define int32_t_be2host(x) uint32_t_be2host(x)
|
---|
65 | #define host2int32_t_be(x) host2uint32_t_be(x)
|
---|
66 |
|
---|
67 | // TODO float endian?
|
---|
68 | #define float_le2host(x) (x)
|
---|
69 | #define float_be2host(x) (x)
|
---|
70 |
|
---|
71 | #define host2float_le(x) (x)
|
---|
72 | #define host2float_be(x) (x)
|
---|
73 |
|
---|
74 | #define from(x, type, endian) (float)(type ## _ ## endian ## 2host(x))
|
---|
75 | #define to(x, type, endian) (float)(host2 ## type ## _ ## endian(x))
|
---|
76 |
|
---|
77 | const pcm_format_t AUDIO_FORMAT_DEFAULT = {
|
---|
78 | .channels = 2,
|
---|
79 | .sampling_rate = 44100,
|
---|
80 | .sample_format = PCM_SAMPLE_SINT16_LE,
|
---|
81 | };
|
---|
82 |
|
---|
83 | const pcm_format_t AUDIO_FORMAT_ANY = {
|
---|
84 | .channels = 0,
|
---|
85 | .sampling_rate = 0,
|
---|
86 | .sample_format = 0,
|
---|
87 | };
|
---|
88 |
|
---|
89 | static float get_normalized_sample(const void *buffer, size_t size,
|
---|
90 | unsigned frame, unsigned channel, const pcm_format_t *f);
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Compare PCM format attribtues.
|
---|
94 | * @param a Format description.
|
---|
95 | * @param b Format description.
|
---|
96 | * @return True if a and b describe the same format, false otherwise.
|
---|
97 | */
|
---|
98 | bool pcm_format_same(const pcm_format_t *a, const pcm_format_t* b)
|
---|
99 | {
|
---|
100 | assert(a);
|
---|
101 | assert(b);
|
---|
102 | return
|
---|
103 | a->sampling_rate == b->sampling_rate &&
|
---|
104 | a->channels == b->channels &&
|
---|
105 | a->sample_format == b->sample_format;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Fill audio buffer with silence in the specified format.
|
---|
110 | * @param dst Destination audio buffer.
|
---|
111 | * @param size Size of the destination audio buffer.
|
---|
112 | * @param f Pointer to the format description.
|
---|
113 | */
|
---|
114 | void pcm_format_silence(void *dst, size_t size, const pcm_format_t *f)
|
---|
115 | {
|
---|
116 | #define SET_NULL(type, endian, nullv) \
|
---|
117 | do { \
|
---|
118 | type *buffer = dst; \
|
---|
119 | const size_t sample_count = size / sizeof(type); \
|
---|
120 | for (unsigned i = 0; i < sample_count; ++i) { \
|
---|
121 | buffer[i] = to((type)nullv, type, endian); \
|
---|
122 | } \
|
---|
123 | } while (0)
|
---|
124 |
|
---|
125 | switch (f->sample_format) {
|
---|
126 | case PCM_SAMPLE_UINT8:
|
---|
127 | SET_NULL(uint8_t, le, INT8_MIN); break;
|
---|
128 | case PCM_SAMPLE_SINT8:
|
---|
129 | SET_NULL(int8_t, le, 0); break;
|
---|
130 | case PCM_SAMPLE_UINT16_LE:
|
---|
131 | SET_NULL(uint16_t, le, INT16_MIN); break;
|
---|
132 | case PCM_SAMPLE_SINT16_LE:
|
---|
133 | SET_NULL(int16_t, le, 0); break;
|
---|
134 | case PCM_SAMPLE_UINT16_BE:
|
---|
135 | SET_NULL(uint16_t, be, INT16_MIN); break;
|
---|
136 | case PCM_SAMPLE_SINT16_BE:
|
---|
137 | SET_NULL(int16_t, be, 0); break;
|
---|
138 | case PCM_SAMPLE_UINT32_LE:
|
---|
139 | SET_NULL(uint32_t, le, INT32_MIN); break;
|
---|
140 | case PCM_SAMPLE_SINT32_LE:
|
---|
141 | SET_NULL(int32_t, le, 0); break;
|
---|
142 | case PCM_SAMPLE_UINT32_BE:
|
---|
143 | SET_NULL(uint32_t, be, INT32_MIN); break;
|
---|
144 | case PCM_SAMPLE_SINT32_BE:
|
---|
145 | SET_NULL(int32_t, le, 0); break;
|
---|
146 | case PCM_SAMPLE_UINT24_32_LE:
|
---|
147 | case PCM_SAMPLE_SINT24_32_LE:
|
---|
148 | case PCM_SAMPLE_UINT24_32_BE:
|
---|
149 | case PCM_SAMPLE_SINT24_32_BE:
|
---|
150 | case PCM_SAMPLE_UINT24_LE:
|
---|
151 | case PCM_SAMPLE_SINT24_LE:
|
---|
152 | case PCM_SAMPLE_UINT24_BE:
|
---|
153 | case PCM_SAMPLE_SINT24_BE:
|
---|
154 | case PCM_SAMPLE_FLOAT32:
|
---|
155 | default: ;
|
---|
156 | }
|
---|
157 | #undef SET_NULL
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Mix audio data of the same format and size.
|
---|
162 | * @param dst Destination buffer
|
---|
163 | * @param src Source buffer
|
---|
164 | * @param size Size of both the destination and the source buffer
|
---|
165 | * @param f Pointer to the format descriptor.
|
---|
166 | * @return Error code.
|
---|
167 | */
|
---|
168 | int pcm_format_mix(void *dst, const void *src, size_t size, const pcm_format_t *f)
|
---|
169 | {
|
---|
170 | return pcm_format_convert_and_mix(dst, size, src, size, f, f);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Add and mix audio data.
|
---|
175 | * @param dst Destination audio buffer
|
---|
176 | * @param dst_size Size of the destination buffer
|
---|
177 | * @param src Source audio buffer
|
---|
178 | * @param src_size Size of the source buffer.
|
---|
179 | * @param sf Pointer to the source format descriptor.
|
---|
180 | * @param df Pointer to the destination format descriptor.
|
---|
181 | * @return Error code.
|
---|
182 | *
|
---|
183 | * Buffers must contain entire frames. Destination buffer is always filled.
|
---|
184 | * If there are not enough data in the source buffer silent data is assumed.
|
---|
185 | */
|
---|
186 | int pcm_format_convert_and_mix(void *dst, size_t dst_size, const void *src,
|
---|
187 | size_t src_size, const pcm_format_t *sf, const pcm_format_t *df)
|
---|
188 | {
|
---|
189 | if (!dst || !src || !sf || !df)
|
---|
190 | return EINVAL;
|
---|
191 | const size_t src_frame_size = pcm_format_frame_size(sf);
|
---|
192 | if ((src_size % src_frame_size) != 0)
|
---|
193 | return EINVAL;
|
---|
194 |
|
---|
195 | const size_t dst_frame_size = pcm_format_frame_size(df);
|
---|
196 | if ((dst_size % dst_frame_size) != 0)
|
---|
197 | return EINVAL;
|
---|
198 |
|
---|
199 | /* This is so ugly it eats kittens, and puppies, and ducklings,
|
---|
200 | * and all little fluffy things...
|
---|
201 | */
|
---|
202 | #define LOOP_ADD(type, endian, low, high) \
|
---|
203 | do { \
|
---|
204 | const unsigned frame_count = dst_size / dst_frame_size; \
|
---|
205 | for (size_t i = 0; i < frame_count; ++i) { \
|
---|
206 | for (unsigned j = 0; j < df->channels; ++j) { \
|
---|
207 | const float a = \
|
---|
208 | get_normalized_sample(dst, dst_size, i, j, df);\
|
---|
209 | const float b = \
|
---|
210 | get_normalized_sample(src, src_size, i, j, sf);\
|
---|
211 | float c = (a + b); \
|
---|
212 | if (c < -1.0) c = -1.0; \
|
---|
213 | if (c > 1.0) c = 1.0; \
|
---|
214 | c += 1.0; \
|
---|
215 | c *= ((float)(type)high - (float)(type)low) / 2; \
|
---|
216 | c += (float)(type)low; \
|
---|
217 | type *dst_buf = dst; \
|
---|
218 | const unsigned pos = i * df->channels + j; \
|
---|
219 | if (pos < (dst_size / sizeof(type))) \
|
---|
220 | dst_buf[pos] = to((type)c, type, endian); \
|
---|
221 | } \
|
---|
222 | } \
|
---|
223 | } while (0)
|
---|
224 |
|
---|
225 | switch (df->sample_format) {
|
---|
226 | case PCM_SAMPLE_UINT8:
|
---|
227 | LOOP_ADD(uint8_t, le, UINT8_MIN, UINT8_MAX); break;
|
---|
228 | case PCM_SAMPLE_SINT8:
|
---|
229 | LOOP_ADD(uint8_t, le, INT8_MIN, INT8_MAX); break;
|
---|
230 | case PCM_SAMPLE_UINT16_LE:
|
---|
231 | LOOP_ADD(uint16_t, le, UINT16_MIN, UINT16_MAX); break;
|
---|
232 | case PCM_SAMPLE_SINT16_LE:
|
---|
233 | LOOP_ADD(int16_t, le, INT16_MIN, INT16_MAX); break;
|
---|
234 | case PCM_SAMPLE_UINT16_BE:
|
---|
235 | LOOP_ADD(uint16_t, be, UINT16_MIN, UINT16_MAX); break;
|
---|
236 | case PCM_SAMPLE_SINT16_BE:
|
---|
237 | LOOP_ADD(int16_t, be, INT16_MIN, INT16_MAX); break;
|
---|
238 | case PCM_SAMPLE_UINT24_32_LE:
|
---|
239 | case PCM_SAMPLE_UINT32_LE: // TODO this are not right for 24bit
|
---|
240 | LOOP_ADD(uint32_t, le, UINT32_MIN, UINT32_MAX); break;
|
---|
241 | case PCM_SAMPLE_SINT24_32_LE:
|
---|
242 | case PCM_SAMPLE_SINT32_LE:
|
---|
243 | LOOP_ADD(int32_t, le, INT32_MIN, INT32_MAX); break;
|
---|
244 | case PCM_SAMPLE_UINT24_32_BE:
|
---|
245 | case PCM_SAMPLE_UINT32_BE:
|
---|
246 | LOOP_ADD(uint32_t, be, UINT32_MIN, UINT32_MAX); break;
|
---|
247 | case PCM_SAMPLE_SINT24_32_BE:
|
---|
248 | case PCM_SAMPLE_SINT32_BE:
|
---|
249 | LOOP_ADD(int32_t, be, INT32_MIN, INT32_MAX); break;
|
---|
250 | case PCM_SAMPLE_UINT24_LE:
|
---|
251 | case PCM_SAMPLE_SINT24_LE:
|
---|
252 | case PCM_SAMPLE_UINT24_BE:
|
---|
253 | case PCM_SAMPLE_SINT24_BE:
|
---|
254 | case PCM_SAMPLE_FLOAT32:
|
---|
255 | default:
|
---|
256 | return ENOTSUP;
|
---|
257 | }
|
---|
258 | return EOK;
|
---|
259 | #undef LOOP_ADD
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Converts all sample formats to float <-1,1>
|
---|
264 | * @param buffer Audio data
|
---|
265 | * @param size Size of the buffer
|
---|
266 | * @param frame Index of the frame to read
|
---|
267 | * @param channel Channel within the frame
|
---|
268 | * @param f Pointer to a format descriptor
|
---|
269 | * @return Normalized sample <-1,1>, 0.0 if the data could not be read
|
---|
270 | */
|
---|
271 | static float get_normalized_sample(const void *buffer, size_t size,
|
---|
272 | unsigned frame, unsigned channel, const pcm_format_t *f)
|
---|
273 | {
|
---|
274 | assert(f);
|
---|
275 | assert(buffer);
|
---|
276 | if (channel >= f->channels)
|
---|
277 | return 0.0f;
|
---|
278 | #define GET(type, endian, low, high) \
|
---|
279 | do { \
|
---|
280 | const type *src = buffer; \
|
---|
281 | const size_t sample_count = size / sizeof(type); \
|
---|
282 | const size_t sample_pos = frame * f->channels + channel; \
|
---|
283 | if (sample_pos >= sample_count) {\
|
---|
284 | return 0.0f; \
|
---|
285 | } \
|
---|
286 | float sample = from(src[sample_pos], type, endian); \
|
---|
287 | /* This makes it positive */ \
|
---|
288 | sample -= (float)(type)low; \
|
---|
289 | /* This makes it <0,2> */ \
|
---|
290 | sample /= (((float)(type)high - (float)(type)low) / 2.0f); \
|
---|
291 | return sample - 1.0f; \
|
---|
292 | } while (0)
|
---|
293 |
|
---|
294 | switch (f->sample_format) {
|
---|
295 | case PCM_SAMPLE_UINT8:
|
---|
296 | GET(uint8_t, le, UINT8_MIN, UINT8_MAX);
|
---|
297 | case PCM_SAMPLE_SINT8:
|
---|
298 | GET(int8_t, le, INT8_MIN, INT8_MAX);
|
---|
299 | case PCM_SAMPLE_UINT16_LE:
|
---|
300 | GET(uint16_t, le, UINT16_MIN, UINT16_MAX);
|
---|
301 | case PCM_SAMPLE_SINT16_LE:
|
---|
302 | GET(int16_t, le, INT16_MIN, INT16_MAX);
|
---|
303 | case PCM_SAMPLE_UINT16_BE:
|
---|
304 | GET(uint16_t, be, UINT16_MIN, UINT16_MAX);
|
---|
305 | case PCM_SAMPLE_SINT16_BE:
|
---|
306 | GET(int16_t, be, INT16_MIN, INT16_MAX);
|
---|
307 | case PCM_SAMPLE_UINT24_32_LE:
|
---|
308 | case PCM_SAMPLE_UINT32_LE:
|
---|
309 | GET(uint32_t, le, UINT32_MIN, UINT32_MAX);
|
---|
310 | case PCM_SAMPLE_SINT24_32_LE:
|
---|
311 | case PCM_SAMPLE_SINT32_LE:
|
---|
312 | GET(int32_t, le, INT32_MIN, INT32_MAX);
|
---|
313 | case PCM_SAMPLE_UINT24_32_BE:
|
---|
314 | case PCM_SAMPLE_UINT32_BE:
|
---|
315 | GET(uint32_t, be, UINT32_MIN, UINT32_MAX);
|
---|
316 | case PCM_SAMPLE_SINT24_32_BE:
|
---|
317 | case PCM_SAMPLE_SINT32_BE:
|
---|
318 | GET(int32_t, le, INT32_MIN, INT32_MAX);
|
---|
319 | case PCM_SAMPLE_UINT24_LE:
|
---|
320 | case PCM_SAMPLE_SINT24_LE:
|
---|
321 | case PCM_SAMPLE_UINT24_BE:
|
---|
322 | case PCM_SAMPLE_SINT24_BE:
|
---|
323 | case PCM_SAMPLE_FLOAT32:
|
---|
324 | default: ;
|
---|
325 | }
|
---|
326 | return 0;
|
---|
327 | #undef GET
|
---|
328 | }
|
---|
329 | /**
|
---|
330 | * @}
|
---|
331 | */
|
---|