bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Mixing engine |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2004-2005 Vassili Karpov (malc) |
| 5 | * |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * Tusen tack till Mike Nordell |
| 27 | * dec++'ified by Dscho |
| 28 | */ |
| 29 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 30 | #ifndef SIGNED |
| 31 | #define HALF (IN_MAX >> 1) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 34 | #define ET glue (ENDIAN_CONVERSION, glue (glue (glue (_, ITYPE), BSIZE), _t)) |
| 35 | #define IN_T glue (glue (ITYPE, BSIZE), _t) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 36 | |
| 37 | #ifdef FLOAT_MIXENG |
Alex Bligh | 203cea2 | 2013-10-22 10:26:28 +0100 | [diff] [blame] | 38 | static inline mixeng_real glue (conv_, ET) (IN_T v) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 39 | { |
| 40 | IN_T nv = ENDIAN_CONVERT (v); |
| 41 | |
| 42 | #ifdef RECIPROCAL |
| 43 | #ifdef SIGNED |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 44 | return nv * (1.f / (mixeng_real) (IN_MAX - IN_MIN)); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 45 | #else |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 46 | return (nv - HALF) * (1.f / (mixeng_real) IN_MAX); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 47 | #endif |
| 48 | #else /* !RECIPROCAL */ |
| 49 | #ifdef SIGNED |
Juha Riihim?ki | 578c7b2 | 2011-05-31 19:40:21 +0100 | [diff] [blame] | 50 | return nv / (mixeng_real) ((mixeng_real) IN_MAX - IN_MIN); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 51 | #else |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 52 | return (nv - HALF) / (mixeng_real) IN_MAX; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 53 | #endif |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 54 | #endif |
| 55 | } |
| 56 | |
Alex Bligh | 203cea2 | 2013-10-22 10:26:28 +0100 | [diff] [blame] | 57 | static inline IN_T glue (clip_, ET) (mixeng_real v) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 58 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 59 | if (v >= 0.5) { |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 60 | return IN_MAX; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 61 | } |
| 62 | else if (v < -0.5) { |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 63 | return IN_MIN; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 64 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 65 | |
| 66 | #ifdef SIGNED |
Juha Riihim?ki | 578c7b2 | 2011-05-31 19:40:21 +0100 | [diff] [blame] | 67 | return ENDIAN_CONVERT ((IN_T) (v * ((mixeng_real) IN_MAX - IN_MIN))); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 68 | #else |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 69 | return ENDIAN_CONVERT ((IN_T) ((v * IN_MAX) + HALF)); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 70 | #endif |
| 71 | } |
| 72 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 73 | #else /* !FLOAT_MIXENG */ |
| 74 | |
| 75 | static inline int64_t glue (conv_, ET) (IN_T v) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 76 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 77 | IN_T nv = ENDIAN_CONVERT (v); |
| 78 | #ifdef SIGNED |
| 79 | return ((int64_t) nv) << (32 - SHIFT); |
| 80 | #else |
| 81 | return ((int64_t) nv - HALF) << (32 - SHIFT); |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | static inline IN_T glue (clip_, ET) (int64_t v) |
| 86 | { |
| 87 | if (v >= 0x7f000000) { |
| 88 | return IN_MAX; |
| 89 | } |
| 90 | else if (v < -2147483648LL) { |
| 91 | return IN_MIN; |
| 92 | } |
| 93 | |
| 94 | #ifdef SIGNED |
| 95 | return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT))); |
| 96 | #else |
| 97 | return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF)); |
| 98 | #endif |
| 99 | } |
| 100 | #endif |
| 101 | |
| 102 | static void glue (glue (conv_, ET), _to_stereo) |
Michael Walle | 00e0767 | 2011-01-05 01:05:47 +0100 | [diff] [blame] | 103 | (struct st_sample *dst, const void *src, int samples) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 104 | { |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 105 | struct st_sample *out = dst; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 106 | IN_T *in = (IN_T *) src; |
Michael Walle | 00e0767 | 2011-01-05 01:05:47 +0100 | [diff] [blame] | 107 | |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 108 | while (samples--) { |
Michael Walle | 00e0767 | 2011-01-05 01:05:47 +0100 | [diff] [blame] | 109 | out->l = glue (conv_, ET) (*in++); |
| 110 | out->r = glue (conv_, ET) (*in++); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 111 | out += 1; |
| 112 | } |
| 113 | } |
| 114 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 115 | static void glue (glue (conv_, ET), _to_mono) |
Michael Walle | 00e0767 | 2011-01-05 01:05:47 +0100 | [diff] [blame] | 116 | (struct st_sample *dst, const void *src, int samples) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 117 | { |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 118 | struct st_sample *out = dst; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 119 | IN_T *in = (IN_T *) src; |
Michael Walle | 00e0767 | 2011-01-05 01:05:47 +0100 | [diff] [blame] | 120 | |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 121 | while (samples--) { |
Michael Walle | 00e0767 | 2011-01-05 01:05:47 +0100 | [diff] [blame] | 122 | out->l = glue (conv_, ET) (in[0]); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 123 | out->r = out->l; |
| 124 | out += 1; |
| 125 | in += 1; |
| 126 | } |
| 127 | } |
| 128 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 129 | static void glue (glue (clip_, ET), _from_stereo) |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 130 | (void *dst, const struct st_sample *src, int samples) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 131 | { |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 132 | const struct st_sample *in = src; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 133 | IN_T *out = (IN_T *) dst; |
| 134 | while (samples--) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 135 | *out++ = glue (clip_, ET) (in->l); |
| 136 | *out++ = glue (clip_, ET) (in->r); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 137 | in += 1; |
| 138 | } |
| 139 | } |
| 140 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 141 | static void glue (glue (clip_, ET), _from_mono) |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 142 | (void *dst, const struct st_sample *src, int samples) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 143 | { |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 144 | const struct st_sample *in = src; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 145 | IN_T *out = (IN_T *) dst; |
| 146 | while (samples--) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 147 | *out++ = glue (clip_, ET) (in->l + in->r); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 148 | in += 1; |
| 149 | } |
| 150 | } |
| 151 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 152 | #undef ET |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 153 | #undef HALF |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 154 | #undef IN_T |