Skip to content

fix(utils-sound): load audio via a ranged fetch into a Blob for WebAudio; list mp3 first - #40

Open
ThisIsSheikhRidwan wants to merge 1 commit into
engineio:mainfrom
ThisIsSheikhRidwan:main
Open

fix(utils-sound): load audio via a ranged fetch into a Blob for WebAudio; list mp3 first#40
ThisIsSheikhRidwan wants to merge 1 commit into
engineio:mainfrom
ThisIsSheikhRidwan:main

Conversation

@ThisIsSheikhRidwan

Copy link
Copy Markdown

Problem

On some setups the game produces no sound even though every asset request succeeds. Two independent causes:

  1. A plain audio GET is answered with 204 No Content. Howler's default (WebAudio) path fetches the sprite over XHR; when that response comes back empty, decoding fails with Decoding audio data failed. and the Howl stays in loading forever. (Reproduced in two independent Chromium instances against the Storybook dev server; a Range request for the same URL returns the full body as 206.)
  2. OGG is not always supported. sounds.json lists sounds.ogg first, and on the affected Chromium the media path rejects it with MEDIA_ERR_SRC_NOT_SUPPORTED.

Browsers also treat the failed fetch as a download in some configurations, prompting a "save file" dialog for the audio.

Change

packages/utils-sound/src/createSound.svelte.ts:

  • Preload the audio ourselves with a Range: bytes=0- request (which returns the whole file as 206), build a Blob, and hand Howler the object URL.
  • Pass the source's extension explicitly through Howler's format option. Without it Howler rejects a blob: URL with No codec support for selected audio sources. because it derives the codec from the URL extension.
  • Keep Howler on the WebAudio path (html5: false), which supports real audio sprites and overlapping one-shots (music + rapid SFX).
  • Fall back to Howler's plain loading (HTML5 path) if the ranged fetch fails, so nothing regresses if a server does not support ranges.
  • Apply the current volume settings once the players are created, since creation is now asynchronous.

apps/{lines,cluster,scatter,ways,price}/static/assets/audio/sounds.json: list sounds.mp3 before sounds.ogg so a supported codec is picked first.

Verification

Against the lines sample in Storybook, on the affected machine:

  • Howl state: loaded (previously stuck at loading)
  • howl._webAudio === true, howl._src is a blob: URL, howl._format === ['mp3']
  • AudioContext.state === 'running', Howler._muted === false after the first user gesture
  • bgm_main and sfx_btn_spin play simultaneously (playing() === true for both)
  • decodeAudioData on the fetched buffer returns a 406s buffer, confirming the file itself is valid

Notes

  • No public API changes. load() still returns { destroy }; player creation is simply deferred until the blob is ready.
  • The change is confined to utils-sound plus the per-game sounds.json files.

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