Repository navigation
audio: bring the backend in-tree, and give the loader a proper seam - #1617
Merged
Merged
Conversation
melonJS depended on a third-party audio library. It is now maintained in-tree
under src/audio/backend, which removes the engine's last runtime dependency
besides core-js and fixes a packaging wart along the way: the emitted type
declarations referenced the library, so consumers type-checking with
`skipLibCheck: false` had to install it themselves.
The starting point was that library's unmerged TypeScript rewrite. Upstream's
last release was 2.2.4 in September 2023 and the rewrite has sat open since
November 2025. It carries defects the shipped 2.2.4 does not, and each one is
now pinned by a regression test written against 2.2.4's behaviour:
- the sound-level and voice-level create hooks shared a name and were
registered into one object literal, so one silently replaced the other
- pos(), orientation() and stereo() lost their group-level read path: called
with no arguments they returned the sound itself, and a set with no instance
id never recorded the value, so a set could not be read back
- the audio context was never created before a spatial call, so pos() on a
clip that had not played yet silently discarded the value
- loop(loop, id) was unreachable through the types
- unregistering the spatial plugin threw before cleaning anything up
- pannerAttr(id) with a numeric argument was undeclared
Eight fixes were also carried over from pull requests that have sat unmerged
against 2.2.4, most of them mobile correctness: panner attributes shared by
reference and leaking between sounds (#1758), the iOS play-after-pause race
(#1761), a resume() rejection wedging the engine in "suspended" (#1764), a
looping clip reporting a position past its loop end (#1776), headphone unplug
leaving playback dead (#1551), a null context on low-end iOS (#1363), a queued
stop() dropping its internal flag (#1648), and a backgrounded tab never
resuming (#1770).
Structurally, being in-tree means the layers can be honest. The plugin registry
is gone — it existed so third parties could extend the library, and there was
only ever one plugin, always installed; spatial audio is now part of the engine,
sound and voice paths directly. Audio context creation is idempotent and belongs
to the engine rather than a free function that reached into the singleton with
no guard. The bundled user-agent parser is gone: it duplicated system/platform
less well, missing iPadOS 13+ entirely, and the one check still reachable from
it now lives there as `appleVendor`. CocoonJS support and the iOS 8 fallback are
gone as unreachable.
The loader keeps its own boundary. Audio is registered through a parser in
loader/parsers/ like every other asset type, rather than the loader importing
the audio module directly, so a future backend change cannot ripple into it. The
shared fetch helper moved out of loader/parsers/ to utils/: it is engine
transport, not loader policy, and its placement was the only reason the audio
module had to import the loader at all. That move fixes a real bug — audio was
the last asset type calling fetch() directly, so sound files failed to load when
a game is served from file://, which is a Cordova or Capacitor APK on Android.
Owning the code also makes capability reachable that was always there. Clips can
declare sprite regions, so one download carries many effects; duration(),
playing() and state() are exposed; and pool, rate, mute, preload, format and a
map of lifecycle callbacks can be set at load time. play()'s second argument
accepts either the original loop boolean or an options object, so every existing
call form is unchanged.
The backend keeps its own CHANGELOG and LICENSE, recording that it is a
customized derivative rather than the upstream library.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Aa37KGXZcnVrbn1yG4j1N
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.
melonJS depended on a third-party audio library. It is now maintained in-tree
under
src/audio/backend, which removes the engine's last runtime dependencybesides
core-jsand fixes a packaging wart along the way: the emitted typedeclarations referenced the library, so consumers type-checking with
skipLibCheck: falsehad to install it themselves.The starting point was that library's unmerged TypeScript rewrite. Upstream's
last release was 2.2.4 in September 2023; the rewrite has sat open since
November 2025.
Defects in the rewrite, each now pinned by a regression test
Written against 2.2.4's behaviour — the version being replaced — so a change
that reintroduces one fails here rather than in a game.
pos(),orientation()andstereo()returned the sound itself instead of the value, and a set with no instance id never recorded it — so a set could not be read backpos()on a clip that had not played yet returned early and silently discarded the valueloop(loop, id)const sound = sound._sounds[j]shadowed the variable it read frompannerAttr(id)Two of these had no coverage at all before, which is why they survived the
swap:
pannerAttrwith an instance id, and unregistering the spatial plugin.Eight fixes carried from unmerged upstream pull requests
Mostly mobile correctness, all open against 2.2.4:
#1758 panner attributes
shared by reference and leaking between sounds,
#1761 the iOS
play-after-pause race,
#1764 a
resume()rejectionwedging the engine in
suspended,#1776 a looping clip
reporting a position past its loop end,
#1551 headphone unplug
leaving playback dead,
#1363 a null context on
low-end iOS, #1648 a queued
stop()dropping itsinternalflag, and#1770 a backgrounded tab
never resuming.
Structure
Being in-tree means the layers can be honest:
library; there was only ever one plugin, always installed. Spatial audio is
part of the engine, sound and voice paths directly. It also removes a bug
class — the colliding-hook defect above was only possible because of it
than a free function reaching into the singleton with no internal guard that
six call sites each worked around
system/platformless well — its iOS check missed iPadOS 13+, which reports the desktop Mac UA
— and only one of its eight exports was still reachable. That check now lives
in
system/platformasappleVendorThe loader keeps its own boundary
Audio is registered through a parser in
loader/parsers/like every other assettype, instead of the loader importing the audio module directly, so a future
backend change cannot ripple into it.
The shared fetch helper moved from
loader/parsers/toutils/: it is enginetransport, not loader policy, and its placement was the only reason the audio
module had to import the loader at all.
That move fixes a real bug. Audio was the last asset type calling
fetch()directly, so sound files failed to load when a game is served from
file://—a Cordova or Capacitor APK on Android. Every other asset type went through the
helper, which falls back to XHR for that scheme. A game would load its maps,
textures and fonts and silently fail on every sound.
Capability that was always there, now reachable
download carries many effects
duration(),playing(),state()pool,rate,mute,preload,formatand anonmap of lifecyclecallbacks at load time
play()'s second argument accepts either the originalloopboolean or anoptions object, so every existing call form is unchanged — asserted directly.
Notes
CHANGELOG.mdandLICENSE, recording that it is acustomized derivative rather than the upstream library
bundled in, so this changes what ships in
node_modules, not what ships tothe browser
with a short guide on transpiling for older browsers