Skip to content

feat: expose Float32 buffers in Wasm for in-place audio - #117

Merged
byteland-app merged 6 commits into
mainfrom
stitch/wasm-float32-in-place-audio-jm1-a5fe900a-fc64-4e26-823e-300af944637f
Nov 26, 2026
Merged

byteland-app merged 6 commits into
mainfrom
stitch/wasm-float32-in-place-audio-jm1-a5fe900a-fc64-4e26-823e-300af944637f

Conversation

@google-labs-stitch

@google-labs-stitch google-labs-stitch Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Share #84's Processor f32 audio and drain methods (process_f32, process_planar_f32, process_mono_in_place_f32, drain_f32, drain_planar_f32 and drain_mono_f32) with exact symmetric f64 input quantization (NaN to 0, clamp -1.0..=1.0, multiply by 32767.0 in f64 with .round(), divide i16 outputs by 32768.0, pass OutputLayout::Stereo / OutputLayout::Mono, and fill unused tail frames with 0.0).
  • Share fixed-capacity input_f32, left_f32 and right_f32 Vec<f32> buffers in WasmProcessor::new (try_reserve_exact) across both #84's copied Float32Array methods (processF32, processMonoF32, drainF32, drainMonoF32) and #117's zero-copy in-place methods (processF32InPlace, processMonoF32InPlace, drainF32InPlace, drainMonoF32InPlace), exposing safe byte-offset getters inputF32Ptr, leftF32Ptr and rightF32Ptr under #![forbid(unsafe_code)].
  • Reuse #99's decode_events(&mut self.events, ...) buffer across all Int16Array and Float32 Wasm process paths with atomic validation before buffer or DSP state changes, keeping process, processMono, drain and drainMono unchanged.
  • Export TuneletAudioViews from src/index.ts over InitOutput["memory"] (without ambient DOM or WebAssembly globals) to cache Float32Array views, rebind them if WebAssembly.Memory.grow replaces memory.buffer, and clear them in dispose() / [Symbol.dispose]().
  • Update scripts/verify-pack.mjs (including #123's node, es2023, dom, and webworker strict tsc profiles and #135's controlActivity()), crate rustdoc and README.md for both copied Float32Array and zero-copy in-place Float32 APIs, view lifetimes and free() rules.

Verification

  • cargo fmt --check, cargo clippy --locked --lib -- -D warnings, cargo build --locked --release, RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps, MSRV 1.88 (--lib and wasm32-unknown-unknown --features wasm), and cargo package --locked pass.
  • RUSTDOCFLAGS="-D warnings" npm run check:wasm and npm run verify:pack pass across all four strict tsc profiles (node, es2023 without DOM globals, dom, webworker) on both unscoped tunelet and scoped @bytelandtechnology/tunelet archives.
  • Native Rust Processor::new live heap stays at 2,071,052 bytes (2,022.512 KiB on 0.3.4) with 0 post-construction allocations and bit-exact IEEE-754 f32 match against i16 reference outputs, feedback and telemetry across stereo, planar, mono and 1,180,136 drain frames.
  • Steady-state processF32InPlace(128) without events executes 0 Wasm-to-JS imports and allocates 0 TypedArray.subarray views (0/sec steady-state and 0/sec during dual-Tunelet crossfades, down from 2,250/sec and 4,500/sec).
  • Verified real Chromium OfflineAudioContext + AudioWorklet execution across standby Tunelet creation and WebAssembly.Memory.grow buffer detachment rebinding without errors:

Chromium AudioWorklet Float32 In-Place Verification


Automated PR initiated by user @byteland-app via Stitch Loop.

- Add f32 process and drain methods to Processor in src/processor.rs
- Quantize f32 input to symmetric i16 PCM on the stack
- Divide clipped i16 and downmix output by 32768.0 for f32 calls
- Reserve reusable f32 buffers in WasmProcessor::new
- Add processF32, processMonoF32, drainF32 and drainMonoF32 to Tunelet
- Check the Float32Array methods and types in scripts/verify-pack.mjs
- Document the f32 and Float32Array methods in src/lib.rs and README.md
- Add f32 process and drain methods to Processor
- Convert f32 audio with exact i16 quantization and scale
- Allocate fixed-capacity f32 buffers in WasmProcessor::new
- Expose inputF32Ptr, leftF32Ptr and rightF32Ptr offsets
- Add in-place Float32 process and drain methods to Tunelet
- Export TuneletAudioViews to refresh detached memory views
- Check Float32 runtime and type exports in verify-pack.mjs
- Document Float32 view lifetimes in README and rustdoc
@byteland-app

Copy link
Copy Markdown
Contributor

Review: Excellent zero-copy in-place Float32 linear memory views (TuneletAudioViews with automatic Memory.grow rebinding) for custom AudioWorklets. Please rebase onto #84 and #99 so we share the same input_f32/left_f32/right_f32 buffers and decode_events path, and update quantize_f32 to use f64 multiplication (matching #84) for exact symmetric quantization.

- Merge main into the f32 audio branch
- Pass OutputLayout::Stereo in stereo f32 calls
- Pass OutputLayout::Mono in mono f32 calls
- Document mono peak measurements after the downmix for f32 calls
- Use decode_events and self.events in processF32 and processMonoF32
- Keep the Wasm initializer checks in scripts/verify-pack.mjs
- Merge main into the feature branch.
- Pass OutputLayout to Processor::next and Processor::drain_into.
- Multiply clamped f32 input samples by 32767.0 in f64.
- Share decode_events in Wasm in-place f32 audio calls.
- Keep SyncInitInput checks in scripts/verify-pack.mjs.
- Merge the #84 branch into this pull request
- Share the f32 Processor methods and symmetric f64 quantization
- Share input_f32, left_f32 and right_f32 across f32 Wasm calls
- Share decode_events across copied and in-place f32 Wasm calls
- Test and document both copied and in-place Float32 APIs
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Addressed in 8b557c9 and 1076496 (merged main / #99 and #84 as force-pushing is disabled in this environment):

  1. Shared Processor f32 implementation & symmetric f64 quantization: Merged #84 (src/processor.rs and src/lib.rs) so all six Processor f32 methods share #84's pcm_from_f32 ((f64::from(sample.clamp(-1.0, 1.0)) * 32_767.0).round() as i16) and OutputLayout::Stereo / OutputLayout::Mono paths.
  2. Shared input_f32 / left_f32 / right_f32 Wasm buffers: Unified WasmProcessor in src/wasm.rs so #84's copied Float32Array calls (processF32, processMonoF32, drainF32, drainMonoF32) and #117's zero-copy in-place calls (inputF32Ptr, leftF32Ptr, rightF32Ptr, processF32InPlace, processMonoF32InPlace, drainF32InPlace, drainMonoF32InPlace) share the same fixed-capacity input_f32, left_f32 and right_f32 buffers.
  3. Shared #99 decode_events buffer & pack checks: Updated processF32InPlace and processMonoF32InPlace (alongside processF32 and processMonoF32) to reuse decode_events(&mut self.events, events, frames)?, and unified scripts/verify-pack.mjs and README.md to verify and document both copied and in-place Float32 APIs.

@byteland-app

Copy link
Copy Markdown
Contributor

Round 2 review: Approved — merging #84 makes this the complete Float32 copy + zero-copy in-place solution (and we closed #84 in favor of this PR). Please rebase onto updated main after #123 and #135, and change 'memory: WebAssembly.Memory' in src/index.ts (TuneletAudioViews) to 'InitOutput["memory"]' so it passes #123's strict ES2023 tsc check without DOM globals.

- Merge main 0.3.4 with #123 and #135 into the Float32 branch.
- Change TuneletAudioViews memory to InitOutput["memory"] in index.ts.
- Check Float32 and TuneletAudioViews in all verify-pack tsc profiles.
- Update README.md with InitOutput["memory"] and controlActivity().
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Addressed in a6f0609 (merged updated main 0.3.4 as force-pushing is disabled in this environment):

  1. Synced with updated main (0.3.4 after #123 and #135): Resolved merge conflicts across src/index.ts, scripts/verify-pack.mjs and README.md, preserving #123's portable declaration sanitization and file: URL loader checks alongside #135's controlActivity() API and docs.
  2. Typed TuneletAudioViews memory as InitOutput["memory"]: Changed private readonly memory and constructor(memory: ...) in src/index.ts from WebAssembly.Memory to InitOutput["memory"], eliminating ambient DOM/WebAssembly globals from src/index.ts and dist/index.d.ts.
  3. Verified strict ES2023 & multi-profile tsc checks: Added TuneletAudioViews, using / [Symbol.dispose](), inputF32Ptr/leftF32Ptr/rightF32Ptr, copied Float32Array calls and zero-copy in-place Float32 calls to verifySharedApi in scripts/verify-pack.mjs (plus WebAssembly.Memory compatibility in the dom and webworker profiles), passing npm run verify:pack (including #123's pure es2023 tsc check without DOM globals) and MSRV 1.88 builds.

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.

1 participant