Building blocks for audio games that run in a browser. It currently has helpers for accessible speech, keyboard, mouse and multifinger input, spatial audio, menus and forms, maps and spatial queries, storage, platform detection, and multiplayer connections.
The package publishes ECMAScript modules and requires no build step. Its only runtime dependency is Cacophony for sound playback.
Local development requires Node.js (and npm) or Deno 2+.
Install from npm:
npm install audiogame-utilsOr from JSR:
deno add jsr:@cartertemm/audiogame-utilsImport common tools from the package root or use a public module path for more focused imports:
import { createStorage, createSpeech } from 'audiogame-utils'
import { createAudio } from 'audiogame-utils/audio'
const storage = createStorage('my-game')
const speech = createSpeech({ storage })
const audio = createAudio()
speech.init()
const startSound = audio.sfx('/sounds/start.ogg')In Deno 2 or later, import from JSR:
import { createStorage, createSpeech } from 'jsr:@cartertemm/audiogame-utils'
import { createAudio } from 'jsr:@cartertemm/audiogame-utils/audio'The npm: specifier also works. Deno resolves the subpaths from the package.json exports map:
import { createStorage, createSpeech } from 'npm:audiogame-utils'
import { createAudio } from 'npm:audiogame-utils/audio'The package ships hand written declaration files next to the sources, so no separate @types install is needed. Node and bundler tools read them through the types condition in the package.json exports map. Deno reads the same files through a @ts-self-types comment on each module, which covers both the npm: specifier and a direct source import.
import { createClock } from 'audiogame-utils/clock'
const clock = createClock({ fps: 60 })
clock.on((dt: number) => { /* dt is a number of seconds */ })See examples/ for more of what you can do.
| Module | Import path | Purpose |
|---|---|---|
| Package root | audiogame-utils |
Common exports from across the package. |
| Audio | audiogame-utils/audio |
Lazy sound loading and playback through Cacophony. |
| Audio pool | audiogame-utils/audio/pool |
Reusable spatial sound sources and listener updates. |
| Audio coordinates | audiogame-utils/audio/coords |
Game coordinate and listener relative audio positions. |
| Audio units | audiogame-utils/audio/units |
Volume, pan, pitch, playback rate, and distance conversions. |
| Buffers | audiogame-utils/buffers |
Reviewable event logs with speech navigation and optional visible UI. |
| Clock | audiogame-utils/clock |
60 FPS game loop timing, delta time calculations, and countdown timers. |
| Events | audiogame-utils/events |
A small event emitter. |
| Focus | audiogame-utils/focus |
Keyboard trapping for gameplay using a virtual cursor. |
| Input | audiogame-utils/input |
Keyboard, mouse, touch gesture, and named action input. |
| Maps | audiogame-utils/map |
Representation of spatial data, map loading, queries, and serialization. Able to load maps with hundreds of thousands of objects in less than a second. |
| Math | audiogame-utils/math |
Range, angle, interpolation, and randomization helpers. |
| Networking | audiogame-utils/net |
WebSocket messaging, reconnection, and player identity. |
| Networking server | audiogame-utils/net/server |
Authoritative WebSocket server sessions, rooms, and messaging. |
| Networking testing | audiogame-utils/net/testing |
In memory socket pairs for networking tests. |
| Physics | audiogame-utils/physics |
Three dimensional coordinate vectors and static R tree spatial indexing. |
| Platform detection | audiogame-utils/platform |
Runtime, operating system, and installed web app detection. |
| Runtime | audiogame-utils/runtime |
Loads native adapters when the game runs under Tauri. |
| Tauri adapters | audiogame-utils/tauri |
Direct setup and construction of native Tauri adapters. |
| Window | audiogame-utils/window |
Title, fullscreen, quit, close guard, keep awake, and external links. |
| Rotation | audiogame-utils/rotation |
Direction, movement, distance, and relative position helpers. |
| Speech | audiogame-utils/speech |
Accessible output through native screen readers, live regions, and text to speech. Sets sensible platform defaults (TTS on iOS, screen reader everywhere else). |
| Stats | audiogame-utils/stats |
Stat tracking, formatted output, list sorting, set operations, and linear/JSON serialization. |
| Storage | audiogame-utils/storage |
Namespaced JSON storage. Saves to the browser session by default. |
| Text | audiogame-utils/text |
sequence to string conversion, time formatting, string distance, and closestMatch helpers. |
| UI | audiogame-utils/ui |
Helpers for quickly creating accessible fields, screens, and menus. With the exception of menu (which is my take on the dynamic menu commonly found in audio games), these should not be used with a focus trap but games where you want an interactive visible UI. |
A game written with this library can run seamlessly as a Windows, macOS, Linux, iOS, or Android application through Tauri, using the same code as the browser version.
npm create tauri-app@latest
cd my-game
npx audiogame-utils createThen await initRuntime() at the top of the game entry:
import { initRuntime } from 'audiogame-utils/runtime'
await initRuntime()Initializing the runtime makes sure that saves are written to disk, window control becomes available, and the paths to audio and map files resolve correctly based on the platform and environment configuration. Everything else stays exactly the same. See the Tauri guide for a full tutorial.
Clone your fork and open the repository directory. Then install the dependencies and run the test suite:
npm install
npm testOr run tests under Deno using the Deno task:
deno task test:denoThe tests use Vitest with happy-dom. Run npm run test:watch while developing to rerun tests when files change.
Type declarations have their own checks. npm run test:types runs tsc over the declaration files and the type usage tests. deno task check runs the same files through Deno, which also proves each @ts-self-types comment resolves.
Bug reports and pull requests are welcome. Open an issue before starting a substantial change so the approach can be discussed first.
Keep changes focused, add or update tests for changed behavior, and run npm test before submitting a pull request. Include a clear description of the problem and the solution in the pull request.
This project is available under the MIT License.