Skip to content

Sound: one-shot playback with no sound object, and bundled sounds copied once per process - #477

Draft
oliedis wants to merge 2 commits into
developfrom
claude/quest-spatial-one-shots
Draft

oliedis wants to merge 2 commits into
developfrom
claude/quest-spatial-one-shots

Conversation

@oliedis

@oliedis oliedis commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Renderer side of a fire-and-forget sound API for apps that fire many short cues, and a fix for bundled sounds being copied out of the APK on every load. Workaround ID for the apps: viroreact-quest-spatial-one-shots. The ViroReact side is ReactVision/viro#640.

Not tested on a device, and not built. No Quest was available and there was no NDK where this was written, so libviro_renderer.so has not been built with these changes. The changed C++ was type-checked with host clang++ -fsyntax-only as the Android platform against stubbed NDK headers, and SoundData.java compiles with javac. iOS and WASM, which share VRODriver.h and the two GVR sound classes, were not compiled. CI's :viroreact:assembleDebug is the first real build.

Why

Soak Squad moved its one-shot sounds (pops, chirps, splashes) off ViroSound / ViroSpatialSound onto another audio library, because each cue mounted a renderer sound. Its Quest logcats show, per mount, Viro: Copied resource res:/… and AVPlayer reset / prepared on the Android main thread. Cues grow with the number of enemies and the game lagged as they built up.

Commit 1: bundled sounds are copied once (VROSoundDataGVR)

  • A res:/ sound was copied out of the APK to the cache directory on every VROSoundDataGVR, on the calling thread. The Java side writes a fixed file name per resource, so every load rewrote the same file, and each VROSoundDataGVR deleted it on destruction while others were still using it.
  • copyBundledResourceOnce keeps a process-wide map from resource to local path, makes the copy once under a lock, and leaves the file in place. The copy runs on a background thread.
  • Behaviour change: a sound from a bundled resource is now ready a moment after it is created, from a background thread, as URL sounds always were. Before, it was ready inside the constructor.
  • The data's status, path and delegate were written from the load's thread and read from the application's with no guard. They are under a mutex, and a delegate hears each status once.

Commit 2: one-shots (VROSoundGVR::playOneShot, VRODriver::playSoundOneShot, SoundData.playOneShot)

  • SoundData.playOneShot(context, position, volume, rolloffModel, minDistance, maxDistance): spatial at a world position, or head-locked when position is null.
  • No VROSoundGVR, no delegate, nothing to dispose. The clip is decoded into GVR audio with PreloadSoundfile on its first play and kept there, so later plays create a source from memory.
  • Voices, not pooled objects. The request asked for a small pool of native sound objects per clip. GVR's API does not allow reuse: a source that has finished is invalid and cannot be replayed. What is pooled is the decoded clip; each play creates a GVR source, and at most 8 of one clip sound at once. One more stops the oldest. Finished sources are released on the next play of that clip.
  • All GVR calls for a one-shot run on a background thread. The JNI call returns at once.
  • VRODriver::playSoundOneShot has a default that returns false, so the iOS and web drivers are untouched and report "not played".
  • VROSoundGVR now preloads its file when its data becomes ready on any thread. It did so only when the data was already ready at setup, which is no longer the case for bundled resources after commit 1.

Known limits

  • VROSoundGVR's destructor calls UnloadSoundfile for its path. If an app unmounts a ViroSpatialSound that used the same preloaded clip, GVR drops the decoded copy and later one-shots of that clip are streamed from the file. Still correct, less efficient. Not changed here.
  • Whether GVR decodes in PreloadSoundfile or lazily is not documented in the header. Either way it is off the main thread.
  • One-shot positions are fixed at play time. A cue cannot follow a moving node.

GVR audio

The audio engine here is Google's GVR SDK audio, which Google has discontinued; libgvr_audio.so is a prebuilt binary that cannot be patched. Its successor, Resonance Audio, is open source, has the same model (preloaded files, sound objects, room effects), and would allow real source reuse and per-source callbacks. Replacing it is worth planning but is a larger change with its own device testing, and is not part of this PR.

To check on a Quest 3

With examples/quest-sound-one-shots from viro#640, release build, adb logcat -s ReactNativeJS Viro:

  1. One Copied resource res:/… line per clip for the whole run.
  2. Pops are heard circling the head, a chirp stays centred, for 5 minutes at 20 cues a second, with no frame-rate drop.
  3. No AVPlayer reset / prepared lines while cues fire.
  4. Existing ViroSound and ViroSpatialSound components with bundled sources still load and play (the readiness timing changed).

🤖 Generated with Claude Code

https://claude.ai/code/session_01JoDeNibskBVXyR57tP3WdH

…he calling thread

Every load of a sound from a res:/ path copied it out of the APK again, to the
same cache file, on the calling thread, and each VROSoundDataGVR deleted that
file when destroyed. The copy is made once on a background thread, shared and
left in place. The data's status and delegate are now under a lock.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JoDeNibskBVXyR57tP3WdH
…eShot)

For cues fired often. The clip is decoded into GVR audio on first play and kept;
up to 8 plays of one clip sound at once. The work runs on a background thread.
VROSoundGVR also preloads its file when its data becomes ready on any thread.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JoDeNibskBVXyR57tP3WdH
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