Skip to content

Fix for fixed point overflow in synthio #10288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions shared-module/audiofreeverb/Freeverb.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Fixed point ideas from - Paul Stoffregen in the Teensy audio library https://github.com/PaulStoffregen/Audio/blob/master/effect_freeverb.cpp
//
#include "shared-bindings/audiofreeverb/Freeverb.h"
#include "shared-module/synthio/__init__.h"

#include <stdint.h>
#include "py/runtime.h"
Expand Down Expand Up @@ -217,27 +218,6 @@ void common_hal_audiofreeverb_freeverb_stop(audiofreeverb_freeverb_obj_t *self)
return;
}

// cleaner sat16 by http://www.moseleyinstruments.com/
static int16_t sat16(int32_t n, int rshift) {
// we should always round towards 0
// to avoid recirculating round-off noise
//
// a 2s complement positive number is always
// rounded down, so we only need to take
// care of negative numbers
if (n < 0) {
n = n + (~(0xFFFFFFFFUL << rshift));
}
n = n >> rshift;
if (n > 32767) {
return 32767;
}
if (n < -32768) {
return -32768;
}
return n;
}

audioio_get_buffer_result_t audiofreeverb_freeverb_get_buffer(audiofreeverb_freeverb_obj_t *self, bool single_channel_output, uint8_t channel,
uint8_t **buffer, uint32_t *buffer_length) {

Expand Down Expand Up @@ -301,35 +281,35 @@ audioio_get_buffer_result_t audiofreeverb_freeverb_get_buffer(audiofreeverb_free
int16_t input, bufout, output;
uint32_t channel_comb_offset = 0, channel_allpass_offset = 0;

input = sat16(sample_word * 8738, 17); // Initial input scaled down so we can add reverb
input = synthio_sat16(sample_word * 8738, 17); // Initial input scaled down so we can add reverb
sum = 0;

// Calculate each of the 8 comb buffers
for (uint32_t j = 0 + channel_comb_offset; j < 8 + channel_comb_offset; j++) {
bufout = self->combbuffers[j][self->combbufferindex[j]];
sum += bufout;
self->combfitlers[j] = sat16(bufout * damp2 + self->combfitlers[j] * damp1, 15);
self->combbuffers[j][self->combbufferindex[j]] = sat16(input + sat16(self->combfitlers[j] * feedback, 15), 0);
self->combfitlers[j] = synthio_sat16(bufout * damp2 + self->combfitlers[j] * damp1, 15);
self->combbuffers[j][self->combbufferindex[j]] = synthio_sat16(input + synthio_sat16(self->combfitlers[j] * feedback, 15), 0);
if (++self->combbufferindex[j] >= self->combbuffersizes[j]) {
self->combbufferindex[j] = 0;
}
}

output = sat16(sum * 31457, 17); // 31457 = 0.24f with shift of 17
output = synthio_sat16(sum * 31457, 17); // 31457 = 0.24f with shift of 17

// Calculate each of the 4 all pass buffers
for (uint32_t j = 0 + channel_allpass_offset; j < 4 + channel_allpass_offset; j++) {
bufout = self->allpassbuffers[j][self->allpassbufferindex[j]];
self->allpassbuffers[j][self->allpassbufferindex[j]] = output + (bufout >> 1); // bufout >> 1 same as bufout*0.5f
output = sat16(bufout - output, 1);
output = synthio_sat16(bufout - output, 1);
if (++self->allpassbufferindex[j] >= self->allpassbuffersizes[j]) {
self->allpassbufferindex[j] = 0;
}
}

word = output * 30; // Add some volume back don't have to saturate as next step will

word = sat16(sample_word * mix_sample, 15) + sat16(word * mix_effect, 15);
word = synthio_sat16(sample_word * mix_sample, 15) + synthio_sat16(word * mix_effect, 15);
word = synthio_mix_down_sample(word, SYNTHIO_MIX_DOWN_SCALE(2));
word_buffer[i] = (int16_t)word;

Expand Down
3 changes: 2 additions & 1 deletion shared-module/synthio/Biquad.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "shared-bindings/synthio/Biquad.h"
#include "shared-module/synthio/Biquad.h"
#include "shared-module/synthio/block.h"
#include "shared-bindings/synthio/__init__.h"

typedef struct {
mp_float_t s, c;
Expand Down Expand Up @@ -212,7 +213,7 @@ void synthio_biquad_filter_samples(mp_obj_t self_in, biquad_filter_state *st, in

for (size_t n = n_samples; n; --n, ++buffer) {
int32_t input = *buffer;
int32_t output = (b0 * input + b1 * x0 + b2 * x1 - a1 * y0 - a2 * y1 + (1 << (BIQUAD_SHIFT - 1))) >> BIQUAD_SHIFT;
int32_t output = synthio_sat16((b0 * input + b1 * x0 + b2 * x1 - a1 * y0 - a2 * y1 + (1 << (BIQUAD_SHIFT - 1))), BIQUAD_SHIFT);

x1 = x0;
x0 = input;
Expand Down
28 changes: 24 additions & 4 deletions shared-module/synthio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ static const int16_t square_wave[] = {-32768, 32767};
static const uint16_t notes[] = {8372, 8870, 9397, 9956, 10548, 11175, 11840,
12544, 13290, 14080, 14917, 15804}; // 9th octave

// cleaner sat16 by http://www.moseleyinstruments.com/
int16_t synthio_sat16(int32_t n, int rshift) {
// we should always round towards 0
// to avoid recirculating round-off noise
//
// a 2s complement positive number is always
// rounded down, so we only need to take
// care of negative numbers
if (n < 0) {
n = n + (~(0xFFFFFFFFUL << rshift));
}
n = n >> rshift;
if (n > 32767) {
return 32767;
}
if (n < -32768) {
return -32768;
}
return n;
}

static int64_t round_float_to_int64(mp_float_t f) {
return (int64_t)(f + MICROPY_FLOAT_CONST(0.5));
Expand Down Expand Up @@ -250,7 +270,7 @@ static bool synth_note_into_buffer(synthio_synth_t *synth, int chan, int32_t *ou
accum = accum - lim + offset;
}
int16_t idx = accum >> SYNTHIO_FREQUENCY_SHIFT;
int16_t wi = (ring_waveform[idx] * out_buffer32[i]) / 32768;
int16_t wi = (ring_waveform[idx] * out_buffer32[i]) / 32768; // consider for synthio_sat16 but had a weird artificat
out_buffer32[i] = wi;
}
synth->ring_accum[chan] = accum;
Expand All @@ -272,12 +292,12 @@ static mp_obj_t synthio_synth_get_note_filter(mp_obj_t note_obj) {
static void sum_with_loudness(int32_t *out_buffer32, int32_t *tmp_buffer32, int16_t loudness[2], size_t dur, int synth_chan) {
if (synth_chan == 1) {
for (size_t i = 0; i < dur; i++) {
*out_buffer32++ += (*tmp_buffer32++ *loudness[0]) >> 16;
*out_buffer32++ += synthio_sat16((*tmp_buffer32++ *loudness[0]), 16);
}
} else {
for (size_t i = 0; i < dur; i++) {
*out_buffer32++ += (*tmp_buffer32 * loudness[0]) >> 16;
*out_buffer32++ += (*tmp_buffer32++ *loudness[1]) >> 16;
*out_buffer32++ += synthio_sat16((*tmp_buffer32 * loudness[0]), 16);
*out_buffer32++ += synthio_sat16((*tmp_buffer32++ *loudness[1]), 16);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions shared-module/synthio/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ int synthio_sweep_in_step(synthio_lfo_state_t *state, uint16_t dur);
extern mp_float_t synthio_global_rate_scale, synthio_global_W_scale;
extern uint8_t synthio_global_tick;
void shared_bindings_synthio_lfo_tick(uint32_t sample_rate, uint16_t num_samples);

int16_t synthio_sat16(int32_t n, int rshift);
Loading
Loading