synthio: lfo: bugfixes & improvements

LFO waveforms are now linearly interpolated by default, but a new
property (interpolated=False) can disable this.

The 'once' logic was improved
This commit is contained in:
Jeff Epler 2023-05-21 12:48:08 -05:00
parent e6c4d12eaf
commit 4ff08e02eb
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
4 changed files with 58 additions and 8 deletions

View File

@ -64,7 +64,8 @@
//| scale: BlockInput = 1.0,
//| offset: BlockInput = 0,
//| phase_offset: BlockInput = 0,
//| once=False
//| once=False,
//| interpolate=True
//| ):
//| pass
static const mp_arg_t lfo_properties[] = {
@ -74,6 +75,7 @@ static const mp_arg_t lfo_properties[] = {
{ MP_QSTR_offset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
{ MP_QSTR_phase_offset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
{ MP_QSTR_once, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
{ MP_QSTR_interpolate, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1) } },
};
STATIC mp_obj_t synthio_lfo_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
@ -201,6 +203,26 @@ MP_PROPERTY_GETSET(synthio_lfo_once_obj,
(mp_obj_t)&synthio_lfo_set_once_obj);
//|
//| interpolate: bool
//| """True if the waveform should perform linear interpolation between values"""
STATIC mp_obj_t synthio_lfo_get_interpolate(mp_obj_t self_in) {
synthio_lfo_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(common_hal_synthio_lfo_get_interpolate(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_lfo_get_interpolate_obj, synthio_lfo_get_interpolate);
STATIC mp_obj_t synthio_lfo_set_interpolate(mp_obj_t self_in, mp_obj_t arg) {
synthio_lfo_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_synthio_lfo_set_interpolate(self, mp_obj_is_true(arg));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(synthio_lfo_set_interpolate_obj, synthio_lfo_set_interpolate);
MP_PROPERTY_GETSET(synthio_lfo_interpolate_obj,
(mp_obj_t)&synthio_lfo_get_interpolate_obj,
(mp_obj_t)&synthio_lfo_set_interpolate_obj);
//|
//| phase: float
//| """The phase of the oscillator, in the range 0 to 1 (read-only)"""
@ -250,6 +272,7 @@ STATIC const mp_rom_map_elem_t synthio_lfo_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_offset), MP_ROM_PTR(&synthio_lfo_offset_obj) },
{ MP_ROM_QSTR(MP_QSTR_phase_offset), MP_ROM_PTR(&synthio_lfo_phase_offset_obj) },
{ MP_ROM_QSTR(MP_QSTR_once), MP_ROM_PTR(&synthio_lfo_once_obj) },
{ MP_ROM_QSTR(MP_QSTR_interpolate), MP_ROM_PTR(&synthio_lfo_interpolate_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&synthio_lfo_value_obj) },
{ MP_ROM_QSTR(MP_QSTR_phase), MP_ROM_PTR(&synthio_lfo_phase_obj) },
{ MP_ROM_QSTR(MP_QSTR_retrigger), MP_ROM_PTR(&synthio_lfo_retrigger_obj) },

View File

@ -49,6 +49,9 @@ void common_hal_synthio_lfo_set_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg
bool common_hal_synthio_lfo_get_once(synthio_lfo_obj_t *self);
void common_hal_synthio_lfo_set_once(synthio_lfo_obj_t *self, bool arg);
bool common_hal_synthio_lfo_get_interpolate(synthio_lfo_obj_t *self);
void common_hal_synthio_lfo_set_interpolate(synthio_lfo_obj_t *self, bool arg);
mp_float_t common_hal_synthio_lfo_get_value(synthio_lfo_obj_t *self);
mp_float_t common_hal_synthio_lfo_get_phase(synthio_lfo_obj_t *self);

View File

@ -31,6 +31,8 @@
#define ONE (MICROPY_FLOAT_CONST(1.))
#define ZERO (MICROPY_FLOAT_CONST(0.))
#define ALMOST_ONE (MICROPY_FLOAT_CONST(32767.) / 32768)
mp_float_t common_hal_synthio_lfo_tick(mp_obj_t self_in) {
synthio_lfo_obj_t *lfo = MP_OBJ_TO_PTR(self_in);
@ -41,8 +43,8 @@ mp_float_t common_hal_synthio_lfo_tick(mp_obj_t self_in) {
if (lfo->once) {
if (rate > 0) {
if (accum >= ONE) {
accum = ONE;
if (accum > ALMOST_ONE) {
accum = ALMOST_ONE;
}
} else if (rate < 0 && accum < ZERO) {
accum = ZERO;
@ -50,17 +52,32 @@ mp_float_t common_hal_synthio_lfo_tick(mp_obj_t self_in) {
} else {
accum = accum - MICROPY_FLOAT_C_FUN(floor)(accum);
}
lfo->accum = accum - phase_offset;
int len = lfo->waveform_bufinfo.len;
size_t idx = (int)(accum * len); // rounds down towards zero
mp_float_t scaled_accum = accum * (len - lfo->once);
size_t idx = (size_t)MICROPY_FLOAT_C_FUN(floor)(scaled_accum);
assert(idx < lfo->waveform_bufinfo.len);
int16_t *waveform = lfo->waveform_bufinfo.buf;
mp_float_t value = waveform[idx];
if (lfo->interpolate) {
mp_float_t frac = scaled_accum - idx;
size_t idxp1 = idx + 1;
if (idxp1 == lfo->waveform_bufinfo.len) {
idxp1 = lfo->once ? idx : 0;
}
value = value * (1 - frac) + waveform[idxp1] * frac;
}
mp_float_t scale = synthio_block_slot_get(&lfo->scale);
mp_float_t offset = synthio_block_slot_get(&lfo->offset);
int16_t *waveform = lfo->waveform_bufinfo.buf;
mp_float_t value = MICROPY_FLOAT_C_FUN(ldexp)(waveform[idx], -15) * scale + offset;
value = MICROPY_FLOAT_C_FUN(ldexp)(value, -15) * scale + offset;
lfo->accum = accum - phase_offset;
return value;
}
@ -104,6 +121,13 @@ void common_hal_synthio_lfo_set_once(synthio_lfo_obj_t *self, bool arg) {
self->once = arg;
}
bool common_hal_synthio_lfo_get_interpolate(synthio_lfo_obj_t *self) {
return self->interpolate;
}
void common_hal_synthio_lfo_set_interpolate(synthio_lfo_obj_t *self, bool arg) {
self->interpolate = arg;
}
mp_float_t common_hal_synthio_lfo_get_value(synthio_lfo_obj_t *self) {
return self->base.value;
}

View File

@ -30,7 +30,7 @@
typedef struct synthio_lfo_obj {
synthio_block_base_t base;
bool once;
bool once, interpolate;
synthio_block_slot_t rate, scale, offset, phase_offset;
mp_float_t accum;