Skip to content

Commit 3151656

Browse files
committed
synthio: more fir-filter removal; fix biquad logic errors
1 parent 1d58b55 commit 3151656

File tree

9 files changed

+17
-27
lines changed

9 files changed

+17
-27
lines changed

shared-bindings/synthio/Synthesizer.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
//| channel_count: int = 1,
5454
//| waveform: Optional[ReadableBuffer] = None,
5555
//| envelope: Optional[Envelope] = None,
56-
//| filter: Optional[ReadableBuffer] = None,
5756
//| ) -> None:
5857
//| """Create a synthesizer object.
5958
//|
@@ -66,17 +65,15 @@
6665
//| :param int sample_rate: The desired playback sample rate; higher sample rate requires more memory
6766
//| :param int channel_count: The number of output channels (1=mono, 2=stereo)
6867
//| :param ReadableBuffer waveform: A single-cycle waveform. Default is a 50% duty cycle square wave. If specified, must be a ReadableBuffer of type 'h' (signed 16 bit)
69-
//| :param ReadableBuffer filter: Coefficients of an FIR filter to apply to notes with ``filter=True``. If specified, must be a ReadableBuffer of type 'h' (signed 16 bit)
7068
//| :param Optional[Envelope] envelope: An object that defines the loudness of a note over time. The default envelope, `None` provides no ramping, voices turn instantly on and off.
7169
//| """
7270
STATIC mp_obj_t synthio_synthesizer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
73-
enum { ARG_sample_rate, ARG_channel_count, ARG_waveform, ARG_envelope, ARG_filter };
71+
enum { ARG_sample_rate, ARG_channel_count, ARG_waveform, ARG_envelope };
7472
static const mp_arg_t allowed_args[] = {
7573
{ MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 11025} },
7674
{ MP_QSTR_channel_count, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} },
7775
{ MP_QSTR_waveform, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
7876
{ MP_QSTR_envelope, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
79-
{ MP_QSTR_filter, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
8077
};
8178
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
8279
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -88,7 +85,6 @@ STATIC mp_obj_t synthio_synthesizer_make_new(const mp_obj_type_t *type, size_t n
8885
args[ARG_sample_rate].u_int,
8986
args[ARG_channel_count].u_int,
9087
args[ARG_waveform].u_obj,
91-
args[ARG_filter].u_obj,
9288
args[ARG_envelope].u_obj);
9389

9490
return MP_OBJ_FROM_PTR(self);

shared-bindings/synthio/Synthesizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
extern const mp_obj_type_t synthio_synthesizer_type;
3333

3434
void common_hal_synthio_synthesizer_construct(synthio_synthesizer_obj_t *self,
35-
uint32_t sample_rate, int channel_count, mp_obj_t waveform_obj, mp_obj_t filter_obj,
35+
uint32_t sample_rate, int channel_count, mp_obj_t waveform_obj,
3636
mp_obj_t envelope_obj);
3737
void common_hal_synthio_synthesizer_deinit(synthio_synthesizer_obj_t *self);
3838
bool common_hal_synthio_synthesizer_deinited(synthio_synthesizer_obj_t *self);

shared-module/synthio/Biquad.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ mp_obj_t common_hal_synthio_new_bpf(mp_float_t w0, mp_float_t Q) {
9494
return namedtuple_make_new((const mp_obj_type_t *)&synthio_biquad_type_obj, MP_ARRAY_SIZE(out_args), 0, out_args);
9595
}
9696

97-
#define BIQUAD_SHIFT (16)
97+
#define BIQUAD_SHIFT (19)
9898
STATIC int32_t biquad_scale_arg_obj(mp_obj_t arg) {
9999
return (int32_t)MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(ldexp)(mp_obj_get_float(arg), BIQUAD_SHIFT));
100100
}
@@ -110,6 +110,10 @@ void synthio_biquad_filter_assign(biquad_filter_state *st, mp_obj_t biquad_obj)
110110
}
111111
}
112112

