Skip to content

Commit e0cfae1

Browse files
committed
synthio: add (untested) phase offset
1 parent e259f8d commit e0cfae1

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

shared-bindings/synthio/LFO.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
//| rate: BlockInput = 1.0,
6464
//| scale: BlockInput = 1.0,
6565
//| offset: BlockInput = 0,
66+
//| phase_offset: BlockInput = 0,
6667
//| once=False
6768
//| ):
6869
//| pass
@@ -71,6 +72,7 @@ static const mp_arg_t lfo_properties[] = {
7172
{ MP_QSTR_rate, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1) } },
7273
{ MP_QSTR_scale, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1) } },
7374
{ MP_QSTR_offset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
75+
{ MP_QSTR_phase_offset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
7476
{ MP_QSTR_once, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
7577
};
7678

@@ -141,6 +143,24 @@ MP_PROPERTY_GETSET(synthio_lfo_offset_obj,
141143
(mp_obj_t)&synthio_lfo_get_offset_obj,
142144
(mp_obj_t)&synthio_lfo_set_offset_obj);
143145

146+
//| phase_offset: BlockInput
147+
//| """An additive value applied to the LFO's phase"""
148+
STATIC mp_obj_t synthio_lfo_get_phase_offset(mp_obj_t self_in) {
149+
synthio_lfo_obj_t *self = MP_OBJ_TO_PTR(self_in);
150+
return common_hal_synthio_lfo_get_phase_offset_obj(self);
151+
}
152+
MP_DEFINE_CONST_FUN_OBJ_1(synthio_lfo_get_phase_offset_obj, synthio_lfo_get_phase_offset);
153+
154+
STATIC mp_obj_t synthio_lfo_set_phase_offset(mp_obj_t self_in, mp_obj_t arg) {
155+
synthio_lfo_obj_t *self = MP_OBJ_TO_PTR(self_in);
156+
common_hal_synthio_lfo_set_phase_offset_obj(self, arg);
157+
return mp_const_none;
158+
}
159+
MP_DEFINE_CONST_FUN_OBJ_2(synthio_lfo_set_phase_offset_obj, synthio_lfo_set_phase_offset);
160+
MP_PROPERTY_GETSET(synthio_lfo_phase_offset_obj,
161+
(mp_obj_t)&synthio_lfo_get_phase_offset_obj,
162+
(mp_obj_t)&synthio_lfo_set_phase_offset_obj);
163+
144164
//| scale: BlockInput
145165
//| """An additive value applied to the LFO's output"""
146166
STATIC mp_obj_t synthio_lfo_get_scale(mp_obj_t self_in) {
@@ -161,7 +181,9 @@ MP_PROPERTY_GETSET(synthio_lfo_scale_obj,
161181

162182
//|
163183
//| once: bool
164-
//| """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"""
184+
//| """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
185+
//|
186+
//| This applies to the ``phase`` *before* the addition of any ``phase_offset`` """
165187
STATIC mp_obj_t synthio_lfo_get_once(mp_obj_t self_in) {
166188
synthio_lfo_obj_t *self = MP_OBJ_TO_PTR(self_in);
167189
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[] = {
226248
{ MP_ROM_QSTR(MP_QSTR_rate), MP_ROM_PTR(&synthio_lfo_rate_obj) },
227249
{ MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&synthio_lfo_scale_obj) },
228250
{ MP_ROM_QSTR(MP_QSTR_offset), MP_ROM_PTR(&synthio_lfo_offset_obj) },
251+
{ MP_ROM_QSTR(MP_QSTR_phase_offset), MP_ROM_PTR(&synthio_lfo_phase_offset_obj) },
229252
{ MP_ROM_QSTR(MP_QSTR_once), MP_ROM_PTR(&synthio_lfo_once_obj) },
230253
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&synthio_lfo_value_obj) },
231254
{ MP_ROM_QSTR(MP_QSTR_phase), MP_ROM_PTR(&synthio_lfo_phase_obj) },

shared-bindings/synthio/LFO.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ void common_hal_synthio_lfo_set_rate_obj(synthio_lfo_obj_t *self, mp_obj_t arg);
4040
mp_obj_t common_hal_synthio_lfo_get_scale_obj(synthio_lfo_obj_t *self);
4141
void common_hal_synthio_lfo_set_scale_obj(synthio_lfo_obj_t *self, mp_obj_t arg);
4242

43+
mp_obj_t common_hal_synthio_lfo_get_phase_offset_obj(synthio_lfo_obj_t *self);
44+
void common_hal_synthio_lfo_set_phase_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg);
45+
4346
mp_obj_t common_hal_synthio_lfo_get_offset_obj(synthio_lfo_obj_t *self);
4447
void common_hal_synthio_lfo_set_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg);
4548

shared-module/synthio/LFO.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,32 @@ mp_float_t common_hal_synthio_lfo_tick(mp_obj_t self_in) {
3535
synthio_lfo_obj_t *lfo = MP_OBJ_TO_PTR(self_in);
3636

3737
mp_float_t rate = synthio_block_slot_get(&lfo->rate) * synthio_global_rate_scale;
38+
mp_float_t phase_offset = synthio_block_slot_get(&lfo->phase_offset);
3839

39-
mp_float_t accum = lfo->accum + rate;
40-
mp_float_t frac = accum - MICROPY_FLOAT_C_FUN(floor)(accum);
41-
size_t idx = (int)(frac * len);
40+
mp_float_t accum = lfo->accum + rate + phase_offset;
4241

4342
if (lfo->once) {
4443
if (rate > 0) {
4544
if (accum >= ONE) {
46-
frac = ONE;
47-
idx = len - 1;
45+
accum = ONE;
4846
}
4947
} else if (rate < 0 && accum < ZERO) {
50-
frac = ZERO;
51-
idx = 0;
48+
accum = ZERO;
5249
}
50+
} else {
51+
accum = accum - MICROPY_FLOAT_C_FUN(floor)(accum);
5352
}
5453

5554
int len = lfo->waveform_bufinfo.len;
56-
lfo->accum = frac;
55+
size_t idx = (int)(accum * len); // rounds down towards zero
56+
assert(idx < lfo->waveform_bufinfo.len);
5757

58-
int16_t *waveform = lfo->waveform_bufinfo.buf;
59-
assert(idx < lfo->waveform_bufinfo.len / 2);
6058
mp_float_t scale = synthio_block_slot_get(&lfo->scale);
6159
mp_float_t offset = synthio_block_slot_get(&lfo->offset);
60+
int16_t *waveform = lfo->waveform_bufinfo.buf;
6261
mp_float_t value = MICROPY_FLOAT_C_FUN(ldexp)(waveform[idx], -15) * scale + offset;
6362

63+
lfo->accum = accum - phase_offset;
6464
return value;
6565
}
6666

@@ -83,6 +83,13 @@ void common_hal_synthio_lfo_set_scale_obj(synthio_lfo_obj_t *self, mp_obj_t arg)
8383
synthio_block_assign_slot(arg, &self->scale, MP_QSTR_scale);
8484
}
8585

86+
mp_obj_t common_hal_synthio_lfo_get_phase_offset_obj(synthio_lfo_obj_t *self) {
87+
return self->phase_offset.obj;
88+
}
89+
void common_hal_synthio_lfo_set_phase_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg) {
90+
synthio_block_assign_slot(arg, &self->phase_offset, MP_QSTR_phase_offset);
91+
}
92+
8693
mp_obj_t common_hal_synthio_lfo_get_offset_obj(synthio_lfo_obj_t *self) {
8794
return self->offset.obj;
8895
}

shared-module/synthio/LFO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef struct synthio_lfo_obj {
3232
synthio_block_base_t base;
3333
bool once;
3434

35-
synthio_block_slot_t rate, scale, offset;
35+
synthio_block_slot_t rate, scale, offset, phase_offset;
3636
mp_float_t accum;
3737

3838
mp_obj_t waveform_obj;

0 commit comments

Comments
 (0)