Sounds on the client
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
// 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 type { SoundCategory, 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>;
|
||||
|
||||
// how far away a sound is heard at volume 1 when its definition doesn't say
|
||||
const DEFAULT_RANGE = 16;
|
||||
// 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 "<mod id>:<path in its sounds/>"
|
||||
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 a loop coming back partway. past the file's length it goes around
|
||||
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;
|
||||
}
|
||||
|
||||
// a louder sound is heard further, like a sound at volume 2 twice as far
|
||||
get range() {
|
||||
return (this.json.range ?? DEFAULT_RANGE) * Math.max(1, this.total_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 "<mod id>:<path>"
|
||||
#files = new Map<string, SoundFile>();
|
||||
#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<string, Uint8Array> }[]) {
|
||||
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<SoundJson>("sounds", sound) !== undefined;
|
||||
}
|
||||
|
||||
play(sound: string, options: ClientPlayOptions = {}): PlayingSound {
|
||||
const json = EverythingRegistry.get<SoundJson>("sounds", sound);
|
||||
if (!json) throw new Error(`unknown sound ${sound}`);
|
||||
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(), at && { ...at });
|
||||
// an entity this client doesn't have can't be followed
|
||||
if (options.follow !== undefined && !at) {
|
||||
playing.done = true;
|
||||
return playing;
|
||||
}
|
||||
this.#playing.push(playing);
|
||||
this.#start(playing);
|
||||
if (!playing.voice && !options.loop) playing.done = true;
|
||||
return playing;
|
||||
}
|
||||
|
||||
// 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) this.#start(playing);
|
||||
}
|
||||
}
|
||||
|
||||
// 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();
|
||||
Reference in New Issue
Block a user