@@ -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
0 commit comments