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
4 changes: 2 additions & 2 deletions shared-module/synthio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static bool synth_note_into_buffer(synthio_synth_t *synth, int chan, int32_t *ou
for (uint16_t i = 0; i < dur; i++) {
accum += dds_rate;
// because dds_rate is low enough, the subtraction is guaranteed to go back into range, no expensive modulo needed
if (accum > lim) {
if (accum >= lim) {
accum = accum - lim + offset;
}
int16_t idx = accum >> SYNTHIO_FREQUENCY_SHIFT;
Expand Down Expand Up @@ -266,7 +266,7 @@ static bool synth_note_into_buffer(synthio_synth_t *synth, int chan, int32_t *ou
for (uint16_t i = 0; i < dur; i++) {
accum += ring_dds_rate;
// because dds_rate is low enough, the subtraction is guaranteed to go back into range, no expensive modulo needed
if (accum > lim) {
if (accum >= lim) {
accum = accum - lim + offset;
}
int16_t idx = accum >> SYNTHIO_FREQUENCY_SHIFT;
Expand Down
33 changes: 33 additions & 0 deletions tests/circuitpython/synthio_oscillator_loop_end.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# A note whose loop end is reached exactly (accum == lim) should wrap back
# to the loop start on that same sample, not read one sample past the loop.
import array
from audiocore import get_buffer
from synthio import Note, Synthesizer

RATE = 8000
LOOP = 256

# 512 samples: the loop is [0, 256) and is silent; sample 256 is a marker
# the oscillator should never reach.
table = array.array("h", [0] * 512)
table[LOOP] = 30000

synth = Synthesizer(sample_rate=RATE, channel_count=1)
note = Note(
frequency=RATE / LOOP, # exactly one table step per output sample
waveform=table,
waveform_loop_start=0,
waveform_loop_end=LOOP,
envelope=None,
)
synth.press(note)

out = []
for _ in range(4):
data = bytes(get_buffer(synth)[1])
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)

hits = [i for i, v in enumerate(out) if abs(v) > 1000]
print("marker hits:", hits)
1 change: 1 addition & 0 deletions tests/circuitpython/synthio_oscillator_loop_end.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
marker hits: []
Loading