forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathMixer.c
346 lines (310 loc) · 11.5 KB
/
Mixer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include "shared-bindings/audiomixer/Mixer.h"
#include "shared-bindings/audiomixer/MixerVoice.h"
#include <stdint.h>
#include "py/runtime.h"
#include "shared-module/audiocore/__init__.h"
#if defined(__arm__) && __arm__
#include "cmsis_compiler.h"
#endif
void common_hal_audiomixer_mixer_construct(audiomixer_mixer_obj_t *self,
uint8_t voice_count,
uint32_t buffer_size,
uint8_t bits_per_sample,
bool samples_signed,
uint8_t channel_count,
uint32_t sample_rate) {
self->len = buffer_size / 2 / sizeof(uint32_t) * sizeof(uint32_t);
self->first_buffer = m_malloc(self->len);
if (self->first_buffer == NULL) {
common_hal_audiomixer_mixer_deinit(self);
m_malloc_fail(self->len);
}
self->second_buffer = m_malloc(self->len);
if (self->second_buffer == NULL) {
common_hal_audiomixer_mixer_deinit(self);
m_malloc_fail(self->len);
}
self->bits_per_sample = bits_per_sample;
self->samples_signed = samples_signed;
self->channel_count = channel_count;
self->sample_rate = sample_rate;
self->voice_count = voice_count;
}
void common_hal_audiomixer_mixer_deinit(audiomixer_mixer_obj_t *self) {
self->first_buffer = NULL;
self->second_buffer = NULL;
}
bool common_hal_audiomixer_mixer_deinited(audiomixer_mixer_obj_t *self) {
return self->first_buffer == NULL;
}
uint32_t common_hal_audiomixer_mixer_get_sample_rate(audiomixer_mixer_obj_t *self) {
return self->sample_rate;
}
uint8_t common_hal_audiomixer_mixer_get_channel_count(audiomixer_mixer_obj_t *self) {
return self->channel_count;
}
uint8_t common_hal_audiomixer_mixer_get_bits_per_sample(audiomixer_mixer_obj_t *self) {
return self->bits_per_sample;
}
bool common_hal_audiomixer_mixer_get_playing(audiomixer_mixer_obj_t *self) {
for (uint8_t v = 0; v < self->voice_count; v++) {
if (common_hal_audiomixer_mixervoice_get_playing(MP_OBJ_TO_PTR(self->voice[v]))) {
return true;
}
}
return false;
}
void audiomixer_mixer_reset_buffer(audiomixer_mixer_obj_t *self,
bool single_channel_output,
uint8_t channel) {
for (uint8_t i = 0; i < self->voice_count; i++) {
common_hal_audiomixer_mixervoice_stop(self->voice[i]);
}
}
__attribute__((always_inline))
static inline uint32_t add16signed(uint32_t a, uint32_t b) {
#if (defined(__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1))
return __QADD16(a, b);
#else
uint32_t result = 0;
for (int8_t i = 0; i < 2; i++) {
int16_t ai = a >> (sizeof(int16_t) * 8 * i);
int16_t bi = b >> (sizeof(int16_t) * 8 * i);
int32_t intermediate = (int32_t)ai + bi;
if (intermediate > SHRT_MAX) {
intermediate = SHRT_MAX;
} else if (intermediate < SHRT_MIN) {
intermediate = SHRT_MIN;
}
result |= (((uint32_t)intermediate) & 0xffff) << (sizeof(int16_t) * 8 * i);
}
return result;
#endif
}
__attribute__((always_inline))
static inline uint32_t mult16signed(uint32_t val, int32_t mul) {
#if (defined(__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1))
mul <<= 16;
int32_t hi, lo;
enum { bits = 16 }; // saturate to 16 bits
enum { shift = 15 }; // shift is done automatically
asm volatile ("smulwb %0, %1, %2" : "=r" (lo) : "r" (mul), "r" (val));
asm volatile ("smulwt %0, %1, %2" : "=r" (hi) : "r" (mul), "r" (val));
asm volatile ("ssat %0, %1, %2, asr %3" : "=r" (lo) : "I" (bits), "r" (lo), "I" (shift));
asm volatile ("ssat %0, %1, %2, asr %3" : "=r" (hi) : "I" (bits), "r" (hi), "I" (shift));
asm volatile ("pkhbt %0, %1, %2, lsl #16" : "=r" (val) : "r" (lo), "r" (hi)); // pack
return val;
#else
uint32_t result = 0;
float mod_mul = (float)mul / (float)((1 << 15) - 1);
for (int8_t i = 0; i < 2; i++) {
int16_t ai = (val >> (sizeof(uint16_t) * 8 * i));
int32_t intermediate = (int32_t)(ai * mod_mul);
if (intermediate > SHRT_MAX) {
intermediate = SHRT_MAX;
} else if (intermediate < SHRT_MIN) {
intermediate = SHRT_MIN;
}
intermediate &= 0x0000FFFF;
result |= (((uint32_t)intermediate)) << (sizeof(int16_t) * 8 * i);
}
return result;
#endif
}
static inline uint32_t tounsigned8(uint32_t val) {
#if (defined(__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1))
return __UADD8(val, 0x80808080);
#else
return val ^ 0x80808080;
#endif
}
static inline uint32_t tounsigned16(uint32_t val) {
#if (defined(__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1))
return __UADD16(val, 0x80008000);
#else
return val ^ 0x80008000;
#endif
}
static inline uint32_t tosigned16(uint32_t val) {
#if (defined(__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1))
return __UADD16(val, 0x80008000);
#else
return val ^ 0x80008000;
#endif
}
static inline uint32_t unpack8(uint16_t val) {
return ((val & 0xff00) << 16) | ((val & 0x00ff) << 8);
}
static inline uint32_t pack8(uint32_t val) {
return ((val & 0xff000000) >> 16) | ((val & 0xff00) >> 8);
}
static void mix_down_one_voice(audiomixer_mixer_obj_t *self,
audiomixer_mixervoice_obj_t *voice, bool voices_active,
uint32_t *word_buffer, uint32_t length) {
while (length != 0) {
if (voice->buffer_length == 0) {
if (!voice->more_data) {
if (voice->loop) {
audiosample_reset_buffer(voice->sample, false, 0);
} else {
voice->sample = NULL;
break;
}
}
if (voice->sample) {
// Load another buffer
audioio_get_buffer_result_t result = audiosample_get_buffer(voice->sample, false, 0, (uint8_t **)&voice->remaining_buffer, &voice->buffer_length);
// Track length in terms of words.
voice->buffer_length /= sizeof(uint32_t);
voice->more_data = result == GET_BUFFER_MORE_DATA;
}
}
uint32_t n = MIN(voice->buffer_length, length);
uint32_t *src = voice->remaining_buffer;
uint16_t level = voice->level;
// First active voice gets copied over verbatim.
if (!voices_active) {
if (MP_LIKELY(self->bits_per_sample == 16)) {
if (MP_LIKELY(self->samples_signed)) {
for (uint32_t i = 0; i < n; i++) {
uint32_t v = src[i];
word_buffer[i] = mult16signed(v, level);
}
} else {
for (uint32_t i = 0; i < n; i++) {
uint32_t v = src[i];
v = tosigned16(v);
word_buffer[i] = mult16signed(v, level);
}
}
} else {
uint16_t *hword_buffer = (uint16_t *)word_buffer;
uint16_t *hsrc = (uint16_t *)src;
for (uint32_t i = 0; i < n * 2; i++) {
uint32_t word = unpack8(hsrc[i]);
if (MP_LIKELY(!self->samples_signed)) {
word = tosigned16(word);
}
word = mult16signed(word, level);
hword_buffer[i] = pack8(word);
}
}
} else {
if (MP_LIKELY(self->bits_per_sample == 16)) {
if (MP_LIKELY(self->samples_signed)) {
for (uint32_t i = 0; i < n; i++) {
uint32_t word = src[i];
word_buffer[i] = add16signed(mult16signed(word, level), word_buffer[i]);
}
} else {
for (uint32_t i = 0; i < n; i++) {
uint32_t word = src[i];
word = tosigned16(word);
word_buffer[i] = add16signed(mult16signed(word, level), word_buffer[i]);
}
}
} else {
uint16_t *hword_buffer = (uint16_t *)word_buffer;
uint16_t *hsrc = (uint16_t *)src;
for (uint32_t i = 0; i < n * 2; i++) {
uint32_t word = unpack8(hsrc[i]);
if (MP_LIKELY(!self->samples_signed)) {
word = tosigned16(word);
}
word = mult16signed(word, level);
word = add16signed(word, unpack8(hword_buffer[i]));
hword_buffer[i] = pack8(word);
}
}
}
length -= n;
word_buffer += n;
voice->remaining_buffer += n;
voice->buffer_length -= n;
}
if (length && !voices_active) {
for (uint32_t i = 0; i < length; i++) {
word_buffer[i] = 0;
}
}
}
audioio_get_buffer_result_t audiomixer_mixer_get_buffer(audiomixer_mixer_obj_t *self,
bool single_channel_output,
uint8_t channel,
uint8_t **buffer,
uint32_t *buffer_length) {
if (!single_channel_output) {
channel = 0;
}
uint32_t channel_read_count = self->left_read_count;
if (channel == 1) {
channel_read_count = self->right_read_count;
}
*buffer_length = self->len;
bool need_more_data = self->read_count == channel_read_count;
if (need_more_data) {
uint32_t *word_buffer;
if (self->use_first_buffer) {
*buffer = (uint8_t *)self->first_buffer;
word_buffer = self->first_buffer;
} else {
*buffer = (uint8_t *)self->second_buffer;
word_buffer = self->second_buffer;
}
self->use_first_buffer = !self->use_first_buffer;
bool voices_active = false;
uint32_t length = self->len / sizeof(uint32_t);
for (int32_t v = 0; v < self->voice_count; v++) {
audiomixer_mixervoice_obj_t *voice = MP_OBJ_TO_PTR(self->voice[v]);
if (voice->sample) {
mix_down_one_voice(self, voice, voices_active, word_buffer, length);
voices_active = true;
}
}
if (!voices_active) {
for (uint32_t i = 0; i < length; i++) {
word_buffer[i] = 0;
}
}
if (!self->samples_signed) {
if (self->bits_per_sample == 16) {
for (uint32_t i = 0; i < length; i++) {
word_buffer[i] = tounsigned16(word_buffer[i]);
}
} else {
for (uint32_t i = 0; i < length; i++) {
word_buffer[i] = tounsigned8(word_buffer[i]);
}
}
}
self->read_count += 1;
} else if (!self->use_first_buffer) {
*buffer = (uint8_t *)self->first_buffer;
} else {
*buffer = (uint8_t *)self->second_buffer;
}
if (channel == 0) {
self->left_read_count += 1;
} else if (channel == 1) {
self->right_read_count += 1;
*buffer = *buffer + self->bits_per_sample / 8;
}
return GET_BUFFER_MORE_DATA;
}
void audiomixer_mixer_get_buffer_structure(audiomixer_mixer_obj_t *self, bool single_channel_output,
bool *single_buffer, bool *samples_signed,
uint32_t *max_buffer_length, uint8_t *spacing) {
*single_buffer = false;
*samples_signed = self->samples_signed;
*max_buffer_length = self->len;
if (single_channel_output) {
*spacing = self->channel_count;
} else {
*spacing = 1;
}
}