Skip to content
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

Add BlockBiquad shelf & peaking, AudioFilter support #9804

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions ports/atmel-samd/boards/pybadge/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ CIRCUITPY_PARALLELDISPLAYBUS= 0
CIRCUITPY_STAGE = 1

FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/pybadge

# We don't have room for the fonts for terminalio for certain languages,
# so turn off terminalio, and if it's off and displayio is on,
# force a clean build.
# Note that we cannot test $(CIRCUITPY_DISPLAYIO) directly with an
# ifeq, because it's not set yet.
ifneq (,$(filter $(TRANSLATION),ja ko ru))
CIRCUITPY_TERMINALIO = 0
RELEASE_NEEDS_CLEAN_BUILD = $(CIRCUITPY_DISPLAYIO)
endif
10 changes: 10 additions & 0 deletions ports/atmel-samd/boards/pygamer/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ CIRCUITPY_PARALLELDISPLAYBUS= 0
CIRCUITPY_STAGE = 1

FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/pygamer

# We don't have room for the fonts for terminalio for certain languages,
# so turn off terminalio, and if it's off and displayio is on,
# force a clean build.
# Note that we cannot test $(CIRCUITPY_DISPLAYIO) directly with an
# ifeq, because it's not set yet.
ifneq (,$(filter $(TRANSLATION),ja ko ru))
CIRCUITPY_TERMINALIO = 0
RELEASE_NEEDS_CLEAN_BUILD = $(CIRCUITPY_DISPLAYIO)
endif
10 changes: 5 additions & 5 deletions shared-bindings/audiofilters/Filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//|
//| def __init__(
//| self,
//| filter: Optional[synthio.Biquad | Tuple[synthio.Biquad]] = None,
//| filter: Optional[synthio.AnyBiquad | Tuple[synthio.AnyBiquad]] = None,
//| mix: synthio.BlockInput = 1.0,
//| buffer_size: int = 512,
//| sample_rate: int = 8000,
Expand All @@ -38,7 +38,7 @@
//| 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 Optional[synthio.Biquad|Tuple[synthio.Biquad]] filter: A normalized biquad filter object or tuple of normalized biquad filter objects. The sample is processed sequentially by each filter to produce the output samples.
//| :param Optional[synthio.AnyBiquad|Tuple[synthio.AnyBiquad]] filter: A normalized biquad filter object or tuple of normalized biquad filter objects. The sample is processed sequentially by each filter to produce the output samples.
//| :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
Expand Down Expand Up @@ -71,8 +71,8 @@
static mp_obj_t audiofilters_filter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_filter, 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_filter, 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_filter, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_NONE} },
{ MP_QSTR_mix, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_ROM_INT(1)} },
{ 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} },
Expand Down Expand Up @@ -129,7 +129,7 @@ static mp_obj_t audiofilters_filter_obj___exit__(size_t n_args, const mp_obj_t *
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiofilters_filter___exit___obj, 4, 4, audiofilters_filter_obj___exit__);


//| filter: synthio.Biquad | Tuple[synthio.Biquad] | None
//| filter: synthio.AnyBiquad | Tuple[synthio.AnyBiquad] | None
//| """A normalized biquad filter object or tuple of normalized biquad filter objects. The sample is processed sequentially by each filter to produce the output samples."""
//|
static mp_obj_t audiofilters_filter_obj_get_filter(mp_obj_t self_in) {
Expand Down
53 changes: 51 additions & 2 deletions shared-bindings/synthio/BlockBiquad.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,30 @@
//| """A band-pass filter"""
//| NOTCH: FilterMode
//| """A notch filter"""
//| LOW_SHELF: FilterMode
//| """A notch filter"""
//| HIGH_SHELF: FilterMode
//| """A notch filter"""
//| PEAKING_EQ: FilterMode
//| """A notch filter"""
//|

MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, LOW_PASS, SYNTHIO_LOW_PASS);
MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, HIGH_PASS, SYNTHIO_HIGH_PASS);
MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, BAND_PASS, SYNTHIO_BAND_PASS);
MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, NOTCH, SYNTHIO_NOTCH);
MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, LOW_SHELF, SYNTHIO_LOW_SHELF);
MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, HIGH_SHELF, SYNTHIO_HIGH_SHELF);
MAKE_ENUM_VALUE(synthio_filter_mode_type, mode, PEAKING_EQ, SYNTHIO_PEAKING_EQ);

