[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] | 55 | const 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] | 64 | const pcm_format_t AUDIO_FORMAT_ANY = {
|
---|
| 65 | .channels = 0,
|
---|
| 66 | .sampling_rate = 0,
|
---|
| 67 | .sample_format = 0,
|
---|
[1433ecda] | 68 | };
|
---|
[fe0b448] | 69 |
|
---|
[bb67def] | 70 | static 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] | 79 | bool 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] | 95 | void pcm_format_silence(void *dst, size_t size, const pcm_format_t *f)
|
---|
| 96 | {
|
---|
| 97 | #define SET_NULL(type, endian, nullv) \
|
---|
| 98 | do { \
|
---|
| 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] | 160 | errno_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] | 178 | errno_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 |
|
---|
[7c3fb9b] | 191 | /*
|
---|
| 192 | * This is so ugly it eats kittens, and puppies, and ducklings,
|
---|
[737b4c0] | 193 | * and all little fluffy things...
|
---|
[bb67def] | 194 | */
|
---|
| 195 | #define LOOP_ADD(type, endian, low, high) \
|
---|
[737b4c0] | 196 | do { \
|
---|
[950110ee] | 197 | const unsigned frame_count = dst_size / dst_frame_size; \
|
---|
[bb67def] | 198 | for (size_t i = 0; i < frame_count; ++i) { \
|
---|
[950110ee] | 199 | for (unsigned j = 0; j < df->channels; ++j) { \
|
---|
[bb67def] | 200 | const float a = \
|
---|
[1f7da3b] | 201 | get_normalized_sample(dst, dst_size, i, j, df);\
|
---|
[bb67def] | 202 | const float b = \
|
---|
[1f7da3b] | 203 | get_normalized_sample(src, src_size, i, j, sf);\
|
---|
[bb67def] | 204 | float c = (a + b); \
|
---|
| 205 | if (c < -1.0) c = -1.0; \
|
---|
| 206 | if (c > 1.0) c = 1.0; \
|
---|
| 207 | c += 1.0; \
|
---|
| 208 | c *= ((float)(type)high - (float)(type)low) / 2; \
|
---|
| 209 | c += (float)(type)low; \
|
---|
| 210 | type *dst_buf = dst; \
|
---|
[950110ee] | 211 | const unsigned pos = i * df->channels + j; \
|
---|
| 212 | if (pos < (dst_size / sizeof(type))) \
|
---|
[bb67def] | 213 | dst_buf[pos] = to((type)c, type, endian); \
|
---|
| 214 | } \
|
---|
[737b4c0] | 215 | } \
|
---|
| 216 | } while (0)
|
---|
| 217 |
|
---|
[950110ee] | 218 | switch (df->sample_format) {
|
---|
[bb67def] | 219 | case PCM_SAMPLE_UINT8:
|
---|
[1433ecda] | 220 | LOOP_ADD(uint8_t, le, UINT8_MIN, UINT8_MAX);
|
---|
| 221 | break;
|
---|
[bb67def] | 222 | case PCM_SAMPLE_SINT8:
|
---|
[1433ecda] | 223 | LOOP_ADD(uint8_t, le, INT8_MIN, INT8_MAX);
|
---|
| 224 | break;
|
---|
[737b4c0] | 225 | case PCM_SAMPLE_UINT16_LE:
|
---|
[1433ecda] | 226 | LOOP_ADD(uint16_t, le, UINT16_MIN, UINT16_MAX);
|
---|
| 227 | break;
|
---|
[737b4c0] | 228 | case PCM_SAMPLE_SINT16_LE:
|
---|
[1433ecda] | 229 | LOOP_ADD(int16_t, le, INT16_MIN, INT16_MAX);
|
---|
| 230 | break;
|
---|
[737b4c0] | 231 | case PCM_SAMPLE_UINT16_BE:
|
---|
[1433ecda] | 232 | LOOP_ADD(uint16_t, be, UINT16_MIN, UINT16_MAX);
|
---|
| 233 | break;
|
---|
[737b4c0] | 234 | case PCM_SAMPLE_SINT16_BE:
|
---|
[1433ecda] | 235 | LOOP_ADD(int16_t, be, INT16_MIN, INT16_MAX);
|
---|
| 236 | break;
|
---|
[737b4c0] | 237 | case PCM_SAMPLE_UINT24_32_LE:
|
---|
[bb67def] | 238 | case PCM_SAMPLE_UINT32_LE: // TODO this are not right for 24bit
|
---|
[1433ecda] | 239 | LOOP_ADD(uint32_t, le, UINT32_MIN, UINT32_MAX);
|
---|
| 240 | break;
|
---|
[43c40a3] | 241 | case PCM_SAMPLE_SINT24_32_LE:
|
---|
[737b4c0] | 242 | case PCM_SAMPLE_SINT32_LE:
|
---|
[1433ecda] | 243 | LOOP_ADD(int32_t, le, INT32_MIN, INT32_MAX);
|
---|
| 244 | break;
|
---|
[737b4c0] | 245 | case PCM_SAMPLE_UINT24_32_BE:
|
---|
| 246 | case PCM_SAMPLE_UINT32_BE:
|
---|
[1433ecda] | 247 | LOOP_ADD(uint32_t, be, UINT32_MIN, UINT32_MAX);
|
---|
| 248 | break;
|
---|
[43c40a3] | 249 | case PCM_SAMPLE_SINT24_32_BE:
|
---|
[737b4c0] | 250 | case PCM_SAMPLE_SINT32_BE:
|
---|
[1433ecda] | 251 | LOOP_ADD(int32_t, be, INT32_MIN, INT32_MAX);
|
---|
| 252 | break;
|
---|
[737b4c0] | 253 | case PCM_SAMPLE_UINT24_LE:
|
---|
| 254 | case PCM_SAMPLE_SINT24_LE:
|
---|
| 255 | case PCM_SAMPLE_UINT24_BE:
|
---|
| 256 | case PCM_SAMPLE_SINT24_BE:
|
---|
| 257 | case PCM_SAMPLE_FLOAT32:
|
---|
| 258 | default:
|
---|
| 259 | return ENOTSUP;
|
---|
| 260 | }
|
---|
| 261 | return EOK;
|
---|
[bb67def] | 262 | #undef LOOP_ADD
|
---|
[737b4c0] | 263 | }
|
---|
| 264 |
|
---|
[5a6f362] | 265 | /**
|
---|
| 266 | * Converts all sample formats to float <-1,1>
|
---|
| 267 | * @param buffer Audio data
|
---|
| 268 | * @param size Size of the buffer
|
---|
| 269 | * @param frame Index of the frame to read
|
---|
| 270 | * @param channel Channel within the frame
|
---|
| 271 | * @param f Pointer to a format descriptor
|
---|
| 272 | * @return Normalized sample <-1,1>, 0.0 if the data could not be read
|
---|
| 273 | */
|
---|
[bb67def] | 274 | static float get_normalized_sample(const void *buffer, size_t size,
|
---|
[ea6c838] | 275 | unsigned frame, unsigned channel, const pcm_format_t *f)
|
---|
[bb67def] | 276 | {
|
---|
| 277 | assert(f);
|
---|
[aef1799] | 278 | assert(buffer);
|
---|
[bb67def] | 279 | if (channel >= f->channels)
|
---|
| 280 | return 0.0f;
|
---|
| 281 | #define GET(type, endian, low, high) \
|
---|
| 282 | do { \
|
---|
| 283 | const type *src = buffer; \
|
---|
| 284 | const size_t sample_count = size / sizeof(type); \
|
---|
| 285 | const size_t sample_pos = frame * f->channels + channel; \
|
---|
| 286 | if (sample_pos >= sample_count) {\
|
---|
| 287 | return 0.0f; \
|
---|
| 288 | } \
|
---|
| 289 | float sample = from(src[sample_pos], type, endian); \
|
---|
| 290 | /* This makes it positive */ \
|
---|
| 291 | sample -= (float)(type)low; \
|
---|
| 292 | /* This makes it <0,2> */ \
|
---|
| 293 | sample /= (((float)(type)high - (float)(type)low) / 2.0f); \
|
---|
| 294 | return sample - 1.0f; \
|
---|
| 295 | } while (0)
|
---|
| 296 |
|
---|
| 297 | switch (f->sample_format) {
|
---|
| 298 | case PCM_SAMPLE_UINT8:
|
---|
| 299 | GET(uint8_t, le, UINT8_MIN, UINT8_MAX);
|
---|
| 300 | case PCM_SAMPLE_SINT8:
|
---|
| 301 | GET(int8_t, le, INT8_MIN, INT8_MAX);
|
---|
| 302 | case PCM_SAMPLE_UINT16_LE:
|
---|
| 303 | GET(uint16_t, le, UINT16_MIN, UINT16_MAX);
|
---|
| 304 | case PCM_SAMPLE_SINT16_LE:
|
---|
| 305 | GET(int16_t, le, INT16_MIN, INT16_MAX);
|
---|
| 306 | case PCM_SAMPLE_UINT16_BE:
|
---|
| 307 | GET(uint16_t, be, UINT16_MIN, UINT16_MAX);
|
---|
| 308 | case PCM_SAMPLE_SINT16_BE:
|
---|
| 309 | GET(int16_t, be, INT16_MIN, INT16_MAX);
|
---|
| 310 | case PCM_SAMPLE_UINT24_32_LE:
|
---|
| 311 | case PCM_SAMPLE_UINT32_LE:
|
---|
| 312 | GET(uint32_t, le, UINT32_MIN, UINT32_MAX);
|
---|
| 313 | case PCM_SAMPLE_SINT24_32_LE:
|
---|
| 314 | case PCM_SAMPLE_SINT32_LE:
|
---|
| 315 | GET(int32_t, le, INT32_MIN, INT32_MAX);
|
---|
| 316 | case PCM_SAMPLE_UINT24_32_BE:
|
---|
| 317 | case PCM_SAMPLE_UINT32_BE:
|
---|
| 318 | GET(uint32_t, be, UINT32_MIN, UINT32_MAX);
|
---|
| 319 | case PCM_SAMPLE_SINT24_32_BE:
|
---|
| 320 | case PCM_SAMPLE_SINT32_BE:
|
---|
| 321 | GET(int32_t, le, INT32_MIN, INT32_MAX);
|
---|
| 322 | case PCM_SAMPLE_UINT24_LE:
|
---|
| 323 | case PCM_SAMPLE_SINT24_LE:
|
---|
| 324 | case PCM_SAMPLE_UINT24_BE:
|
---|
| 325 | case PCM_SAMPLE_SINT24_BE:
|
---|
| 326 | case PCM_SAMPLE_FLOAT32:
|
---|
[84239b1] | 327 | default:
|
---|
| 328 | break;
|
---|
[bb67def] | 329 | }
|
---|
| 330 | return 0;
|
---|
| 331 | #undef GET
|
---|
| 332 | }
|
---|
[737b4c0] | 333 | /**
|
---|
| 334 | * @}
|
---|
| 335 | */
|
---|