Delay and Decay to BlockInput · csamuelson/circuitpython@4774807 · GitHub
Skip to content

Commit 4774807

Browse files
committed
Delay and Decay to BlockInput
1 parent d88b0c7 commit 4774807

4 files changed

Lines changed: 67 additions & 63 deletions

File tree

shared-bindings/audiodelays/Echo.c

Lines changed: 14 additions & 26 deletions

shared-bindings/audiodelays/Echo.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
extern const mp_obj_type_t audiodelays_echo_type;
1212

13-
void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self,
14-
uint32_t delay_ms, mp_float_t decay, mp_obj_t mix,
13+
void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_t max_delay_ms,
14+
mp_obj_t delay_ms, mp_obj_t decay, mp_obj_t mix,
1515
uint32_t buffer_size, uint8_t bits_per_sample, bool samples_signed,
1616
uint8_t channel_count, uint32_t sample_rate);
1717

@@ -22,11 +22,11 @@ uint32_t common_hal_audiodelays_echo_get_sample_rate(audiodelays_echo_obj_t *sel
2222
uint8_t common_hal_audiodelays_echo_get_channel_count(audiodelays_echo_obj_t *self);
2323
uint8_t common_hal_audiodelays_echo_get_bits_per_sample(audiodelays_echo_obj_t *self);
2424

25-
uint32_t common_hal_audiodelays_echo_get_delay_ms(audiodelays_echo_obj_t *self);
26-
void common_hal_audiodelays_echo_set_delay_ms(audiodelays_echo_obj_t *self, uint32_t delay_ms);
25+
mp_obj_t common_hal_audiodelays_echo_get_delay_ms(audiodelays_echo_obj_t *self);
26+
void common_hal_audiodelays_echo_set_delay_ms(audiodelays_echo_obj_t *self, mp_obj_t delay_ms);
2727

28-
mp_float_t common_hal_audiodelays_echo_get_decay(audiodelays_echo_obj_t *self);
29-
void common_hal_audiodelays_echo_set_decay(audiodelays_echo_obj_t *self, mp_float_t decay);
28+
mp_obj_t common_hal_audiodelays_echo_get_decay(audiodelays_echo_obj_t *self);
29+
void common_hal_audiodelays_echo_set_decay(audiodelays_echo_obj_t *self, mp_obj_t decay);
3030

3131
mp_obj_t common_hal_audiodelays_echo_get_mix(audiodelays_echo_obj_t *self);
3232
void common_hal_audiodelays_echo_set_mix(audiodelays_echo_obj_t *self, mp_obj_t arg);

shared-module/audiodelays/Echo.c

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#include "shared-module/audiomixer/utils.h"
1111

1212

13-
void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_t delay_ms, mp_float_t decay, mp_obj_t mix,
13+
void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_t max_delay_ms,
14+
mp_obj_t delay_ms, mp_obj_t decay, mp_obj_t mix,
1415
uint32_t buffer_size, uint8_t bits_per_sample,
1516
bool samples_signed, uint8_t channel_count, uint32_t sample_rate) {
1617

@@ -19,7 +20,6 @@ void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_
1920
self->channel_count = channel_count;
2021
self->sample_rate = sample_rate;
2122

22-
// check that buffer_size <= echo_buffer_size
2323
self->buffer_len = buffer_size;
2424
self->buffer = m_malloc(self->buffer_len);
2525
if (self->buffer == NULL) {
@@ -28,23 +28,34 @@ void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_
2828
}
2929
memset(self->buffer, 0, self->buffer_len);
3030

31-
self->decay = (uint16_t)(decay * (1 << 15));
31+
if (decay == MP_OBJ_NULL) {
32+
decay = mp_obj_new_float(0.7);
33+
}
34+
synthio_block_assign_slot(decay, &self->decay, MP_QSTR_decay);
35+
36+
if (delay_ms == MP_OBJ_NULL) {
37+
delay_ms = mp_obj_new_float(0.05);
38+
}
39+
synthio_block_assign_slot(delay_ms, &self->delay_ms, MP_QSTR_delay_ms);
3240

3341
if (mix == MP_OBJ_NULL) {
3442
mix = mp_obj_new_float(0.5);
3543
}
3644
synthio_block_assign_slot(mix, &self->mix, MP_QSTR_mix);
3745

3846
// calculate buffer size for the set delay
39-
self->delay_ms = delay_ms;
40-
self->echo_buffer_len = self->sample_rate / 1000.0f * self->delay_ms * (self->channel_count * (self->bits_per_sample / 8));
47+
mp_float_t f_delay_ms = synthio_block_slot_get(&self->delay_ms);
48+
self->echo_buffer_len = self->sample_rate / 1000.0f * f_delay_ms * (self->channel_count * (self->bits_per_sample / 8));
4149

42-
self->echo_buffer = m_malloc(self->echo_buffer_len);
50+
// Set the echo buffer for the max possible delay
51+
self->max_delay_ms = max_delay_ms;
52+
self->max_echo_buffer_len = self->sample_rate / 1000.0f * max_delay_ms * (self->channel_count * (self->bits_per_sample / 8));
53+
self->echo_buffer = m_malloc(self->max_echo_buffer_len);
4354
if (self->echo_buffer == NULL) {
4455
common_hal_audiodelays_echo_deinit(self);
45-
m_malloc_fail(self->echo_buffer_len);
56+
m_malloc_fail(self->max_echo_buffer_len);
4657
}
47-
memset(self->echo_buffer, 0, self->echo_buffer_len);
58+
memset(self->echo_buffer, 0, self->max_echo_buffer_len);
4859

4960
// read is where we store the incoming sample
5061
// write is what we send to the outgoing buffer
@@ -74,19 +85,15 @@ void common_hal_audiodelays_echo_deinit(audiodelays_echo_obj_t *self) {
7485
}
7586

7687

77-
uint32_t common_hal_audiodelays_echo_get_delay_ms(audiodelays_echo_obj_t *self) {
78-
return self->delay_ms;
88+
mp_obj_t common_hal_audiodelays_echo_get_delay_ms(audiodelays_echo_obj_t *self) {
89+
return self->delay_ms.obj;
7990
}
8091

81-
void common_hal_audiodelays_echo_set_delay_ms(audiodelays_echo_obj_t *self, uint32_t delay_ms) {
82-
self->delay_ms = delay_ms;
83-
self->echo_buffer_len = self->sample_rate / 1000.0f * self->delay_ms * (self->channel_count * (self->bits_per_sample / 8));
92+
void common_hal_audiodelays_echo_set_delay_ms(audiodelays_echo_obj_t *self, mp_obj_t delay_ms) {
93+
synthio_block_assign_slot(delay_ms, &self->delay_ms, MP_QSTR_delay_ms);
8494

85-
self->echo_buffer = m_realloc(self->echo_buffer, self->echo_buffer_len);
86-
if (self->echo_buffer == NULL) {
87-
common_hal_audiodelays_echo_deinit(self);
88-
m_malloc_fail(self->echo_buffer_len);
89-
}
95+
mp_float_t f_delay_ms = synthio_block_slot_get(&self->delay_ms);
96+
self->echo_buffer_len = self->sample_rate / 1000.0f * f_delay_ms * (self->channel_count * (self->bits_per_sample / 8));
9097

9198
uint32_t max_ebuf_length = self->echo_buffer_len / sizeof(uint32_t);
9299

@@ -99,12 +106,12 @@ void common_hal_audiodelays_echo_set_delay_ms(audiodelays_echo_obj_t *self, uint
99106
}
100107
}
101108

102-
mp_float_t common_hal_audiodelays_echo_get_decay(audiodelays_echo_obj_t *self) {
103-
return (mp_float_t)self->decay / (1 << 15);
109+
mp_obj_t common_hal_audiodelays_echo_get_decay(audiodelays_echo_obj_t *self) {
110+
return self->decay.obj;
104111
}
105112

106-
void common_hal_audiodelays_echo_set_decay(audiodelays_echo_obj_t *self, mp_float_t decay) {
107-
self->decay = (uint16_t)(decay * (1 << 15));
113+
void common_hal_audiodelays_echo_set_decay(audiodelays_echo_obj_t *self, mp_obj_t decay) {
114+
synthio_block_assign_slot(decay, &self->decay, MP_QSTR_decay);
108115
}
109116

110117
mp_obj_t common_hal_audiodelays_echo_get_mix(audiodelays_echo_obj_t *self) {
@@ -169,8 +176,6 @@ void common_hal_audiodelays_echo_play(audiodelays_echo_obj_t *self, mp_obj_t sam
169176

170177
void common_hal_audiodelays_echo_stop(audiodelays_echo_obj_t *self) {
171178
self->sample = NULL;
172-
// memset(self->echo_buffer, 0, self->echo_buffer_len); // clear echo
173-
// memset(self->buffer, 0, self->buffer_len);
174179
return;
175180
}
176181

@@ -188,6 +193,14 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
188193
}
189194
uint16_t mix = (uint16_t)(f_mix * (1 << 15));
190195

196+
mp_float_t f_decay = synthio_block_slot_get(&self->decay);
197+
if (f_decay > 1.0) {
198+
f_decay = 1.0;
199+
} else if (f_decay < 0.0) {
200+
f_decay = 0.0;
201+
}
202+
uint16_t decay = (uint16_t)(f_decay * (1 << 15));
203+
191204
while (length != 0) {
192205
if (self->sample_buffer_length == 0) {
193206
if (!self->more_data) {
@@ -219,7 +232,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
219232
// sample signed/unsigned won't matter as we have no sample
220233
for (uint32_t i = 0; i < length; i++) {
221234
uint32_t echo = self->echo_buffer[self->echo_buffer_read_pos++];
222-
word_buffer[i] = mult16signed(echo, self->decay);
235+
word_buffer[i] = mult16signed(echo, decay);
223236
self->echo_buffer[self->echo_buffer_write_pos++] = word_buffer[i];
224237

225238
word_buffer[i] = mult16signed(word_buffer[i], mix);
@@ -238,7 +251,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
238251
uint16_t *echo_hsrc = (uint16_t *)self->echo_buffer;
239252
for (uint32_t i = 0; i < length * 2; i++) {
240253
uint32_t echo_word = unpack8(echo_hsrc[i]);
241-
echo_word = mult16signed(echo_word, self->decay);
254+
echo_word = mult16signed(echo_word, decay);
242255
hword_buffer[i] = pack8(echo_word);
243256

244257
echo_hsrc[self->echo_buffer_write_pos++] = hword_buffer[i];
@@ -267,7 +280,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
267280
sample_word = tosigned16(sample_word);
268281
}
269282
uint32_t echo = self->echo_buffer[self->echo_buffer_read_pos++];
270-
word_buffer[i] = add16signed(mult16signed(echo, self->decay), sample_word);
283+
word_buffer[i] = add16signed(mult16signed(echo, decay), sample_word);
271284
self->echo_buffer[self->echo_buffer_write_pos++] = word_buffer[i];
272285

273286
word_buffer[i] = add16signed(mult16signed(sample_word, 32768 - mix), mult16signed(word_buffer[i], mix));
@@ -290,7 +303,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
290303
if (MP_LIKELY(!self->samples_signed)) {
291304
sample_word = tosigned16(sample_word);
292305
}
293-
echo_word = mult16signed(echo_word, self->decay);
306+
echo_word = mult16signed(echo_word, decay);
294307
sample_word = add16signed(sample_word, echo_word);
295308
hword_buffer[i] = pack8(sample_word);
296309

shared-module/audiodelays/Echo.h

Lines changed: 6 additions & 3 deletions

0 commit comments

Comments
 (0)