8000 Add in mix · carlossless/circuitpython@6255c46 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6255c46

Browse files
committed
Add in mix
1 parent 702c33f commit 6255c46

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

shared-bindings/audiodelays/Chorus.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
//| :param int max_delay_ms: The maximum time the chorus can be in milliseconds
4242
//| :param synthio.BlockInput delay_ms: The current time of the chorus delay in milliseconds. Must be less the max_delay_ms.
4343
//| :param synthio.BlockInput voices: The number of voices playing split evenly over the delay buffer.
44+
//| :param synthio.BlockInput mix: How much of the wet audio to include along with the original signal.
4445
//| :param int buffer_size: The total size in bytes of each of the two playback buffers to use
4546
//| :param int sample_rate: The sample rate to be used
4647
//| :param int channel_count: The number of channels the source samples contain. 1 = mono; 2 = stereo.
@@ -70,11 +71,12 @@
7071
//| ...
7172
//|
7273
static mp_obj_t audiodelays_chorus_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
73-
enum { ARG_max_delay_ms, ARG_delay_ms, ARG_voices, ARG_buffer_size, ARG_sample_rate, ARG_bits_per_sample, ARG_samples_signed, ARG_channel_count, };
74+
enum { ARG_max_delay_ms, ARG_delay_ms, ARG_voices, ARG_mix, ARG_buffer_size, ARG_sample_rate, ARG_bits_per_sample, ARG_samples_signed, ARG_channel_count, };
7475
static const mp_arg_t allowed_args[] = {
7576
{ MP_QSTR_max_delay_ms, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 50 } },
7677
{ MP_QSTR_delay_ms, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
7778
{ MP_QSTR_voices, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
79+
{ MP_QSTR_mix, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
7880
{ MP_QSTR_buffer_size, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 512} },
7981
{ MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 8000} },
8082
{ MP_QSTR_bits_per_sample, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 16} },
@@ -95,7 +97,7 @@ static mp_obj_t audiodelays_chorus_make_new(const mp_obj_type_t *type, size_t n_
9597
}
9698

9799
audiodelays_chorus_obj_t *self = mp_obj_malloc(audiodelays_chorus_obj_t, &audiodelays_chorus_type);
98-
common_hal_audiodelays_chorus_construct(self, max_delay_ms, args[ARG_delay_ms].u_obj, args[ARG_voices].u_obj, args[ARG_buffer_size].u_int, bits_per_sample, args[ARG_samples_signed].u_bool, channel_count, sample_rate);
100+
common_hal_audiodelays_chorus_construct(self, max_delay_ms, args[ARG_delay_ms].u_obj, args[ARG_voices].u_obj, args[ARG_mix].u_obj, args[ARG_buffer_size].u_int, bits_per_sample, args[ARG_samples_signed].u_bool, channel_count, sample_rate);
99101

100102
return MP_OBJ_FROM_PTR(self);
101103
}
@@ -173,6 +175,24 @@ MP_PROPERTY_GETSET(audiodelays_chorus_voices_obj,
173175
(mp_obj_t)&audiodelays_chorus_get_voices_obj,
174176
(mp_obj_t)&audiodelays_chorus_set_voices_obj);
175177

178+
//| mix: synthio.BlockInput
179+
//| """The rate the echo mix between 0 and 1 where 0 is only sample and 1 is all effect."""
180+
static mp_obj_t audiodelays_chorus_obj_get_mix(mp_obj_t self_in) {
181+
return common_hal_audiodelays_chorus_get_mix(self_in);
182+
}
183+
MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_chorus_get_mix_obj, audiodelays_chorus_obj_get_mix);
184+
185+
static mp_obj_t audiodelays_chorus_obj_set_mix(mp_obj_t self_in, mp_obj_t mix_in) {
186+
audiodelays_chorus_obj_t *self = MP_OBJ_TO_PTR(self_in);
187+
common_hal_audiodelays_chorus_set_mix(self, mix_in);
188+
return mp_const_none;
189+
}
190+
MP_DEFINE_CONST_FUN_OBJ_2(audiodelays_chorus_set_mix_obj, audiodelays_chorus_obj_set_mix);
191+
192+
MP_PROPERTY_GETSET(audiodelays_chorus_mix_obj,
193+
(mp_obj_t)&audiodelays_chorus_get_mix_obj,
194+
(mp_obj_t)&audiodelays_chorus_set_mix_obj);
195+
176196
//| playing: bool
177197
//| """True when the effect is playing a sample. (read-only)"""
178198
//|
@@ -237,6 +257,7 @@ static const mp_rom_map_elem_t audiodelays_chorus_locals_dict_table[] = {
237257
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audiodelays_chorus_playing_obj) },
238258
{ MP_ROM_QSTR(MP_QSTR_delay_ms), MP_ROM_PTR(&audiodelays_chorus_delay_ms_obj) },
239259
{ MP_ROM_QSTR(MP_QSTR_voices), MP_ROM_PTR(&audiodelays_chorus_voices_obj) },
260+
{ MP_ROM_QSTR(MP_QSTR_mix), MP_ROM_PTR(&audiodelays_chorus_mix_obj) },
240261
AUDIOSAMPLE_FIELDS,
241262
};
242263
static MP_DEFINE_CONST_DICT(audiodelays_chorus_locals_dict, audiodelays_chorus_locals_dict_table);