MAKE_ENUM_MAP(synthio_filter_mode) {
MAKE_ENUM_MAP_ENTRY(mode, LOW_PASS),
MAKE_ENUM_MAP_ENTRY(mode, HIGH_PASS),
MAKE_ENUM_MAP_ENTRY(mode, BAND_PASS),
MAKE_ENUM_MAP_ENTRY(mode, NOTCH),
MAKE_ENUM_MAP_ENTRY(mode, LOW_SHELF),
MAKE_ENUM_MAP_ENTRY(mode, HIGH_SHELF),
MAKE_ENUM_MAP_ENTRY(mode, PEAKING_EQ),
};

static MP_DEFINE_CONST_DICT(synthio_filter_mode_locals_dict, synthio_filter_mode_locals_table);
Expand All @@ -51,8 +63,17 @@ static synthio_filter_mode validate_synthio_filter_mode(mp_obj_t obj, qstr arg_n
//| mode: FilterMode,
//| frequency: BlockInput,
//| Q: BlockInput = 0.7071067811865475,
//| A: BlockInput = None,
//| ) -> None:
//| """Construct a biquad filter object with dynamic center frequency & q factor
//| """Construct a biquad filter object with given settings.
//|
//| ``frequency`` gives the center frequency or corner frequency of the filter,
//| depending on the mode.
//|
//| ``Q`` gives the gain or sharpness of the filter.
//|
//| ``A`` controls the gain of peaking and shelving filters according to the
//| formula ``A = 10^(dBgain/40)``. For other filter types it is ignored.
//|
//| Since ``frequency`` and ``Q`` are `BlockInput` objects, they can
//| be varied dynamically. Internally, this is evaluated as "direct form 1"
Expand All @@ -68,6 +89,7 @@ static const mp_arg_t block_biquad_properties[] = {
{ MP_QSTR_mode, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL } },
{ MP_QSTR_frequency, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL } },
{ MP_QSTR_Q, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL } },
{ MP_QSTR_A, MP_ARG_OBJ, {.u_obj = MP_ROM_NONE } },
};

static mp_obj_t synthio_block_biquad_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
Expand All @@ -81,7 +103,9 @@ static mp_obj_t synthio_block_biquad_make_new(const mp_obj_type_t *type_in, size
}

synthio_filter_mode mode = validate_synthio_filter_mode(args[ARG_mode].u_obj, MP_QSTR_mode);
return common_hal_synthio_block_biquad_new(mode, args[ARG_frequency].u_obj, args[ARG_Q].u_obj);
mp_obj_t result = common_hal_synthio_block_biquad_new(mode);
properties_construct_helper(result, block_biquad_properties + 1, args + 1, MP_ARRAY_SIZE(block_biquad_properties) - 1);
return result;
}

//|
Expand Down Expand Up @@ -136,10 +160,35 @@ MP_PROPERTY_GETSET(synthio_block_biquad_Q_obj,
(mp_obj_t)&synthio_block_biquad_get_Q_obj,
(mp_obj_t)&synthio_block_biquad_set_Q_obj);

