Skip to content

Commit 2455002

Browse files
committed
Changes for PR #10036
1 parent 23a4df4 commit 2455002

File tree

3 files changed

+30
-88
lines changed

3 files changed

+30
-88
lines changed

shared-bindings/audiodelays/Chorus.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "shared-bindings/audiodelays/Chorus.h"
1010
#include "shared-module/audiodelays/Chorus.h"
11+
#include "shared-bindings/audiocore/__init__.h"
1112

1213
#include "shared/runtime/context_manager_helpers.h"
1314
#include "py/binary.h"
@@ -110,9 +111,7 @@ static mp_obj_t audiodelays_chorus_deinit(mp_obj_t self_in) {
110111
static MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_chorus_deinit_obj, audiodelays_chorus_deinit);
111112

112113
static void check_for_deinit(audiodelays_chorus_obj_t *self) {
113-
if (common_hal_audiodelays_chorus_deinited(self)) {
114-
raise_deinited_error();
115-
}
114+
audiosample_check_for_deinit(&self->base);
116115
}
117116

118117
//| def __enter__(self) -> Chorus:
@@ -237,17 +236,14 @@ static const mp_rom_map_elem_t audiodelays_chorus_locals_dict_table[] = {
237236
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audiodelays_chorus_playing_obj) },
238237
{ MP_ROM_QSTR(MP_QSTR_delay_ms), MP_ROM_PTR(&audiodelays_chorus_delay_ms_obj) },
239238
{ MP_ROM_QSTR(MP_QSTR_voices), MP_ROM_PTR(&audiodelays_chorus_voices_obj) },
239+
AUDIOSAMPLE_FIELDS,
240240
};
241241
static MP_DEFINE_CONST_DICT(audiodelays_chorus_locals_dict, audiodelays_chorus_locals_dict_table);
242242

243243
static const audiosample_p_t audiodelays_chorus_proto = {
244244
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample)
245-
.sample_rate = (audiosample_sample_rate_fun)common_hal_audiodelays_chorus_get_sample_rate,
246-
.bits_per_sample = (audiosample_bits_per_sample_fun)common_hal_audiodelays_chorus_get_bits_per_sample,
247-
.channel_count = (audiosample_channel_count_fun)common_hal_audiodelays_chorus_get_channel_count,
248245
.reset_buffer = (audiosample_reset_buffer_fun)audiodelays_chorus_reset_buffer,
249246
.get_buffer = (audiosample_get_buffer_fun)audiodelays_chorus_get_buffer,
250-
.get_buffer_structure = (audiosample_get_buffer_structure_fun)audiodelays_chorus_get_buffer_structure,
251247
};
252248

