Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

synthio: Calculate LFO.value at construction #9247

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions shared-bindings/synthio/LFO.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ STATIC const uint16_t triangle[] = {0, 32767, 0, -32767};
//| should be considered an implementation detail, though it affects how LFOs
//| behave for instance when used to implement an integrator (``l.offset = l``).
//|
//| An LFO's output, which is reflected in its `value` property, is not
//| updated in any other way than when its associated synthesizer updates it.
//| For instance, if an LFO is created with ``offset=1``, its `value` will still
//| be ``0`` until it is updated by its associated synthesizer. Similarly, merely
//| updating its properties does not update its value property.
//| An LFO's ``value`` property is computed once when it is constructed, and then
//| when its associated synthesizer updates it.
//|
//| This means that for instance an LFO **created** with ``offset=1`` has ```value==1``
//| immediately, but **updating** the ``offset`` property alone does not
//| change ``value``; it only updates through an association with an active synthesizer.
//|
//| The interpolation of the waveform is necessarily different depending on the
//| ``once`` property. Consider a LFO with ``waveform=np.array([0, 100],
Expand Down Expand Up @@ -120,11 +121,17 @@ STATIC mp_obj_t synthio_lfo_make_new(const mp_obj_type_t *type_in, size_t n_args
synthio_synth_parse_waveform(&self->waveform_bufinfo, args[ARG_waveform].u_obj);
}
self->waveform_obj = args[ARG_waveform].u_obj;
self->base.last_tick = synthio_global_tick;

mp_obj_t result = MP_OBJ_FROM_PTR(self);
properties_construct_helper(result, lfo_properties + 1, args + 1, MP_ARRAY_SIZE(lfo_properties) - 1);

// Force computation of the LFO's initial output
synthio_global_rate_scale = 0;
self->base.last_tick = synthio_global_tick - 1;
synthio_block_slot_t slot;
synthio_block_assign_slot(MP_OBJ_FROM_PTR(result), &slot, MP_QSTR_self);
(void)synthio_block_slot_get(&slot);

return result;
};

Expand Down
2 changes: 2 additions & 0 deletions tests/circuitpython/synthlfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
lfos[2].rate = lfos[1]
lfos[2].offset = lfos[0]

print("Initial values", *(l.value for l in lfos))

for i in range(100):
print(i * 256 / 48000, *list(lfo_tick(*lfos)) + [l.phase for l in lfos])
1 change: 1 addition & 0 deletions tests/circuitpython/synthlfo.py.exp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Initial values 0.0 1.99993896484375 0.0 0.999969482421875 0.999969482421875
0.0 0.02133268229166667 1.973273111979167 0.06342789066420661 0.999969482421875 0.9466377766927083 0.02133333333333333 0.01333333333333333 0.01052412326388889 0.02666666666666667 0.02666666666666667
0.005333333333333333 0.04266536458333333 1.946607259114583 0.1262869271612167 0.999969482421875 0.8933060709635416 0.04266666666666667 0.02666666666666667 0.02090602864583333 0.05333333333333333 0.05333333333333333
0.01066666666666667 0.06399804687500001 1.91994140625 0.1885771094910304 0.999969482421875 0.8399743652343751 0.064 0.03999999999999999 0.03114571614583333 0.07999999999999998 0.07999999999999998
Expand Down