//|
//| A: BlockInput
//| """The gain (A) of the filter
//|
//| This setting only has an effect for peaking and shelving EQ filters. It is related
//| to the filter gain according to the formula ``A = 10^(dBgain/40)``.
//| """
//|
static mp_obj_t synthio_block_biquad_get_A(mp_obj_t self_in) {
synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_synthio_block_biquad_get_A(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_block_biquad_get_A_obj, synthio_block_biquad_get_A);

static mp_obj_t synthio_block_biquad_set_A(mp_obj_t self_in, mp_obj_t arg) {
synthio_block_biquad_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_synthio_block_biquad_set_A(self, arg);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(synthio_block_biquad_set_A_obj, synthio_block_biquad_set_A);
MP_PROPERTY_GETSET(synthio_block_biquad_A_obj,
(mp_obj_t)&synthio_block_biquad_get_A_obj,
(mp_obj_t)&synthio_block_biquad_set_A_obj);

static const mp_rom_map_elem_t synthio_block_biquad_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&synthio_block_biquad_mode_obj) },
{ MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&synthio_block_biquad_frequency_obj) },
{ MP_ROM_QSTR(MP_QSTR_Q), MP_ROM_PTR(&synthio_block_biquad_Q_obj) },
{ MP_ROM_QSTR(MP_QSTR_A), MP_ROM_PTR(&synthio_block_biquad_A_obj) },
};
static MP_DEFINE_CONST_DICT(synthio_block_biquad_locals_dict, synthio_block_biquad_locals_dict_table);

Expand Down
9 changes: 7 additions & 2 deletions shared-bindings/synthio/BlockBiquad.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ extern const mp_obj_type_t synthio_filter_mode_type;
typedef struct synthio_block_biquad synthio_block_biquad_t;

typedef enum {
SYNTHIO_LOW_PASS, SYNTHIO_HIGH_PASS, SYNTHIO_BAND_PASS, SYNTHIO_NOTCH
SYNTHIO_LOW_PASS, SYNTHIO_HIGH_PASS, SYNTHIO_BAND_PASS, SYNTHIO_NOTCH,
// filters beyond this line use the "A" parameter (in addition to f0 and Q)
SYNTHIO_PEAKING_EQ, SYNTHIO_LOW_SHELF, SYNTHIO_HIGH_SHELF
} synthio_filter_mode;


mp_obj_t common_hal_synthio_block_biquad_get_A(synthio_block_biquad_t *self);
void common_hal_synthio_block_biquad_set_A(synthio_block_biquad_t *self, mp_obj_t A);

mp_obj_t common_hal_synthio_block_biquad_get_Q(synthio_block_biquad_t *self);
void common_hal_synthio_block_biquad_set_Q(synthio_block_biquad_t *self, mp_obj_t Q);

