Skip to content

Audio Effect Reverb #10196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a405019
Initial commit
gamblor21 Mar 1, 2025
aba78f9
Initial buffers
gamblor21 Mar 22, 2025
c7991ad
Temp stereo fix and send the right sample
gamblor21 Mar 22, 2025
d2173ae
Changes to stereo handling
gamblor21 Mar 22, 2025
9a148f7
Temp mix test
gamblor21 Mar 23, 2025
4359a21
Fix error if no sample add in comments
gamblor21 Apr 2, 2025
89c4110
Fixed copyright year oops
gamblor21 Apr 3, 2025
9cdc3d7
Doc addition and better mix
gamblor21 Apr 5, 2025
0e79f5b
Comments and unix build
gamblor21 Apr 5, 2025
edb6810
Unix build file update
gamblor21 Apr 5, 2025
f8647a3
Removing extra f float markers
gamblor21 Apr 5, 2025
6e7d2b5
Spelling mistake
gamblor21 Apr 11, 2025
86eca38
Moving to audiofreeverb and renaming to Freeverb
gamblor21 Apr 11, 2025
c9a2a40
Fix the mix calculation
gamblor21 Apr 11, 2025
168b16d
Rename unix build pathes
gamblor21 Apr 11, 2025
f011df2
Fixes for zephyr build
gamblor21 Apr 19, 2025
4b7d9c5
Add zephyr script
gamblor21 Apr 19, 2025
fa863cf
Removing autogen files
gamblor21 Apr 19, 2025
fb34bc7
Revert "Removing autogen files"
gamblor21 Apr 19, 2025
eb53338
Revert "Fixes for zephyr build"
gamblor21 Apr 19, 2025
f6357d5
Change to how malloc is called
gamblor21 Apr 19, 2025
3478eea
Merge remote-tracking branch 'adafruit/main' into audioeffect-reverb
gamblor21 Apr 20, 2025
a8f29b4
Updated zephyr autogen files
gamblor21 Apr 20, 2025
b46532b
Oops old folders causing errors
gamblor21 Apr 20, 2025
ca38d29
Overwrite some autogen items incorrectly - fixed now
gamblor21 Apr 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2575,6 +2575,11 @@ msgstr ""

#: shared-bindings/audiodelays/Echo.c shared-bindings/audiodelays/PitchShift.c
#: shared-bindings/audiofilters/Distortion.c
#: shared-bindings/audiodelays/Reverb.c
msgid "bits_per_sample must be 16"
msgstr ""

#: shared-bindings/audiodelays/Echo.c shared-bindings/audiofilters/Distortion.c
#: shared-bindings/audiofilters/Filter.c shared-bindings/audiomixer/Mixer.c
msgid "bits_per_sample must be 8 or 16"
msgstr ""
Expand Down Expand Up @@ -3992,6 +3997,10 @@ msgstr ""
msgid "rsplit(None,n)"
msgstr ""

#: shared-bindings/audiodelays/Reverb.c
msgid "samples_signed must be true"
msgstr ""

#: ports/atmel-samd/common-hal/audiobusio/PDMIn.c
#: ports/raspberrypi/common-hal/audiobusio/PDMIn.c
msgid "sampling rate out of range"
Expand Down
2 changes: 2 additions & 0 deletions ports/unix/variants/coverage/mpconfigvariant.mk
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SRC_BITMAP := \
shared-bindings/audiocore/WaveFile.c \
shared-bindings/audiodelays/Echo.c \
shared-bindings/audiodelays/PitchShift.c \
shared-bindings/audiodelays/Reverb.c \
shared-bindings/audiodelays/__init__.c \
shared-bindings/audiofilters/Distortion.c \
shared-bindings/audiofilters/Filter.c \
Expand Down Expand Up @@ -79,6 +80,7 @@ SRC_BITMAP := \
shared-module/audiocore/WaveFile.c \
shared-module/audiodelays/Echo.c \
shared-module/audiodelays/PitchShift.c \
shared-module/audiodelays/Reverb.c \
shared-module/audiodelays/__init__.c \
shared-module/audiofilters/Distortion.c \
shared-module/audiofilters/Filter.c \
Expand Down
1 change: 1 addition & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ SRC_SHARED_MODULE_ALL = \
audiocore/__init__.c \
audiodelays/Echo.c \
audiodelays/PitchShift.c \
audiodelays/Reverb.c \
audiodelays/__init__.c \
audiofilters/Distortion.c \
audiofilters/Filter.c \
Expand Down
268 changes: 268 additions & 0 deletions shared-bindings/audiodelays/Reverb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2025 Mark Komus
//
// SPDX-License-Identifier: MIT

#include <stdint.h>

