Skip to content

audio: bring the backend in-tree, and give the loader a proper seam - #1617

Merged
obiot merged 1 commit into
masterfrom
feat/audio-backend-vendor
Aug 30, 2026
Merged

obiot merged 1 commit into
masterfrom
feat/audio-backend-vendor

Conversation

@obiot

@obiot obiot commented Aug 30, 2026

Copy link
Copy Markdown
Member

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; 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.

Colliding create hooks the sound-level and voice-level hooks shared a name and were registered into one object literal, so one silently replaced the other
Lost group getters pos(), orientation() and stereo() 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 back
Lazy audio context pos() on a clip that had not played yet returned early and silently discarded the value
loop(loop, id) the implementation read an instance id the overloads never declared
Plugin unregistration threw before cleaning anything up — const sound = sound._sounds[j] shadowed the variable it read from
pannerAttr(id) the numeric form was undeclared

Two of these had no coverage at all before, which is why they survived the
swap: pannerAttr with 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() rejection
wedging 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 its internal flag, and
#1770 a backgrounded tab
never resuming.

Structure

Being in-tree means the layers can be honest:

  • The plugin registry is gone. It existed so third parties could extend the
    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
  • Audio context creation is idempotent and belongs to the engine, rather
    than a free function reaching into the singleton with no internal guard that
    six call sites each worked around
  • The bundled user-agent parser is gone. It duplicated system/platform
    less 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/platform as appleVendor
  • CocoonJS 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, 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/ 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:// —
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

  • Audio sprites — a clip declares named regions of one file, so a single
    download carries many effects
  • duration(), playing(), state()
  • pool, rate, mute, preload, format and an on map of lifecycle
    callbacks at load time

play()'s second argument accepts either the original loop boolean or an
options object, so every existing call form is unchanged — asserted directly.

Notes

  • The backend keeps its own CHANGELOG.md and LICENSE, recording that it is a
    customized derivative rather than the upstream library
  • Bundle size is unchanged: 246.0 → 246.2 kB gzip. The library was already
    bundled in, so this changes what ships in node_modules, not what ships to
    the browser
  • The README's ES6 claims were corrected to state the actual ES2022 target,
    with a short guide on transpiling for older browsers

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
Copilot AI lite review requested due to automatic review settings August 30, 2026 03:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@obiot
obiot merged commit 0a6fd85 into master Aug 30, 2026
6 checks passed
@obiot
obiot deleted the feat/audio-backend-vendor branch August 30, 2026 03:42
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