Expand All @@ -25,4 +30,4 @@ void common_hal_synthio_block_biquad_set_frequency(synthio_block_biquad_t *self,

synthio_filter_mode common_hal_synthio_block_biquad_get_mode(synthio_block_biquad_t *self);

mp_obj_t common_hal_synthio_block_biquad_new(synthio_filter_mode mode, mp_obj_t frequency, mp_obj_t Q);
mp_obj_t common_hal_synthio_block_biquad_new(synthio_filter_mode mode);
2 changes: 1 addition & 1 deletion shared-bindings/synthio/LFO.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static const uint16_t triangle[] = {0, 32767, 0, -32767};
//| including indirectly via a `Note` or another intermediate LFO.
//|
//| Using the same LFO as an input to multiple other LFOs or Notes is OK, but
//| the result if an LFO is tied to multiple Synthtesizer objects is undefined.
//| the result if an LFO is tied to multiple `Synthesizer` objects is undefined.
//|
//| In the current implementation, LFOs are updated every 256 samples. This
//| should be considered an implementation detail, though it affects how LFOs
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/synthio/Math.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ MAKE_ENUM_TYPE(synthio, MathOperation, synthio_math_operation,
//| including indirectly via a `Note` or another intermediate Math.
//|
//| Using the same Math as an input to multiple other Maths or Notes is OK, but
//| the result if an Math is tied to multiple Synthtesizer objects is undefined.
//| the result if an Math is tied to multiple `Synthesizer` objects is undefined.
//|
//| In the current implementation, Maths are updated every 256 samples. This
//| should be considered an implementation detail.
Expand Down
4 changes: 2 additions & 2 deletions shared-bindings/synthio/Note.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static const mp_arg_t note_properties[] = {
//| envelope: Optional[Envelope] = None,
//| amplitude: BlockInput = 1.0,
//| bend: BlockInput = 0.0,
//| filter: Optional[Biquad] = None,
//| filter: Optional[AnyBiquad] = None,
//| ring_frequency: float = 0.0,
//| ring_bend: float = 0.0,
//| ring_waveform: Optional[ReadableBuffer] = None,
Expand Down Expand Up @@ -85,7 +85,7 @@ MP_PROPERTY_GETSET(synthio_note_frequency_obj,
(mp_obj_t)&synthio_note_get_frequency_obj,
(mp_obj_t)&synthio_note_set_frequency_obj);

//| filter: Optional[Biquad]
//| filter: Optional[AnyBiquad]
//| """If not None, the output of this Note is filtered according to the provided coefficients.
//|
//| Construct an appropriate filter by calling a filter-making method on the
Expand Down
2 changes: 2 additions & 0 deletions shared-bindings/synthio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
//| """
//|

//| AnyBiquad = Union["Biquad", "BlockBiquad"]
//|
//| class EnvelopeState:
//| ATTACK: EnvelopeState
//| """The note is in its attack phase"""
Expand Down
103 changes: 50 additions & 53 deletions shared-module/audiofilters/Filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: MIT
#include "shared-bindings/audiofilters/Filter.h"

#include "shared-module/synthio/BlockBiquad.h"
#include <stdint.h>
#include "py/runtime.h"

Expand Down Expand Up @@ -48,15 +49,8 @@ void common_hal_audiofilters_filter_construct(audiofilters_filter_obj_t *self,

// The below section sets up the effect's starting values.

if (filter == MP_OBJ_NULL) {
filter = mp_const_none;
}
common_hal_audiofilters_filter_set_filter(self, filter);

// If we did not receive a BlockInput we need to create a default float value
if (mix == MP_OBJ_NULL) {
mix = mp_obj_new_float(1.0);
}
synthio_block_assign_slot(mix, &self->mix, MP_QSTR_mix);
}

Expand All @@ -73,65 +67,62 @@ void common_hal_audiofilters_filter_deinit(audiofilters_filter_obj_t *self) {
}
self->buffer[0] = NULL;
self->buffer[1] = NULL;
self->filter = mp_const_none;
self->filter_buffer = NULL;
self->filter_states = NULL;
}

void reset_filter_states(audiofilters_filter_obj_t *self) {
self->filter_states_len = 0;
self->filter_states = NULL;

void common_hal_audiofilters_filter_set_filter(audiofilters_filter_obj_t *self, mp_obj_t filter_in) {
size_t n_items;
mp_obj_t *items;
if (mp_obj_is_type(self->filter, (const mp_obj_type_t *)&synthio_biquad_type_obj)) {
self->filter_states_len = 1;
items = self->filter;
} else if (mp_obj_is_tuple_compatible(self->filter)) {
mp_obj_tuple_get(self->filter, &self->filter_states_len, &items);
}

if (!self->filter_states_len) {
return;
}

self->filter_states = m_malloc(self->filter_states_len * sizeof(biquad_filter_state));

if (mp_obj_is_type(items, (const mp_obj_type_t *)&synthio_biquad_type_obj)) {
synthio_biquad_filter_assign(&self->filter_states[0], items);
} else {
for (size_t i = 0; i < self->filter_states_len; i++) {
synthio_biquad_filter_assign(&self->filter_states[i], items[i]);
mp_obj_t *filter_objs;

if (filter_in == mp_const_none) {
n_items = 0;
filter_objs = NULL;
} else if (MP_OBJ_TYPE_HAS_SLOT(mp_obj_get_type(filter_in), iter)) {
// convert object to tuple if it wasn't before
filter_in = MP_OBJ_TYPE_GET_SLOT(&mp_type_tuple, make_new)(
&mp_type_tuple, 1, 0, &filter_in);
mp_obj_tuple_get(filter_in, &n_items, &items);
for (size_t i = 0; i < n_items; i++) {
if (!synthio_is_any_biquad(items[i])) {
mp_raise_TypeError_varg(
MP_ERROR_TEXT("%q in %q must be of type %q, not %q"),
MP_QSTR_object,
MP_QSTR_filter,
MP_QSTR_AnyBiquad,
mp_obj_get_type(items[i])->name);
}
}
}
}

mp_obj_t common_hal_audiofilters_filter_get_filter(audiofilters_filter_obj_t *self) {
if (mp_obj_is_type(self->filter, (const mp_obj_type_t *)&synthio_biquad_type_obj) || mp_obj_is_tuple_compatible(self->filter)) {
return self->filter;
filter_objs = items;
} else {
return mp_const_none;
n_items = 1;
if (!synthio_is_any_biquad(filter_in)) {
mp_raise_TypeError_varg(
MP_ERROR_TEXT("%q must be of type %q or %q, not %q"),
MP_QSTR_filter, MP_QSTR_AnyBiquad, MP_QSTR_iterable, mp_obj_get_type(filter_in)->name);
}
filter_objs = &self->filter;
}
}

void common_hal_audiofilters_filter_set_filter(audiofilters_filter_obj_t *self, mp_obj_t arg) {
if (arg == mp_const_none || mp_obj_is_type(arg, (const mp_obj_type_t *)&synthio_biquad_type_obj)) {
self->filter = arg;
} else if (mp_obj_is_tuple_compatible(arg) || mp_obj_is_type(arg, &mp_type_list)) {
size_t tuple_len;
mp_obj_t *tuple_items = NULL;

mp_obj_get_array(arg, &tuple_len, &tuple_items);
// everything has been checked, so we can do the following without fear

mp_obj_t *biquad_objects[tuple_len];
for (size_t i = 0; i < tuple_len; i++) {
biquad_objects[i] = mp_arg_validate_type_in(tuple_items[i], (const mp_obj_type_t *)&synthio_biquad_type_obj, MP_QSTR_filter);
}
self->filter = filter_in;
self->filter_objs = filter_objs;
self->filter_states = m_renew(biquad_filter_state,
self->filter_states,
self->filter_states_len,
n_items);
self->filter_states_len = n_items;

self->filter = mp_obj_new_tuple(tuple_len, (const mp_obj_t *)biquad_objects);
} else {
mp_raise_ValueError_varg(MP_ERROR_TEXT("%q must be a %q object, %q, or %q"), MP_QSTR_filter, MP_QSTR_Biquad, MP_QSTR_tuple, MP_QSTR_None);
for (size_t i = 0; i < n_items; i++) {
synthio_biquad_filter_assign(&self->filter_states[i], items[i]);
}
}

reset_filter_states(self);
mp_obj_t common_hal_audiofilters_filter_get_filter(audiofilters_filter_obj_t *self) {
return self->filter;
}

mp_obj_t common_hal_audiofilters_filter_get_mix(audiofilters_filter_obj_t *self) {
Expand Down Expand Up @@ -250,6 +241,8 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o
channel = 0;
}

shared_bindings_synthio_lfo_tick(self->sample_rate);

// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
mp_float_t mix = MIN(1.0, MAX(synthio_block_slot_get(&self->mix), 0.0));

Expand Down Expand Up @@ -334,6 +327,10 @@ audioio_get_buffer_result_t audiofilters_filter_get_buffer(audiofilters_filter_o

// Process biquad filters
for (uint8_t j = 0; j < self->filter_states_len; j++) {
mp_obj_t filter_obj = self->filter_objs[j];
if (mp_obj_is_type(filter_obj, &synthio_block_biquad_type_obj)) {
common_hal_synthio_block_biquad_tick(filter_obj, &self->filter_states[j]);
}
synthio_biquad_filter_samples(&self->filter_states[j], self->filter_buffer, n_samples);
}

Expand Down
Loading