Skip to main content

SoundState

The SoundStateInfo interface provides complete state information about a sound at a given moment. Use getSoundState() to retrieve the current state of any loaded sound.

export interface SoundStateInfo {
id: string; // The sound ID
instanceId?: string; // Instance ID for multi-instance sounds
isPlaying: boolean; // Whether the sound is currently playing
isPaused: boolean; // Whether the sound is currently paused
isStopped: boolean; // Whether the sound is currently stopped
isMuted: boolean; // Whether the sound is muted
isLoaded: boolean; // Whether the sound buffer is loaded
isSpatialAudioActive: boolean; // Whether spatial audio is active on this sound
isStereoPanActive: boolean; // Whether stereo panning is active on this sound
currentTime: number; // Current playback position in seconds
duration: number; // Total duration in seconds (0 if not loaded)
volume: number; // Current volume (0 to 1)
playbackRate: number; // Current playback rate (0.5 to 4)
pan: number; // Current stereo pan (-1 to 1)
loop: boolean; // Whether the sound is looping
maxLoops: number; // Maximum number of loops (-1 for infinite)
groupId?: string; // The group this sound belongs to (if any)
spatialPosition?: { x: number; y: number; z: number }; // 3D spatial position
pannerConfig?: SoundPannerConfig; // Panner configuration
}

Getting Sound State

const mySoundManager = new SoundManager();

// Load and play a sound
await mySoundManager.loadSound('piano', '/audio/piano.mp3');
mySoundManager.play('piano', { volume: 0.7, pan: -0.5 });

// Get the current state
const state = mySoundManager.getSoundState('piano');
console.log('Is playing:', state.isPlaying); // true
console.log('Volume:', state.volume); // 0.7
console.log('Pan:', state.pan); // -0.5
console.log('Current time:', state.currentTime);
console.log('Duration:', state.duration);

Checking State with Helper Methods

The Sound Manager provides dedicated methods for checking specific state aspects:

const mySoundManager = new SoundManager();
await mySoundManager.loadSound('sfx', '/audio/sfx.mp3');

// Check if a sound is playing
if (mySoundManager.isPlaying('sfx')) {
console.log('Sound is currently playing');
}

// Check if a sound is paused
if (mySoundManager.isPaused('sfx')) {
console.log('Sound is paused');
}

// Check if a sound is stopped
if (mySoundManager.isStopped('sfx')) {
console.log('Sound is stopped');
}

// Check if a sound is loaded
if (mySoundManager.isSoundLoaded('sfx')) {
console.log('Sound buffer is loaded');
}

Using SoundState with Multiple Instances

When using createNewInstance: true, each instance has its own state:

const mySoundManager = new SoundManager();
await mySoundManager.loadSound('laser', '/audio/laser.mp3');

// Play multiple instances
mySoundManager.play('laser', {
createNewInstance: true,
volume: 0.5
});
mySoundManager.play('laser', {
createNewInstance: true,
volume: 1.0,
pan: 0.8
});

// Check state for a specific instance
const allSounds = mySoundManager.getSoundIds();
allSounds.forEach(id => {
const state = mySoundManager.getSoundState(id);
console.log(`${id}: playing=${state.isPlaying}, volume=${state.volume}`);
});

Reacting to State Changes with Events

const mySoundManager = new SoundManager();
await mySoundManager.loadSound('ambient', '/audio/ambient.mp3');

// Listen for state changes
mySoundManager.addEventListener(SoundEventsEnum.PLAYBACK_RATE_CHANGED, (event) => {
const state = mySoundManager.getSoundState(event.soundId!);
console.log('New playback rate:', state.playbackRate);
});

mySoundManager.addEventListener(SoundEventsEnum.VOLUME_CHANGED, (event) => {
const state = mySoundManager.getSoundState(event.soundId!);
console.log('New volume:', state.volume);
});