Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion shared-module/synthio/Biquad.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void common_hal_synthio_biquad_tick(mp_obj_t self_in) {
}

void synthio_biquad_filter_reset(biquad_filter_state *st) {
memset(&st->x, 0, 4 * sizeof(int16_t));
memset(st, 0, sizeof(*st));
}

static inline int32_t biquad_filter_sample(int32_t input, int32_t a1, int32_t a2, int32_t b0, int32_t b1, int32_t b2, int32_t x0, int32_t x1, int32_t y0, int32_t y1) {
Expand Down
43 changes: 43 additions & 0 deletions tests/circuitpython/audiofilter_filter_reset_buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# audiofilters.Filter.reset_buffer() should leave no audio behind: a biquad
# reset should clear both its input and output history, not just the input.
import array
import math
from audiocore import get_buffer, reset_buffer, RawSample
from audiofilters import Filter
from synthio import Biquad, FilterMode

RATE = 48000
FRAMES = 1024


def s16(data):
out = []
for index in range(0, len(data) - 1, 2):
value = data[index] | (data[index + 1] << 8)
out.append(value - 65536 if value >= 32768 else value)
return out


loud = array.array(
"h", [int(30000 * math.sin(2 * math.pi * 200 * frame / RATE)) for frame in range(FRAMES)]
)
silence = array.array("h", [0] * FRAMES)

biquad = Biquad(FilterMode.LOW_PASS, 800.0)
node = Filter(
filter=biquad,
sample_rate=RATE,
channel_count=1,
bits_per_sample=16,
samples_signed=True,
buffer_size=FRAMES * 2,
)

node.play(RawSample(loud, sample_rate=RATE, channel_count=1), loop=True)
for _ in range(4):
get_buffer(node) # fill the filter's memory

node.play(RawSample(silence, sample_rate=RATE, channel_count=1), loop=True)
reset_buffer(node) # what an AudioOut does on play()
first = s16(bytes(get_buffer(node)[1]))
print("peak after reset_buffer:", max(abs(value) for value in first))
1 change: 1 addition & 0 deletions tests/circuitpython/audiofilter_filter_reset_buffer.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
peak after reset_buffer: 0
Loading