Modules

Starter modules

Audio

prefab-audio@0.1.1

Game/app audio over raw Web Audio: load samples from your bundle, play with volume/rate/loop, zero-asset beeps. Replaces maintenance-mode audio shims.

Version
0.1.1
License
MIT
Size
2 KB
Runs on
Desktop and mobile browsers.

Add it

Declare it in prototir.json at the root of your ZIP. Prototir checks the version at upload and adds it to the page's import map.

prototir.json
{
  "modules": [
    "prefab-audio@0.1.1"
  ]
}

Then import it by name, from 'prefab-audio', as you would from npm.

Smallest example

Example
import { createAudio } from 'prefab-audio';

const audio = createAudio();
await audio.load('coin', 'assets/coin.ogg'); // a file in your bundle
audio.play('coin', { volume: 0.8 });
audio.beep(880, 0.08); // no file needed

API

createAudio({ volume = 1 })

await audio.load(name, url)
Fetches and decodes a sample from your bundle and keeps it under name. Throws if the file is missing.
audio.play(name, { volume = 1, rate = 1, loop = false })
Plays a loaded sample. Returns a voice { stop(), source, gain }, or null when the name is unknown.
audio.beep(freq = 880, duration = 0.08, { volume = 0.5, type = 'sine' })
A synthesized blip for UI feedback, with no audio file. Returns the same voice shape.
audio.volume
Master volume, readable and settable.
audio.stopAll()
Stops every playing voice.
audio.destroy()
Stops everything, removes its listeners and closes the AudioContext.
audio.ctx, audio.master
The AudioContext and master GainNode, for your own routing.

Good to know

  • Browsers start audio suspended; the context resumes on the first pointer or key press, so the first sound should follow a player action.

Versions

Every version stays available, so a prototype keeps working exactly as published. Declare the one you tested with.

  • prefab-audio@0.1.1
  • prefab-audio@0.1.0

As JSON: /modules/prefab-audio.json. Source: prototir.