Skip to content

Commit d88b0c7

Browse files
committed
Change mix to BlockInput
1 parent 4da22c5 commit d88b0c7

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

shared-bindings/audiodelays/Echo.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ static mp_obj_t audiodelays_echo_make_new(const mp_obj_type_t *type, size_t n_ar
4444
: mp_obj_get_float(args[ARG_decay].u_obj);
4545
mp_arg_validate_float_range(decay, 0.0f, 1.0f, MP_QSTR_decay);
4646

47-
mp_float_t mix = (args[ARG_mix].u_obj == MP_OBJ_NULL)
48-
? (mp_float_t)MIX_DEFAULT
49-
: mp_obj_get_float(args[ARG_mix].u_obj);
50-
mp_arg_validate_float_range(mix, 0.0f, 1.0f, MP_QSTR_mix);
51-
5247
mp_int_t channel_count = mp_arg_validate_int_range(args[ARG_channel_count].u_int, 1, 2, MP_QSTR_channel_count);
5348
mp_int_t sample_rate = mp_arg_validate_int_min(args[ARG_sample_rate].u_int, 1, MP_QSTR_sample_rate);
5449
mp_int_t bits_per_sample = args[ARG_bits_per_sample].u_int;
@@ -57,7 +52,7 @@ static mp_obj_t audiodelays_echo_make_new(const mp_obj_type_t *type, size_t n_ar
5752
}
5853

5954
audiodelays_echo_obj_t *self = mp_obj_malloc(audiodelays_echo_obj_t, &audiodelays_echo_type);
60-
common_hal_audiodelays_echo_construct(self, delay_ms, decay, mix, args[ARG_buffer_size].u_int, bits_per_sample, args[ARG_samples_signed].u_bool, channel_count, sample_rate);
55+
common_hal_audiodelays_echo_construct(self, delay_ms, decay, args[ARG_mix].u_obj, args[ARG_buffer_size].u_int, bits_per_sample, args[ARG_samples_signed].u_bool, channel_count, sample_rate);
6156

6257
return MP_OBJ_FROM_PTR(self);
6358
}
@@ -162,7 +157,7 @@ MP_PROPERTY_GETSET(audiodelays_echo_decay_obj,
162157
//| mix: float
163158
//| """The rate the echo mix between 0 and 1."""
164159
static mp_obj_t audiodelays_echo_obj_get_mix(mp_obj_t self_in) {
165-
return mp_obj_new_float(common_hal_audiodelays_echo_get_mix(self_in));
160+
return common_hal_audiodelays_echo_get_mix(self_in);
166161
}
167162
MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_echo_get_mix_obj, audiodelays_echo_obj_get_mix);
168163

@@ -175,10 +170,7 @@ static mp_obj_t audiodelays_echo_obj_set_mix(size_t n_args, const mp_obj_t *pos_
175170
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
176171
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
177172

178-
mp_float_t mix = mp_obj_get_float(args[ARG_mix].u_obj);
179-
mp_arg_validate_float_range(mix, 0.0f, 1.0f, MP_QSTR_mix);
180-
181-
common_hal_audiodelays_echo_set_mix(self, mix);
173+
common_hal_audiodelays_echo_set_mix(self, args[ARG_mix].u_obj);
182174

183175
return mp_const_none;
184176
}

shared-bindings/audiodelays/Echo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
extern const mp_obj_type_t audiodelays_echo_type;
1212

1313
void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self,
14-
uint32_t delay_ms, mp_float_t decay, mp_float_t mix,
14+
uint32_t delay_ms, mp_float_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

