Skip to content

Commit 5fbbeed

Browse files
committed
Call shared_bindings_synthio_lfo_tick on audioeffects in SYNTHIO_MAX_DUR intervals.
1 parent 0e64e1c commit 5fbbeed

File tree

3 files changed

+69
-52
lines changed

3 files changed

+69
-52
lines changed

shared-module/audiodelays/Echo.c

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -292,28 +292,8 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
292292
int8_t *hword_buffer = self->buffer[self->last_buf_idx];
293293
uint32_t length = self->buffer_len / (self->bits_per_sample / 8);
294294

295-
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
296-
shared_bindings_synthio_lfo_tick(self->sample_rate, length / self->channel_count);
297-
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
298-
mp_float_t decay = synthio_block_slot_get_limited(&self->decay, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
299-
300-
uint32_t delay_ms = (uint32_t)synthio_block_slot_get(&self->delay_ms);
301-
if (self->current_delay_ms != delay_ms) {
302-
recalculate_delay(self, delay_ms);
303-
}
304-
305295
// The echo buffer is always stored as a 16-bit value internally
306296
int16_t *echo_buffer = (int16_t *)self->echo_buffer;
307-
uint32_t echo_buf_len = self->echo_buffer_len / sizeof(uint16_t);
308-
309-
// Set our echo buffer position accounting for stereo
310-
uint32_t echo_buffer_pos = 0;
311-
if (self->freq_shift) {
312-
echo_buffer_pos = self->echo_buffer_left_pos;
313-
if (channel == 1) {
314-
echo_buffer_pos = self->echo_buffer_right_pos;
315-
}
316-
}
317297

318298
// Loop over the entire length of our buffer to fill it, this may require several calls to get data from the sample
319299
while (length != 0) {
@@ -335,6 +315,35 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
335315
}
336316
}
337317

