source: mainline/uspace/lib/pcm/src/format.c@ 8a7d78cc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8a7d78cc was b1dfe13, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libpcm: comments

  • Property mode set to 100644
File size: 10.2 KB
Line 
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/** Default linear PCM format */
78const pcm_format_t AUDIO_FORMAT_DEFAULT = {
79 .channels = 2,
80 .sampling_rate = 44100,
81 .sample_format = PCM_SAMPLE_SINT16_LE,
82 };
83
84/** Special ANY PCM format.
85 * This format is used if the real format is no know or important.
86 */
87const pcm_format_t AUDIO_FORMAT_ANY = {
88 .channels = 0,
89 .sampling_rate = 0,
90 .sample_format = 0,
91 };
92
93static float get_normalized_sample(const void *buffer, size_t size,
94 unsigned frame, unsigned channel, const pcm_format_t *f);
95
96/**
97 * Compare PCM format attribtues.
98 * @param a Format description.
99 * @param b Format description.
100 * @return True if a and b describe the same format, false otherwise.
101 */
102bool pcm_format_same(const pcm_format_t *a, const pcm_format_t* b)
103{
104 assert(a);
105 assert(b);
106 return
107 a->sampling_rate == b->sampling_rate &&
108 a->channels == b->channels &&
109 a->sample_format == b->sample_format;
110}
111
112/**
113 * Fill audio buffer with silence in the specified format.
114 * @param dst Destination audio buffer.
115 * @param size Size of the destination audio buffer.
116 * @param f Pointer to the format description.
117 */
118void pcm_format_silence(void *dst, size_t size, const pcm_format_t *f)
119{
120#define SET_NULL(type, endian, nullv) \
121do { \
122 type *buffer = dst; \
123 const size_t sample_count = size / sizeof(type); \
124 for (unsigned i = 0; i < sample_count; ++i) { \
125 buffer[i] = to((type)nullv, type, endian); \
126 } \
127} while (0)
128
129 switch (f->sample_format) {
130 case PCM_SAMPLE_UINT8:
131 SET_NULL(uint8_t, le, INT8_MIN); break;
132 case PCM_SAMPLE_SINT8:
133 SET_NULL(int8_t, le, 0); break;
134 case PCM_SAMPLE_UINT16_LE:
135 SET_NULL(uint16_t, le, INT16_MIN); break;
136 case PCM_SAMPLE_SINT16_LE:
137 SET_NULL(int16_t, le, 0); break;
138 case PCM_SAMPLE_UINT16_BE:
139 SET_NULL(uint16_t, be, INT16_MIN); break;
140 case PCM_SAMPLE_SINT16_BE:
141 SET_NULL(int16_t, be, 0); break;
142 case PCM_SAMPLE_UINT32_LE:
143 SET_NULL(uint32_t, le, INT32_MIN); break;
144 case PCM_SAMPLE_SINT32_LE:
145 SET_NULL(int32_t, le, 0); break;
146 case PCM_SAMPLE_UINT32_BE:
147 SET_NULL(uint32_t, be, INT32_MIN); break;
148 case PCM_SAMPLE_SINT32_BE:
149 SET_NULL(int32_t, le, 0); break;
150 case PCM_SAMPLE_UINT24_32_LE:
151 case PCM_SAMPLE_SINT24_32_LE:
152 case PCM_SAMPLE_UINT24_32_BE:
153 case PCM_SAMPLE_SINT24_32_BE:
154 case PCM_SAMPLE_UINT24_LE:
155 case PCM_SAMPLE_SINT24_LE:
156 case PCM_SAMPLE_UINT24_BE:
157 case PCM_SAMPLE_SINT24_BE:
158 case PCM_SAMPLE_FLOAT32:
159 default: ;
160 }
161#undef SET_NULL
162}
163
164/**
165 * Mix audio data of the same format and size.
166 * @param dst Destination buffer
167 * @param src Source buffer
168 * @param size Size of both the destination and the source buffer
169 * @param f Pointer to the format descriptor.
170 * @return Error code.
171 */
172int pcm_format_mix(void *dst, const void *src, size_t size, const pcm_format_t *f)
173{
174 return pcm_format_convert_and_mix(dst, size, src, size, f, f);
175}
176
177/**
178 * Add and mix audio data.
179 * @param dst Destination audio buffer
180 * @param dst_size Size of the destination buffer
181 * @param src Source audio buffer
182 * @param src_size Size of the source buffer.
183 * @param sf Pointer to the source format descriptor.
184 * @param df Pointer to the destination format descriptor.
185 * @return Error code.
186 *
187 * Buffers must contain entire frames. Destination buffer is always filled.
188 * If there are not enough data in the source buffer silent data is assumed.
189 */
190int pcm_format_convert_and_mix(void *dst, size_t dst_size, const void *src,
191 size_t src_size, const pcm_format_t *sf, const pcm_format_t *df)
192{
193 if (!dst || !src || !sf || !df)
194 return EINVAL;
195 const size_t src_frame_size = pcm_format_frame_size(sf);
196 if ((src_size % src_frame_size) != 0)
197 return EINVAL;
198
199 const size_t dst_frame_size = pcm_format_frame_size(df);
200 if ((dst_size % dst_frame_size) != 0)
201 return EINVAL;
202
203 /* This is so ugly it eats kittens, and puppies, and ducklings,
204 * and all little fluffy things...
205 */
206#define LOOP_ADD(type, endian, low, high) \
207do { \
208 const unsigned frame_count = dst_size / dst_frame_size; \
209 for (size_t i = 0; i < frame_count; ++i) { \
210 for (unsigned j = 0; j < df->channels; ++j) { \
211 const float a = \
212 get_normalized_sample(dst, dst_size, i, j, df);\
213 const float b = \
214 get_normalized_sample(src, src_size, i, j, sf);\
215 float c = (a + b); \
216 if (c < -1.0) c = -1.0; \
217 if (c > 1.0) c = 1.0; \
218 c += 1.0; \
219 c *= ((float)(type)high - (float)(type)low) / 2; \
220 c += (float)(type)low; \
221 type *dst_buf = dst; \
222 const unsigned pos = i * df->channels + j; \
223 if (pos < (dst_size / sizeof(type))) \
224 dst_buf[pos] = to((type)c, type, endian); \
225 } \
226 } \
227} while (0)
228
229 switch (df->sample_format) {
230 case PCM_SAMPLE_UINT8:
231 LOOP_ADD(uint8_t, le, UINT8_MIN, UINT8_MAX); break;
232 case PCM_SAMPLE_SINT8:
233 LOOP_ADD(uint8_t, le, INT8_MIN, INT8_MAX); break;
234 case PCM_SAMPLE_UINT16_LE:
235 LOOP_ADD(uint16_t, le, UINT16_MIN, UINT16_MAX); break;
236 case PCM_SAMPLE_SINT16_LE:
237 LOOP_ADD(int16_t, le, INT16_MIN, INT16_MAX); break;
238 case PCM_SAMPLE_UINT16_BE:
239 LOOP_ADD(uint16_t, be, UINT16_MIN, UINT16_MAX); break;
240 case PCM_SAMPLE_SINT16_BE:
241 LOOP_ADD(int16_t, be, INT16_MIN, INT16_MAX); break;
242 case PCM_SAMPLE_UINT24_32_LE:
243 case PCM_SAMPLE_UINT32_LE: // TODO this are not right for 24bit
244 LOOP_ADD(uint32_t, le, UINT32_MIN, UINT32_MAX); break;
245 case PCM_SAMPLE_SINT24_32_LE:
246 case PCM_SAMPLE_SINT32_LE:
247 LOOP_ADD(int32_t, le, INT32_MIN, INT32_MAX); break;
248 case PCM_SAMPLE_UINT24_32_BE:
249 case PCM_SAMPLE_UINT32_BE:
250 LOOP_ADD(uint32_t, be, UINT32_MIN, UINT32_MAX); break;
251 case PCM_SAMPLE_SINT24_32_BE:
252 case PCM_SAMPLE_SINT32_BE:
253 LOOP_ADD(int32_t, be, INT32_MIN, INT32_MAX); break;
254 case PCM_SAMPLE_UINT24_LE:
255 case PCM_SAMPLE_SINT24_LE:
256 case PCM_SAMPLE_UINT24_BE:
257 case PCM_SAMPLE_SINT24_BE:
258 case PCM_SAMPLE_FLOAT32:
259 default:
260 return ENOTSUP;
261 }
262 return EOK;
263#undef LOOP_ADD
264}
265
266/**
267 * Converts all sample formats to float <-1,1>
268 * @param buffer Audio data
269 * @param size Size of the buffer
270 * @param frame Index of the frame to read
271 * @param channel Channel within the frame
272 * @param f Pointer to a format descriptor
273 * @return Normalized sample <-1,1>, 0.0 if the data could not be read
274 */
275static float get_normalized_sample(const void *buffer, size_t size,
276 unsigned frame, unsigned channel, const pcm_format_t *f)
277{
278 assert(f);
279 assert(buffer);
280 if (channel >= f->channels)
281 return 0.0f;
282#define GET(type, endian, low, high) \
283do { \
284 const type *src = buffer; \
285 const size_t sample_count = size / sizeof(type); \
286 const size_t sample_pos = frame * f->channels + channel; \
287 if (sample_pos >= sample_count) {\
288 return 0.0f; \
289 } \
290 float sample = from(src[sample_pos], type, endian); \
291 /* This makes it positive */ \
292 sample -= (float)(type)low; \
293 /* This makes it <0,2> */ \
294 sample /= (((float)(type)high - (float)(type)low) / 2.0f); \
295 return sample - 1.0f; \
296} while (0)
297
298 switch (f->sample_format) {
299 case PCM_SAMPLE_UINT8:
300 GET(uint8_t, le, UINT8_MIN, UINT8_MAX);
301 case PCM_SAMPLE_SINT8:
302 GET(int8_t, le, INT8_MIN, INT8_MAX);
303 case PCM_SAMPLE_UINT16_LE:
304 GET(uint16_t, le, UINT16_MIN, UINT16_MAX);
305 case PCM_SAMPLE_SINT16_LE:
306 GET(int16_t, le, INT16_MIN, INT16_MAX);
307 case PCM_SAMPLE_UINT16_BE:
308 GET(uint16_t, be, UINT16_MIN, UINT16_MAX);
309 case PCM_SAMPLE_SINT16_BE:
310 GET(int16_t, be, INT16_MIN, INT16_MAX);
311 case PCM_SAMPLE_UINT24_32_LE:
312 case PCM_SAMPLE_UINT32_LE:
313 GET(uint32_t, le, UINT32_MIN, UINT32_MAX);
314 case PCM_SAMPLE_SINT24_32_LE:
315 case PCM_SAMPLE_SINT32_LE:
316 GET(int32_t, le, INT32_MIN, INT32_MAX);
317 case PCM_SAMPLE_UINT24_32_BE:
318 case PCM_SAMPLE_UINT32_BE:
319 GET(uint32_t, be, UINT32_MIN, UINT32_MAX);
320 case PCM_SAMPLE_SINT24_32_BE:
321 case PCM_SAMPLE_SINT32_BE:
322 GET(int32_t, le, INT32_MIN, INT32_MAX);
323 case PCM_SAMPLE_UINT24_LE:
324 case PCM_SAMPLE_SINT24_LE:
325 case PCM_SAMPLE_UINT24_BE:
326 case PCM_SAMPLE_SINT24_BE:
327 case PCM_SAMPLE_FLOAT32:
328 default: ;
329 }
330 return 0;
331#undef GET
332}
333/**
334 * @}
335 */
Note: See TracBrowser for help on using the repository browser.