8000 Merge pull request #9994 from relic-se/audiodelays_mix_fix · adafruit/circuitpython@a7a4d9d · GitHub
[go: up one dir, main page]

Skip to content

Commit a7a4d9d

Browse files
authored
Merge pull request #9994 from relic-se/audiodelays_mix_fix
Change mix calculation on `audiodelays.Echo` to allow full range.
2 parents 361e0eb + 8f9843e commit a7a4d9d

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

shared-bindings/audiodelays/Echo.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
#include "shared-bindings/util.h"
1818
#include "shared-module/synthio/block.h"
1919

20-
#define DECAY_DEFAULT 0.7f
21-
#define MIX_DEFAULT 0.5f
22-
2320
//| class Echo:
2421
//| """An Echo effect"""
2522
//|
@@ -28,7 +25,7 @@
2825
//| max_delay_ms: int = 500,
2926
//| delay_ms: synthio.BlockInput = 250.0,
3027
//| decay: synthio.BlockInput = 0.7,
31-
//| mix: synthio.BlockInput = 0.5,
28+
//| mix: synthio.BlockInput = 0.25,
3229
//| buffer_size: int = 512,
3330
//| sample_rate: int = 8000,
3431
//| bits_per_sample: int = 16,
@@ -163,7 +160,7 @@ MP_PROPERTY_GETSET(audiodelays_echo_delay_ms_obj,
163160
(mp_obj_t)&audiodelays_echo_set_delay_ms_obj);
164161

165162
//| decay: synthio.BlockInput
166-
//| """The rate the echo decays between 0 and 1 where 1 is forever and 0 is no echo."""
163+
//| """The rate the echo fades between 0 and 1 where 0 is instant and 1 is never."""
167164
static mp_obj_t audiodelays_echo_obj_get_decay(mp_obj_t self_in) {
168165
return common_hal_audiodelays_echo_get_decay(self_in);
169166
}
@@ -181,7 +178,7 @@ MP_PROPERTY_GETSET(audiodelays_echo_decay_obj,
181178
(mp_obj_t)&audiodelays_echo_set_decay_obj);
182179

183180
//| mix: synthio.BlockInput
184-
//| """The rate the echo mix between 0 and 1 where 0 is only sample and 1 is all effect."""
181+
//| """The rate the echo mix between 0 and 1 where 0 is only sample, 0.5 is an equal mix of the sample and the effect and 1 is all effect."""
185182
static mp_obj_t audiodelays_echo_obj_get_mix(mp_obj_t self_in) {
186183
return common_hal_audiodelays_echo_get_mix(self_in);
187184
}

shared-bindings/audiofilters/Filter.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include "shared-bindings/util.h"
1818
#include "shared-module/synthio/block.h"
1919

20-
#define MIX_DEFAULT 1.0f
21-
2220
//| class Filter:
2321
//| """A Filter effect"""
2422
//|

shared-module/audiodelays/Echo.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_
7171
synthio_block_assign_slot(delay_ms, &self->delay_ms, MP_QSTR_delay_ms);
7272

7373
if (mix == MP_OBJ_NULL) {
74-
mix = mp_obj_new_float(MICROPY_FLOAT_CONST(0.5));
74+
mix = mp_obj_new_float(MICROPY_FLOAT_CONST(0.25));
7575
}
7676
synthio_block_assign_slot(mix, &self->mix, MP_QSTR_mix);
7777

@@ -268,7 +268,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
268268

269269
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
270270
shared_bindings_synthio_lfo_tick(self->base.sample_rate, n / self->base.channel_count);
271-
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
271+
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0)) * MICROPY_FLOAT_CONST(2.0);
272272
mp_float_t decay = synthio_block_slot_get_limited(&self->decay, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
273273

274274
mp_float_t f_delay_ms = synthio_block_slot_get(&self->delay_ms);
@@ -323,7 +323,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
323323
echo_buffer[self->echo_buffer_write_pos++] = word;
324324
}
325325

326-
word = (int16_t)(echo * mix);
326+
word = (int16_t)(echo * MIN(mix, MICROPY_FLOAT_CONST(1.0)));
327327

328328
if (MP_LIKELY(self->base.bits_per_sample == 16)) {
329329
word_buffer[i] = word;
@@ -414,16 +414,17 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
414414
}
415415
}
416416

417-
word = echo + sample_word;
417+
word = (int32_t)((sample_word * MIN(MICROPY_FLOAT_CONST(2.0) - mix, MICROPY_FLOAT_CONST(1.0)))
418+
+ (echo * MIN(mix, MICROPY_FLOAT_CONST(1.0))));
418419
word = synthio_mix_down_sample(word, SYNTHIO_MIX_DOWN_SCALE(2));
419420

420421
if (MP_LIKELY(self->base.bits_per_sample == 16)) {
421-
word_buffer[i] = (int16_t)((sample_word * (MICROPY_FLOAT_CONST(1.0) - mix)) + (word * mix));
422+
word_buffer[i] = (int16_t)word;
422423
if (!self->base.samples_signed) {
423424
word_buffer[i] ^= 0x8000;
424425
}
425426
} else {
426-
int8_t mixed = (int16_t)((sample_word * (MICROPY_FLOAT_CONST(1.0) - mix)) + (word * mix));
427+
int8_t mixed = (int16_t)word;
427428
if (self->base.samples_signed) {
428429
hword_buffer[i] = mixed;
429430
} else {

0 commit comments

Comments
 (0)
0