318+
// Determine how many bytes we can process to our buffer, the less of the sample we have left and our buffer remaining
319+
uint32_t n;
320+
if (self->sample == NULL) {
321+
n = MIN(length, SYNTHIO_MAX_DUR * self->channel_count);
322+
} else {
323+
n = MIN(MIN(self->sample_buffer_length, length), SYNTHIO_MAX_DUR * self->channel_count);
324+
}
325+
326+
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
327+
shared_bindings_synthio_lfo_tick(self->sample_rate, n / self->channel_count);
328+
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
329+
mp_float_t decay = synthio_block_slot_get_limited(&self->decay, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
330+
331+
uint32_t delay_ms = (uint32_t)synthio_block_slot_get(&self->delay_ms);
332+
if (self->current_delay_ms != delay_ms) {
333+
recalculate_delay(self, delay_ms);
334+
}
335+
336+
uint32_t echo_buf_len = self->echo_buffer_len / sizeof(uint16_t);
337+
338+
// Set our echo buffer position accounting for stereo
339+
uint32_t echo_buffer_pos = 0;
340+
if (self->freq_shift) {
341+
echo_buffer_pos = self->echo_buffer_left_pos;
342+
if (channel == 1) {
343+
echo_buffer_pos = self->echo_buffer_right_pos;
344+
}
345+
}
346+
338347
// If we have no sample keep the echo echoing
339348
if (self->sample == NULL) {
340349
if (mix <= MICROPY_FLOAT_CONST(0.01)) { // Mix of 0 is pure sample sound. We have no sample so no sound
@@ -401,9 +410,6 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
401410
length = 0;
402411
} else {
403412
// we have a sample to play and echo
404-
// Determine how many bytes we can process to our buffer, the less of the sample we have left and our buffer remaining
405-
uint32_t n = MIN(self->sample_buffer_length, length);
406-
407413
int16_t *sample_src = (int16_t *)self->sample_remaining_buffer; // for 16-bit samples
408414
int8_t *sample_hsrc = (int8_t *)self->sample_remaining_buffer; // for 8-bit samples
409415

@@ -501,13 +507,13 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
501507
self->sample_remaining_buffer += (n * (self->bits_per_sample / 8));
502508
self->sample_buffer_length -= n;
503509
}
504-
}
505510

506-
if (self->freq_shift) {
507-
if (channel == 0) {
508-
self->echo_buffer_left_pos = echo_buffer_pos;
509-
} else if (channel == 1) {
510-
self->echo_buffer_right_pos = echo_buffer_pos;
511+
if (self->freq_shift) {
512+
if (channel == 0) {
513+
self->echo_buffer_left_pos = echo_buffer_pos;
514+
} else if (channel == 1) {
515+
self->echo_buffer_right_pos = echo_buffer_pos;
516+
}
511517
}
512518
}
513519

shared-module/audiofilters/Distortion.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,6 @@ audioio_get_buffer_result_t audiofilters_distortion_get_buffer(audiofilters_dist
216216
int8_t *hword_buffer = self->buffer[self->last_buf_idx];
217217
uint32_t length = self->buffer_len / (self->bits_per_sample / 8);
218218

219-
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
220-
shared_bindings_synthio_lfo_tick(self->sample_rate, length / self->channel_count);
221-
mp_float_t drive = synthio_block_slot_get_limited(&self->drive, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
222-
mp_float_t pre_gain = db_to_linear(synthio_block_slot_get_limited(&self->pre_gain, MICROPY_FLOAT_CONST(-60.0), MICROPY_FLOAT_CONST(60.0)));
223-
mp_float_t post_gain = db_to_linear(synthio_block_slot_get_limited(&self->post_gain, MICROPY_FLOAT_CONST(-80.0), MICROPY_FLOAT_CONST(24.0)));
224-
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
225-
226-
// LOFI mode bit mask
227-
uint32_t word_mask = 0xFFFFFFFF ^ ((1 << (uint32_t)MICROPY_FLOAT_C_FUN(round)(drive * MICROPY_FLOAT_CONST(14.0))) - 1);
228-
229-
// Modify drive value depending on mode
230-
if (self->mode == DISTORTION_MODE_CLIP) {
231-
drive = MICROPY_FLOAT_CONST(1.0001) - drive;
232-
} else if (self->mode == DISTORTION_MODE_WAVESHAPE) {
233-
drive = MICROPY_FLOAT_CONST(2.0) * drive / (MICROPY_FLOAT_CONST(1.0001) - drive);
234-
}
235-
236219
// Loop over the entire length of our buffer to fill it, this may require several calls to get data from the sample
237220
while (length != 0) {
238221
// Check if there is no more sample to play, we will either load more data, reset the sample if loop is on or clear the sample
@@ -265,15 +248,39 @@ audioio_get_buffer_result_t audiofilters_distortion_get_buffer(audiofilters_dist
265248
}
266249
}
267250

251+
// tick all block inputs
252+
shared_bindings_synthio_lfo_tick(self->sample_rate, length / self->channel_count);
253+
(void)synthio_block_slot_get(&self->drive);
254+
(void)synthio_block_slot_get(&self->pre_gain);
255+
(void)synthio_block_slot_get(&self->post_gain);
256+
(void)synthio_block_slot_get(&self->mix);
257+
268258
length = 0;
269259
} else {
270260
// we have a sample to play and apply effect
271261
// Determine how many bytes we can process to our buffer, the less of the sample we have left and our buffer remaining
272-
uint32_t n = MIN(self->sample_buffer_length, length);
262+
uint32_t n = MIN(MIN(self->sample_buffer_length, length), SYNTHIO_MAX_DUR * self->channel_count);
273263

274264
int16_t *sample_src = (int16_t *)self->sample_remaining_buffer; // for 16-bit samples
275265
int8_t *sample_hsrc = (int8_t *)self->sample_remaining_buffer; // for 8-bit samples
276266

267+
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
268+
shared_bindings_synthio_lfo_tick(self->sample_rate, n / self->channel_count);
269+
mp_float_t drive = synthio_block_slot_get_limited(&self->drive, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
270+
mp_float_t pre_gain = db_to_linear(synthio_block_slot_get_limited(&self->pre_gain, MICROPY_FLOAT_CONST(-60.0), MICROPY_FLOAT_CONST(60.0)));
271+
mp_float_t post_gain = db_to_linear(synthio_block_slot_get_limited(&self->post_gain, MICROPY_FLOAT_CONST(-80.0), MICROPY_FLOAT_CONST(24.0)));
272+
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
273+
274+
// LOFI mode bit mask
275+
uint32_t word_mask = 0xFFFFFFFF ^ ((1 << (uint32_t)MICROPY_FLOAT_C_FUN(round)(drive * MICROPY_FLOAT_CONST(14.0))) - 1);
276+
277+
// Modify drive value depending on mode
278+
if (self->mode == DISTORTION_MODE_CLIP) {
279+
drive = MICROPY_FLOAT_CONST(1.0001) - drive;
280+
} else if (self->mode == DISTORTION_MODE_WAVESHAPE) {
281+
drive = MICROPY_FLOAT_CONST(2.0) * drive / (MICROPY_FLOAT_CONST(1.0001) - drive);
282+
}
283+
277284
if (mix <= MICROPY_FLOAT_CONST(0.01)) { // if mix is zero pure sample only
278285
for (uint32_t i = 0; i < n; i++) {
279286
if (MP_LIKELY(self->bits_per_sample == 16)) {

shared-module/audiofilters/Filter.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,6 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o
254254
int8_t *hword_buffer = self->buffer[self->last_buf_idx];
255255
uint32_t length = self->buffer_len / (self->bits_per_sample / 8);
256256

257-
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
258-
shared_bindings_synthio_lfo_tick(self->sample_rate, length / self->channel_count);
259-
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
260-
261257
// Loop over the entire length of our buffer to fill it, this may require several calls to get data from the sample
262258
while (length != 0) {
263259
// Check if there is no more sample to play, we will either load more data, reset the sample if loop is on or clear the sample
@@ -293,15 +289,23 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o
293289
}
294290
}
295291

292+
// tick all block inputs
293+
shared_bindings_synthio_lfo_tick(self->sample_rate, length / self->channel_count);
294+
(void)synthio_block_slot_get(&self->mix);
295+
296296
length = 0;
297297
} else {
298298
// we have a sample to play and filter
299299
// Determine how many bytes we can process to our buffer, the less of the sample we have left and our buffer remaining
300-
uint32_t n = MIN(self->sample_buffer_length, length);
300+
uint32_t n = MIN(MIN(self->sample_buffer_length, length), SYNTHIO_MAX_DUR * self->channel_count);
301301

302302
int16_t *sample_src = (int16_t *)self->sample_remaining_buffer; // for 16-bit samples
303303
int8_t *sample_hsrc = (int8_t *)self->sample_remaining_buffer; // for 8-bit samples
304304

305+
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
306+
shared_bindings_synthio_lfo_tick(self->sample_rate, n / self->channel_count);
307+
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
308+
305309
if (mix <= MICROPY_FLOAT_CONST(0.01) || !self->filter_states) { // if mix is zero pure sample only or no biquad filter objects are provided
306310
for (uint32_t i = 0; i < n; i++) {
307311
if (MP_LIKELY(self->bits_per_sample == 16)) {

0 commit comments

Comments
 (0)