Skip to content

Commit 222ce2c

Browse files
committed
Apply similar updates to audiofilters.Filter and audiodelays.Echo: MICROPY_FLOAT_CONST, MP_ROM_INT, and synthio_block_slot_get_limited.
1 parent 5c981f0 commit 222ce2c

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

shared-bindings/audiofilters/Filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static mp_obj_t audiofilters_filter_make_new(const mp_obj_type_t *type, size_t n
7272
enum { ARG_filter, ARG_mix, ARG_buffer_size, ARG_sample_rate, ARG_bits_per_sample, ARG_samples_signed, ARG_channel_count, };
7373
static const mp_arg_t allowed_args[] = {
7474
{ MP_QSTR_filter, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
75-
{ MP_QSTR_mix, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
75+
{ MP_QSTR_mix, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1)} },
7676
{ MP_QSTR_buffer_size, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 512} },
7777
{ MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 8000} },
7878
{ MP_QSTR_bits_per_sample, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 16} },

shared-module/audiodelays/Echo.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_
5757

5858
// If we did not receive a BlockInput we need to create a default float value
5959
if (decay == MP_OBJ_NULL) {
60-
decay = mp_obj_new_float(0.7);
60+
decay = mp_obj_new_float(MICROPY_FLOAT_CONST(0.7));
6161
}
6262
synthio_block_assign_slot(decay, &self->decay, MP_QSTR_decay);
6363

6464
if (delay_ms == MP_OBJ_NULL) {
65-
delay_ms = mp_obj_new_float(250.0);
65+
delay_ms = mp_obj_new_float(MICROPY_FLOAT_CONST(250.0));
6666
}
6767
synthio_block_assign_slot(delay_ms, &self->delay_ms, MP_QSTR_delay_ms);
6868

6969
if (mix == MP_OBJ_NULL) {
70-
mix = mp_obj_new_float(0.5);
70+
mix = mp_obj_new_float(MICROPY_FLOAT_CONST(0.5));
7171
}
7272
synthio_block_assign_slot(mix, &self->mix, MP_QSTR_mix);
7373

@@ -77,7 +77,7 @@ void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_
7777

7878
// Allocate the echo buffer for the max possible delay, echo is always 16-bit
7979
self->max_delay_ms = max_delay_ms;
80-
self->max_echo_buffer_len = (uint32_t)(self->sample_rate / 1000.0f * max_delay_ms) * (self->channel_count * sizeof(uint16_t)); // bytes
80+
self->max_echo_buffer_len = (uint32_t)(self->sample_rate / MICROPY_FLOAT_CONST(1000.0) * max_delay_ms) * (self->channel_count * sizeof(uint16_t)); // bytes
8181
self->echo_buffer = m_malloc(self->max_echo_buffer_len);
8282
if (self->echo_buffer == NULL) {
8383
common_hal_audiodelays_echo_deinit(self);
@@ -285,8 +285,8 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
285285
}
286286