#include "shared-bindings/audiodelays/Reverb.h"
#include "shared-bindings/audiocore/__init__.h"
#include "shared-module/audiodelays/Reverb.h"

#include "shared/runtime/context_manager_helpers.h"
#include "py/binary.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/util.h"
#include "shared-module/synthio/block.h"

//| class Reverb:
//| """An Reverb effect"""
//|
//| def __init__(
//| self,
//| roomsize: synthio.BlockInput = 0.5,
//| damp: synthio.BlockInput = 0.5,
//| mix: synthio.BlockInput = 0.5,
//| buffer_size: int = 512,
//| sample_rate: int = 8000,
//| bits_per_sample: int = 16,
//| samples_signed: bool = True,
//| channel_count: int = 1,
//| ) -> None:
//| """Create a Reverb effect simulating the audio taking place in a large room where you get echos
//| off of various surfaces at various times. The size of the room can be adjusted as well as how
//| much the higher frequencies get absorbed by the walls.
//|
//| The mix parameter allows you to change how much of the unchanged sample passes through to
//| the output to how much of the effect audio you hear as the output.
//|
//| :param synthio.BlockInput roomsize: The size of the room. 0.0 = smallest; 1.0 = largest.
//| :param synthio.BlockInput damp: How much the walls absorb. 0.0 = least; 1.0 = most.
//| :param synthio.BlockInput mix: The mix as a ratio of the sample (0.0) to the effect (1.0).
//| :param int buffer_size: The total size in bytes of each of the two playback buffers to use
//| :param int sample_rate: The sample rate to be used
//| :param int channel_count: The number of channels the source samples contain. 1 = mono; 2 = stereo.
//| :param int bits_per_sample: The bits per sample of the effect. Reverb requires 16 bits.
//| :param bool samples_signed: Effect is signed (True) or unsigned (False). Reverb requires signed (True).
//|
//| Playing adding an reverb to a synth::
//|
//| import time
//| import board
//| import audiobusio
//| import synthio
//| import audiodelays
//|
//| audio = audiobusio.I2SOut(bit_clock=board.GP20, word_select=board.GP21, data=board.GP22)
//| synth = synthio.Synthesizer(channel_count=1, sample_rate=44100)
//| reverb = audiodelays.Reverb(roomsize=0.7, damp=0.3, buffer_size=1024, channel_count=1, sample_rate=44100, mix=0.7)
//| reverb.play(synth)
//| audio.play(reverb)
//|
//| note = synthio.Note(261)
//| while True:
//| synth.press(note)
//| time.sleep(0.55)
//| synth.release(note)
//| time.sleep(5)"""
//| ...
//|
static mp_obj_t audiodelays_reverb_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_roomsize, ARG_damp, ARG_mix, ARG_buffer_size, ARG_sample_rate, ARG_bits_per_sample, ARG_samples_signed, ARG_channel_count, };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_roomsize, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_damp, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_mix, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_buffer_size, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 512} },
{ MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 8000} },
{ MP_QSTR_bits_per_sample, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 16} },
{ MP_QSTR_samples_signed, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} },
{ MP_QSTR_channel_count, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1 } },
};

mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

mp_int_t channel_count = mp_arg_validate_int_range(args[ARG_channel_count].u_int, 1, 2, MP_QSTR_channel_count);
mp_int_t sample_rate = mp_arg_validate_int_min(args[ARG_sample_rate].u_int, 1, MP_QSTR_sample_rate);
if (args[ARG_samples_signed].u_bool != true) {
mp_raise_ValueError(MP_ERROR_TEXT("samples_signed must be true"));
}
mp_int_t bits_per_sample = args[ARG_bits_per_sample].u_int;
if (bits_per_sample != 16) {
mp_raise_ValueError(MP_ERROR_TEXT("bits_per_sample must be 16"));
}

audiodelays_reverb_obj_t *self = mp_obj_malloc(audiodelays_reverb_obj_t, &audiodelays_reverb_type);
common_hal_audiodelays_reverb_construct(self, args[ARG_roomsize].u_obj, args[ARG_damp].u_obj, args[ARG_mix].u_obj, args[ARG_buffer_size].u_int, bits_per_sample, args[ARG_samples_signed].u_bool, channel_count, sample_rate);

return MP_OBJ_FROM_PTR(self);
}

//| def deinit(self) -> None:
//| """Deinitialises the Reverb."""
//| ...
//|
static mp_obj_t audiodelays_reverb_deinit(mp_obj_t self_in) {
audiodelays_reverb_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_audiodelays_reverb_deinit(self);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_reverb_deinit_obj, audiodelays_reverb_deinit);

static void check_for_deinit(audiodelays_reverb_obj_t *self) {
audiosample_check_for_deinit(&self->base);
}

