Skip to content

fix: byte-swap PortAudio PCM on big-endian hosts - #119

Merged
chrisuthe merged 1 commit into
mainfrom
fix/portaudio-big-endian
Nov 10, 2026
Merged

chrisuthe merged 1 commit into
mainfrom
fix/portaudio-big-endian

Conversation

@chrisuthe

Copy link
Copy Markdown
Member

Fixes #78

Why

The player carries little-endian PCM, and every other backend opens an explicitly little-endian format. PortAudio has no little-endian format to ask for: paInt16, paInt24 and paInt32 are native-endian. On a big-endian host, -o portaudio therefore played every 16-, 24- and 32-bit sample byte-reversed, which is noise at any volume. --input portaudio, added after #78 was filed, had the mirror-image bug: it sent native-endian capture upstream as if it were little-endian.

What

  • New header-only src/pcm_endian.h with pcm_le_native_swap(), which swaps each sample in place between little-endian and native order. The swap undoes itself, so it serves both directions. Thanks to if constexpr (std::endian::native == std::endian::little), it compiles to nothing on little-endian hosts, so x86 and ARM builds do no extra work.
  • Sink: the PortAudio callback swaps after apply_volume_ramp(), which expects little-endian data.
  • Source: read() swaps as it takes frames out of the ring, on the capture thread rather than in PortAudio's callback. It keeps a bytes_per_sample_ for this.
  • The doc comment on pa_format_for(), which claimed PortAudio takes little-endian PCM, is corrected.
  • 8-bit is unaffected, as are ALSA, PulseAudio, PipeWire and CoreAudio.

Testing

  • tests/pcm_volume_le_check.cpp, which the big-endian (mips) CI job already cross-compiles and runs under qemu, now checks the swap too. It compares 16- and 32-bit results against memcpy into native int16_t/int32_t rather than repeating the swap logic, and checks that a second swap restores the original bytes. ci.yml is unchanged.
  • Ran it locally as a big-endian MIPS32 static binary under qemu-mips-static: all checks pass. A mutant with the swap disabled fails it (5 wrong samples at each of 16, 24 and 32 bits), so the check catches the bug.
  • Windows (clang-cl, -Werror, PortAudio built in): 540/540 passed, so both changed PortAudio files compile cleanly.
  • Linux (WSL Ubuntu 24.04, gcc 13, -Werror, no PortAudio): 549/549 passed.
  • clang-format 20.1.8 on the changed files.
  • Not tested: audio playback on real big-endian hardware.

The player carries little-endian PCM, but PortAudio's paInt16, paInt24 and
paInt32 are native-endian. On a big-endian host the sink handed PortAudio
byte-reversed samples and played noise, and --input portaudio sent
byte-reversed capture upstream.

pcm_le_native_swap() converts in place in either direction and compiles to
nothing on little-endian hosts. The sink swaps after the volume ramp, which
works on little-endian data; the source swaps as it reads from its ring.

The standalone check that CI cross-compiles for big-endian MIPS now covers
the swap too, against memcpy into native integers.

Fixes #78
@chrisuthe
chrisuthe merged commit c18a960 into main Nov 10, 2026
20 checks passed
chrisuthe added a commit that referenced this pull request Nov 12, 2026
Brings in #116, so macOS builds on the macos-15 runner, and everything since through #119.
chrisuthe added a commit that referenced this pull request Nov 12, 2026
Brings the re-pin to sendspin-cpp 78b13d5e and main through #119. The endpoint now reads the token through client_pairing_token(), which replaces the deprecated pairing_token().
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.

PortAudio sink plays little-endian PCM as native-endian on big-endian hosts

1 participant