Skip to content

Commit f5a8e3d

Browse files
committed
Calculate current delay for a BlockInput correctly
1 parent af93217 commit f5a8e3d

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

shared-bindings/audiodelays/Echo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//| samples_signed: bool = True,
3434
//| channel_count: int = 1,
3535
//| ) -> None:
36-
//| """Create a Echo effect that echos an audio sample every set number of microseconds.
36+
//| """Create a Echo effect that echos an audio sample every set number of milliseconds.
3737
//|
3838
//| :param int max_delay_ms: The maximum delay the echo can be
3939
//| :param BlockInput delay_ms: The current echo delay
@@ -132,7 +132,7 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiodelays_echo___exit___obj, 4, 4,
132132

133133

134134
//| delay_ms: BlockInput
135-
//| """Delay of the echo in microseconds. (read-only)"""
135+
//| """Delay of the echo in milliseconds. (read-only)"""
136136
//|
137137
static mp_obj_t audiodelays_echo_obj_get_delay_ms(mp_obj_t self_in) {
138138
audiodelays_echo_obj_t *self = MP_OBJ_TO_PTR(self_in);

shared-module/audiodelays/Echo.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_
8686

8787
// calculate current echo buffer size we use for the given delay
8888
mp_float_t f_delay_ms = synthio_block_slot_get(&self->delay_ms);
89+
self->current_delay_ms = f_delay_ms;
8990
self->echo_buffer_len = self->sample_rate / 1000.0f * f_delay_ms * (self->channel_count * sizeof(uint16_t));
9091

91-
// read is where we store the incoming sample + previous echo
92-
// write is what we send to the outgoing buffer
92+
// read is where we read previous echo from delay_ms ago to play back now
93+
// write is where the store the latest playing sample to echo back later
9394
self->echo_buffer_read_pos = self->buffer_len / sizeof(uint16_t);
9495
self->echo_buffer_write_pos = 0;
9596
}
@@ -119,17 +120,24 @@ void common_hal_audiodelays_echo_set_delay_ms(audiodelays_echo_obj_t *self, mp_o
119120
synthio_block_assign_slot(delay_ms, &self->delay_ms, MP_QSTR_delay_ms);
120121

121122
mp_float_t f_delay_ms = synthio_block_slot_get(&self->delay_ms);
122-
self->echo_buffer_len = self->sample_rate / 1000.0f * f_delay_ms * (self->channel_count * 2);// (self->bits_per_sample / 8));
123123

124-
uint32_t max_ebuf_length = self->echo_buffer_len / sizeof(uint16_t);
124+
recalculate_delay(self, f_delay_ms);
125+
}
126+
127+
void recalculate_delay(audiodelays_echo_obj_t *self, mp_float_t f_delay_ms) {
128+
// Calculate the current echo buffer length in bytes
125129

126-
if (self->echo_buffer_read_pos > max_ebuf_length) {
127-
self->echo_buffer_read_pos = 0;
128-
self->echo_buffer_write_pos = max_ebuf_length - (self->buffer_len / sizeof(uint16_t));
129-
} else if (self->echo_buffer_write_pos > max_ebuf_length) {
130-
self->echo_buffer_read_pos = self->buffer_len / sizeof(uint16_t);
131-
self->echo_buffer_write_pos = 0;
130+
uint32_t new_echo_buffer_len = self->sample_rate / 1000.0f * f_delay_ms * sizeof(uint16_t);
131+
132+
// Check if our new echo is too long for our maximum buffer
133+
if (new_echo_buffer_len > self->max_echo_buffer_len) {
134+
return;
135+
} else if (new_echo_buffer_len < 0.0) { // or too short!
136+
return;
132137
}
138+
139+
self->echo_buffer_len = new_echo_buffer_len;
140+
self->current_delay_ms = f_delay_ms;
133141
}
134142

135143
mp_obj_t common_hal_audiodelays_echo_get_decay(audiodelays_echo_obj_t *self) {
@@ -250,6 +258,11 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
250258
mp_float_t mix = MIN(1.0, MAX(synthio_block_slot_get(&self->mix), 0.0));
251259
mp_float_t decay = MIN(1.0, MAX(synthio_block_slot_get(&self->decay), 0.0));
252260

261+
uint32_t delay_ms = (uint32_t)synthio_block_slot_get(&self->delay_ms);
262+
if (self->current_delay_ms != delay_ms) {
263+
recalculate_delay(self, delay_ms);
264+
}
265+
253266
// Switch our buffers to the other buffer
254267
self->last_buf_idx = !self->last_buf_idx;
255268

shared-module/audiodelays/Echo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ typedef struct {
1616
mp_obj_base_t base;
1717
uint32_t max_delay_ms;
1818
synthio_block_slot_t delay_ms;
19+
uint32_t current_delay_ms;
1920
synthio_block_slot_t decay;
2021
synthio_block_slot_t mix;
2122

@@ -44,6 +45,8 @@ typedef struct {
4445
mp_obj_t sample;
4546
} audiodelays_echo_obj_t;
4647

48+
void recalculate_delay(audiodelays_echo_obj_t *self, mp_float_t f_delay_ms);
49+
4750
void audiodelays_echo_reset_buffer(audiodelays_echo_obj_t *self,
4851
bool single_channel_output,
4952
uint8_t channel);

0 commit comments

Comments
 (0)