import type { GameMoment, SoundContext, SoundCue, SoundPeriod, SoundVenue } from "./sound.mix.ts"; import type { SoundLayer, SoundLevel, SoundVoice } from "./sound.sets.ts"; import type { ReplayController } from "./replay.control.ts"; import type { ReplayEvent } from "./replay.types.ts"; /** The recordings a page may give, by the layer or the voice they stand for; anything not given is made in the browser. */ export type SoundFileName = SoundLayer | SoundVoice; /** What the engine needs of an audio context: a page's `AudioContext`, or a stand-in in a test. */ export type SoundAudioContext = BaseAudioContext & { resume(): Promise; suspend(): Promise; }; /** How the engine fetches a recording: `fetch` by default. */ export type SoundFetch = (url: string) => Promise<{ ok: boolean; arrayBuffer(): Promise; }>; /** A crowd level, or `auto`: the level follows the game a sound follows (`excitementAt`), and is `engaged` with no game. */ export type SoundLevelChoice = SoundLevel | "auto"; /** The settings of a surface's sound. */ export interface SoundSettings { /** The surface (`nhl`, `soccer`, ...). A surface with no sound set makes no sound and offers no levels (`state().has`). */ sport: string; venue?: SoundVenue; /** The share of the seats taken, 0 to 1: 0 is no crowd (a page's Crowd off). */ crowd?: number; view?: "2d" | "3d"; /** The crowd's level: `engaged` by default. */ level?: SoundLevelChoice; /** 0 to 1: 0.6 by default. 0 is muted, and nothing is heard or made. */ volume?: number; /** Recordings by name, for the voices and layers a page has files for (the demo's: `demo/sounds/`). Fetched only when the sound is first turned on, and decoded only when it is heard. */ files?: Partial>; /** Makes the page's one audio context: `new AudioContext()` by default. A test gives a stand-in. */ audio?: () => SoundAudioContext; fetch?: SoundFetch; /** The element the surface is drawn in: the sound stops while it is out of sight. */ host?: Element; /** The page, for whether it is hidden: `document` by default. */ doc?: Document; /** Told of every cue as it plays: a page may show it. */ onCue?: (cue: SoundCue) => void; } /** Why a sound is not heard. */ export type SoundQuiet = "no-sound" | "off" | "muted" | "hidden" | "out-of-sight" | "paused"; /** What a sound is doing. */ export interface SoundState { /** The surface has a sound set: a page shows the sound's controls only when it has. */ has: boolean; on: boolean; /** Heard now: on, with volume, the page shown, the surface in sight and its game not paused. */ audible: boolean; /** Why it is not heard, when it is not. */ quiet?: SoundQuiet; sport: string; volume: number; level: SoundLevelChoice; /** The level heard: the choice, the game's under `auto`, held to what the surface offers. */ heard: SoundLevel; /** The levels the surface offers, quietest first. */ levels: readonly SoundLevel[]; /** The voices its cues use. */ voices: readonly SoundVoice[]; /** The recordings fetched (they are decoded when heard). */ fetched: readonly SoundFileName[]; /** Following a replay. */ following: boolean; } /** Following a replay: its periods and its score at a time, for the cues of the periods' ends and for the level under `auto`. */ export interface SoundFollow { periods?: readonly SoundPeriod[]; scoreAt?: (t: number) => { home: number; away: number; }; } /** A surface's sound in a page. */ export interface KyogijoSound { /** Turn it on: call it from a press, so that the browser lets the audio start. The first call makes the audio context and fetches the recordings. */ enable(): Promise; /** Turn it off: nothing is heard, made or decoded, and the audio context is suspended when no other sound of the page is heard. */ disable(): void; setVolume(volume: number): void; setLevel(level: SoundLevelChoice): void; /** Change the context: another surface, venue, crowd or view. The levels and cues offered follow the surface. */ setContext(context: Partial): void; /** Follow a replay's controller: the cues of its events as they go by while it plays, quiet while it is paused, the level under `auto` from its game. Returns a function that stops it. */ follow(controller: ReplayController, game?: SoundFollow): () => void; /** A live event, pushed as it happens: its cues play now. A goal lifts the level under `auto` for a few seconds. */ cue(event: Pick): void; /** The live game's state for the level under `auto`, or `undefined` for none. */ game(moment: GameMoment | undefined): void; state(): SoundState; /** Stop and take everything away. */ destroy(): void; } /** How long the sound is held on after a game's clock stopped on a cue (its end), in seconds. */ export declare const SOUND_TAIL = 5; /** * The sound of a surface: its crowd bed at a level and the cues of its game. Nothing happens until `enable()`, which a page calls from a press. * @example * ```ts no-run * import { createKyogijoSound } from "@johnmorrisdotca/kyogijo-stadiums/sound"; * import { mountKyogijoTurn } from "@johnmorrisdotca/kyogijo-stadiums/turn"; * * const host = document.querySelector("#rink")!; * const sound = createKyogijoSound({ sport: "nhl", venue: { roof: "fixed", seats: 18000 }, view: "3d", level: "auto", host }); * document.querySelector("#sound")!.addEventListener("click", () => sound.enable()); * const view = mountKyogijoTurn(host, { sport: "nhl" }); * const replay = view.replay({ frames: [{ t: 0, objects: [] }, { t: 60, objects: [] }], events: [{ t: 30, kind: "goal", team: "home" }] }); * sound.follow(replay, { periods: [{ start: 0, end: 60 }] }); * ``` */ export declare function createKyogijoSound(settings: SoundSettings): KyogijoSound;