Skip to content

Commit e049337

Browse files
committed
Allow variable mix down sample scale.
1 parent 99b4fae commit e049337

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

shared-module/audiodelays/Echo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
428428
}
429429

430430
if (MP_LIKELY(self->bits_per_sample == 16)) {
431-
word = synthio_mix_down_sample(word);
431+
word = synthio_mix_down_sample(word, SYNTHIO_MIX_DOWN_SCALE(2));
432432
if (self->freq_shift) {
433433
for (uint32_t j = echo_buffer_pos >> 8; j < next_buffer_pos >> 8; j++) {
434434
echo_buffer[j % echo_buf_len] = (int16_t)word;
@@ -453,7 +453,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
453453
}
454454

455455
word = echo + sample_word;
456-
word = synthio_mix_down_sample(word);
456+
word = synthio_mix_down_sample(word, SYNTHIO_MIX_DOWN_SCALE(2));
457457

458458
if (MP_LIKELY(self->bits_per_sample == 16)) {
459459
word_buffer[i] = (int16_t)((sample_word * (MICROPY_FLOAT_CONST(1.0) - mix)) + (word * mix));

shared-module/audiofilters/Filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o
315315
// Mix processed signal with original sample and transfer to output buffer
316316
for (uint32_t j = 0; j < n_samples; j++) {
317317
if (MP_LIKELY(self->bits_per_sample == 16)) {
318-
word_buffer[i + j] = synthio_mix_down_sample((int32_t)((sample_src[i + j] * (MICROPY_FLOAT_CONST(1.0) - mix)) + (self->filter_buffer[j] * mix)));
318+
word_buffer[i + j] = synthio_mix_down_sample((int32_t)((sample_src[i + j] * (MICROPY_FLOAT_CONST(1.0) - mix)) + (self->filter_buffer[j] * mix)), SYNTHIO_MIX_DOWN_SCALE(2));
319319
if (!self->samples_signed) {
320320
word_buffer[i + j] ^= 0x8000;
321321
}

shared-module/synthio/__init__.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,24 @@ static synthio_envelope_definition_t *synthio_synth_get_note_envelope(synthio_sy
129129
}
130130

131131

132-
#define RANGE_LOW (-28000)
133-
#define RANGE_HIGH (28000)
134132
#define RANGE_SHIFT (16)
135-
#define RANGE_SCALE (0xfffffff / (32768 * CIRCUITPY_SYNTHIO_MAX_CHANNELS - RANGE_HIGH))
136133

137134
// dynamic range compression via a downward compressor with hard knee
138135
//
139136
// When the output value is within the range +-28000 (about 85% of full scale),
140137
// it is unchanged. Otherwise, it undergoes a gain reduction so that the
141-
// largest possible values, (+32768,-32767) * CIRCUITPY_SYNTHIO_MAX_CHANNELS,
138+
// largest possible values, (+32768,-32767) * count,
142139
// still fit within the output range
143140
//
144141
// This produces a much louder overall volume with multiple voices, without
145142
// much additional processing.
146143
//
147144
// https://en.wikipedia.org/wiki/Dynamic_range_compression
148-
int16_t synthio_mix_down_sample(int32_t sample) {
149-
if (sample < RANGE_LOW) {
150-
sample = (((sample - RANGE_LOW) * RANGE_SCALE) >> RANGE_SHIFT) + RANGE_LOW;
151-
} else if (sample > RANGE_HIGH) {
152-
sample = (((sample - RANGE_HIGH) * RANGE_SCALE) >> RANGE_SHIFT) + RANGE_HIGH;
145+
int16_t synthio_mix_down_sample(int32_t sample, int32_t scale) {
146+
if (sample < SYNTHIO_MIX_DOWN_RANGE_LOW) {
147+
sample = (((sample - SYNTHIO_MIX_DOWN_RANGE_LOW) * scale) >> RANGE_SHIFT) + SYNTHIO_MIX_DOWN_RANGE_LOW;
148+
} else if (sample > SYNTHIO_MIX_DOWN_RANGE_HIGH) {
149+
sample = (((sample - SYNTHIO_MIX_DOWN_RANGE_HIGH) * scale) >> RANGE_SHIFT) + SYNTHIO_MIX_DOWN_RANGE_HIGH;
153150
}
154151
return sample;
155152
}
@@ -344,7 +341,7 @@ void synthio_synth_synthesize(synthio_synth_t *synth, uint8_t **bufptr, uint32_t
344341
// mix down audio
345342
for (size_t i = 0; i < dur * synth->channel_count; i++) {
346343
int32_t sample = out_buffer32[i];
347-
out_buffer16[i] = synthio_mix_down_sample(sample);
344+
out_buffer16[i] = synthio_mix_down_sample(sample, SYNTHIO_MIX_DOWN_SCALE(CIRCUITPY_SYNTHIO_MAX_CHANNELS));
348345
}
349346

350347
// advance envelope states

shared-module/synthio/__init__.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#define SYNTHIO_NOTE_IS_PLAYING(synth, i) ((synth)->envelope_state[(i)].state != SYNTHIO_ENVELOPE_STATE_RELEASE)
1515
#define SYNTHIO_FREQUENCY_SHIFT (16)
1616

17+
#define SYNTHIO_MIX_DOWN_RANGE_LOW (-28000)
18+
#define SYNTHIO_MIX_DOWN_RANGE_HIGH (28000)
19+
#define SYNTHIO_MIX_DOWN_SCALE(x) (0xfffffff / (32768 * x - SYNTHIO_MIX_DOWN_RANGE_HIGH))
20+
1721
#include "shared-module/audiocore/__init__.h"
1822
#include "shared-bindings/synthio/__init__.h"
1923

@@ -79,7 +83,7 @@ bool synthio_span_change_note(synthio_synth_t *synth, mp_obj_t old_note, mp_obj_
7983
void synthio_envelope_step(synthio_envelope_definition_t *definition, synthio_envelope_state_t *state, int n_samples);
8084
void synthio_envelope_definition_set(synthio_envelope_definition_t *envelope, mp_obj_t obj, uint32_t sample_rate);
8185

82-
int16_t synthio_mix_down_sample(int32_t sample);
86+
int16_t synthio_mix_down_sample(int32_t sample, int32_t scale);
8387

8488
uint64_t synthio_frequency_convert_float_to_scaled(mp_float_t frequency_hz);
8589
uint32_t synthio_frequency_convert_float_to_dds(mp_float_t frequency_hz, int32_t sample_rate);

0 commit comments

Comments
 (0)