Skip to content

Commit f7f471f

Browse files
authored
Merge pull request #9788 from dcooperdalrymple/synthio_waveform_loop_blockinput
2 parents 973f597 + fd24006 commit f7f471f

File tree

6 files changed

+62
-80
lines changed

6 files changed

+62
-80
lines changed

shared-bindings/synthio/Note.c

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ static const mp_arg_t note_properties[] = {
2020
{ MP_QSTR_amplitude, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1) } },
2121
{ MP_QSTR_bend, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
2222
{ MP_QSTR_waveform, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_NONE } },
23-
{ MP_QSTR_waveform_loop_start, MP_ARG_OBJ, {.u_obj = MP_ROM_INT(0) } },
24-
{ MP_QSTR_waveform_loop_end, MP_ARG_OBJ, {.u_obj = MP_ROM_INT(SYNTHIO_WAVEFORM_SIZE) } },
23+
{ MP_QSTR_waveform_loop_start, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
24+
{ MP_QSTR_waveform_loop_end, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(SYNTHIO_WAVEFORM_SIZE) } },
2525
{ MP_QSTR_envelope, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_NONE } },
2626
{ MP_QSTR_filter, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_NONE } },
27-
{ MP_QSTR_ring_frequency, MP_ARG_OBJ, {.u_obj = MP_ROM_INT(0) } },
28-
{ MP_QSTR_ring_bend, MP_ARG_OBJ, {.u_obj = MP_ROM_INT(0) } },
27+
{ MP_QSTR_ring_frequency, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
28+
{ MP_QSTR_ring_bend, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
2929
{ MP_QSTR_ring_waveform, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_NONE } },
30-
{ MP_QSTR_ring_waveform_loop_start, MP_ARG_OBJ, {.u_obj = MP_ROM_INT(0) } },
31-
{ MP_QSTR_ring_waveform_loop_end, MP_ARG_OBJ, {.u_obj = MP_ROM_INT(SYNTHIO_WAVEFORM_SIZE) } },
30+
{ MP_QSTR_ring_waveform_loop_start, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(0) } },
31+
{ MP_QSTR_ring_waveform_loop_end, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(SYNTHIO_WAVEFORM_SIZE) } },
3232
};
3333
//| class Note:
3434
//| def __init__(
@@ -37,17 +37,17 @@ static const mp_arg_t note_properties[] = {
3737
//| frequency: float,
3838
//| panning: BlockInput = 0.0,
3939
//| waveform: Optional[ReadableBuffer] = None,
40-
//| waveform_loop_start: int = 0,
41-
//| waveform_loop_end: int = waveform_max_length,
40+
//| waveform_loop_start: BlockInput = 0,
41+
//| waveform_loop_end: BlockInput = waveform_max_length,
4242
//| envelope: Optional[Envelope] = None,
43-
//| amplitude: BlockInput = 0.0,
43+
//| amplitude: BlockInput = 1.0,
4444
//| bend: BlockInput = 0.0,
4545
//| filter: Optional[Biquad] = None,
4646
//| ring_frequency: float = 0.0,
4747
//| ring_bend: float = 0.0,
4848
//| ring_waveform: Optional[ReadableBuffer] = None,
49-
//| ring_waveform_loop_start: int = 0,
50-
//| ring_waveform_loop_end: int = waveform_max_length,
49+
//| ring_waveform_loop_start: BlockInput = 0,
50+
//| ring_waveform_loop_end: BlockInput = waveform_max_length,
5151
//| ) -> None:
5252
//| """Construct a Note object, with a frequency in Hz, and optional panning, waveform, envelope, tremolo (volume change) and bend (frequency change).
5353
//|
@@ -198,46 +198,44 @@ MP_PROPERTY_GETSET(synthio_note_waveform_obj,
198198
(mp_obj_t)&synthio_note_get_waveform_obj,
199199
(mp_obj_t)&synthio_note_set_waveform_obj);
200200

201-
//| waveform_loop_start: int
201+
202+
203+
//| waveform_loop_start: BlockInput
202204
//| """The sample index of where to begin looping waveform data.
203205
//|
204-
//| Values outside the range ``0`` to ``waveform_max_length-1`` (inclusive) are rejected with a `ValueError`.
205-
//|
206-
//| Values greater than or equal to the actual waveform length are treated as 0."""
206+
//| The value is limited to the range ``0`` to ``len(waveform)-1`` (inclusive)."""
207207
static mp_obj_t synthio_note_get_waveform_loop_start(mp_obj_t self_in) {
208208
synthio_note_obj_t *self = MP_OBJ_TO_PTR(self_in);
209-
return mp_obj_new_int(common_hal_synthio_note_get_waveform_loop_start(self));
209+
return common_hal_synthio_note_get_waveform_loop_start(self);
210210
}
211211
MP_DEFINE_CONST_FUN_OBJ_1(synthio_note_get_waveform_loop_start_obj, synthio_note_get_waveform_loop_start);
212212

213213
static mp_obj_t synthio_note_set_waveform_loop_start(mp_obj_t self_in, mp_obj_t arg) {
214214
synthio_note_obj_t *self = MP_OBJ_TO_PTR(self_in);
215-
common_hal_synthio_note_set_waveform_loop_start(self, mp_obj_get_int(arg));
215+
common_hal_synthio_note_set_waveform_loop_start(self, arg);
216216
return mp_const_none;
217217
}
218218
MP_DEFINE_CONST_FUN_OBJ_2(synthio_note_set_waveform_loop_start_obj, synthio_note_set_waveform_loop_start);
219219
MP_PROPERTY_GETSET(synthio_note_waveform_loop_start_obj,
220220
(mp_obj_t)&synthio_note_get_waveform_loop_start_obj,
221221
(mp_obj_t)&synthio_note_set_waveform_loop_start_obj);
222222

223-
//| waveform_loop_end: int
223+
//| waveform_loop_end: BlockInput
224224
//| """The sample index of where to end looping waveform data.
225225
//|
226-
//| Values outside the range ``1`` to ``waveform_max_length`` (inclusive) are rejected with a `ValueError`.
227-
//|
228-
//| If the value is greater than the actual waveform length, or less than or equal to the loop start, the loop will occur at the end of the waveform.
226+
//| The value is limited to the range ``waveform_loop_start+1`` to ``len(waveform)`` (inclusive).
229227
//|
230228
//| Use the `synthio.waveform_max_length` constant to set the loop point at the end of the wave form, no matter its length."""
231229
//|
232230
static mp_obj_t synthio_note_get_waveform_loop_end(mp_obj_t self_in) {
233231
synthio_note_obj_t *self = MP_OBJ_TO_PTR(self_in);
234-
return mp_obj_new_int(common_hal_synthio_note_get_waveform_loop_end(self));
232+
return common_hal_synthio_note_get_waveform_loop_end(self);
235233
}
236234
MP_DEFINE_CONST_FUN_OBJ_1(synthio_note_get_waveform_loop_end_obj, synthio_note_get_waveform_loop_end);
237235

238236
static mp_obj_t synthio_note_set_waveform_loop_end(mp_obj_t self_in, mp_obj_t arg) {
239237
synthio_note_obj_t *self = MP_OBJ_TO_PTR(self_in);
240-
common_hal_synthio_note_set_waveform_loop_end(self, mp_obj_get_int(arg));
238+
common_hal_synthio_note_set_waveform_loop_end(self, arg);
241239
return mp_const_none;
242240
}
243241
MP_DEFINE_CONST_FUN_OBJ_2(synthio_note_set_waveform_loop_end_obj, synthio_note_set_waveform_loop_end);
@@ -331,46 +329,42 @@ MP_PROPERTY_GETSET(synthio_note_ring_waveform_obj,
331329
(mp_obj_t)&synthio_note_get_ring_waveform_obj,
332330
(mp_obj_t)&synthio_note_set_ring_waveform_obj);
333331

334-
//| ring_waveform_loop_start: int
332+
//| ring_waveform_loop_start: BlockInput
335333
//| """The sample index of where to begin looping waveform data.
336334
//|
337-
//| Values outside the range ``0`` to ``waveform_max_length-1`` (inclusive) are rejected with a `ValueError`.
338-
//|
339-
//| Values greater than or equal to the actual waveform length are treated as 0."""
335+
//| The value is limited to the range ``0`` to ``len(ring_waveform)-1`` (inclusive)."""
340336
static mp_obj_t synthio_note_get_ring_waveform_loop_start(mp_obj_t self_in) {
341337
synthio_note_obj_t *self = MP_OBJ_TO_PTR(self_in);
342-
return mp_obj_new_int(common_hal_synthio_note_get_ring_waveform_loop_start(self));
338+
return common_hal_synthio_note_get_ring_waveform_loop_start(self);
343339
}
344340
MP_DEFINE_CONST_FUN_OBJ_1(synthio_note_get_ring_waveform_loop_start_obj, synthio_note_get_ring_waveform_loop_start);
345341

346342
static mp_obj_t synthio_note_set_ring_waveform_loop_start(mp_obj_t self_in, mp_obj_t arg) {
347343
synthio_note_obj_t *self = MP_OBJ_TO_PTR(self_in);
348-
common_hal_synthio_note_set_ring_waveform_loop_start(self, mp_obj_get_int(arg));
344+
common_hal_synthio_note_set_ring_waveform_loop_start(self, arg);
349345
return mp_const_none;
350346
}
351347
MP_DEFINE_CONST_FUN_OBJ_2(synthio_note_set_ring_waveform_loop_start_obj, synthio_note_set_ring_waveform_loop_start);
352348
MP_PROPERTY_GETSET(synthio_note_ring_waveform_loop_start_obj,
353349
(mp_obj_t)&synthio_note_get_ring_waveform_loop_start_obj,
354350
(mp_obj_t)&synthio_note_set_ring_waveform_loop_start_obj);
355351

356-
//| ring_waveform_loop_end: int
352+
//| ring_waveform_loop_end: BlockInput
357353
//| """The sample index of where to end looping waveform data.
358354
//|
359-
//| Values outside the range ``1`` to ``waveform_max_length`` (inclusive) are rejected with a `ValueError`.
360-
//|
361-
//| If the value is greater than the actual waveform length, or less than or equal to the loop start, the loop will occur at the end of the waveform.
355+
//| The value is limited to the range ``ring_waveform_loop_start+1`` to ``len(ring_waveform)`` (inclusive).
362356
//|
363357
//| Use the `synthio.waveform_max_length` constant to set the loop point at the end of the wave form, no matter its length."""
364358
//|
365359
static mp_obj_t synthio_note_get_ring_waveform_loop_end(mp_obj_t self_in) {
366360
synthio_note_obj_t *self = MP_OBJ_TO_PTR(self_in);
367-
return mp_obj_new_int(common_hal_synthio_note_get_ring_waveform_loop_end(self));
361+
return common_hal_synthio_note_get_ring_waveform_loop_end(self);
368362
}
369363
MP_DEFINE_CONST_FUN_OBJ_1(synthio_note_get_ring_waveform_loop_end_obj, synthio_note_get_ring_waveform_loop_end);
370364

371365
static mp_obj_t synthio_note_set_ring_waveform_loop_end(mp_obj_t self_in, mp_obj_t arg) {
372366
synthio_note_obj_t *self = MP_OBJ_TO_PTR(self_in);
373-
common_hal_synthio_note_set_ring_waveform_loop_end(self, mp_obj_get_int(arg));
367+
common_hal_synthio_note_set_ring_waveform_loop_end(self, arg);
374368
return mp_const_none;
375369
}
376370
MP_DEFINE_CONST_FUN_OBJ_2(synthio_note_set_ring_waveform_loop_end_obj, synthio_note_set_ring_waveform_loop_end);

shared-bindings/synthio/Note.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ void common_hal_synthio_note_set_bend(synthio_note_obj_t *self, mp_obj_t value);
3131
mp_obj_t common_hal_synthio_note_get_waveform_obj(synthio_note_obj_t *self);
3232
void common_hal_synthio_note_set_waveform(synthio_note_obj_t *self, mp_obj_t value);
3333

34-
mp_int_t common_hal_synthio_note_get_waveform_loop_start(synthio_note_obj_t *self);
35-
void common_hal_synthio_note_set_waveform_loop_start(synthio_note_obj_t *self, mp_int_t value_in);
34+
mp_obj_t common_hal_synthio_note_get_waveform_loop_start(synthio_note_obj_t *self);
35+
void common_hal_synthio_note_set_waveform_loop_start(synthio_note_obj_t *self, mp_obj_t value);
3636

37-
mp_int_t common_hal_synthio_note_get_waveform_loop_end(synthio_note_obj_t *self);
38-
void common_hal_synthio_note_set_waveform_loop_end(synthio_note_obj_t *self, mp_int_t value_in);
37+
mp_obj_t common_hal_synthio_note_get_waveform_loop_end(synthio_note_obj_t *self);
38+
void common_hal_synthio_note_set_waveform_loop_end(synthio_note_obj_t *self, mp_obj_t value);
3939

4040
mp_float_t common_hal_synthio_note_get_ring_frequency(synthio_note_obj_t *self);
4141
void common_hal_synthio_note_set_ring_frequency(synthio_note_obj_t *self, mp_float_t value);
@@ -46,11 +46,11 @@ void common_hal_synthio_note_set_ring_bend(synthio_note_obj_t *self, mp_obj_t va
4646
mp_obj_t common_hal_synthio_note_get_ring_waveform_obj(synthio_note_obj_t *self);
4747
void common_hal_synthio_note_set_ring_waveform(synthio_note_obj_t *self, mp_obj_t value);
4848

49-
mp_int_t common_hal_synthio_note_get_ring_waveform_loop_start(synthio_note_obj_t *self);
50-
void common_hal_synthio_note_set_ring_waveform_loop_start(synthio_note_obj_t *self, mp_int_t value_in);
49+
mp_obj_t common_hal_synthio_note_get_ring_waveform_loop_start(synthio_note_obj_t *self);
50+
void common_hal_synthio_note_set_ring_waveform_loop_start(synthio_note_obj_t *self, mp_obj_t value);
5151

52-
mp_int_t common_hal_synthio_note_get_ring_waveform_loop_end(synthio_note_obj_t *self);
53-
void common_hal_synthio_note_set_ring_waveform_loop_end(synthio_note_obj_t *self, mp_int_t value_in);
52+
mp_obj_t common_hal_synthio_note_get_ring_waveform_loop_end(synthio_note_obj_t *self);
53+
void common_hal_synthio_note_set_ring_waveform_loop_end(synthio_note_obj_t *self, mp_obj_t value);
5454

5555
mp_obj_t common_hal_synthio_note_get_envelope_obj(synthio_note_obj_t *self);
5656
void common_hal_synthio_note_set_envelope(synthio_note_obj_t *self, mp_obj_t value);

shared-module/synthio/Note.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,20 @@ void common_hal_synthio_note_set_waveform(synthio_note_obj_t *self, mp_obj_t wav
100100
self->waveform_obj = waveform_in;
101101
}
102102

103-
mp_int_t common_hal_synthio_note_get_waveform_loop_start(synthio_note_obj_t *self) {
104-
return self->waveform_loop_start;
103+
mp_obj_t common_hal_synthio_note_get_waveform_loop_start(synthio_note_obj_t *self) {
104+
return self->waveform_loop_start.obj;
105105
}
106106

107-
void common_hal_synthio_note_set_waveform_loop_start(synthio_note_obj_t *self, mp_int_t value_in) {
108-
mp_int_t val = mp_arg_validate_int_range(value_in, 0, SYNTHIO_WAVEFORM_SIZE - 1, MP_QSTR_waveform_loop_start);
109-
self->waveform_loop_start = val;
107+
void common_hal_synthio_note_set_waveform_loop_start(synthio_note_obj_t *self, mp_obj_t value_in) {
108+
synthio_block_assign_slot(value_in, &self->waveform_loop_start, MP_QSTR_waveform_loop_start);
110109
}
111110

112-
mp_int_t common_hal_synthio_note_get_waveform_loop_end(synthio_note_obj_t *self) {
113-
return self->waveform_loop_end;
111+
mp_obj_t common_hal_synthio_note_get_waveform_loop_end(synthio_note_obj_t *self) {
112+
return self->waveform_loop_end.obj;
114113
}
115114

116-
void common_hal_synthio_note_set_waveform_loop_end(synthio_note_obj_t *self, mp_int_t value_in) {
117-
mp_int_t val = mp_arg_validate_int_range(value_in, 1, SYNTHIO_WAVEFORM_SIZE, MP_QSTR_waveform_loop_end);
118-
self->waveform_loop_end = val;
115+
void common_hal_synthio_note_set_waveform_loop_end(synthio_note_obj_t *self, mp_obj_t value_in) {
116+
synthio_block_assign_slot(value_in, &self->waveform_loop_end, MP_QSTR_waveform_loop_end);
119117
}
120118

121119
mp_obj_t common_hal_synthio_note_get_ring_waveform_obj(synthio_note_obj_t *self) {
@@ -133,22 +131,20 @@ void common_hal_synthio_note_set_ring_waveform(synthio_note_obj_t *self, mp_obj_
133131
self->ring_waveform_obj = ring_waveform_in;
134132
}
135133

136-
mp_int_t common_hal_synthio_note_get_ring_waveform_loop_start(synthio_note_obj_t *self) {
137-
return self->ring_waveform_loop_start;
134+
mp_obj_t common_hal_synthio_note_get_ring_waveform_loop_start(synthio_note_obj_t *self) {
135+
return self->ring_waveform_loop_start.obj;
138136
}
139137

140-
void common_hal_synthio_note_set_ring_waveform_loop_start(synthio_note_obj_t *self, mp_int_t value_in) {
141-
mp_int_t val = mp_arg_validate_int_range(value_in, 0, SYNTHIO_WAVEFORM_SIZE - 1, MP_QSTR_ring_waveform_loop_start);
142-
self->ring_waveform_loop_start = val;
138+
void common_hal_synthio_note_set_ring_waveform_loop_start(synthio_note_obj_t *self, mp_obj_t value_in) {
139+
synthio_block_assign_slot(value_in, &self->ring_waveform_loop_start, MP_QSTR_ring_waveform_loop_start);
143140
}
144141

145-
mp_int_t common_hal_synthio_note_get_ring_waveform_loop_end(synthio_note_obj_t *self) {
146-
return self->ring_waveform_loop_end;
142+
mp_obj_t common_hal_synthio_note_get_ring_waveform_loop_end(synthio_note_obj_t *self) {
143+
return self->ring_waveform_loop_end.obj;
147144
}
148145

149-
void common_hal_synthio_note_set_ring_waveform_loop_end(synthio_note_obj_t *self, mp_int_t value_in) {
150-
mp_int_t val = mp_arg_validate_int_range(value_in, 1, SYNTHIO_WAVEFORM_SIZE, MP_QSTR_ring_waveform_loop_end);
151-
self->ring_waveform_loop_end = val;
146+
void common_hal_synthio_note_set_ring_waveform_loop_end(synthio_note_obj_t *self, mp_obj_t value_in) {
147+
synthio_block_assign_slot(value_in, &self->ring_waveform_loop_end, MP_QSTR_ring_waveform_loop_end);
152148
}
153149

154150
void synthio_note_recalculate(synthio_note_obj_t *self, int32_t sample_rate) {

shared-module/synthio/Note.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ typedef struct synthio_note_obj {
2828
int32_t ring_frequency_scaled, ring_frequency_bent;
2929

3030
mp_buffer_info_t waveform_buf;
31-
uint32_t waveform_loop_start, waveform_loop_end;
31+
synthio_block_slot_t waveform_loop_start, waveform_loop_end;
3232
mp_buffer_info_t ring_waveform_buf;
33-
uint32_t ring_waveform_loop_start, ring_waveform_loop_end;
33+
synthio_block_slot_t ring_waveform_loop_start, ring_waveform_loop_end;
3434
synthio_envelope_definition_t envelope_def;
3535
} synthio_note_obj_t;
3636

shared-module/synthio/__init__.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,23 +184,15 @@ static bool synth_note_into_buffer(synthio_synth_t *synth, int chan, int32_t *ou
184184
if (note->waveform_buf.buf) {
185185
waveform = note->waveform_buf.buf;
186186
waveform_length = note->waveform_buf.len;
187-
if (note->waveform_loop_start > 0 && note->waveform_loop_start < waveform_length) {
188-
waveform_start = note->waveform_loop_start;
189-
}
190-
if (note->waveform_loop_end > waveform_start && note->waveform_loop_end < waveform_length) {
191-
waveform_length = note->waveform_loop_end;
192-
}
187+
waveform_start = (uint32_t)synthio_block_slot_get_limited(&note->waveform_loop_start, 0, waveform_length - 1);
188+
waveform_length = (uint32_t)synthio_block_slot_get_limited(&note->waveform_loop_end, waveform_start + 1, waveform_length);
193189
}
194190
dds_rate = synthio_frequency_convert_scaled_to_dds((uint64_t)frequency_scaled * (waveform_length - waveform_start), sample_rate);
195191
if (note->ring_frequency_scaled != 0 && note->ring_waveform_buf.buf) {
196192
ring_waveform = note->ring_waveform_buf.buf;
197193
ring_waveform_length = note->ring_waveform_buf.len;
198-
if (note->ring_waveform_loop_start > 0 && note->ring_waveform_loop_start < ring_waveform_length) {
199-
ring_waveform_start = note->waveform_loop_start;
200-
}
201-
if (note->ring_waveform_loop_end > ring_waveform_start && note->ring_waveform_loop_end < ring_waveform_length) {
202-
ring_waveform_length = note->ring_waveform_loop_end;
203-
}
194+
ring_waveform_start = (uint32_t)synthio_block_slot_get_limited(&note->ring_waveform_loop_start, 0, ring_waveform_length - 1);
195+
ring_waveform_length = (uint32_t)synthio_block_slot_get_limited(&note->ring_waveform_loop_end, ring_waveform_start + 1, ring_waveform_length);
204196
ring_dds_rate = synthio_frequency_convert_scaled_to_dds((uint64_t)note->ring_frequency_bent * (ring_waveform_length - ring_waveform_start), sample_rate);
205197
uint32_t lim = ring_waveform_length << SYNTHIO_FREQUENCY_SHIFT;
206198
if (ring_dds_rate > lim / sizeof(int16_t)) {

0 commit comments

Comments
 (0)