import { Signal } from './signal.js'; import type { Game } from './game.js'; import type { Tween } from './tween.js'; /** A named span within a sound file. */ export type SoundMarker = { name: string; start: number; stop: number; volume: number; duration: number; durationMS: number; loop: boolean; }; /** The pre-standard Web Audio names, still the only ones some older engines expose. */ type LegacyAudioContext = { createGainNode: () => GainNode }; /** The pre-standard source-node names, alongside the playback position those engines reported. */ type LegacyBufferSource = { noteOff: (when: number) => void; noteGrainOn: (when: number, offset: number, duration: number) => void; currentTime: number; }; export class Sound { public _paused!: boolean; public game!: Game; public name!: string; public key!: string; public loop!: boolean; public markers!: Record; public context!: AudioContext | null; public autoplay!: boolean; public totalDuration!: number; public startTime!: number; public currentTime!: number; public duration!: number; public durationMS!: number; public position!: number; public stopTime!: number; public paused!: boolean; public pausedPosition!: number; public pausedTime!: number; public isPlaying!: boolean; public currentMarker!: string; public fadeTween!: Tween | null; public pendingPlayback!: boolean; public override!: boolean; public allowMultiple!: boolean; public externalNode!: AudioNode | null; public masterGainNode!: GainNode | null; public gainNode!: GainNode; public _sound!: AudioBufferSourceNode; public _markedToDelete!: boolean; public _removeFromSoundManager!: boolean; public onPlay!: Signal; public onPause!: Signal; public onResume!: Signal; public onLoop!: Signal; public onStop!: Signal; public onMute!: Signal; public onMarkerComplete!: Signal; public onFadeComplete!: Signal; public _volume!: number; public _buffer!: AudioBuffer | null; public _muted!: boolean; public _tempMarker!: string; public _tempPosition!: number; public _tempVolume!: number; public _tempPause!: number; public _muteVolume!: number; public _tempLoop!: boolean; /** * Creates a new Sound instance. * @param {Game} game - Reference to the Phaser Game instance. * @param {string} key - The key of the sound to play. * @param {number} volume - The initial volume level (0.0 to 1.0). * @param {boolean} loop - Whether the sound should loop. * @param {boolean} connect - Whether to connect to the master gain node. */ public constructor(game: Game, key: string, volume = 1, loop = false, connect: boolean | null = null) { // https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Migrating_from_webkitAudioContext const connectToMaster = connect ?? game.sound.connectToMaster; this.game = game; this.name = key; this.key = key; this.loop = loop; this.markers = {}; this.autoplay = false; this.totalDuration = 0; this.startTime = 0; this.currentTime = 0; this.duration = 0; this.durationMS = 0; this.position = 0; this.stopTime = 0; this.paused = false; this.pausedPosition = 0; this.pausedTime = 0; this.isPlaying = false; this.currentMarker = ''; this.fadeTween = null; this.pendingPlayback = false; this.override = false; this.allowMultiple = false; /** @type {GainNode} */ this.externalNode = null; this._markedToDelete = false; this._removeFromSoundManager = false; this.context = this.game.sound.context; this.masterGainNode = this.game.sound.masterGain; const gainNode: GainNode = this.context!.createGain === undefined ? (this.context as unknown as LegacyAudioContext).createGainNode() : this.context!.createGain(); this.gainNode = gainNode; gainNode.gain.value = volume * this.game.sound.volume; if (connectToMaster && this.masterGainNode !== null) { gainNode.connect(this.masterGainNode); } this.onPlay = new Signal(); this.onPause = new Signal(); this.onResume = new Signal(); this.onLoop = new Signal(); this.onStop = new Signal(); this.onMute = new Signal(); this.onMarkerComplete = new Signal(); this.onFadeComplete = new Signal(); this._volume = volume; /** @type {AudioBuffer} */ this._buffer = null; this._muted = false; this._tempMarker = ''; this._tempPosition = 0; this._tempVolume = 0; this._tempPause = 0; this._muteVolume = 0; this._tempLoop = false; this._paused = false; } /** * Callback when a sound has been unlocked. * @param {string} key - The key of the sound that was unlocked. */ public soundHasUnlocked(key: string): void { if (key === this.key) { // Unlocking parks the decoded buffer in _sound; play() replaces it with a real source node. const buffer = this.game.cache.getSoundData(this.key) as AudioBuffer; this._sound = buffer as unknown as AudioBufferSourceNode; this.totalDuration = buffer.duration; } } /** * Adds a marker to the sound. * @param {string} name - The name of the marker. * @param {number} start - The start time (in seconds) of the marker. * @param {number} duration - The duration (in seconds) of the marker. * @param {number} volume - The volume level (0.0 to 1.0) of the marker. * @param {boolean} loop - Whether the marker should loop. */ public addMarker(name: string, start: number, duration = 1, volume = 1, loop = false): void { this.markers[name] = { name, start, stop: start + duration, volume, duration, durationMS: duration * 1000, loop, }; } /** * Removes a marker from the sound. * @param {string} name - The name of the marker to remove. */ public removeMarker(name: string): void { delete this.markers[name]; } /** * Handler for when the sound ends. */ public onEndedHandler = (): void => { this.isPlaying = false; this.currentTime = this.durationMS; this.stop(); if (!this._markedToDelete) { return; } if (this.externalNode) { this._sound.disconnect(this.externalNode); } else if (this.gainNode as GainNode | undefined) { this._sound.disconnect(this.gainNode); } if (this._removeFromSoundManager) { this.game.sound.remove(this); } else { this.markers = {}; this.context = null!; this._buffer = null; this.externalNode = null; this.onPlay.dispose(); this.onPause.dispose(); this.onResume.dispose(); this.onLoop.dispose(); this.onStop.dispose(); this.onMute.dispose(); this.onMarkerComplete.dispose(); this.onFadeComplete.dispose(); } }; /** * Updates the sound state. */ public update(): void { if (!this.game.cache.checkSoundKey(this.key)) { this.destroy(); return; } if (this.pendingPlayback && this.game.cache.isSoundReady(this.key)) { this.pendingPlayback = false; this.play(this._tempMarker, this._tempPosition, this._tempVolume, this._tempLoop); } if (!this.isPlaying) { return; } this.currentTime = this.game.time.time - this.startTime; if (this.currentTime >= this.durationMS) { if (this.loop) { // Won't work with markers, needs to reset the position this.onLoop.dispatch(this); // Gets reset by the play function this.isPlaying = false; if (this.currentMarker === '') { this.currentTime = 0; this.startTime = this.game.time.time; this.isPlaying = true; // Play not called again in this case } else { this.onMarkerComplete.dispatch(this.currentMarker, this); this.play(this.currentMarker, 0, this.volume, true, true); } } else if (this.currentMarker !== '') { // Stop if we're using an audio marker, otherwise we let onended handle it this.stop(); } } } /** * Plays the sound at full volume in a loop. * @param {number} volume - The volume level (0.0 to 1.0) to play at. * @returns {Sound} This Sound instance for chaining. */ public loopFull(volume: number): this { return this.play(null, 0, volume, true); } /** * Plays the sound, optionally using a marker. * @param {string} marker - The name of the marker to play, or null/undefined to play the entire sound. * @param {number} position - The position (in seconds) to start playing from. * @param {number} volume - The volume level (0.0 to 1.0) to play at. * @param {boolean} loop - Whether the sound should loop. * @param {boolean} forceRestart - Whether to force restarting the sound even if it's already playing. * @returns {Sound} This Sound instance for chaining. */ public play(marker: string | false | null = '', position = 0, volume = 1, loop = false, forceRestart = true): this { const markerName = marker === undefined || marker === false || marker === null ? '' : marker; if (this.isPlaying && !this.allowMultiple && !forceRestart && !this.override) { // Use Restart instead return this; } const source = this._sound as AudioBufferSourceNode | undefined; if (source && this.isPlaying && !this.allowMultiple && (this.override || forceRestart)) { if (this._sound.stop === undefined) { (this._sound as unknown as LegacyBufferSource).noteOff(0); } else { this._sound.stop(0); } if (this.externalNode) { this._sound.disconnect(this.externalNode); } else if (this.gainNode as GainNode | undefined) { this._sound.disconnect(this.gainNode); } this.isPlaying = false; } if (markerName === '' && Object.keys(this.markers).length > 0) { // If they didn't specify a marker but this is an audio sprite, // We should never play the entire thing return this; } const markerData = markerName === '' ? undefined : this.markers[markerName]; if (markerName !== '') { if (markerData) { this.currentMarker = markerName; // Playing a marker? Then we default to the marker values this.position = markerData.start; this.volume = markerData.volume; this.loop = markerData.loop; this.duration = markerData.duration; this.durationMS = markerData.durationMS; if (volume !== undefined) { this.volume = volume; } if (loop !== undefined) { this.loop = loop; } this._tempMarker = markerName; this._tempPosition = this.position; this._tempVolume = this.volume; this._tempLoop = this.loop; } else { this.game.logger.warn(`Sound.play: audio marker ${markerName} does not exist`); return this; } } else { this.position = Math.max(0, position ?? 0); this.volume = volume ?? this._volume; this.loop = loop ?? this.loop; this.duration = 0; this.durationMS = 0; this._tempMarker = markerName; this._tempPosition = position; this._tempVolume = volume; this._tempLoop = loop; } // Does the sound need decoding? if (this.game.cache.isSoundDecoded(this.key) === true) { this._sound = this.context!.createBufferSource(); if (this.externalNode) { this._sound.connect(this.externalNode); } else { this._sound.connect(this.gainNode); } this._buffer = this.game.cache.getSoundData(this.key) as AudioBuffer | null; this._sound.buffer = this._buffer; if (this.loop && markerName === '') { this._sound.loop = true; } if (!this.loop && markerName === '') { this._sound.addEventListener('ended', this.onEndedHandler, { once: true }); } this.totalDuration = this._sound.buffer?.duration ?? 0; if (this.duration === 0) { this.duration = this.totalDuration; this.durationMS = Math.ceil(this.totalDuration * 1000); } // Useful to cache this somewhere perhaps? if (this._sound.start === undefined) { (this._sound as unknown as LegacyBufferSource).noteGrainOn(0, this.position, this.duration); } else if (this.loop && markerName === '') { this._sound.start(0, 0); } else { this._sound.start(0, this.position, this.duration); } this.isPlaying = true; this.startTime = this.game.time.time; this.currentTime = 0; this.stopTime = this.startTime + this.durationMS; this.onPlay.dispatch(this); } else { this.pendingPlayback = true; if (this.game.cache.getSound(this.key)?.isDecoding === false) { void this.game.sound.decode(this.key); } } return this; } /** * Restarts the sound, optionally using a marker. * @param {string} marker - The name of the marker to play, or empty string to play the entire sound. * @param {number} position - The position (in seconds) to start playing from. * @param {number} volume - The volume level (0.0 to 1.0) to play at. * @param {boolean} loop - Whether the sound should loop. */ public restart(marker = '', position = 0, volume = 1, loop = false): void { this.play(marker, position, volume, loop, true); } /** * Pauses the sound. */ public pause(): void { if (this.isPlaying && (this._sound as AudioBufferSourceNode | undefined)) { this.paused = true; this.pausedPosition = this.currentTime; this.pausedTime = this.game.time.time; this._tempPause = (this._sound as unknown as LegacyBufferSource).currentTime; this.onPause.dispatch(this); this.stop(); } } /** * Resumes the sound. */ public resume(): void { if (this.paused && (this._sound as AudioBufferSourceNode | undefined)) { const p = Math.max(0, this.position + this.pausedPosition / 1000); this._sound = this.context!.createBufferSource(); this._sound.buffer = this._buffer; if (this.externalNode) { this._sound.connect(this.externalNode); } else { this._sound.connect(this.gainNode); } if (this.loop) { this._sound.loop = true; } if (!this.loop && this.currentMarker === '') { this._sound.addEventListener('ended', this.onEndedHandler, { once: true }); } const duration = this.duration - this.pausedPosition / 1000; if (this._sound.start === undefined) { (this._sound as unknown as LegacyBufferSource).noteGrainOn(0, p, duration); } else { this._sound.start(0, p, duration); } this.isPlaying = true; this.paused = false; this.startTime += this.game.time.time - this.pausedTime; this.onResume.dispatch(this); } } /** * Stops the sound. */ public stop(): void { if (this.isPlaying && (this._sound as AudioBufferSourceNode | undefined)) { if (this._sound.stop === undefined) { (this._sound as unknown as LegacyBufferSource).noteOff(0); } else { this._sound.stop(0); } if (this.externalNode) { this._sound.disconnect(this.externalNode); } else if (this.gainNode as GainNode | undefined) { this._sound.disconnect(this.gainNode); } } this.pendingPlayback = false; this.isPlaying = false; if (!this.paused) { const prevMarker = this.currentMarker; if (this.currentMarker !== '') { this.onMarkerComplete.dispatch(this.currentMarker, this); } this.currentMarker = ''; if (this.fadeTween !== null) { this.fadeTween.stop(); } this.onStop.dispatch(this, prevMarker); } } /** * Fades in the sound to full volume. * @param {number} duration - The fade-in duration (in milliseconds). * @param {boolean} loop - Whether the sound should loop. * @param {string} marker - The name of the marker to fade in, or empty string to use current marker. */ public fadeIn(duration: number, loop = false, marker: string = this.currentMarker): void { if (this.paused) { return; } this.play(marker, 0, 0, loop); this.fadeTo(duration, 1); } /** * Fades out the sound to silence. * @param {number} duration - The fade-out duration (in milliseconds). */ public fadeOut(duration: number): void { this.fadeTo(duration, 0); } /** * Fades the sound to a specific volume level. * @param {number} duration - The fade duration (in milliseconds). * @param {number} volume - The target volume level (0.0 to 1.0). */ public fadeTo(duration = 100, volume = 0): void { if (!this.isPlaying || this.paused || volume === this.volume) { return; } this.fadeTween = this.game.tweens.create(this).to({ volume }, duration, 'Linear', true); this.fadeTween.onComplete.addOnce(this.fadeComplete, this); } /** * Callback when fade completes. */ public fadeComplete(): void { this.onFadeComplete.dispatch(this, this.volume); if (this.volume === 0) { this.stop(); } } /** * Destroys the sound and cleans up resources. * @param {boolean} remove - Whether to remove the sound from the SoundManager. */ public destroy(remove = true): void { this._markedToDelete = true; this._removeFromSoundManager = remove; this.stop(); if (remove) { this.game.sound.remove(this); } else { this.markers = {}; this.context = null!; this._buffer = null; this.externalNode = null; this.onPlay.dispose(); this.onPause.dispose(); this.onResume.dispose(); this.onLoop.dispose(); this.onStop.dispose(); this.onMute.dispose(); this.onMarkerComplete.dispose(); this.onFadeComplete.dispose(); } } /** * Gets the muted state of the sound. * @returns {boolean} True if the sound is muted, false otherwise. */ public get mute(): boolean | null { return this._muted || this.game.sound.mute; } /** * Sets the muted state of the sound. */ public set mute(value: boolean | null) { const muted = value ?? false; if (muted === this._muted) { return; } if (muted) { this._muted = true; this._muteVolume = this._tempVolume; this.gainNode.gain.value = 0; } else { this._muted = false; this.gainNode.gain.value = this._muteVolume; } this.onMute.dispatch(this); } /** * Gets the volume of the sound. * @returns {number} The volume level (0.0 to 1.0). */ public get volume(): number { return this._volume; } /** * Sets the volume of the sound. */ public set volume(value: number) { if (this._muted) { this._muteVolume = value; return; } this._tempVolume = value; this._volume = value; this.gainNode.gain.value = value; } }