Skip to content

Commit e2a52af

Browse files
committed
Add support for synthio.BlockInput on synthio.Note.waveform_loop_start, synthio.Note.waveform_loop_end, synthio.Note.ring_waveform_loop_start, and synthio.Note.ring_waveform_loop_end.
1 parent ce12d90 commit e2a52af

File tree

5 files changed

+52
-62
lines changed

5 files changed

+52
-62
lines changed

shared-bindings/synthio/Note.c

Lines changed: 22 additions & 20 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 } },
2727
{ MP_QSTR_ring_frequency, MP_ARG_OBJ, {.u_obj = MP_ROM_INT(0) } },
2828
{ MP_QSTR_ring_bend, MP_ARG_OBJ, {.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,
4343
//| 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,29 +198,31 @@ 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
//|
204206
//| Values outside the range ``0`` to ``waveform_max_length-1`` (inclusive) are rejected with a `ValueError`.
205207
//|
206208
//| Values greater than or equal to the actual waveform length are treated as 0."""
207209
static mp_obj_t synthio_note_get_waveform_loop_start(mp_obj_t self_in) {
208210
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));
211+
return common_hal_synthio_note_get_waveform_loop_start(self);
210212
}
211213
MP_DEFINE_CONST_FUN_OBJ_1(synthio_note_get_waveform_loop_start_obj, synthio_note_get_waveform_loop_start);
212214

213215
static mp_obj_t synthio_note_set_waveform_loop_start(mp_obj_t self_in, mp_obj_t arg) {
214216
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));
217+
common_hal_synthio_note_set_waveform_loop_start(self, arg);
216218
return mp_const_none;
217219
}
218220
MP_DEFINE_CONST_FUN_OBJ_2(synthio_note_set_waveform_loop_start_obj, synthio_note_set_waveform_loop_start);
219221
MP_PROPERTY_GETSET(synthio_note_waveform_loop_start_obj,
220222
(mp_obj_t)&synthio_note_get_waveform_loop_start_obj,
221223
(mp_obj_t)&synthio_note_set_waveform_loop_start_obj);
222224

223-
//| waveform_loop_end: int
225+
//| waveform_loop_end: BlockInput
224226
//| """The sample index of where to end looping waveform data.
225227
//|
226228
//| Values outside the range ``1`` to ``waveform_max_length`` (inclusive) are rejected with a `ValueError`.
@@ -231,13 +233,13 @@ MP_PROPERTY_GETSET(synthio_note_waveform_loop_start_obj,
231233
//|
232234
static mp_obj_t synthio_note_get_waveform_loop_end(mp_obj_t self_in) {
233235
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));
236+
return common_hal_synthio_note_get_waveform_loop_end(self);
235237
}
236238
MP_DEFINE_CONST_FUN_OBJ_1(synthio_note_get_waveform_loop_end_obj, synthio_note_get_waveform_loop_end);
237239

238240
static mp_obj_t synthio_note_set_waveform_loop_end(mp_obj_t self_in, mp_obj_t arg) {
239241
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));
242+
common_hal_synthio_note_set_waveform_loop_end(self, arg);
241243
return mp_const_none;
242244
}
243245
MP_DEFINE_CONST_FUN_OBJ_2(synthio_note_set_waveform_loop_end_obj, synthio_note_set_waveform_loop_end);
@@ -331,29 +333,29 @@ MP_PROPERTY_GETSET(synthio_note_ring_waveform_obj,
331333
(mp_obj_t)&synthio_note_get_ring_waveform_obj,
332334
(mp_obj_t)&synthio_note_set_ring_waveform_obj);
333335

334-
//| ring_waveform_loop_start: int
336+
//| ring_waveform_loop_start: BlockInput
335337
//| """The sample index of where to begin looping waveform data.
336338
//|
337339
//| Values outside the range ``0`` to ``waveform_max_length-1`` (inclusive) are rejected with a `ValueError`.
338340
//|
339341
//| Values greater than or equal to the actual waveform length are treated as 0."""
340342
static mp_obj_t synthio_note_get_ring_waveform_loop_start(mp_obj_t self_in) {
341343
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));
344+
return common_hal_synthio_note_get_ring_waveform_loop_start(self);
343345
}
344346
MP_DEFINE_CONST_FUN_OBJ_1(synthio_note_get_ring_waveform_loop_start_obj, synthio_note_get_ring_waveform_loop_start);
345347

346348
static mp_obj_t synthio_note_set_ring_waveform_loop_start(mp_obj_t self_in, mp_obj_t arg) {
347349
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));
350+
common_hal_synthio_note_set_ring_waveform_loop_start(self, arg);
349351
return mp_const_none;
350352
}
351353
MP_DEFINE_CONST_FUN_OBJ_2(synthio_note_set_ring_waveform_loop_start_obj, synthio_note_set_ring_waveform_loop_start);
352354
MP_PROPERTY_GETSET(synthio_note_ring_waveform_loop_start_obj,
353355
(mp_obj_t)&synthio_note_get_ring_waveform_loop_start_obj,
354356
(mp_obj_t)&synthio_note_set_ring_waveform_loop_start_obj);
355357

356-
//| ring_waveform_loop_end: int
358+
//| ring_waveform_loop_end: BlockInput
357359
//| """The sample index of where to end looping waveform data.
358360
//|
359361
//| Values outside the range ``1`` to ``waveform_max_length`` (inclusive) are rejected with a `ValueError`.
@@ -364,13 +366,13 @@ MP_PROPERTY_GETSET(synthio_note_ring_waveform_loop_start_obj,
364366
//|
365367
static mp_obj_t synthio_note_get_ring_waveform_loop_end(mp_obj_t self_in) {
366368
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));
369+
return common_hal_synthio_note_get_ring_waveform_loop_end(self);
368370
}
369371
MP_DEFINE_CONST_FUN_OBJ_1(synthio_note_get_ring_waveform_loop_end_obj, synthio_note_get_ring_waveform_loop_end);
370372

371373
static mp_obj_t synthio_note_set_ring_waveform_loop_end(mp_obj_t self_in, mp_obj_t arg) {
372374
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));
375+
common_hal_synthio_note_set_ring_waveform_loop_end(self, arg);
374376
return mp_const_none;
375377
}
376378
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 = synthio_block_slot_get_limited(&note->waveform_loop_start, 0, waveform_length - 1);
188+
waveform_length = 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->ring_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 = synthio_block_slot_get_limited(&note->ring_waveform_loop_start, 0, ring_waveform_length - 1);
195+
ring_waveform_length = 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)