source: mainline/uspace/lib/pcm/src/format.c@ 9fa14d8d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9fa14d8d was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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