| 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 | // 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 |
|
|---|
| 54 | /** Default linear PCM format */
|
|---|
| 55 | const pcm_format_t AUDIO_FORMAT_DEFAULT = {
|
|---|
| 56 | .channels = 2,
|
|---|
| 57 | .sampling_rate = 44100,
|
|---|
| 58 | .sample_format = PCM_SAMPLE_SINT16_LE,
|
|---|
| 59 | };
|
|---|
| 60 |
|
|---|
| 61 | /** Special ANY PCM format.
|
|---|
| 62 | * This format is used if the real format is no know or important.
|
|---|
| 63 | */
|
|---|
| 64 | const pcm_format_t AUDIO_FORMAT_ANY = {
|
|---|
| 65 | .channels = 0,
|
|---|
| 66 | .sampling_rate = 0,
|
|---|
| 67 | .sample_format = 0,
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | static float get_normalized_sample(const void *buffer, size_t size,
|
|---|
| 71 | unsigned frame, unsigned channel, const pcm_format_t *f);
|
|---|
| 72 |
|
|---|
| 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 | */
|
|---|
| 79 | bool pcm_format_same(const pcm_format_t *a, const pcm_format_t *b)
|
|---|
| 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 |
|
|---|
| 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 | */
|
|---|
| 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:
|
|---|
| 108 | SET_NULL(uint8_t, le, INT8_MIN);
|
|---|
| 109 | break;
|
|---|
| 110 | case PCM_SAMPLE_SINT8:
|
|---|
| 111 | SET_NULL(int8_t, le, 0);
|
|---|
| 112 | break;
|
|---|
| 113 | case PCM_SAMPLE_UINT16_LE:
|
|---|
| 114 | SET_NULL(uint16_t, le, INT16_MIN);
|
|---|
| 115 | break;
|
|---|
| 116 | case PCM_SAMPLE_SINT16_LE:
|
|---|
| 117 | SET_NULL(int16_t, le, 0);
|
|---|
| 118 | break;
|
|---|
| 119 | case PCM_SAMPLE_UINT16_BE:
|
|---|
| 120 | SET_NULL(uint16_t, be, INT16_MIN);
|
|---|
| 121 | break;
|
|---|
| 122 | case PCM_SAMPLE_SINT16_BE:
|
|---|
| 123 | SET_NULL(int16_t, be, 0);
|
|---|
| 124 | break;
|
|---|
| 125 | case PCM_SAMPLE_UINT32_LE:
|
|---|
| 126 | SET_NULL(uint32_t, le, INT32_MIN);
|
|---|
| 127 | break;
|
|---|
| 128 | case PCM_SAMPLE_SINT32_LE:
|
|---|
| 129 | SET_NULL(int32_t, le, 0);
|
|---|
| 130 | break;
|
|---|
| 131 | case PCM_SAMPLE_UINT32_BE:
|
|---|
| 132 | SET_NULL(uint32_t, be, INT32_MIN);
|
|---|
| 133 | break;
|
|---|
| 134 | case PCM_SAMPLE_SINT32_BE:
|
|---|
| 135 | SET_NULL(int32_t, le, 0);
|
|---|
| 136 | break;
|
|---|
| 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:
|
|---|
| 146 | default:
|
|---|
| 147 | break;
|
|---|
| 148 | }
|
|---|
| 149 | #undef SET_NULL
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 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 | */
|
|---|
| 160 | errno_t pcm_format_mix(void *dst, const void *src, size_t size, const pcm_format_t *f)
|
|---|
| 161 | {
|
|---|
| 162 | return pcm_format_convert_and_mix(dst, size, src, size, f, f);
|
|---|
| 163 | }
|
|---|
| 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 | */
|
|---|
| 178 | errno_t pcm_format_convert_and_mix(void *dst, size_t dst_size, const void *src,
|
|---|
| 179 | size_t src_size, const pcm_format_t *sf, const pcm_format_t *df)
|
|---|
| 180 | {
|
|---|
| 181 | if (!dst || !src || !sf || !df)
|
|---|
| 182 | return EINVAL;
|
|---|
| 183 | const size_t src_frame_size = pcm_format_frame_size(sf);
|
|---|
| 184 | if ((src_size % src_frame_size) != 0)
|
|---|
| 185 | return EINVAL;
|
|---|
| 186 |
|
|---|
| 187 | const size_t dst_frame_size = pcm_format_frame_size(df);
|
|---|
| 188 | if ((dst_size % dst_frame_size) != 0)
|
|---|
| 189 | return EINVAL;
|
|---|
| 190 |
|
|---|
| 191 | /*
|
|---|
| 192 | * This is so ugly it eats kittens, and puppies, and ducklings,
|
|---|
| 193 | * and all little fluffy things...
|
|---|
| 194 | */
|
|---|
| 195 | #define LOOP_ADD(type, endian, low, high) \
|
|---|
| 196 | do { \
|
|---|
| 197 | const unsigned frame_count = dst_size / dst_frame_size; \
|
|---|
| 198 | for (size_t i = 0; i < frame_count; ++i) { \
|
|---|
| 199 | for (unsigned j = 0; j < df->channels; ++j) { \
|
|---|
| 200 | const float a = \
|
|---|
| 201 | get_normalized_sample(dst, dst_size, i, j, df);\
|
|---|
| 202 | const float b = \
|
|---|
| 203 | get_normalized_sample(src, src_size, i, j, sf);\
|
|---|
| 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; \
|
|---|
| 211 | const unsigned pos = i * df->channels + j; \
|
|---|
| 212 | if (pos < (dst_size / sizeof(type))) \
|
|---|
| 213 | dst_buf[pos] = to((type)c, type, endian); \
|
|---|
| 214 | } \
|
|---|
| 215 | } \
|
|---|
| 216 | } while (0)
|
|---|
| 217 |
|
|---|
| 218 | switch (df->sample_format) {
|
|---|
| 219 | case PCM_SAMPLE_UINT8:
|
|---|
| 220 | LOOP_ADD(uint8_t, le, UINT8_MIN, UINT8_MAX);
|
|---|
| 221 | break;
|
|---|
| 222 | case PCM_SAMPLE_SINT8:
|
|---|
| 223 | LOOP_ADD(uint8_t, le, INT8_MIN, INT8_MAX);
|
|---|
| 224 | break;
|
|---|
| 225 | case PCM_SAMPLE_UINT16_LE:
|
|---|
| 226 | LOOP_ADD(uint16_t, le, UINT16_MIN, UINT16_MAX);
|
|---|
| 227 | break;
|
|---|
| 228 | case PCM_SAMPLE_SINT16_LE:
|
|---|
| 229 | LOOP_ADD(int16_t, le, INT16_MIN, INT16_MAX);
|
|---|
| 230 | break;
|
|---|
| 231 | case PCM_SAMPLE_UINT16_BE:
|
|---|
| 232 | LOOP_ADD(uint16_t, be, UINT16_MIN, UINT16_MAX);
|
|---|
| 233 | break;
|
|---|
| 234 | case PCM_SAMPLE_SINT16_BE:
|
|---|
| 235 | LOOP_ADD(int16_t, be, INT16_MIN, INT16_MAX);
|
|---|
| 236 | break;
|
|---|
| 237 | case PCM_SAMPLE_UINT24_32_LE:
|
|---|
| 238 | case PCM_SAMPLE_UINT32_LE: // TODO this are not right for 24bit
|
|---|
| 239 | LOOP_ADD(uint32_t, le, UINT32_MIN, UINT32_MAX);
|
|---|
| 240 | break;
|
|---|
| 241 | case PCM_SAMPLE_SINT24_32_LE:
|
|---|
| 242 | case PCM_SAMPLE_SINT32_LE:
|
|---|
| 243 | LOOP_ADD(int32_t, le, INT32_MIN, INT32_MAX);
|
|---|
| 244 | break;
|
|---|
| 245 | case PCM_SAMPLE_UINT24_32_BE:
|
|---|
| 246 | case PCM_SAMPLE_UINT32_BE:
|
|---|
| 247 | LOOP_ADD(uint32_t, be, UINT32_MIN, UINT32_MAX);
|
|---|
| 248 | break;
|
|---|
| 249 | case PCM_SAMPLE_SINT24_32_BE:
|
|---|
| 250 | case PCM_SAMPLE_SINT32_BE:
|
|---|
| 251 | LOOP_ADD(int32_t, be, INT32_MIN, INT32_MAX);
|
|---|
| 252 | break;
|
|---|
| 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;
|
|---|
| 262 | #undef LOOP_ADD
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 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 | */
|
|---|
| 274 | static float get_normalized_sample(const void *buffer, size_t size,
|
|---|
| 275 | unsigned frame, unsigned channel, const pcm_format_t *f)
|
|---|
| 276 | {
|
|---|
| 277 | assert(f);
|
|---|
| 278 | assert(buffer);
|
|---|
| 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:
|
|---|
| 327 | default:
|
|---|
| 328 | break;
|
|---|
| 329 | }
|
|---|
| 330 | return 0;
|
|---|
| 331 | #undef GET
|
|---|
| 332 | }
|
|---|
| 333 | /**
|
|---|
| 334 | * @}
|
|---|
| 335 | */
|
|---|