bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Mixing engine |
| 3 | * |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004-2005 Vassili Karpov (malc) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 5 | * Copyright (c) 1998 Fabrice Bellard |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
Peter Maydell | 6086a56 | 2016-01-18 17:33:52 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 26 | #include "qemu-common.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 27 | #include "qemu/bswap.h" |
Pavel Dovgalyuk | 3d4d16f | 2017-02-02 08:50:54 +0300 | [diff] [blame] | 28 | #include "qemu/error-report.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 29 | #include "audio.h" |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 30 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 31 | #define AUDIO_CAP "mixeng" |
| 32 | #include "audio_int.h" |
| 33 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 34 | /* 8 bit */ |
| 35 | #define ENDIAN_CONVERSION natural |
| 36 | #define ENDIAN_CONVERT(v) (v) |
| 37 | |
| 38 | /* Signed 8 bit */ |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 39 | #define BSIZE 8 |
| 40 | #define ITYPE int |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 41 | #define IN_MIN SCHAR_MIN |
| 42 | #define IN_MAX SCHAR_MAX |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 43 | #define SIGNED |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 44 | #define SHIFT 8 |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 45 | #include "mixeng_template.h" |
| 46 | #undef SIGNED |
| 47 | #undef IN_MAX |
| 48 | #undef IN_MIN |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 49 | #undef BSIZE |
| 50 | #undef ITYPE |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 51 | #undef SHIFT |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 52 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 53 | /* Unsigned 8 bit */ |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 54 | #define BSIZE 8 |
| 55 | #define ITYPE uint |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 56 | #define IN_MIN 0 |
| 57 | #define IN_MAX UCHAR_MAX |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 58 | #define SHIFT 8 |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 59 | #include "mixeng_template.h" |
| 60 | #undef IN_MAX |
| 61 | #undef IN_MIN |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 62 | #undef BSIZE |
| 63 | #undef ITYPE |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 64 | #undef SHIFT |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 65 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 66 | #undef ENDIAN_CONVERT |
| 67 | #undef ENDIAN_CONVERSION |
| 68 | |
| 69 | /* Signed 16 bit */ |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 70 | #define BSIZE 16 |
| 71 | #define ITYPE int |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 72 | #define IN_MIN SHRT_MIN |
| 73 | #define IN_MAX SHRT_MAX |
| 74 | #define SIGNED |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 75 | #define SHIFT 16 |
| 76 | #define ENDIAN_CONVERSION natural |
| 77 | #define ENDIAN_CONVERT(v) (v) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 78 | #include "mixeng_template.h" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 79 | #undef ENDIAN_CONVERT |
| 80 | #undef ENDIAN_CONVERSION |
| 81 | #define ENDIAN_CONVERSION swap |
| 82 | #define ENDIAN_CONVERT(v) bswap16 (v) |
| 83 | #include "mixeng_template.h" |
| 84 | #undef ENDIAN_CONVERT |
| 85 | #undef ENDIAN_CONVERSION |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 86 | #undef SIGNED |
| 87 | #undef IN_MAX |
| 88 | #undef IN_MIN |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 89 | #undef BSIZE |
| 90 | #undef ITYPE |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 91 | #undef SHIFT |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 92 | |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 93 | /* Unsigned 16 bit */ |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 94 | #define BSIZE 16 |
| 95 | #define ITYPE uint |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 96 | #define IN_MIN 0 |
| 97 | #define IN_MAX USHRT_MAX |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 98 | #define SHIFT 16 |
| 99 | #define ENDIAN_CONVERSION natural |
| 100 | #define ENDIAN_CONVERT(v) (v) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 101 | #include "mixeng_template.h" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 102 | #undef ENDIAN_CONVERT |
| 103 | #undef ENDIAN_CONVERSION |
| 104 | #define ENDIAN_CONVERSION swap |
| 105 | #define ENDIAN_CONVERT(v) bswap16 (v) |
| 106 | #include "mixeng_template.h" |
| 107 | #undef ENDIAN_CONVERT |
| 108 | #undef ENDIAN_CONVERSION |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 109 | #undef IN_MAX |
| 110 | #undef IN_MIN |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 111 | #undef BSIZE |
| 112 | #undef ITYPE |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 113 | #undef SHIFT |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 114 | |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 115 | /* Signed 32 bit */ |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 116 | #define BSIZE 32 |
| 117 | #define ITYPE int |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 118 | #define IN_MIN INT32_MIN |
| 119 | #define IN_MAX INT32_MAX |
| 120 | #define SIGNED |
| 121 | #define SHIFT 32 |
| 122 | #define ENDIAN_CONVERSION natural |
| 123 | #define ENDIAN_CONVERT(v) (v) |
| 124 | #include "mixeng_template.h" |
| 125 | #undef ENDIAN_CONVERT |
| 126 | #undef ENDIAN_CONVERSION |
| 127 | #define ENDIAN_CONVERSION swap |
| 128 | #define ENDIAN_CONVERT(v) bswap32 (v) |
| 129 | #include "mixeng_template.h" |
| 130 | #undef ENDIAN_CONVERT |
| 131 | #undef ENDIAN_CONVERSION |
| 132 | #undef SIGNED |
| 133 | #undef IN_MAX |
| 134 | #undef IN_MIN |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 135 | #undef BSIZE |
| 136 | #undef ITYPE |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 137 | #undef SHIFT |
| 138 | |
malc | ad483a5 | 2010-03-11 18:28:38 +0300 | [diff] [blame] | 139 | /* Unsigned 32 bit */ |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 140 | #define BSIZE 32 |
| 141 | #define ITYPE uint |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 142 | #define IN_MIN 0 |
| 143 | #define IN_MAX UINT32_MAX |
| 144 | #define SHIFT 32 |
| 145 | #define ENDIAN_CONVERSION natural |
| 146 | #define ENDIAN_CONVERT(v) (v) |
| 147 | #include "mixeng_template.h" |
| 148 | #undef ENDIAN_CONVERT |
| 149 | #undef ENDIAN_CONVERSION |
| 150 | #define ENDIAN_CONVERSION swap |
| 151 | #define ENDIAN_CONVERT(v) bswap32 (v) |
| 152 | #include "mixeng_template.h" |
| 153 | #undef ENDIAN_CONVERT |
| 154 | #undef ENDIAN_CONVERSION |
| 155 | #undef IN_MAX |
| 156 | #undef IN_MIN |
Roger Pau Monne | a288538 | 2012-05-18 12:08:14 +0100 | [diff] [blame] | 157 | #undef BSIZE |
| 158 | #undef ITYPE |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 159 | #undef SHIFT |
| 160 | |
| 161 | t_sample *mixeng_conv[2][2][2][3] = { |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 162 | { |
| 163 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 164 | { |
| 165 | conv_natural_uint8_t_to_mono, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 166 | conv_natural_uint16_t_to_mono, |
| 167 | conv_natural_uint32_t_to_mono |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 168 | }, |
| 169 | { |
| 170 | conv_natural_uint8_t_to_mono, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 171 | conv_swap_uint16_t_to_mono, |
| 172 | conv_swap_uint32_t_to_mono, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 173 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 174 | }, |
| 175 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 176 | { |
| 177 | conv_natural_int8_t_to_mono, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 178 | conv_natural_int16_t_to_mono, |
| 179 | conv_natural_int32_t_to_mono |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 180 | }, |
| 181 | { |
| 182 | conv_natural_int8_t_to_mono, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 183 | conv_swap_int16_t_to_mono, |
| 184 | conv_swap_int32_t_to_mono |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 185 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 186 | } |
| 187 | }, |
| 188 | { |
| 189 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 190 | { |
| 191 | conv_natural_uint8_t_to_stereo, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 192 | conv_natural_uint16_t_to_stereo, |
| 193 | conv_natural_uint32_t_to_stereo |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 194 | }, |
| 195 | { |
| 196 | conv_natural_uint8_t_to_stereo, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 197 | conv_swap_uint16_t_to_stereo, |
| 198 | conv_swap_uint32_t_to_stereo |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 199 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 200 | }, |
| 201 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 202 | { |
| 203 | conv_natural_int8_t_to_stereo, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 204 | conv_natural_int16_t_to_stereo, |
| 205 | conv_natural_int32_t_to_stereo |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 206 | }, |
| 207 | { |
| 208 | conv_natural_int8_t_to_stereo, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 209 | conv_swap_int16_t_to_stereo, |
| 210 | conv_swap_int32_t_to_stereo, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 211 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | }; |
| 215 | |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 216 | f_sample *mixeng_clip[2][2][2][3] = { |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 217 | { |
| 218 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 219 | { |
| 220 | clip_natural_uint8_t_from_mono, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 221 | clip_natural_uint16_t_from_mono, |
| 222 | clip_natural_uint32_t_from_mono |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 223 | }, |
| 224 | { |
| 225 | clip_natural_uint8_t_from_mono, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 226 | clip_swap_uint16_t_from_mono, |
| 227 | clip_swap_uint32_t_from_mono |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 228 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 229 | }, |
| 230 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 231 | { |
| 232 | clip_natural_int8_t_from_mono, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 233 | clip_natural_int16_t_from_mono, |
| 234 | clip_natural_int32_t_from_mono |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 235 | }, |
| 236 | { |
| 237 | clip_natural_int8_t_from_mono, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 238 | clip_swap_int16_t_from_mono, |
| 239 | clip_swap_int32_t_from_mono |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 240 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 241 | } |
| 242 | }, |
| 243 | { |
| 244 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 245 | { |
| 246 | clip_natural_uint8_t_from_stereo, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 247 | clip_natural_uint16_t_from_stereo, |
| 248 | clip_natural_uint32_t_from_stereo |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 249 | }, |
| 250 | { |
| 251 | clip_natural_uint8_t_from_stereo, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 252 | clip_swap_uint16_t_from_stereo, |
| 253 | clip_swap_uint32_t_from_stereo |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 254 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 255 | }, |
| 256 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 257 | { |
| 258 | clip_natural_int8_t_from_stereo, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 259 | clip_natural_int16_t_from_stereo, |
| 260 | clip_natural_int32_t_from_stereo |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 261 | }, |
| 262 | { |
| 263 | clip_natural_int8_t_from_stereo, |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 264 | clip_swap_int16_t_from_stereo, |
| 265 | clip_swap_int32_t_from_stereo |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 266 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | }; |
| 270 | |
Pavel Dovgalyuk | 3d4d16f | 2017-02-02 08:50:54 +0300 | [diff] [blame] | 271 | |
| 272 | void audio_sample_to_uint64(void *samples, int pos, |
| 273 | uint64_t *left, uint64_t *right) |
| 274 | { |
| 275 | struct st_sample *sample = samples; |
| 276 | sample += pos; |
| 277 | #ifdef FLOAT_MIXENG |
| 278 | error_report( |
| 279 | "Coreaudio and floating point samples are not supported by replay yet"); |
| 280 | abort(); |
| 281 | #else |
| 282 | *left = sample->l; |
| 283 | *right = sample->r; |
| 284 | #endif |
| 285 | } |
| 286 | |
| 287 | void audio_sample_from_uint64(void *samples, int pos, |
| 288 | uint64_t left, uint64_t right) |
| 289 | { |
| 290 | struct st_sample *sample = samples; |
| 291 | sample += pos; |
| 292 | #ifdef FLOAT_MIXENG |
| 293 | error_report( |
| 294 | "Coreaudio and floating point samples are not supported by replay yet"); |
| 295 | abort(); |
| 296 | #else |
| 297 | sample->l = left; |
| 298 | sample->r = right; |
| 299 | #endif |
| 300 | } |
| 301 | |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 302 | /* |
| 303 | * August 21, 1998 |
| 304 | * Copyright 1998 Fabrice Bellard. |
| 305 | * |
Stefan Weil | cb8d4c8 | 2016-03-23 15:59:57 +0100 | [diff] [blame] | 306 | * [Rewrote completely the code of Lance Norskog And Sundry |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 307 | * Contributors with a more efficient algorithm.] |
| 308 | * |
| 309 | * This source code is freely redistributable and may be used for |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 310 | * any purpose. This copyright notice must be maintained. |
| 311 | * Lance Norskog And Sundry Contributors are not responsible for |
| 312 | * the consequences of using this software. |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 313 | */ |
| 314 | |
| 315 | /* |
| 316 | * Sound Tools rate change effect file. |
| 317 | */ |
| 318 | /* |
| 319 | * Linear Interpolation. |
| 320 | * |
| 321 | * The use of fractional increment allows us to use no buffer. It |
| 322 | * avoid the problems at the end of the buffer we had with the old |
| 323 | * method which stored a possibly big buffer of size |
| 324 | * lcm(in_rate,out_rate). |
| 325 | * |
| 326 | * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If |
| 327 | * the input & output frequencies are equal, a delay of one sample is |
| 328 | * introduced. Limited to processing 32-bit count worth of samples. |
| 329 | * |
| 330 | * 1 << FRAC_BITS evaluating to zero in several places. Changed with |
| 331 | * an (unsigned long) cast to make it safe. MarkMLl 2/1/99 |
| 332 | */ |
| 333 | |
| 334 | /* Private data */ |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 335 | struct rate { |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 336 | uint64_t opos; |
| 337 | uint64_t opos_inc; |
| 338 | uint32_t ipos; /* position in the input stream (integer) */ |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 339 | struct st_sample ilast; /* last sample in the input stream */ |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 340 | }; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * Prepare processing. |
| 344 | */ |
| 345 | void *st_rate_start (int inrate, int outrate) |
| 346 | { |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 347 | struct rate *rate = audio_calloc(__func__, 1, sizeof(*rate)); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 348 | |
| 349 | if (!rate) { |
bellard | e7cad33 | 2005-11-11 00:03:36 +0000 | [diff] [blame] | 350 | dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate)); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 351 | return NULL; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | rate->opos = 0; |
| 355 | |
| 356 | /* increment */ |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 357 | rate->opos_inc = ((uint64_t) inrate << 32) / outrate; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 358 | |
| 359 | rate->ipos = 0; |
| 360 | rate->ilast.l = 0; |
| 361 | rate->ilast.r = 0; |
| 362 | return rate; |
| 363 | } |
| 364 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 365 | #define NAME st_rate_flow_mix |
| 366 | #define OP(a, b) a += b |
| 367 | #include "rate_template.h" |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 368 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 369 | #define NAME st_rate_flow |
| 370 | #define OP(a, b) a = b |
| 371 | #include "rate_template.h" |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 372 | |
| 373 | void st_rate_stop (void *opaque) |
| 374 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 375 | g_free (opaque); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 376 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 377 | |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 378 | void mixeng_clear (struct st_sample *buf, int len) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 379 | { |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 380 | memset (buf, 0, len * sizeof (struct st_sample)); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 381 | } |
Michael Walle | 00e0767 | 2011-01-05 01:05:47 +0100 | [diff] [blame] | 382 | |
| 383 | void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol) |
| 384 | { |
Michael Walle | 00e0767 | 2011-01-05 01:05:47 +0100 | [diff] [blame] | 385 | if (vol->mute) { |
| 386 | mixeng_clear (buf, len); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | while (len--) { |
| 391 | #ifdef FLOAT_MIXENG |
| 392 | buf->l = buf->l * vol->l; |
| 393 | buf->r = buf->r * vol->r; |
| 394 | #else |
| 395 | buf->l = (buf->l * vol->l) >> 32; |
| 396 | buf->r = (buf->r * vol->r) >> 32; |
| 397 | #endif |
| 398 | buf += 1; |
| 399 | } |
Michael Walle | 00e0767 | 2011-01-05 01:05:47 +0100 | [diff] [blame] | 400 | } |