Skip to content

Fix audiomixer phase inversion and use DSP on Cortex-M33 - #11617

Merged
tannewt merged 2 commits into
adafruit:mainfrom
MakerClassCZ:audiomixer-dsp
Oct 16, 2026
Merged

tannewt merged 2 commits into
adafruit:mainfrom
MakerClassCZ:audiomixer-dsp

Conversation

@lynt-smitka

Copy link
Copy Markdown

On boards with DSP instructions (SAMD51, nRF52840, i.MX RT, STM32F4), a voice at level 1.0 came out negated. The level is 1 << 15, and the code shifted it left by 16, which overflowed into the sign bit. Mono output was inverted. With centered stereo the right channel was out of phase with the left, so it cancelled out when the two channels were summed to mono.

Speed: The DSP code ran only on ARMv7E-M. The RP2350 has the same DSP instructions, so it now uses them too. Chips without DSP, like the RP2040, now mix with integers instead of float. The output is the same on every chip.

Time to mix 4 voices, 1024 samples:

Board Before After
PicoPad (RP2040) 6250 µs 705 µs
Fruit Jam (RP2350) 648 µs 249 µs

Flash is 1.1 KB smaller on RP2040 and 1.8 KB smaller on RP2350.

Testing: I read the mixer output with audiocore.get_buffer() on all four boards.

The code was written with AI help.

On cores with DSP instructions, a level of 1.0 (1 << 15) shifted left by
16 overflowed into the sign bit, so the mixed sample came out negated:
mono output was inverted and the right channel of a centered stereo voice
was out of phase with the left. Shift by 15 instead.

A centered pan now scales both channels by 1.0 instead of the left one
by 32767/32768.
The SIMD helpers were limited to ARMv7E-M, so the Cortex-M33 in the
RP2350 fell back to a float divide per sample. Check __ARM_FEATURE_DSP
instead, and mix with integers where there is no DSP, which matches the
DSP result bit for bit.

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome! Thank you!

@tannewt
tannewt merged commit 8253c03 into adafruit:main Oct 16, 2026
548 checks passed
bdbarnett added a commit to PyDevices/micropython-pydevices that referenced this pull request Oct 22, 2026
…, rebase the ESP-IDF components patch (#173)

alpha.2 fixes the audiomixer level-1.0 rounding upstream (adafruit/circuitpython#11617: it does the multiply in integers, shifted by 15, so a level of 1.0 is exact), so patch 0007 is removed; the series skips its number. The espressif port moved to ESP-IDF 6.1 and rewrote its component list and Makefile, so 0006 (the ESP-IDF components a user C module asks for) is rebased onto it. The other patches apply unchanged.
bdbarnett added a commit to PyDevices/audiodsp that referenced this pull request Oct 22, 2026
* Take CircuitPython 11.0.0-alpha.2's integer mixer and inline synthio_sat16

audiomixer (11a329885, fc57c0f36; adafruit/circuitpython#11617): a voice is
scaled as (ai * mul) >> 15 where it was ai * (mul / 32767.0f) truncated toward
zero, and a centred pan is 32768 - panning where it was 32767 - panning. Taken
in upstream's words in src/audiomixer/Mixer.c, in the CPython extension's
scaling kernel and in src/cpython/audiomixer.py, whose float32 helpers go.

Every level below 1.0 moves by up to one LSB, and a small negative sample now
rounds down (-1 at level 0.5 stays -1). A voice at level 1.0 is a wire without
help, so the unity branch this port added in audiodsp#95 is deleted, and a
stereo voice at pan 0 no longer has its left channel one step below the right.
The CPython target's block-level pass-through at unity is keyed on Q15 32768
alone now: 32767 no longer multiplies by exactly 1.

synthio (cbbde1208; adafruit/circuitpython#11621): synthio_sat16 is inline in
src/synthio/__init__.h as upstream has it, with __ssat on cores that define
__ARM_FEATURE_SAT. The rounding is the same arithmetic without a branch, so no
sample changes.

mixer_q15_level_probe.py replaces mixer_level_precision_probe.py, whose
material was the float32-versus-float64 flips that no longer exist. It renders
the rails and the values either side of zero at four levels and five pans,
mono and stereo, and verify_dsp.py holds CircuitPython, MicroPython and CPython
to the same bytes. test_cpython_audiomixer_level.py and
test_cpython_mixer_scale.py are rewritten around the integer arithmetic.

* Move the oracle to CircuitPython 11.0.0-alpha.2 and re-capture two digests

CIRCUITPYTHON_ORACLE names d53b2277c, and tests/test_voice_ceiling_consistency.py
pins the sha256 of the 64-voice binary built from it (ee0cc93c...).

Two stored digests moved with the integer mixer, re-captured from the new
oracle's own output, which CPython and desktop MicroPython reproduce:

- streaming_component, streaming half: f3ffad8e... to 33b27a2f.... The mixer
  voices in streaming_component_probe.py are below unity.
- synthtools_acceptance: b6e3b06f... to 757d944a.... The lead, bend and bass
  lines moved; the other five lines are unchanged.

Every other stored digest held and reproduces on the new oracle:
midi_component, effects_component, biquad_component and mixdown_knee. The
oracle labels in effects_component and streaming_component follow the pin.

* Record the alpha.2 resync and retire "a voice at level 1.0 is a wire"

upstream-sync.md gets the new "last resync" entry (the alpha.1 one becomes the
one before it), upstream-diff.md says what upstream's integer mixer changed
and which digests moved, and the audiodsp#95 section becomes a record of a
difference upstream now shares, as the alpha.1 resync did for its five.

* Print the Q15 probe's levels by name

A float32 MicroPython build prints 100 / 127 with fewer digits than CPython,
so the probe's label line differed where the samples agreed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants