Repository navigation
web_audio: convert each write's PCM16 to float in one JS call - #206
Merged
Merged
Conversation
…mple by sample in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
web_audioplayback converted every write sample by sample in Python: slice two bytes, build an int, divide, then store each float into theAudioBufferone 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_jsof the memoryview), and a smalljs.Functiondecodes it, divides by 32768 and copies each channel into theAudioBufferwithcopyToChannel. 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.pypins 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._writewith 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. Whennodeis 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.