//| def __enter__(self) -> Reverb:
//| """No-op used by Context Managers."""
//| ...
//|
// Provided by context manager helper.

//| def __exit__(self) -> None:
//| """Automatically deinitializes when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info."""
//| ...
//|
// Provided by context manager helper.

//| roomsize: synthio.BlockInput
//| """Apparent size of the room 0.0-1.0"""
static mp_obj_t audiodelays_reverb_obj_get_roomsize(mp_obj_t self_in) {
return common_hal_audiodelays_reverb_get_roomsize(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_reverb_get_roomsize_obj, audiodelays_reverb_obj_get_roomsize);

static mp_obj_t audiodelays_reverb_obj_set_roomsize(mp_obj_t self_in, mp_obj_t roomsize) {
audiodelays_reverb_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_audiodelays_reverb_set_roomsize(self, roomsize);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(audiodelays_reverb_set_roomsize_obj, audiodelays_reverb_obj_set_roomsize);

MP_PROPERTY_GETSET(audiodelays_reverb_roomsize_obj,
(mp_obj_t)&audiodelays_reverb_get_roomsize_obj,
(mp_obj_t)&audiodelays_reverb_set_roomsize_obj);

//| damp: synthio.BlockInput
//| """How reverbrent the area is. 0.0-1.0"""
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be spelled "reverberant", but also I feel that this description isn't precise. A higher value "dampens" the high frequency response and reduces reverberation among those frequencies (lower frequencies aren't affected as much).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded it.

static mp_obj_t audiodelays_reverb_obj_get_damp(mp_obj_t self_in) {
return common_hal_audiodelays_reverb_get_damp(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_reverb_get_damp_obj, audiodelays_reverb_obj_get_damp);

static mp_obj_t audiodelays_reverb_obj_set_damp(mp_obj_t self_in, mp_obj_t damp) {
audiodelays_reverb_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_audiodelays_reverb_set_damp(self, damp);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(audiodelays_reverb_set_damp_obj, audiodelays_reverb_obj_set_damp);

MP_PROPERTY_GETSET(audiodelays_reverb_damp_obj,
(mp_obj_t)&audiodelays_reverb_get_damp_obj,
(mp_obj_t)&audiodelays_reverb_set_damp_obj);

//| mix: synthio.BlockInput
//| """The rate the reverb mix between 0 and 1 where 0 is only sample and 1 is all effect."""
static mp_obj_t audiodelays_reverb_obj_get_mix(mp_obj_t self_in) {
return common_hal_audiodelays_reverb_get_mix(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_reverb_get_mix_obj, audiodelays_reverb_obj_get_mix);

static mp_obj_t audiodelays_reverb_obj_set_mix(mp_obj_t self_in, mp_obj_t mix_in) {
audiodelays_reverb_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_audiodelays_reverb_set_mix(self, mix_in);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(audiodelays_reverb_set_mix_obj, audiodelays_reverb_obj_set_mix);

MP_PROPERTY_GETSET(audiodelays_reverb_mix_obj,
(mp_obj_t)&audiodelays_reverb_get_mix_obj,
(mp_obj_t)&audiodelays_reverb_set_mix_obj);

//| playing: bool
//| """True when the effect is playing a sample. (read-only)"""
//|
static mp_obj_t audiodelays_reverb_obj_get_playing(mp_obj_t self_in) {
audiodelays_reverb_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return mp_obj_new_bool(common_hal_audiodelays_reverb_get_playing(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_reverb_get_playing_obj, audiodelays_reverb_obj_get_playing);

MP_PROPERTY_GETTER(audiodelays_reverb_playing_obj,
(mp_obj_t)&audiodelays_reverb_get_playing_obj);

//| def play(self, sample: circuitpython_typing.AudioSample, *, loop: bool = False) -> None:
//| """Plays the sample once when loop=False and continuously when loop=True.
//| Does not block. Use `playing` to block.
//|
//| The sample must match the encoding settings given in the constructor."""
//| ...
//|
static mp_obj_t audiodelays_reverb_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_sample, ARG_loop };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_sample, MP_ARG_OBJ | MP_ARG_REQUIRED, {} },
{ MP_QSTR_loop, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
};
audiodelays_reverb_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);


mp_obj_t sample = args[ARG_sample].u_obj;
common_hal_audiodelays_reverb_play(self, sample, args[ARG_loop].u_bool);

return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(audiodelays_reverb_play_obj, 1, audiodelays_reverb_obj_play);

//| def stop(self) -> None:
//| """Stops playback of the sample. The reverb continues playing."""
//| ...
//|
//|
static mp_obj_t audiodelays_reverb_obj_stop(mp_obj_t self_in) {
audiodelays_reverb_obj_t *self = MP_OBJ_TO_PTR(self_in);

common_hal_audiodelays_reverb_stop(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(audiodelays_reverb_stop_obj, audiodelays_reverb_obj_stop);

static const mp_rom_map_elem_t audiodelays_reverb_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiodelays_reverb_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audiodelays_reverb_play_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&audiodelays_reverb_stop_obj) },

// Properties
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audiodelays_reverb_playing_obj) },
{ MP_ROM_QSTR(MP_QSTR_roomsize), MP_ROM_PTR(&audiodelays_reverb_roomsize_obj) },
{ MP_ROM_QSTR(MP_QSTR_damp), MP_ROM_PTR(&audiodelays_reverb_damp_obj) },
{ MP_ROM_QSTR(MP_QSTR_mix), MP_ROM_PTR(&audiodelays_reverb_mix_obj) },
AUDIOSAMPLE_FIELDS,
};
static MP_DEFINE_CONST_DICT(audiodelays_reverb_locals_dict, audiodelays_reverb_locals_dict_table);

static const audiosample_p_t audiodelays_reverb_proto = {
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample)
.reset_buffer = (audiosample_reset_buffer_fun)audiodelays_reverb_reset_buffer,
.get_buffer = (audiosample_get_buffer_fun)audiodelays_reverb_get_buffer,
};