287287
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
288-
mp_float_t mix = MIN(1.0, MAX(synthio_block_slot_get(&self->mix), 0.0));
289-
mp_float_t decay = MIN(1.0, MAX(synthio_block_slot_get(&self->decay), 0.0));
288+
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
289+
mp_float_t decay = synthio_block_slot_get_limited(&self->decay, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
290290

291291
uint32_t delay_ms = (uint32_t)synthio_block_slot_get(&self->delay_ms);
292292
if (self->current_delay_ms != delay_ms) {
@@ -336,7 +336,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
336336

337337
// If we have no sample keep the echo echoing
338338
if (self->sample == NULL) {
339-
if (mix <= 0.01) { // Mix of 0 is pure sample sound. We have no sample so no sound
339+
if (mix <= MICROPY_FLOAT_CONST(0.01)) { // Mix of 0 is pure sample sound. We have no sample so no sound
340340
if (self->samples_signed) {
341341
memset(word_buffer, 0, length * (self->bits_per_sample / 8));
342342
} else {
@@ -406,7 +406,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
406406
int16_t *sample_src = (int16_t *)self->sample_remaining_buffer; // for 16-bit samples
407407
int8_t *sample_hsrc = (int8_t *)self->sample_remaining_buffer; // for 8-bit samples
408408

409-
if (mix <= 0.01) { // if mix is zero pure sample only
409+
if (mix <= MICROPY_FLOAT_CONST(0.01)) { // if mix is zero pure sample only
410410
for (uint32_t i = 0; i < n; i++) {
411411
if (MP_LIKELY(self->bits_per_sample == 16)) {
412412
word_buffer[i] = sample_src[i];
@@ -467,12 +467,12 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
467467
word = echo + sample_word;
468468

469469
if (MP_LIKELY(self->bits_per_sample == 16)) {
470-
word_buffer[i] = (int16_t)((sample_word * (1.0 - mix)) + (word * mix));
470+
word_buffer[i] = (int16_t)((sample_word * (MICROPY_FLOAT_CONST(1.0) - mix)) + (word * mix));
471471
if (!self->samples_signed) {
472472
word_buffer[i] ^= 0x8000;
473473
}
474474
} else {
475-
int8_t mixed = (int16_t)((sample_word * (1.0 - mix)) + (word * mix));
475+
int8_t mixed = (int16_t)((sample_word * (MICROPY_FLOAT_CONST(1.0) - mix)) + (word * mix));
476476
if (self->samples_signed) {
477477
hword_buffer[i] = mixed;
478478
} else {

shared-module/audiofilters/Distortion.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ audioio_get_buffer_result_t audiofilters_distortion_get_buffer(audiofilters_dist
204204
}
205205

206206
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
207-
mp_float_t drive = synthio_block_slot_get_limited(&self->drive, 0.0, 1.0);
208-
mp_float_t pre_gain = db_to_linear(synthio_block_slot_get_limited(&self->pre_gain, -60.0, 60.0));
209-
mp_float_t post_gain = db_to_linear(synthio_block_slot_get_limited(&self->post_gain, -80.0, 24.0));
210-
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, 0.0, 1.0);
207+
mp_float_t drive = synthio_block_slot_get_limited(&self->drive, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
208+
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)));
209+
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)));
210+
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
211211

212212
// Switch our buffers to the other buffer
213213
self->last_buf_idx = !self->last_buf_idx;

shared-module/audiofilters/Filter.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ void common_hal_audiofilters_filter_construct(audiofilters_filter_obj_t *self,
5353
}
5454
common_hal_audiofilters_filter_set_filter(self, filter);
5555

56-
// If we did not receive a BlockInput we need to create a default float value
57-
if (mix == MP_OBJ_NULL) {
58-
mix = mp_obj_new_float(1.0);
59-
}
6056
synthio_block_assign_slot(mix, &self->mix, MP_QSTR_mix);
6157
}
6258

@@ -251,7 +247,7 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o
251247
}
252248

253249
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
254-
mp_float_t mix = MIN(1.0, MAX(synthio_block_slot_get(&self->mix), 0.0));
250+
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
255251

256252
// Switch our buffers to the other buffer
257253
self->last_buf_idx = !self->last_buf_idx;
@@ -305,7 +301,7 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o
305301
int16_t *sample_src = (int16_t *)self->sample_remaining_buffer; // for 16-bit samples
306302
int8_t *sample_hsrc = (int8_t *)self->sample_remaining_buffer; // for 8-bit samples
307303

308-
if (mix <= 0.01 || !self->filter_states) { // if mix is zero pure sample only or no biquad filter objects are provided
304+
if (mix <= MICROPY_FLOAT_CONST(0.01) || !self->filter_states) { // if mix is zero pure sample only or no biquad filter objects are provided
309305
for (uint32_t i = 0; i < n; i++) {
310306
if (MP_LIKELY(self->bits_per_sample == 16)) {
311307
word_buffer[i] = sample_src[i];
@@ -346,7 +342,7 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o
346342
}
347343
} else {
348344
if (self->samples_signed) {
349-
hword_buffer[i + j] = (int8_t)((sample_hsrc[i + j] * (1.0 - mix)) + (self->filter_buffer[j] * mix));
345+
hword_buffer[i + j] = (int8_t)((sample_hsrc[i + j] * (MICROPY_FLOAT_CONST(1.0) - mix)) + (self->filter_buffer[j] * mix));
350346
} else {
351347
hword_buffer[i + j] = (uint8_t)(((int8_t)(((uint8_t)sample_hsrc[i + j]) ^ 0x80) * (MICROPY_FLOAT_CONST(1.0) - mix)) + (self->filter_buffer[j] * mix)) ^ 0x80;
352348
}

0 commit comments

Comments
 (0)