Skip to content

web_audio: convert each write's PCM16 to float in one JS call - #206

Merged
bdbarnett merged 1 commit into
mainfrom
ostinato/web-audio-convert
Oct 15, 2026
Merged

bdbarnett merged 1 commit into
mainfrom
ostinato/web-audio-convert

Conversation

@bdbarnett

Copy link
Copy Markdown
Contributor

web_audio playback converted every write sample by sample in Python: slice two bytes, build an int, divide, then store each float into the AudioBuffer one element at a time. In Pyodide every one of those stores is an FFI crossing, so a second of stereo audio cost about 96,000 of them.

Now each write is one JS call. The PCM crosses as a typed array (to_js of the memoryview), and a small js.Function decodes it, divides by 32768 and copies each channel into the AudioBuffer with copyToChannel. The output is identical to before: same scaling, same de-interleave.

If the page's FFI can't hand JS a typed array, the module notices once and converts in Python instead, still without per-sample slicing (array('h') decodes the whole buffer).

Tests: tests/test_web_audio.py pins the new conversion against a copy of the old loop for mono and stereo, including the edge values -32768 and 32767. It covers the Python fallback, WebPCMOutput._write with a fake context, that the JS function is built once and called once per write, and that a failing JS call falls back and stays off. When node is installed it also runs the exact JS source over the same data, read from an odd byte offset, and compares it float32 for float32. The JS path was also checked under real Pyodide (3.14): it is the path taken, and its output matched exactly.

@bdbarnett
bdbarnett merged commit 3b72f14 into main Oct 15, 2026
3 checks passed
@bdbarnett
bdbarnett deleted the ostinato/web-audio-convert branch October 15, 2026 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant