Skip to content

Commit a891e14

Browse files
committed
synthio: Fix multichannel biquad filtering
1 parent 5102797 commit a891e14

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

shared-module/synthio/Biquad.c

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,35 @@ void synthio_biquad_filter_assign(biquad_filter_state *st, mp_obj_t biquad_obj)
110110
}
111111
}
112112

113-
void synthio_biquad_filter_samples(biquad_filter_state *st, int32_t *out, const int32_t *in, size_t n, size_t stride) {
113+
void synthio_biquad_filter_samples(biquad_filter_state *st, int32_t *out0, const int32_t *in0, size_t n0, size_t n_channels) {
114114
int32_t a1 = st->a1;
115115
int32_t a2 = st->a2;
116116
int32_t b0 = st->b0;
117117
int32_t b1 = st->b1;
118118
int32_t b2 = st->b2;
119119

120-
int32_t x0 = st->x[0];
121-
int32_t x1 = st->x[1];
122-
int32_t y0 = st->y[0];
123-
int32_t y1 = st->y[1];
120+
for (size_t i = 0; i < n_channels; i++) {
121+
const int32_t *in = in0 + i;
122+
int32_t *out = out0 + i;
124123

125-
for (; n; --n, in += stride, out += stride) {
126-
int16_t input = *in;
127-
int32_t output = (b0 * input + b1 * x0 + b2 * x1 - a1 * y0 - a2 * y1) >> BIQUAD_SHIFT;
124+
int32_t x0 = st->x[i][0];
125+
int32_t x1 = st->x[i][1];
126+
int32_t y0 = st->y[i][0];
127+
int32_t y1 = st->y[i][1];
128128

129-
x1 = x0;
130-
x0 = input;
131-
y1 = y0;
132-
y0 = output;
133-
*out = output;
129+
for (size_t n = n0; n; --n, in += n_channels, out += n_channels) {
130+
int32_t input = *in;
131+
int32_t output = (b0 * input + b1 * x0 + b2 * x1 - a1 * y0 - a2 * y1) >> BIQUAD_SHIFT;
132+
133+
x1 = x0;
134+
x0 = input;
135+
y1 = y0;
136+
y0 = output;
137+
*out = output;
138+
}
139+
st->x[i][0] = x0;
140+
st->x[i][1] = x1;
141+
st->y[i][0] = y0;
142+
st->y[i][1] = y1;
134143
}
135-
st->x[0] = x0;
136-
st->x[1] = x1;
137-
st->y[0] = y0;
138-
st->y[1] = y1;
139144
}

shared-module/synthio/Biquad.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
typedef struct {
3232
int32_t a1, a2, b0, b1, b2;
33-
int32_t x[2], y[2];
33+
int32_t x[2][2], y[2][2];
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_samples(biquad_filter_state *st, int32_t *out, const int32_t *in, size_t n, size_t stride);
37+
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/__init__.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,7 @@ void synthio_synth_synthesize(synthio_synth_t *synth, uint8_t **bufptr, uint32_t
352352
memset(filter_buffer32, 0, sizeof(filter_buffer32));
353353

354354
synth_note_into_buffer(synth, chan, filter_buffer32, dur);
355-
int synth_chan = synth->channel_count;
356-
for (int i = 0; i < synth_chan; i++) {
357-
synthio_biquad_filter_samples(&note->filter_state, &out_buffer32[i], &filter_buffer32[i], dur, i);
358-
}
355+
synthio_biquad_filter_samples(&note->filter_state, out_buffer32, filter_buffer32, dur, synth->channel_count);
359356
}
360357
}
361358

0 commit comments

Comments
 (0)