synthio: add (untested) phase offset

This commit is contained in:
Jeff Epler 2023-05-20 19:43:15 -05:00
parent e259f8d1ba
commit e0cfae1d05
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
4 changed files with 45 additions and 12 deletions

View File

@ -63,6 +63,7 @@
//| rate: BlockInput = 1.0, //| rate: BlockInput = 1.0,
//| scale: BlockInput = 1.0, //| scale: BlockInput = 1.0,
//| offset: BlockInput = 0, //| offset: BlockInput = 0,
//| phase_offset: BlockInput = 0,
//| once=False //| once=False
//| ): //| ):
//| pass //| pass
@ -71,6 +72,7 @@ static const mp_arg_t lfo_properties[] = {
{ MP_QSTR_rate, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1) } }, { MP_QSTR_rate, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1) } },
{ MP_QSTR_scale, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1) } }, { MP_QSTR_scale, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1) } },
{ MP_QSTR_offset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } }, { 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_once, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
}; };
@ -141,6 +143,24 @@ MP_PROPERTY_GETSET(synthio_lfo_offset_obj,
(mp_obj_t)&synthio_lfo_get_offset_obj, (mp_obj_t)&synthio_lfo_get_offset_obj,
(mp_obj_t)&synthio_lfo_set_offset_obj); (mp_obj_t)&synthio_lfo_set_offset_obj);
//| phase_offset: BlockInput
//| """An additive value applied to the LFO's phase"""
STATIC mp_obj_t synthio_lfo_get_phase_offset(mp_obj_t self_in) {
synthio_lfo_obj_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_synthio_lfo_get_phase_offset_obj(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_lfo_get_phase_offset_obj, synthio_lfo_get_phase_offset);
STATIC mp_obj_t synthio_lfo_set_phase_offset(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_phase_offset_obj(self, arg);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(synthio_lfo_set_phase_offset_obj, synthio_lfo_set_phase_offset);
MP_PROPERTY_GETSET(synthio_lfo_phase_offset_obj,
(mp_obj_t)&synthio_lfo_get_phase_offset_obj,
(mp_obj_t)&synthio_lfo_set_phase_offset_obj);
//| scale: BlockInput //| scale: BlockInput
//| """An additive value applied to the LFO's output""" //| """An additive value applied to the LFO's output"""
STATIC mp_obj_t synthio_lfo_get_scale(mp_obj_t self_in) { STATIC mp_obj_t synthio_lfo_get_scale(mp_obj_t self_in) {
@ -161,7 +181,9 @@ MP_PROPERTY_GETSET(synthio_lfo_scale_obj,
//| //|
//| once: bool //| once: bool
//| """True if the waveform should stop when it reaches its last output value, false if it should re-start at the beginning of its waveform""" //| """True if the waveform should stop when it reaches its last output value, false if it should re-start at the beginning of its waveform
//|
//| This applies to the ``phase`` *before* the addition of any ``phase_offset`` """
STATIC mp_obj_t synthio_lfo_get_once(mp_obj_t self_in) { STATIC mp_obj_t synthio_lfo_get_once(mp_obj_t self_in) {
synthio_lfo_obj_t *self = MP_OBJ_TO_PTR(self_in); synthio_lfo_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(common_hal_synthio_lfo_get_once(self)); return mp_obj_new_bool(common_hal_synthio_lfo_get_once(self));
@ -226,6 +248,7 @@ STATIC const mp_rom_map_elem_t synthio_lfo_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_rate), MP_ROM_PTR(&synthio_lfo_rate_obj) }, { MP_ROM_QSTR(MP_QSTR_rate), MP_ROM_PTR(&synthio_lfo_rate_obj) },
{ MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&synthio_lfo_scale_obj) }, { MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&synthio_lfo_scale_obj) },
{ MP_ROM_QSTR(MP_QSTR_offset), MP_ROM_PTR(&synthio_lfo_offset_obj) }, { 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_once), MP_ROM_PTR(&synthio_lfo_once_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&synthio_lfo_value_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_phase), MP_ROM_PTR(&synthio_lfo_phase_obj) },

View File

@ -40,6 +40,9 @@ void common_hal_synthio_lfo_set_rate_obj(synthio_lfo_obj_t *self, mp_obj_t arg);
mp_obj_t common_hal_synthio_lfo_get_scale_obj(synthio_lfo_obj_t *self); mp_obj_t common_hal_synthio_lfo_get_scale_obj(synthio_lfo_obj_t *self);
void common_hal_synthio_lfo_set_scale_obj(synthio_lfo_obj_t *self, mp_obj_t arg); void common_hal_synthio_lfo_set_scale_obj(synthio_lfo_obj_t *self, mp_obj_t arg);
mp_obj_t common_hal_synthio_lfo_get_phase_offset_obj(synthio_lfo_obj_t *self);
void common_hal_synthio_lfo_set_phase_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg);
mp_obj_t common_hal_synthio_lfo_get_offset_obj(synthio_lfo_obj_t *self); mp_obj_t common_hal_synthio_lfo_get_offset_obj(synthio_lfo_obj_t *self);
void common_hal_synthio_lfo_set_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg); void common_hal_synthio_lfo_set_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg);