MP_DEFINE_CONST_OBJ_TYPE(
audiodelays_reverb_type,
MP_QSTR_Reverb,
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
make_new, audiodelays_reverb_make_new,
locals_dict, &audiodelays_reverb_locals_dict,
protocol, &audiodelays_reverb_proto
);
36 changes: 36 additions & 0 deletions shared-bindings/audiodelays/Reverb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2025 Mark Komus
//
// SPDX-License-Identifier: MIT

#pragma once

#include "shared-module/audiodelays/Reverb.h"

extern const mp_obj_type_t audiodelays_reverb_type;

void common_hal_audiodelays_reverb_construct(audiodelays_reverb_obj_t *self,
mp_obj_t roomsize, mp_obj_t damp, mp_obj_t mix,
uint32_t buffer_size, uint8_t bits_per_sample, bool samples_signed,
uint8_t channel_count, uint32_t sample_rate);

void common_hal_audiodelays_reverb_deinit(audiodelays_reverb_obj_t *self);
bool common_hal_audiodelays_reverb_deinited(audiodelays_reverb_obj_t *self);

uint32_t common_hal_audiodelays_reverb_get_sample_rate(audiodelays_reverb_obj_t *self);
uint8_t common_hal_audiodelays_reverb_get_channel_count(audiodelays_reverb_obj_t *self);
uint8_t common_hal_audiodelays_reverb_get_bits_per_sample(audiodelays_reverb_obj_t *self);

mp_obj_t common_hal_audiodelays_reverb_get_roomsize(audiodelays_reverb_obj_t *self);
void common_hal_audiodelays_reverb_set_roomsize(audiodelays_reverb_obj_t *self, mp_obj_t feedback);

mp_obj_t common_hal_audiodelays_reverb_get_damp(audiodelays_reverb_obj_t *self);
void common_hal_audiodelays_reverb_set_damp(audiodelays_reverb_obj_t *self, mp_obj_t damp);

mp_obj_t common_hal_audiodelays_reverb_get_mix(audiodelays_reverb_obj_t *self);
void common_hal_audiodelays_reverb_set_mix(audiodelays_reverb_obj_t *self, mp_obj_t mix);

bool common_hal_audiodelays_reverb_get_playing(audiodelays_reverb_obj_t *self);
void common_hal_audiodelays_reverb_play(audiodelays_reverb_obj_t *self, mp_obj_t sample, bool loop);
void common_hal_audiodelays_reverb_stop(audiodelays_reverb_obj_t *self);
3 changes: 3 additions & 0 deletions shared-bindings/audiodelays/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "shared-bindings/audiodelays/__init__.h"
#include "shared-bindings/audiodelays/Echo.h"
#include "shared-bindings/audiodelays/PitchShift.h"
#include "shared-bindings/audiodelays/Reverb.h"


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

//| """Support for audio delay effects
//|
Expand All @@ -23,6 +25,7 @@ static const mp_rom_map_elem_t audiodelays_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_audiodelays) },
{ MP_ROM_QSTR(MP_QSTR_Echo), MP_ROM_PTR(&audiodelays_echo_type) },
{ MP_ROM_QSTR(MP_QSTR_PitchShift), MP_ROM_PTR(&audiodelays_pitch_shift_type) },
{ MP_ROM_QSTR(MP_QSTR_Reverb), MP_ROM_PTR(&audiodelays_reverb_type) },
};

static MP_DEFINE_CONST_DICT(audiodelays_module_globals, audiodelays_module_globals_table);
Expand Down
Loading