@@ -129,27 +129,24 @@ static synthio_envelope_definition_t *synthio_synth_get_note_envelope(synthio_sy
129
129
}
130
130
131
131
132
- #define RANGE_LOW (-28000)
133
- #define RANGE_HIGH (28000)
134
132
#define RANGE_SHIFT (16)
135
- #define RANGE_SCALE (0xfffffff / (32768 * CIRCUITPY_SYNTHIO_MAX_CHANNELS - RANGE_HIGH))
136
133
137
134
// dynamic range compression via a downward compressor with hard knee
138
135
//
139
136
// When the output value is within the range +-28000 (about 85% of full scale),
140
137
// 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 ,
142
139
// still fit within the output range
143
140
//
144
141
// This produces a much louder overall volume with multiple voices, without
145
142
// much additional processing.
146
143
//
147
144
// 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 ;
153
150
}
154
151
return sample ;
155
152
}
@@ -344,7 +341,7 @@ void synthio_synth_synthesize(synthio_synth_t *synth, uint8_t **bufptr, uint32_t
344
341
// mix down audio
345
342
for (size_t i = 0 ; i < dur * synth -> channel_count ; i ++ ) {
346
343
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 ) );
348
345
}
349
346
350
347
// advance envelope states
0 commit comments