@@ -28,8 +28,8 @@ void common_hal_audiodelays_echo_set_delay_ms(audiodelays_echo_obj_t *self, uint
2828
mp_float_t common_hal_audiodelays_echo_get_decay(audiodelays_echo_obj_t *self);
2929
void common_hal_audiodelays_echo_set_decay(audiodelays_echo_obj_t *self, mp_float_t decay);
3030

31-
mp_float_t common_hal_audiodelays_echo_get_mix(audiodelays_echo_obj_t *self);
32-
void common_hal_audiodelays_echo_set_mix(audiodelays_echo_obj_t *self, mp_float_t decay);
31+
mp_obj_t common_hal_audiodelays_echo_get_mix(audiodelays_echo_obj_t *self);
32+
void common_hal_audiodelays_echo_set_mix(audiodelays_echo_obj_t *self, mp_obj_t arg);
3333

3434
bool common_hal_audiodelays_echo_get_playing(audiodelays_echo_obj_t *self);
3535
void common_hal_audiodelays_echo_play(audiodelays_echo_obj_t *self, mp_obj_t sample, bool loop);

shared-module/audiodelays/Echo.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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_float_t mix,
13+
void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_t delay_ms, mp_float_t decay, mp_obj_t mix,
1414
uint32_t buffer_size, uint8_t bits_per_sample,
1515
bool samples_signed, uint8_t channel_count, uint32_t sample_rate) {
1616

@@ -29,7 +29,11 @@ void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_
2929
memset(self->buffer, 0, self->buffer_len);
3030

3131
self->decay = (uint16_t)(decay * (1 << 15));
32-
self->mix = (uint16_t)(mix * (1 << 15));
32+
33+
if (mix == MP_OBJ_NULL) {
34+
mix = mp_obj_new_float(0.5);
35+
}
36+
synthio_block_assign_slot(mix, &self->mix, MP_QSTR_mix);
3337

3438
// calculate buffer size for the set delay
3539
self->delay_ms = delay_ms;
@@ -103,13 +107,12 @@ void common_hal_audiodelays_echo_set_decay(audiodelays_echo_obj_t *self, mp_floa
103107
self->decay = (uint16_t)(decay * (1 << 15));
104108
}
105109

106-
mp_float_t common_hal_audiodelays_echo_get_mix(audiodelays_echo_obj_t *self) {
107-
return (mp_float_t)self->mix / (1 << 15);
110+
mp_obj_t common_hal_audiodelays_echo_get_mix(audiodelays_echo_obj_t *self) {
111+
return self->mix.obj;
108112
}
109113

110-
void common_hal_audiodelays_echo_set_mix(audiodelays_echo_obj_t *self, mp_float_t mix) {
111-
self->mix = (uint16_t)(mix * (1 << 15));
112-
mp_printf(&mp_plat_print, "mix %d\n", self->mix);
114+
void common_hal_audiodelays_echo_set_mix(audiodelays_echo_obj_t *self, mp_obj_t arg) {
115+
synthio_block_assign_slot(arg, &self->mix, MP_QSTR_mix);
113116
}
114117

115118
uint32_t common_hal_audiodelays_echo_get_sample_rate(audiodelays_echo_obj_t *self) {
@@ -177,6 +180,13 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
177180
uint32_t *word_buffer = (uint32_t *)self->buffer;
178181
uint32_t length = self->buffer_len / sizeof(uint32_t);
179182
uint32_t echo_buf_len = self->echo_buffer_len / sizeof(uint32_t);
183+
mp_float_t f_mix = synthio_block_slot_get(&self->mix);
184+
if (f_mix > 1.0) {
185+
f_mix = 1.0;
186+
} else if (f_mix < 0.0) {
187+
f_mix = 0.0;
188+
}
189+
uint16_t mix = (uint16_t)(f_mix * (1 << 15));
180190

181191
while (length != 0) {
182192
if (self->sample_buffer_length == 0) {
@@ -201,7 +211,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
201211
// If we have no sample keep the echo echoing
202212
if (self->sample == NULL) {
203213
if (MP_LIKELY(self->bits_per_sample == 16)) {
204-
if (self->mix == 0) { // no effect and no sample sound
214+
if (mix == 0) { // no effect and no sample sound
205215
for (uint32_t i = 0; i < length; i++) {
206216
word_buffer[i] = 0;
207217
}
@@ -212,7 +222,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
212222
word_buffer[i] = mult16signed(echo, self->decay);
213223
self->echo_buffer[self->echo_buffer_write_pos++] = word_buffer[i];
214224

215-
word_buffer[i] = mult16signed(word_buffer[i], self->mix);
225+
word_buffer[i] = mult16signed(word_buffer[i], mix);
216226

217227
if (self->echo_buffer_read_pos >= echo_buf_len) {
218228
self->echo_buffer_read_pos = 0;
@@ -246,7 +256,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
246256
uint32_t *sample_src = self->sample_remaining_buffer;
247257

248258
if (MP_LIKELY(self->bits_per_sample == 16)) {
249-
if (self->mix == 0) { // sample only
259+
if (mix == 0) { // sample only
250260
for (uint32_t i = 0; i < n; i++) {
251261
word_buffer[i] = sample_src[i];
252262
}
@@ -260,7 +270,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
260270
word_buffer[i] = add16signed(mult16signed(echo, self->decay), sample_word);
261271
self->echo_buffer[self->echo_buffer_write_pos++] = word_buffer[i];
262272

263-
word_buffer[i] = add16signed(mult16signed(sample_word, 32768 - self->mix), mult16signed(word_buffer[i], self->mix));
273+
word_buffer[i] = add16signed(mult16signed(sample_word, 32768 - mix), mult16signed(word_buffer[i], mix));
264274

265275
if (self->echo_buffer_read_pos >= echo_buf_len) {
266276
self->echo_buffer_read_pos = 0;

shared-module/audiodelays/Echo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#include "py/obj.h"
99

1010
#include "shared-module/audiocore/__init__.h"
11+
#include "shared-module/synthio/block.h"
1112

1213
extern const mp_obj_type_t audiodelays_echo_type;
1314

1415
typedef struct {
1516
mp_obj_base_t base;
1617
uint32_t delay_ms;
1718
uint16_t decay;
18-
uint16_t mix;
19+
synthio_block_slot_t mix;
1920
uint8_t bits_per_sample;
2021
bool samples_signed;
2122
uint8_t channel_count;

0 commit comments

Comments
 (0)