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
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