| [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>
|
|---|
| 39 |
|
|---|
| 40 | #include "audio_format.h"
|
|---|
| 41 |
|
|---|
| 42 | bool audio_format_same(const audio_format_t *a, const audio_format_t* b)
|
|---|
| 43 | {
|
|---|
| 44 | assert(a);
|
|---|
| 45 | assert(b);
|
|---|
| 46 | return
|
|---|
| 47 | a->sampling_rate == b->sampling_rate &&
|
|---|
| 48 | a->channels == b->channels &&
|
|---|
| 49 | a->sample_format == b->sample_format;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | int audio_format_mix(void *dst, const void *src, size_t size, const audio_format_t *f)
|
|---|
| 53 | {
|
|---|
| 54 | if (!dst || !src || !f)
|
|---|
| 55 | return EINVAL;
|
|---|
| 56 | const size_t sample_size = pcm_sample_format_size(f->sample_format);
|
|---|
| [ab07cf0] | 57 | if ((size % sample_size) != 0)
|
|---|
| [737b4c0] | 58 | return EINVAL;
|
|---|
| 59 |
|
|---|
| 60 | /* This is so ugly it eats kittens, and puppies, and ducklings,
|
|---|
| 61 | * and all little fluffy things...
|
|---|
| 62 | * AND it does not check for overflows (FIXME)*/
|
|---|
| 63 | #define LOOP_ADD(type, endian) \
|
|---|
| 64 | do { \
|
|---|
| 65 | const type *src_buff = src; \
|
|---|
| 66 | type *dst_buff = dst; \
|
|---|
| [ab07cf0] | 67 | for (size_t i = 0; i < size / sizeof(type); ++i) { \
|
|---|
| 68 | const type a = type ## _ ## endian ##2host(dst_buff[i]); \
|
|---|
| 69 | const type b = type ## _ ## endian ##2host(src_buff[i]); \
|
|---|
| 70 | const type c = a + b; \
|
|---|
| [737b4c0] | 71 | dst_buff[i] = host2 ## type ## _ ## endian(c); \
|
|---|
| 72 | } \
|
|---|
| 73 | } while (0)
|
|---|
| 74 |
|
|---|
| 75 | switch (f->sample_format) {
|
|---|
| 76 | case PCM_SAMPLE_UINT8: {
|
|---|
| 77 | const uint8_t *src_buff = src;
|
|---|
| 78 | uint8_t *dst_buff = dst;
|
|---|
| 79 | for (size_t i = 0; i < size; ++i )
|
|---|
| 80 | dst_buff[i] += src_buff[i];
|
|---|
| 81 | break;
|
|---|
| 82 | }
|
|---|
| 83 | case PCM_SAMPLE_SINT8: {
|
|---|
| 84 | const int8_t *src_buff = src;
|
|---|
| 85 | int8_t *dst_buff = dst;
|
|---|
| 86 | for (size_t i = 0; i < size; ++i)
|
|---|
| 87 | dst_buff[i] += src_buff[i];
|
|---|
| 88 | break;
|
|---|
| 89 | }
|
|---|
| 90 | case PCM_SAMPLE_UINT16_LE:
|
|---|
| 91 | case PCM_SAMPLE_SINT16_LE:
|
|---|
| 92 | LOOP_ADD(uint16_t, le); break;
|
|---|
| 93 | case PCM_SAMPLE_UINT16_BE:
|
|---|
| 94 | case PCM_SAMPLE_SINT16_BE:
|
|---|
| 95 | LOOP_ADD(uint16_t, be); break;
|
|---|
| 96 | case PCM_SAMPLE_UINT24_32_LE:
|
|---|
| 97 | case PCM_SAMPLE_SINT24_32_LE:
|
|---|
| 98 | case PCM_SAMPLE_UINT32_LE:
|
|---|
| 99 | case PCM_SAMPLE_SINT32_LE:
|
|---|
| 100 | LOOP_ADD(uint32_t, le); break;
|
|---|
| 101 | case PCM_SAMPLE_UINT24_32_BE:
|
|---|
| 102 | case PCM_SAMPLE_SINT24_32_BE:
|
|---|
| 103 | case PCM_SAMPLE_UINT32_BE:
|
|---|
| 104 | case PCM_SAMPLE_SINT32_BE:
|
|---|
| 105 | LOOP_ADD(uint32_t, be); break;
|
|---|
| 106 | case PCM_SAMPLE_UINT24_LE:
|
|---|
| 107 | case PCM_SAMPLE_SINT24_LE:
|
|---|
| 108 | case PCM_SAMPLE_UINT24_BE:
|
|---|
| 109 | case PCM_SAMPLE_SINT24_BE:
|
|---|
| 110 | case PCM_SAMPLE_FLOAT32:
|
|---|
| 111 | default:
|
|---|
| 112 | return ENOTSUP;
|
|---|
| 113 | }
|
|---|
| 114 | return EOK;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | * @}
|
|---|
| 119 | */
|
|---|