Skip to content

Commit 4215215

Browse files
committed
Convert floating point operations to fixed point and use synthio_sat16.
1 parent e855e3e commit 4215215

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

shared-module/audiofilters/Phaser.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ audioio_get_buffer_result_t audiofilters_phaser_get_buffer(audiofilters_phaser_o
221221
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
222222
shared_bindings_synthio_lfo_tick(self->base.sample_rate, n / self->base.channel_count);
223223
mp_float_t frequency = synthio_block_slot_get_limited(&self->frequency, MICROPY_FLOAT_CONST(0.0), self->nyquist);
224-
mp_float_t feedback = synthio_block_slot_get_limited(&self->feedback, MICROPY_FLOAT_CONST(0.1), MICROPY_FLOAT_CONST(0.9));
225-
mp_float_t mix = synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0));
224+
int16_t feedback = (int16_t)(synthio_block_slot_get_limited(&self->feedback, MICROPY_FLOAT_CONST(0.1), MICROPY_FLOAT_CONST(0.9)) * 32767);
225+
int16_t mix = (int16_t)(synthio_block_slot_get_limited(&self->mix, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0)) * 32767);
226226

227-
if (mix <= MICROPY_FLOAT_CONST(0.01)) { // if mix is zero pure sample only
227+
if (mix <= 328) { // if mix is zero (0.01 in fixed point), pure sample only
228228
for (uint32_t i = 0; i < n; i++) {
229229
if (MP_LIKELY(self->base.bits_per_sample == 16)) {
230230
word_buffer[i] = sample_src[i];
@@ -233,10 +233,9 @@ audioio_get_buffer_result_t audiofilters_phaser_get_buffer(audiofilters_phaser_o
233233
}
234234
}
235235
} else {
236-
237236
// Update all-pass filter coefficient
238-
mp_float_t allpasscoef = frequency / self->nyquist;
239-
allpasscoef = (MICROPY_FLOAT_CONST(1.0) - allpasscoef) / (MICROPY_FLOAT_CONST(1.0) + allpasscoef);
237+
frequency /= self->nyquist; // scale relative to frequency range
238+
int16_t allpasscoef = (int16_t)((MICROPY_FLOAT_CONST(1.0) - frequency) / (MICROPY_FLOAT_CONST(1.0) + frequency) * 32767);
240239

241240
for (uint32_t i = 0; i < n; i++) {
242241
bool right_channel = (single_channel_output && channel == 1) || (!single_channel_output && (i % self->base.channel_count) == 1);
@@ -254,19 +253,19 @@ audioio_get_buffer_result_t audiofilters_phaser_get_buffer(audiofilters_phaser_o
254253
}
255254
}
256255

257-
int32_t word = sample_word + self->word_buffer[right_channel] * feedback;
256+
int32_t word = synthio_sat16(sample_word + synthio_sat16(self->word_buffer[right_channel] * feedback, 15), 0);
258257
int32_t allpass_word = 0;
259258

260259
// Update all-pass filters
261260
for (uint32_t j = 0; j < self->stages; j++) {
262-
allpass_word = word * -allpasscoef + self->allpass_buffer[j + allpass_buffer_offset];
263-
self->allpass_buffer[j + allpass_buffer_offset] = allpass_word * allpasscoef + word;
261+
allpass_word = synthio_sat16(synthio_sat16(word * -allpasscoef, 15) + self->allpass_buffer[j + allpass_buffer_offset], 0);
262+
self->allpass_buffer[j + allpass_buffer_offset] = synthio_sat16(synthio_sat16(allpass_word * allpasscoef, 15) + word, 0);
264263
word = allpass_word;
265264
}
266265
self->word_buffer[(bool)allpass_buffer_offset] = word;
267266

268267
// Add original sample + effect
269-
word = sample_word + (int32_t)(word * mix);
268+
word = sample_word + (int32_t)(synthio_sat16(word * mix, 15));
270269
word = synthio_mix_down_sample(word, 2);
271270

272271
if (MP_LIKELY(self->base.bits_per_sample == 16)) {

0 commit comments

Comments
 (0)