View File

@ -35,32 +35,32 @@ mp_float_t common_hal_synthio_lfo_tick(mp_obj_t self_in) {
synthio_lfo_obj_t *lfo = MP_OBJ_TO_PTR(self_in); synthio_lfo_obj_t *lfo = MP_OBJ_TO_PTR(self_in);
mp_float_t rate = synthio_block_slot_get(&lfo->rate) * synthio_global_rate_scale; mp_float_t rate = synthio_block_slot_get(&lfo->rate) * synthio_global_rate_scale;
mp_float_t phase_offset = synthio_block_slot_get(&lfo->phase_offset);
mp_float_t accum = lfo->accum + rate; mp_float_t accum = lfo->accum + rate + phase_offset;
mp_float_t frac = accum - MICROPY_FLOAT_C_FUN(floor)(accum);
size_t idx = (int)(frac * len);
if (lfo->once) { if (lfo->once) {
if (rate > 0) { if (rate > 0) {
if (accum >= ONE) { if (accum >= ONE) {
frac = ONE; accum = ONE;
idx = len - 1;
} }
} else if (rate < 0 && accum < ZERO) { } else if (rate < 0 && accum < ZERO) {
frac = ZERO; accum = ZERO;
idx = 0;
} }
} else {
accum = accum - MICROPY_FLOAT_C_FUN(floor)(accum);
} }
int len = lfo->waveform_bufinfo.len; int len = lfo->waveform_bufinfo.len;
lfo->accum = frac; size_t idx = (int)(accum * len); // rounds down towards zero
assert(idx < lfo->waveform_bufinfo.len);
int16_t *waveform = lfo->waveform_bufinfo.buf;
assert(idx < lfo->waveform_bufinfo.len / 2);
mp_float_t scale = synthio_block_slot_get(&lfo->scale); mp_float_t scale = synthio_block_slot_get(&lfo->scale);
mp_float_t offset = synthio_block_slot_get(&lfo->offset); 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; mp_float_t value = MICROPY_FLOAT_C_FUN(ldexp)(waveform[idx], -15) * scale + offset;
lfo->accum = accum - phase_offset;
return value; return value;
} }
@ -83,6 +83,13 @@ void common_hal_synthio_lfo_set_scale_obj(synthio_lfo_obj_t *self, mp_obj_t arg)
synthio_block_assign_slot(arg, &self->scale, MP_QSTR_scale); synthio_block_assign_slot(arg, &self->scale, MP_QSTR_scale);
} }
mp_obj_t common_hal_synthio_lfo_get_phase_offset_obj(synthio_lfo_obj_t *self) {
return self->phase_offset.obj;
}
void common_hal_synthio_lfo_set_phase_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg) {
synthio_block_assign_slot(arg, &self->phase_offset, MP_QSTR_phase_offset);
}
mp_obj_t common_hal_synthio_lfo_get_offset_obj(synthio_lfo_obj_t *self) { mp_obj_t common_hal_synthio_lfo_get_offset_obj(synthio_lfo_obj_t *self) {
return self->offset.obj; return self->offset.obj;
} }

View File

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