113+
void synthio_biquad_filter_reset(biquad_filter_state *st) {
114+
memset(&st->x, 0, 8 * sizeof(int16_t));
115+
}
116+
113117
void synthio_biquad_filter_samples(biquad_filter_state *st, int32_t *out0, const int32_t *in0, size_t n0, size_t n_channels) {
114118
int32_t a1 = st->a1;
115119
int32_t a2 = st->a2;
@@ -134,7 +138,7 @@ void synthio_biquad_filter_samples(biquad_filter_state *st, int32_t *out0, const
134138
x0 = input;
135139
y1 = y0;
136140
y0 = output;
137-
*out = output;
141+
*out += output;
138142
}
139143
st->x[i][0] = x0;
140144
st->x[i][1] = x1;

shared-module/synthio/Biquad.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ typedef struct {
3434
} biquad_filter_state;
3535

3636
void synthio_biquad_filter_assign(biquad_filter_state *st, mp_obj_t biquad_obj);
37+
void synthio_biquad_filter_reset(biquad_filter_state *st);
3738
void synthio_biquad_filter_samples(biquad_filter_state *st, int32_t *out, const int32_t *in, size_t n_samples, size_t n_channels);

shared-module/synthio/MidiTrack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void common_hal_synthio_miditrack_construct(synthio_miditrack_obj_t *self,
122122
self->track.buf = (void *)buffer;
123123
self->track.len = len;
124124

125-
synthio_synth_init(&self->synth, sample_rate, 1, waveform_obj, mp_const_none, envelope_obj);
125+
synthio_synth_init(&self->synth, sample_rate, 1, waveform_obj, envelope_obj);
126126

127127
start_parse(self);
128128
}

shared-module/synthio/Note.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ void synthio_note_recalculate(synthio_note_obj_t *self, int32_t sample_rate) {
148148

149149
void synthio_note_start(synthio_note_obj_t *self, int32_t sample_rate) {
150150
synthio_note_recalculate(self, sample_rate);
151+
synthio_biquad_filter_reset(&self->filter_state);
151152
}
152153

153154
// Perform a pitch bend operation

shared-module/synthio/Synthesizer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333

3434

3535
void common_hal_synthio_synthesizer_construct(synthio_synthesizer_obj_t *self,
36-
uint32_t sample_rate, int channel_count, mp_obj_t waveform_obj, mp_obj_t filter_obj,
36+
uint32_t sample_rate, int channel_count, mp_obj_t waveform_obj,
3737
mp_obj_t envelope_obj) {
3838

39-
synthio_synth_init(&self->synth, sample_rate, channel_count, waveform_obj, filter_obj, envelope_obj);
39+
synthio_synth_init(&self->synth, sample_rate, channel_count, waveform_obj, envelope_obj);
4040
self->blocks = mp_obj_new_list(0, NULL);
4141
}
4242

shared-module/synthio/__init__.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ bool synthio_synth_deinited(synthio_synth_t *synth) {
389389
}
390390

391391
void synthio_synth_deinit(synthio_synth_t *synth) {
392-
synth->filter_buffer = NULL;
393392
synth->buffers[0] = NULL;
394393
synth->buffers[1] = NULL;
395394
}
@@ -403,17 +402,12 @@ mp_obj_t synthio_synth_envelope_get(synthio_synth_t *synth) {
403402
return synth->envelope_obj;
404403
}
405404

406-
void synthio_synth_init(synthio_synth_t *synth, uint32_t sample_rate, int channel_count, mp_obj_t waveform_obj, mp_obj_t filter_obj, mp_obj_t envelope_obj) {
405+
void synthio_synth_init(synthio_synth_t *synth, uint32_t sample_rate, int channel_count, mp_obj_t waveform_obj, mp_obj_t envelope_obj) {
407406
synthio_synth_parse_waveform(&synth->waveform_bufinfo, waveform_obj);
408-
synthio_synth_parse_filter(&synth->filter_bufinfo, filter_obj);
409407
mp_arg_validate_int_range(channel_count, 1, 2, MP_QSTR_channel_count);
410408
synth->buffer_length = SYNTHIO_MAX_DUR * SYNTHIO_BYTES_PER_SAMPLE * channel_count;
411409
synth->buffers[0] = m_malloc(synth->buffer_length, false);
412410
synth->buffers[1] = m_malloc(synth->buffer_length, false);
413-
if (synth->filter_bufinfo.len) {
414-
synth->filter_buffer_length = (synth->filter_bufinfo.len + SYNTHIO_MAX_DUR) * channel_count * sizeof(int32_t);
415-
synth->filter_buffer = m_malloc(synth->filter_buffer_length, false);
416-
}
417411
synth->channel_count = channel_count;
418412
synth->other_channel = -1;
419413
synth->waveform_obj = waveform_obj;
@@ -453,11 +447,6 @@ void synthio_synth_parse_waveform(mp_buffer_info_t *bufinfo_waveform, mp_obj_t w
453447
parse_common(bufinfo_waveform, waveform_obj, MP_QSTR_waveform, 16384);
454448
}
455449

456-
void synthio_synth_parse_filter(mp_buffer_info_t *bufinfo_filter, mp_obj_t filter_obj) {
457-
*bufinfo_filter = ((mp_buffer_info_t) { .buf = NULL, .len = 0 });
458-
parse_common(bufinfo_filter, filter_obj, MP_QSTR_filter, 128);
459-
}
460-
461450
STATIC int find_channel_with_note(synthio_synth_t *synth, mp_obj_t note) {
462451
for (int i = 0; i < CIRCUITPY_SYNTHIO_MAX_CHANNELS; i++) {
463452
if (synth->span.note_obj[i] == note) {

shared-module/synthio/__init__.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ typedef struct synthio_synth {
6464
uint32_t sample_rate;
6565
uint32_t total_envelope;
6666
int16_t *buffers[2];
67-
int32_t *filter_buffer;
6867
uint8_t channel_count;
69-
uint16_t buffer_length, filter_buffer_length;
68+
uint16_t buffer_length;
7069
uint16_t last_buffer_length;
7170
uint8_t other_channel, buffer_index, other_buffer_index;
72-
mp_buffer_info_t waveform_bufinfo, filter_bufinfo;
71+
mp_buffer_info_t waveform_bufinfo;
7372
synthio_envelope_definition_t global_envelope_definition;
7473
mp_obj_t waveform_obj, filter_obj, envelope_obj;
7574
synthio_midi_span_t span;
@@ -91,7 +90,7 @@ typedef struct {
9190
void synthio_synth_synthesize(synthio_synth_t *synth, uint8_t **buffer, uint32_t *buffer_length, uint8_t channel);
9291
void synthio_synth_deinit(synthio_synth_t *synth);
9392
bool synthio_synth_deinited(synthio_synth_t *synth);
94-
void synthio_synth_init(synthio_synth_t *synth, uint32_t sample_rate, int channel_count, mp_obj_t waveform_obj, mp_obj_t filter_obj, mp_obj_t envelope);
93+
void synthio_synth_init(synthio_synth_t *synth, uint32_t sample_rate, int channel_count, mp_obj_t waveform_obj, mp_obj_t envelope);
9594
void synthio_synth_get_buffer_structure(synthio_synth_t *synth, bool single_channel_output,
9695
bool *single_buffer, bool *samples_signed, uint32_t *max_buffer_length, uint8_t *spacing);
9796
void synthio_synth_reset_buffer(synthio_synth_t *synth, bool single_channel_output, uint8_t channel);

0 commit comments

Comments
 (0)