Skip to content

Commit

Permalink
Add audiodelays & audioeffects to unix coverage port
Browse files Browse the repository at this point in the history
.. and add a very basic audioeffects test, showing that it plausibly
is working

I had to address several build errors that occurred in the Unix build,
mostly related to conversion from FP types to integral types (replaced
by explicit casts) and by
accidental mixing of regular & f-suffixed floating constants (replaced
with the MICROPY_FLOAT_CONST macro)

Particularly this change could use consideration:
```diff
-    self->max_echo_buffer_len = self->sample_rate / 1000.0f * max_delay_ms * (self->channel_count * sizeof(uint16_t)); // bytes
+    self->max_echo_buffer_len = (uint32_t)(self->sample_rate / 1000.0f * max_delay_ms) * (self->channel_count * sizeof(uint16_t)); // bytes
```

The buffer length is being calculated in floating point based on the
millisecond delay & the sample rate. The result could then be a fractional
number such as 529.2 for a 12ms delay at 44.1kHz. Multiplying a floating
number by the size required for each echo buffer item
(`(self->channel_count * sizeof(uint16_t))`) could yield a number of bytes
that doesn't correspond to an integral number of buffer items. I grouped
the float->int conversion so that it converts the number of echo buffer
items to an integer and then multiplies by the size of the item.
  • Loading branch information
jepler committed Nov 5, 2024
1 parent 3a0b97d commit f8afdcb
Show file tree
Hide file tree
Showing 6 changed files with 1,603 additions and 13 deletions.
11 changes: 11 additions & 0 deletions ports/unix/variants/coverage/mpconfigvariant.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ SRC_BITMAP := \
shared-bindings/audiocore/__init__.c \
shared-bindings/audiocore/RawSample.c \
shared-bindings/audiocore/WaveFile.c \
shared-bindings/audiodelays/Echo.c \
shared-bindings/audiodelays/__init__.c \
shared-bindings/audiofilters/Filter.c \
shared-bindings/audiofilters/__init__.c \
shared-bindings/audiomixer/__init__.c \
shared-bindings/audiomixer/Mixer.c \
shared-bindings/audiomixer/MixerVoice.c \
Expand Down Expand Up @@ -70,6 +74,10 @@ SRC_BITMAP := \
shared-module/audiocore/__init__.c \
shared-module/audiocore/RawSample.c \
shared-module/audiocore/WaveFile.c \
shared-module/audiodelays/Echo.c \
shared-module/audiodelays/__init__.c \
shared-module/audiofilters/Filter.c \
shared-module/audiofilters/__init__.c \
shared-module/audiomixer/__init__.c \
shared-module/audiomp3/MP3Decoder.c \
shared-module/audiomixer/Mixer.c \
Expand Down Expand Up @@ -127,6 +135,9 @@ $(BUILD)/lib/mp3/src/buffers.o: CFLAGS += -include "shared-module/audiomp3/__ini
CFLAGS += \
-DCIRCUITPY_AESIO=1 \
-DCIRCUITPY_AUDIOCORE=1 \
-DCIRCUITPY_AUDIOEFFECTS=1 \
-DCIRCUITPY_AUDIODELAYS=1 \
-DCIRCUITPY_AUDIOFILTERS=1 \
-DCIRCUITPY_AUDIOMIXER=1 \
-DCIRCUITPY_AUDIOMP3=1 \
-DCIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR=0 \
Expand Down
22 changes: 11 additions & 11 deletions shared-module/audiodelays/Echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_