shared-bindings/audiodelays/Chorus.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
extern const mp_obj_type_t audiodelays_chorus_type;
1212

1313
void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uint32_t max_delay_ms,
14-
mp_obj_t delay_ms, mp_obj_t voices,
14+
mp_obj_t delay_ms, mp_obj_t voices, mp_obj_t mix,
1515
uint32_t buffer_size, uint8_t bits_per_sample,
1616
bool samples_signed, uint8_t channel_count, uint32_t sample_rate);
1717

@@ -28,6 +28,9 @@ void common_hal_audiodelays_chorus_set_delay_ms(audiodelays_chorus_obj_t *self,
2828
mp_obj_t common_hal_audiodelays_chorus_get_voices(audiodelays_chorus_obj_t *self);
2929
void common_hal_audiodelays_chorus_set_voices(audiodelays_chorus_obj_t *self, mp_obj_t voices);
3030

31+
mp_obj_t common_hal_audiodelays_chorus_get_mix(audiodelays_chorus_obj_t *self);
32+
void common_hal_audiodelays_chorus_set_mix(audiodelays_chorus_obj_t *self, mp_obj_t arg);
33+
3134
bool common_hal_audiodelays_chorus_get_playing(audiodelays_chorus_obj_t *self);
3235
void common_hal_audiodelays_chorus_play(audiodelays_chorus_obj_t *self, mp_obj_t sample, bool loop);
3336
void common_hal_audiodelays_chorus_stop(audiodelays_chorus_obj_t *self);

shared-module/audiodelays/Chorus.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "py/runtime.h"
1111

1212
void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uint32_t max_delay_ms,
13-
mp_obj_t delay_ms, mp_obj_t voices,
13+
mp_obj_t delay_ms, mp_obj_t voices, 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

@@ -66,6 +66,11 @@ void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uin
6666
}
6767
synthio_block_assign_slot(delay_ms, &self->delay_ms, MP_QSTR_delay_ms);
6868

69+
if (mix == MP_OBJ_NULL) {
70+
mix = mp_obj_new_float(MICROPY_FLOAT_CONST(0.5));
71+
}
72+
synthio_block_assign_slot(mix, &self->mix, MP_QSTR_mix);
73+
6974
// Many effects may need buffers of what was played this shows how it was done for the chorus
7075
// A maximum length buffer was created and then the current chorus length can be dynamically changes
7176
// without having to reallocate a large chunk of memory.
@@ -148,6 +153,14 @@ void audiodelays_chorus_reset_buffer(audiodelays_chorus_obj_t *self,
148153
memset(self->chorus_buffer, 0, self->chorus_buffer_len);
149154
}
150155

156+
mp_obj_t common_hal_audiodelays_chorus_get_mix(audiodelays_chorus_obj_t *self) {
157+
return self->mix.obj;
158+
}
159+
160+
void common_hal_audiodelays_chorus_set_mix(audiodelays_chorus_obj_t *self, mp_obj_t arg) {
161+
synthio_block_assign_slot(arg, &self->mix, MP_QSTR_mix);
162+
}
163+
151164
bool common_hal_audiodelays_chorus_get_playing(audiodelays_chorus_obj_t *self) {
152165
return self->sample != NULL;
153166
}
@@ -225,6 +238,7 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
225238

226239
int32_t voices = (int32_t)MAX(synthio_block_slot_get(&self->voices), 1.0);
227240
int32_t mix_down_scale = SYNTHIO_MIX_DOWN_SCALE(voices);
241+
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
228242

229243
mp_float_t f_delay_ms = synthio_block_slot_get(&self->delay_ms);
230244
if (MICROPY_FLOAT_C_FUN(fabs)(self->current_delay_ms - f_delay_ms) >= self->sample_ms) {
@@ -287,6 +301,10 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
287301
word = synthio_mix_down_sample(word, mix_down_scale);
288302
}
289303

304+
// Add original sample + effect
305+
word = sample_word + (word * mix);
306+
word = synthio_mix_down_sample(word, 2);
307+
290308
if (MP_LIKELY(self->base.bits_per_sample == 16)) {
291309
word_buffer[i] = word;
292310
if (!self->base.samples_signed) {

shared-module/audiodelays/Chorus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef struct {
1919
mp_float_t current_delay_ms;
2020
mp_float_t sample_ms;
2121
synthio_block_slot_t voices;
22+
synthio_block_slot_t mix;
2223

2324
int8_t *buffer[2];
2425
uint8_t last_buf_idx;

0 commit comments

Comments
 (0)
0