Skip to content

Commit 99b4fae

Browse files
committed
Remove unnecessary copies of mix_down_sample.
1 parent 48f272e commit 99b4fae

File tree

6 files changed

+9
-58
lines changed

6 files changed

+9
-58
lines changed

shared-module/audiodelays/Echo.c

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -258,32 +258,6 @@ void common_hal_audiodelays_echo_stop(audiodelays_echo_obj_t *self) {
258258
return;
259259
}
260260

261-
#define RANGE_LOW_16 (-28000)
262-
#define RANGE_HIGH_16 (28000)
263-
#define RANGE_SHIFT_16 (16)
264-
#define RANGE_SCALE_16 (0xfffffff / (32768 * 2 - RANGE_HIGH_16)) // 2 for echo+sample
265-
266-
// dynamic range compression via a downward compressor with hard knee
267-
//
268-
// When the output value is within the range +-28000 (about 85% of full scale),
269-
// it is unchanged. Otherwise, it undergoes a gain reduction so that the
270-
// largest possible values, (+32768,-32767) * 2 (2 for echo and sample),
271-
// still fit within the output range
272-
//
273-
// This produces a much louder overall volume with multiple voices, without
274-
// much additional processing.
275-
//
276-
// https://en.wikipedia.org/wiki/Dynamic_range_compression
277-
static
278-
int16_t mix_down_sample(int32_t sample) {
279-
if (sample < RANGE_LOW_16) {
280-
sample = (((sample - RANGE_LOW_16) * RANGE_SCALE_16) >> RANGE_SHIFT_16) + RANGE_LOW_16;
281-
} else if (sample > RANGE_HIGH_16) {
282-
sample = (((sample - RANGE_HIGH_16) * RANGE_SCALE_16) >> RANGE_SHIFT_16) + RANGE_HIGH_16;
283-
}
284-
return sample;
285-
}
286-
287261
audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *self, bool single_channel_output, uint8_t channel,
288262
uint8_t **buffer, uint32_t *buffer_length) {
289263

@@ -454,7 +428,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
454428
}
455429

456430
if (MP_LIKELY(self->bits_per_sample == 16)) {
457-
word = mix_down_sample(word);
431+
word = synthio_mix_down_sample(word);
458432
if (self->freq_shift) {
459433
for (uint32_t j = echo_buffer_pos >> 8; j < next_buffer_pos >> 8; j++) {
460434
echo_buffer[j % echo_buf_len] = (int16_t)word;
@@ -479,7 +453,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
479453
}
480454

481455
word = echo + sample_word;
482-
word = mix_down_sample(word);
456+
word = synthio_mix_down_sample(word);
483457

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

shared-module/audiodelays/Echo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "py/obj.h"
99

1010
#include "shared-module/audiocore/__init__.h"
11+
#include "shared-module/synthio/__init__.h"
1112
#include "shared-module/synthio/block.h"
1213

1314
extern const mp_obj_type_t audiodelays_echo_type;

shared-module/audiofilters/Filter.c

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,32 +212,6 @@ void common_hal_audiofilters_filter_stop(audiofilters_filter_obj_t *self) {
212212
return;
213213
}
214214

215-
#define RANGE_LOW_16 (-28000)
216-
#define RANGE_HIGH_16 (28000)
217-
#define RANGE_SHIFT_16 (16)
218-
#define RANGE_SCALE_16 (0xfffffff / (32768 * 2 - RANGE_HIGH_16)) // 2 for echo+sample
219-
220-
// dynamic range compression via a downward compressor with hard knee
221-
//
222-
// When the output value is within the range +-28000 (about 85% of full scale),
223-
// it is unchanged. Otherwise, it undergoes a gain reduction so that the
224-
// largest possible values, (+32768,-32767) * 2 (2 for echo and sample),
225-
// still fit within the output range
226-
//
227-
// This produces a much louder overall volume with multiple voices, without
228-
// much additional processing.
229-
//
230-
// https://en.wikipedia.org/wiki/Dynamic_range_compression
231-
static
232-
int16_t mix_down_sample(int32_t sample) {
233-
if (sample < RANGE_LOW_16) {
234-
sample = (((sample - RANGE_LOW_16) * RANGE_SCALE_16) >> RANGE_SHIFT_16) + RANGE_LOW_16;
235-
} else if (sample > RANGE_HIGH_16) {
236-
sample = (((sample - RANGE_HIGH_16) * RANGE_SCALE_16) >> RANGE_SHIFT_16) + RANGE_HIGH_16;
237-
}
238-
return sample;
239-
}
240-
241215
audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_obj_t *self, bool single_channel_output, uint8_t channel,
242216
uint8_t **buffer, uint32_t *buffer_length) {
243217
(void)channel;
@@ -341,7 +315,7 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o
341315
// Mix processed signal with original sample and transfer to output buffer
342316
for (uint32_t j = 0; j < n_samples; j++) {
343317
if (MP_LIKELY(self->bits_per_sample == 16)) {
344-
word_buffer[i + j] = 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)));
345319
if (!self->samples_signed) {
346320
word_buffer[i + j] ^= 0x8000;
347321
}

shared-module/audiofilters/Filter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "shared-bindings/synthio/Biquad.h"
1111
#include "shared-module/audiocore/__init__.h"
12+
#include "shared-module/synthio/__init__.h"
1213
#include "shared-module/synthio/block.h"
1314
#include "shared-module/synthio/Biquad.h"
1415

shared-module/synthio/__init__.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ static synthio_envelope_definition_t *synthio_synth_get_note_envelope(synthio_sy
145145
// much additional processing.
146146
//
147147
// https://en.wikipedia.org/wiki/Dynamic_range_compression
148-
static
149-
int16_t mix_down_sample(int32_t sample) {
148+
int16_t synthio_mix_down_sample(int32_t sample) {
150149
if (sample < RANGE_LOW) {
151150
sample = (((sample - RANGE_LOW) * RANGE_SCALE) >> RANGE_SHIFT) + RANGE_LOW;
152151
} else if (sample > RANGE_HIGH) {
@@ -345,7 +344,7 @@ void synthio_synth_synthesize(synthio_synth_t *synth, uint8_t **bufptr, uint32_t
345344
// mix down audio
346345
for (size_t i = 0; i < dur * synth->channel_count; i++) {
347346
int32_t sample = out_buffer32[i];
348-
out_buffer16[i] = mix_down_sample(sample);
347+
out_buffer16[i] = synthio_mix_down_sample(sample);
349348
}
350349

351350
// advance envelope states

shared-module/synthio/__init__.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ bool synthio_span_change_note(synthio_synth_t *synth, mp_obj_t old_note, mp_obj_
7979
void synthio_envelope_step(synthio_envelope_definition_t *definition, synthio_envelope_state_t *state, int n_samples);
8080
void synthio_envelope_definition_set(synthio_envelope_definition_t *envelope, mp_obj_t obj, uint32_t sample_rate);
8181

82+
int16_t synthio_mix_down_sample(int32_t sample);
83+
8284
uint64_t synthio_frequency_convert_float_to_scaled(mp_float_t frequency_hz);
8385
uint32_t synthio_frequency_convert_float_to_dds(mp_float_t frequency_hz, int32_t sample_rate);
8486
uint32_t synthio_frequency_convert_scaled_to_dds(uint64_t frequency_scaled, int32_t sample_rate);

0 commit comments

Comments
 (0)