253249
MP_DEFINE_CONST_OBJ_TYPE(

shared-module/audiodelays/Chorus.c

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uin
1616

1717
// Basic settings every effect and audio sample has
1818
// These are the effects values, not the source sample(s)
19-
self->bits_per_sample = bits_per_sample; // Most common is 16, but 8 is also supported in many places
20-
self->samples_signed = samples_signed; // Are the samples we provide signed (common is true)
21-
self->channel_count = channel_count; // Channels can be 1 for mono or 2 for stereo
22-
self->sample_rate = sample_rate; // Sample rate for the effect, this generally needs to match all audio objects
19+
self->base.bits_per_sample = bits_per_sample; // Most common is 16, but 8 is also supported in many places
20+
self->base.samples_signed = samples_signed; // Are the samples we provide signed (common is true)
21+
self->base.channel_count = channel_count; // Channels can be 1 for mono or 2 for stereo
22+
self->base.sample_rate = sample_rate; // Sample rate for the effect, this generally needs to match all audio objects
23+
self->base.single_buffer = false;
24+
self->base.max_buffer_length = buffer_size;
2325

2426
// To smooth things out as CircuitPython is doing other tasks most audio objects have a buffer
2527
// A double buffer is set up here so the audio output can use DMA on buffer 1 while we
@@ -70,7 +72,7 @@ void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uin
7072

7173
// Allocate the chorus buffer for the max possible delay, chorus is always 16-bit
7274
self->max_delay_ms = max_delay_ms;
73-
self->max_chorus_buffer_len = self->sample_rate / MICROPY_FLOAT_CONST(1000.0) * max_delay_ms * (self->channel_count * sizeof(uint16_t)); // bytes
75+
self->max_chorus_buffer_len = self->base.sample_rate / MICROPY_FLOAT_CONST(1000.0) * max_delay_ms * (self->base.channel_count * sizeof(uint16_t)); // bytes
7476
self->chorus_buffer = m_malloc(self->max_chorus_buffer_len);
7577
if (self->chorus_buffer == NULL) {
7678
common_hal_audiodelays_chorus_deinit(self);
@@ -79,7 +81,7 @@ void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uin
7981
memset(self->chorus_buffer, 0, self->max_chorus_buffer_len);
8082

8183
// calculate the length of a single sample in milliseconds
82-
self->sample_ms = MICROPY_FLOAT_CONST(1000.0) / self->sample_rate;
84+
self->sample_ms = MICROPY_FLOAT_CONST(1000.0) / self->base.sample_rate;
8385

8486
// calculate everything needed for the current delay
8587
mp_float_t f_delay_ms = synthio_block_slot_get(&self->delay_ms);
@@ -122,7 +124,7 @@ void chorus_recalculate_delay(audiodelays_chorus_obj_t *self, mp_float_t f_delay
122124
f_delay_ms = MAX(f_delay_ms, self->sample_ms);
123125

124126
// Calculate the current chorus buffer length in bytes
125-
uint32_t new_chorus_buffer_len = (uint32_t)(self->sample_rate / MICROPY_FLOAT_CONST(1000.0) * f_delay_ms) * (self->channel_count * sizeof(uint16_t));
127+
uint32_t new_chorus_buffer_len = (uint32_t)(self->base.sample_rate / MICROPY_FLOAT_CONST(1000.0) * f_delay_ms) * (self->base.channel_count * sizeof(uint16_t));
126128

127129
if (new_chorus_buffer_len < 0) { // or too short!
128130
return;
@@ -141,18 +143,6 @@ void common_hal_audiodelays_chorus_set_voices(audiodelays_chorus_obj_t *self, mp
141143
synthio_block_assign_slot(voices, &self->voices, MP_QSTR_voices);
142144
}
143145

144-
uint32_t common_hal_audiodelays_chorus_get_sample_rate(audiodelays_chorus_obj_t *self) {
145-
return self->sample_rate;
146-
}
147-
148-
uint8_t common_hal_audiodelays_chorus_get_channel_count(audiodelays_chorus_obj_t *self) {
149-
return self->channel_count;
150-
}
151-
152-
uint8_t common_hal_audiodelays_chorus_get_bits_per_sample(audiodelays_chorus_obj_t *self) {
153-
return self->bits_per_sample;
154-
}
155-
156146
void audiodelays_chorus_reset_buffer(audiodelays_chorus_obj_t *self,
157147
bool single_channel_output,
158148
uint8_t channel) {
@@ -167,27 +157,7 @@ bool common_hal_audiodelays_chorus_get_playing(audiodelays_chorus_obj_t *self) {
167157
}
168158

169159
void common_hal_audiodelays_chorus_play(audiodelays_chorus_obj_t *self, mp_obj_t sample, bool loop) {
170-
// When a sample is to be played we must ensure the samples values matches what we expect
171-
// Then we reset the sample and get the first buffer to play
172-
// The get_buffer function will actually process that data
173-
174-
if (audiosample_sample_rate(sample) != self->sample_rate) {
175-
mp_raise_ValueError_varg(MP_ERROR_TEXT("The sample's %q does not match"), MP_QSTR_sample_rate);
176-
}
177-
if (audiosample_channel_count(sample) != self->channel_count) {
178-
mp_raise_ValueError_varg(MP_ERROR_TEXT("The sample's %q does not match"), MP_QSTR_channel_count);
179-
}
180-
if (audiosample_bits_per_sample(sample) != self->bits_per_sample) {
181-
mp_raise_ValueError_varg(MP_ERROR_TEXT("The sample's %q does not match"), MP_QSTR_bits_per_sample);
182-
}
183-
bool single_buffer;
184-
bool samples_signed;
185-
uint32_t max_buffer_length;
186-
uint8_t spacing;
187-
audiosample_get_buffer_structure(sample, false, &single_buffer, &samples_signed, &max_buffer_length, &spacing);
188-
if (samples_signed != self->samples_signed) {
189-
mp_raise_ValueError_varg(MP_ERROR_TEXT("The sample's %q does not match"), MP_QSTR_signedness);
190-
}
160+
audiosample_must_match(&self->base, sample);
191161

192162
self->sample = sample;
193163
self->loop = loop;
@@ -196,7 +166,7 @@ void common_hal_audiodelays_chorus_play(audiodelays_chorus_obj_t *self, mp_obj_t
196166
audioio_get_buffer_result_t result = audiosample_get_buffer(self->sample, false, 0, (uint8_t **)&self->sample_remaining_buffer, &self->sample_buffer_length);
197167

198168
// Track remaining sample length in terms of bytes per sample
199-
self->sample_buffer_length /= (self->bits_per_sample / 8);
169+
self->sample_buffer_length /= (self->base.bits_per_sample / 8);
200170
// Store if we have more data in the sample to retrieve
201171
self->more_data = result == GET_BUFFER_MORE_DATA;
202172

@@ -219,7 +189,7 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
219189
// If we are using 16 bit samples we need a 16 bit pointer, 8 bit needs an 8 bit pointer
220190
int16_t *word_buffer = (int16_t *)self->buffer[self->last_buf_idx];
221191
int8_t *hword_buffer = self->buffer[self->last_buf_idx];
222-
uint32_t length = self->buffer_len / (self->bits_per_sample / 8);
192+
uint32_t length = self->buffer_len / (self->base.bits_per_sample / 8);
223193

224194
// The chorus buffer is always stored as a 16-bit value internally
225195
int16_t *chorus_buffer = (int16_t *)self->chorus_buffer;
@@ -240,21 +210,21 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
240210
// Load another sample buffer to play
241211
audioio_get_buffer_result_t result = audiosample_get_buffer(self->sample, false, 0, (uint8_t **)&self->sample_remaining_buffer, &self->sample_buffer_length);
242212
// Track length in terms of words.
243-
self->sample_buffer_length /= (self->bits_per_sample / 8);
213+
self->sample_buffer_length /= (self->base.bits_per_sample / 8);
244214
self->more_data = result == GET_BUFFER_MORE_DATA;
245215
}
246216
}
247217

248218
// Determine how many bytes we can process to our buffer, the less of the sample we have left and our buffer remaining
249219
uint32_t n;
250220
if (self->sample == NULL) {
251-
n = MIN(length, SYNTHIO_MAX_DUR * self->channel_count);
221+
n = MIN(length, SYNTHIO_MAX_DUR * self->base.channel_count);
252222
} else {
253-
n = MIN(MIN(self->sample_buffer_length, length), SYNTHIO_MAX_DUR * self->channel_count);
223+
n = MIN(MIN(self->sample_buffer_length, length), SYNTHIO_MAX_DUR * self->base.channel_count);
254224
}
255225

256226
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
257-
shared_bindings_synthio_lfo_tick(self->sample_rate, n / self->channel_count);
227+
shared_bindings_synthio_lfo_tick(self->base.sample_rate, n / self->base.channel_count);
258228

259229
int32_t voices = MAX(synthio_block_slot_get(&self->voices), 1.0);
260230

@@ -264,17 +234,17 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
264234
}
265235

266236
if (self->sample == NULL) {
267-
if (self->samples_signed) {
268-
memset(word_buffer, 0, n * (self->bits_per_sample / 8));
237+
if (self->base.samples_signed) {
238+
memset(word_buffer, 0, n * (self->base.bits_per_sample / 8));
269239
} else {
270240
// For unsigned samples set to the middle which is "quiet"
271-
if (MP_LIKELY(self->bits_per_sample == 16)) {
241+
if (MP_LIKELY(self->base.bits_per_sample == 16)) {
272242
uint16_t *uword_buffer = (uint16_t *)word_buffer;
273243
for (uint32_t i = 0; i < n; i++) {
274244
*uword_buffer++ = 32768;
275245
}
276246
} else {
277-
memset(hword_buffer, 128, n * (self->bits_per_sample / 8));
247+
memset(hword_buffer, 128, n * (self->base.bits_per_sample / 8));
278248
}
279249
}
280250
} else {
@@ -283,10 +253,10 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
283253

284254
for (uint32_t i = 0; i < n; i++) {
285255
int32_t sample_word = 0;
286-
if (MP_LIKELY(self->bits_per_sample == 16)) {
256+
if (MP_LIKELY(self->base.bits_per_sample == 16)) {
287257
sample_word = sample_src[i];
288258
} else {
289-
if (self->samples_signed) {
259+
if (self->base.samples_signed) {
290260
sample_word = sample_hsrc[i];
291261
} else {
292262
// Be careful here changing from an 8 bit unsigned to signed into a 32-bit signed
@@ -316,14 +286,14 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
316286
word = synthio_mix_down_sample(word, SYNTHIO_MIX_DOWN_SCALE(2));
317287
}
318288

319-
if (MP_LIKELY(self->bits_per_sample == 16)) {
289+
if (MP_LIKELY(self->base.bits_per_sample == 16)) {
320290
word_buffer[i] = word;
321-
if (!self->samples_signed) {
291+
if (!self->base.samples_signed) {
322292
word_buffer[i] ^= 0x8000;
323293
}
324294
} else {
325295
int8_t out = word;
326-
if (self->samples_signed) {
296+
if (self->base.samples_signed) {
327297
hword_buffer[i] = out;
328298
} else {
329299
hword_buffer[i] = (uint8_t)out ^ 0x80;
@@ -334,7 +304,7 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
334304
self->chorus_buffer_pos = 0;
335305
}
336306
}
337-
self->sample_remaining_buffer += (n * (self->bits_per_sample / 8));
307+
self->sample_remaining_buffer += (n * (self->base.bits_per_sample / 8));
338308
self->sample_buffer_length -= n;
339309
}
340310
// Update the remaining length and the buffer positions based on how much we wrote into our buffer
@@ -350,18 +320,3 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
350320
// Chorus always returns more data but some effects may return GET_BUFFER_DONE or GET_BUFFER_ERROR (see audiocore/__init__.h)
351321
return GET_BUFFER_MORE_DATA;
352322
}
353-
354-
void audiodelays_chorus_get_buffer_structure(audiodelays_chorus_obj_t *self, bool single_channel_output,
355-
bool *single_buffer, bool *samples_signed, uint32_t *max_buffer_length, uint8_t *spacing) {
356-
357-
// Return information about the effect's buffer (not the sample's)
358-
// These are used by calling audio objects to determine how to handle the effect's buffer
359-
*single_buffer = false;
360-
*samples_signed = self->samples_signed;
361-
*max_buffer_length = self->buffer_len;
362-
if (single_channel_output) {
363-
*spacing = self->channel_count;
364-
} else {
365-
*spacing = 1;
366-
}
367-
}

shared-module/audiodelays/Chorus.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@
1313
extern const mp_obj_type_t audiodelays_chorus_type;
1414

1515
typedef struct {
16-
mp_obj_base_t base;
16+
audiosample_base_t base;
1717
uint32_t max_delay_ms;
1818
synthio_block_slot_t delay_ms;
1919
mp_float_t current_delay_ms;
2020
mp_float_t sample_ms;
2121
synthio_block_slot_t voices;
2222

23-
uint8_t bits_per_sample;
24-
bool samples_signed;
25-
uint8_t channel_count;
26-
uint32_t sample_rate;
27-
2823
int8_t *buffer[2];
2924
uint8_t last_buf_idx;
3025
uint32_t buffer_len; // max buffer in bytes
@@ -55,7 +50,3 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
5550
uint8_t channel,
5651
uint8_t **buffer,
5752
uint32_t *buffer_length); // length in bytes
58-
59-
void audiodelays_chorus_get_buffer_structure(audiodelays_chorus_obj_t *self, bool single_channel_output,
60-
bool *single_buffer, bool *samples_signed,
61-
uint32_t *max_buffer_length, uint8_t *spacing);

0 commit comments

Comments
 (0)