// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // what sounds play on this client, and where from: ctx.sounds.play for client phases. the sounds are the mods' // sounds.json, see docs/modding/resources/sounds.md. this decides what plays, an output (client/audio_output.ts in the // browser) plays it import { EverythingRegistry } from "@/common/everything_registry.ts"; import { sound_of, sound_range, sound_to_play, type SoundCategory, type SoundJson } from "@/common/mod_data.ts"; import type { ClientPlayOptions, PlayingSound } from "@/common/mod_api/client.ts"; import type { Position } from "@/common/mod_api/shared.ts"; export type Volumes = Record<"master" | SoundCategory, number>; // sounds playing at once at most. past that a new one is dropped, and loops wait for room const MAX_VOICES = 64; // what sounds come out of: web audio in the browser, a list in tests export interface SoundOutput { // starts a file playing. undefined when nothing can play now, like before the player's first click start(file: SoundFile, settings: VoiceSettings): Voice | undefined; // where the player hears from, and which way they look, in radians like the camera listen(x: number, y: number, z: number, yaw: number, pitch: number): void; set_volumes(volumes: Volumes): void; } // a mod's file, by ":" export interface SoundFile { readonly id: string; readonly bytes: Uint8Array; } export interface VoiceSettings { // only master's volume when not given category: SoundCategory | undefined; // heard as it is when not given at: Position | undefined; // where it's faded to nothing range: number; // over 1 is heard further, which range has already, not louder volume: number; pitch: number; loop: boolean; // seconds into the file it starts at, for one played with an offset or a loop coming back partway. past the file's // length a loop goes around, and anything else has finished offset: number; } // one file playing export interface Voice { // it played to its end readonly ended: boolean; move(at: Position): void; set(volume: number, pitch: number, range: number): void; stop(): void; } // where the player hears from, like the camera export interface Listener extends Position { yaw: number; pitch: number; } class Playing implements PlayingSound { voice: Voice | undefined; done = false; #volume: number; #pitch: number; constructor( readonly json: SoundJson, readonly file: SoundFile, readonly options: ClientPlayOptions, readonly started: number, public at: Position | undefined, ) { this.#volume = options.volume ?? 1; this.#pitch = options.pitch ?? 1; } get playing() { return !this.done; } stop() { this.done = true; this.voice?.stop(); this.voice = undefined; } get volume() { return this.#volume; } set volume(volume: number) { this.#volume = volume; this.voice?.set(this.total_volume, this.total_pitch, this.range); } get pitch() { return this.#pitch; } set pitch(pitch: number) { this.#pitch = pitch; this.voice?.set(this.total_volume, this.total_pitch, this.range); } get total_volume() { return (this.json.volume ?? 1) * this.#volume; } get total_pitch() { return (this.json.pitch ?? 1) * this.#pitch; } get range() { return sound_range(this.json, this.#volume); } // whether the listener is close enough to hear it, always for one heard as it is in_range(listener: Position) { if (!this.at) return true; const { x, y, z } = this.at; return Math.hypot(x - listener.x, y - listener.y, z - listener.z) < this.range; } } export class Sounds { #output: SoundOutput | undefined; // the mods' files, by ":" #files = new Map(); #playing: Playing[] = []; #listener: Position = { x: 0, y: 0, z: 0 }; #entity_position: (id: string) => Position | undefined = () => undefined; // seconds, for how far a loop has got constructor(readonly clock = () => performance.now() / 1000) {} set_output(output: SoundOutput) { this.#output = output; } // the loaded mods' sound files, in place of the last server's set_files(mods: { id: string; sounds?: Map }[]) { this.stop_all(); this.#files.clear(); for (const mod of mods) { for (const [path, bytes] of mod.sounds ?? []) { const id = `${mod.id}:${path}`; this.#files.set(id, { id, bytes }); } } } exists(sound: string) { return EverythingRegistry.get("sounds", sound) !== undefined; } play(sound: string, options: ClientPlayOptions = {}): PlayingSound { const json = sound_to_play(sound, options); const offset = options.offset ?? 0; if (typeof offset !== "number" || !(offset >= 0) || !Number.isFinite(offset)) { throw new Error("a sound's offset must be 0 or more seconds"); } const path = json.files[Math.floor(Math.random() * json.files.length)]; const file = this.#files.get(`${sound.split(":")[0]}:${path}`); if (!file) throw new Error(`sound ${sound} has no file ${path}`); const at = options.follow !== undefined ? this.#entity_position(options.follow) : options.at; const playing = new Playing(json, file, { ...options }, this.clock() - offset, at && { ...at }); this.#playing.push(playing); // an entity this client doesn't have yet, like one whose animator is being made, has until the next frame to // turn up if (options.follow !== undefined && !at) return playing; this.#start(playing); if (!playing.voice && !options.loop) playing.done = true; return playing; } // the sound a block's or entity's "sounds" prefix has for kind, when it has one, see BLOCK_SOUNDS and ENTITY_SOUNDS play_of(prefix: string | undefined, kind: string, options: ClientPlayOptions) { const sound = sound_of(prefix, kind); if (sound) this.play(sound, options); } // before each frame: the player hears from listener, sounds following an entity go where it's drawn frame(listener: Listener, entity_position: (id: string) => Position | undefined, volumes: Volumes) { this.#listener = { x: listener.x, y: listener.y, z: listener.z }; this.#entity_position = entity_position; this.#output?.listen(listener.x, listener.y, listener.z, listener.yaw, listener.pitch); this.#output?.set_volumes(volumes); for (const playing of this.#playing) { if (playing.done) continue; const follow = playing.options.follow; if (follow !== undefined) { const at = entity_position(follow); if (!at) { playing.stop(); continue; } playing.at = { x: at.x, y: at.y, z: at.z }; playing.voice?.move(playing.at); } if (playing.voice?.ended) { playing.voice = undefined; if (!playing.options.loop) playing.done = true; } // loops out of range wait without a voice if (playing.voice && playing.options.loop && !playing.in_range(this.#listener)) { playing.voice.stop(); playing.voice = undefined; } } this.#playing = this.#playing.filter((playing) => !playing.done); for (const playing of this.#playing) { if (playing.voice) continue; this.#start(playing); // one waiting for its entity that can't play now never will if (!playing.voice && !playing.options.loop) playing.done = true; } } // leaving the server stop_all() { for (const playing of this.#playing) playing.stop(); this.#playing = []; } // how many are playing now, for tests get voices() { return this.#playing.filter((playing) => playing.voice).length; } #start(playing: Playing) { if (!this.#output || !playing.in_range(this.#listener) || this.voices >= MAX_VOICES) return; playing.voice = this.#output.start(playing.file, { category: playing.json.category, at: playing.at, range: playing.range, volume: playing.total_volume, pitch: playing.total_pitch, loop: playing.options.loop ?? false, offset: (this.clock() - playing.started) * playing.total_pitch, }); } } // the client's, which ctx.sounds plays on export const sounds = new Sounds();