// Allocate the echo buffer for the max possible delay, echo is always 16-bit
self->max_delay_ms = max_delay_ms;
self->max_echo_buffer_len = self->sample_rate / 1000.0f * max_delay_ms * (self->channel_count * sizeof(uint16_t)); // bytes
self->max_echo_buffer_len = (uint32_t)(self->sample_rate / 1000.0f * max_delay_ms) * (self->channel_count * sizeof(uint16_t)); // bytes
self->echo_buffer = m_malloc(self->max_echo_buffer_len);
if (self->echo_buffer == NULL) {
common_hal_audiodelays_echo_deinit(self);
Expand Down Expand Up @@ -129,11 +129,11 @@ void common_hal_audiodelays_echo_set_delay_ms(audiodelays_echo_obj_t *self, mp_o
void recalculate_delay(audiodelays_echo_obj_t *self, mp_float_t f_delay_ms) {
if (self->freq_shift) {
// Calculate the rate of iteration over the echo buffer with 8 sub-bits
self->echo_buffer_rate = MAX(self->max_delay_ms / f_delay_ms * 256.0f, 1.0);
self->echo_buffer_rate = (uint32_t)MAX(self->max_delay_ms / f_delay_ms * MICROPY_FLOAT_CONST(256.0), MICROPY_FLOAT_CONST(1.0));
self->echo_buffer_len = self->max_echo_buffer_len;
} else {
// Calculate the current echo buffer length in bytes
uint32_t new_echo_buffer_len = self->sample_rate / 1000.0f * f_delay_ms * (self->channel_count * sizeof(uint16_t));
uint32_t new_echo_buffer_len = (uint32_t)(self->sample_rate / MICROPY_FLOAT_CONST(1000.0) * f_delay_ms) * (self->channel_count * sizeof(uint16_t));

// Check if our new echo is too long for our maximum buffer
if (new_echo_buffer_len > self->max_echo_buffer_len) {
Expand All @@ -153,7 +153,7 @@ void recalculate_delay(audiodelays_echo_obj_t *self, mp_float_t f_delay_ms) {
memset(self->echo_buffer + self->echo_buffer_len, 0, self->max_echo_buffer_len - self->echo_buffer_len);
}

self->current_delay_ms = f_delay_ms;
self->current_delay_ms = (uint32_t)f_delay_ms;
}

mp_obj_t common_hal_audiodelays_echo_get_decay(audiodelays_echo_obj_t *self) {
Expand Down Expand Up @@ -360,17 +360,17 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
echo = echo_buffer[echo_buffer_pos >> 8];
next_buffer_pos = echo_buffer_pos + self->echo_buffer_rate;

word = echo * decay;
word = (int16_t)(echo * decay);
for (uint32_t j = echo_buffer_pos >> 8; j < next_buffer_pos >> 8; j++) {
echo_buffer[j % echo_buf_len] = word;
}
} else {
echo = echo_buffer[self->echo_buffer_read_pos++];
word = echo * decay;
word = (int16_t)(echo * decay);
echo_buffer[self->echo_buffer_write_pos++] = word;
}

word = echo * mix;
word = (int16_t)(echo * mix);

if (MP_LIKELY(self->bits_per_sample == 16)) {
word_buffer[i] = word;
Expand Down Expand Up @@ -433,10 +433,10 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
if (self->freq_shift) {
echo = echo_buffer[echo_buffer_pos >> 8];
next_buffer_pos = echo_buffer_pos + self->echo_buffer_rate;
word = echo * decay + sample_word;
word = (int32_t)(echo * decay + sample_word);
} else {
echo = echo_buffer[self->echo_buffer_read_pos++];
word = echo * decay + sample_word;
word = (int32_t)(echo * decay + sample_word);
}

if (MP_LIKELY(self->bits_per_sample == 16)) {
Expand Down Expand Up @@ -467,12 +467,12 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
word = echo + sample_word;

if (MP_LIKELY(self->bits_per_sample == 16)) {
word_buffer[i] = (sample_word * (1.0 - mix)) + (word * mix);
word_buffer[i] = (int16_t)((sample_word * (1.0 - mix)) + (word * mix));
if (!self->samples_signed) {
word_buffer[i] ^= 0x8000;
}
} else {
int8_t mixed = (sample_word * (1.0 - mix)) + (word * mix);
int8_t mixed = (int16_t)((sample_word * (1.0 - mix)) + (word * mix));
if (self->samples_signed) {
hword_buffer[i] = mixed;
} else {
Expand Down
5 changes: 3 additions & 2 deletions shared-module/audiofilters/Filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ int16_t mix_down_sample(int32_t sample) {

audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_obj_t *self, bool single_channel_output, uint8_t channel,
uint8_t **buffer, uint32_t *buffer_length) {
(void)channel;

if (!single_channel_output) {
channel = 0;
Expand Down Expand Up @@ -297,15 +298,15 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o
// Mix processed signal with original sample and transfer to output buffer
for (uint32_t j = 0; j < n_samples; j++) {
if (MP_LIKELY(self->bits_per_sample == 16)) {
word_buffer[i + j] = mix_down_sample((sample_src[i + j] * (1.0 - mix)) + (self->filter_buffer[j] * mix));
word_buffer[i + j] = mix_down_sample((int32_t)((sample_src[i + j] * (MICROPY_FLOAT_CONST(1.0) - mix)) + (self->filter_buffer[j] * mix)));
if (!self->samples_signed) {
word_buffer[i + j] ^= 0x8000;
}
} else {
if (self->samples_signed) {
hword_buffer[i + j] = (int8_t)((sample_hsrc[i + j] * (1.0 - mix)) + (self->filter_buffer[j] * mix));
} else {
hword_buffer[i + j] = (uint8_t)(((int8_t)(((uint8_t)sample_hsrc[i + j]) ^ 0x80) * (1.0 - mix)) + (self->filter_buffer[j] * mix)) ^ 0x80;
hword_buffer[i + j] = (uint8_t)(((int8_t)(((uint8_t)sample_hsrc[i + j]) ^ 0x80) * (MICROPY_FLOAT_CONST(1.0) - mix)) + (self->filter_buffer[j] * mix)) ^ 0x80;
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/circuitpython/audiofilter_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from audiofilters import Filter
from audiofilterhelper import synth_test, sine8k

@synth_test
def basic_filter():
effect = Filter(
bits_per_sample=16,
samples_signed=True,
)
yield effect, []

effect.play(sine8k, loop=True)
yield 4

effect.stop()
yield 2
Loading

0 comments on commit f8afdcb

Please sign in to comment.