# SoundSlot

Class · extends [`EventHandler`](https://api.playcanvas.com/engine/classes/EventHandler.md) · category: Sound

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/framework/components/sound/slot.js#L54

The SoundSlot controls the playback of [SoundInstance](https://api.playcanvas.com/engine/classes/SoundInstance.md)s. SoundSlots are managed by
[SoundComponent](https://api.playcanvas.com/engine/classes/SoundComponent.md)s. To add and remove SoundSlots on a SoundComponent, use
[SoundComponent#addSlot](https://api.playcanvas.com/engine/classes/SoundComponent.md#addslot) and [SoundComponent#removeSlot](https://api.playcanvas.com/engine/classes/SoundComponent.md#removeslot) respectively.

A slot holds one audio [asset](https://api.playcanvas.com/engine/classes/SoundSlot.md#asset) and the settings applied to every instance it creates:
[volume](https://api.playcanvas.com/engine/classes/SoundSlot.md#volume), [pitch](https://api.playcanvas.com/engine/classes/SoundSlot.md#pitch), [loop](https://api.playcanvas.com/engine/classes/SoundSlot.md#loop), [startTime](https://api.playcanvas.com/engine/classes/SoundSlot.md#starttime) and [duration](https://api.playcanvas.com/engine/classes/SoundSlot.md#duration), plus
[autoPlay](https://api.playcanvas.com/engine/classes/SoundSlot.md#autoplay) to start as soon as the asset has loaded and [overlap](https://api.playcanvas.com/engine/classes/SoundSlot.md#overlap) to let several
instances play at once instead of stopping the previous one. [play](https://api.playcanvas.com/engine/classes/SoundSlot.md#play) returns the new
[SoundInstance](https://api.playcanvas.com/engine/classes/SoundInstance.md) and [instances](https://api.playcanvas.com/engine/classes/SoundSlot.md#instances) lists those currently playing. The slot forwards the
instances' `play`, `pause`, `resume`, `stop` and `end` events and fires `load` when its asset
is ready.

**Example**

```ts
const slot = entity.sound.slot('footsteps');
slot.overlap = true;   // let quick steps overlap rather than cut each other off
slot.volume = 0.6;
slot.play();
@hideconstructor
```

## Properties

### instances

```ts
instances: SoundInstance[] = []
```

An array that contains all the [SoundInstance](https://api.playcanvas.com/engine/classes/SoundInstance.md)s currently being played by the slot.

### name

```ts
name: string
```

The name of the slot.

## Accessors

### asset

```ts
get asset(): number | null
set asset(value: number | null)
```

Gets the asset id.

### autoPlay

```ts
get autoPlay(): boolean
set autoPlay(value: boolean)
```

Gets whether the slot will begin playing as soon as it is loaded.

### duration

```ts
get duration(): number
set duration(value: number)
```

Gets the duration of the sound that the slot will play starting from [startTime](https://api.playcanvas.com/engine/classes/SoundSlot.md#starttime). The
returned value is clamped to the time available after the normalized start time.

### isLoaded

```ts
get isLoaded(): boolean
```

Gets whether the asset of the slot is loaded.

### isPaused

```ts
get isPaused(): boolean
```

Gets whether the slot is currently paused.

### isPlaying

```ts
get isPlaying(): boolean
```

Gets whether the slot is currently playing.

### isStopped

```ts
get isStopped(): boolean
```

Gets whether the slot is currently stopped.

### loop

```ts
get loop(): boolean
set loop(value: boolean)
```

Gets whether the slot will restart when it finishes playing.

### overlap

```ts
get overlap(): boolean
set overlap(value: boolean)
```

Gets whether the sounds played from this slot will be played independently of each other.

### pitch

```ts
get pitch(): number
set pitch(value: number)
```

Gets the pitch modifier to play the sound with.

### startTime

```ts
get startTime(): number
set startTime(value: number)
```

Gets the start time from which the sound will start playing.

### volume

```ts
get volume(): number
set volume(value: number)
```

Gets the volume modifier to play the sound with.

## Methods

### clearExternalNodes

```ts
clearExternalNodes(): void
```

Clears any external nodes set by [setExternalNodes](https://api.playcanvas.com/engine/classes/SoundSlot.md#setexternalnodes).

### getExternalNodes

```ts
getExternalNodes(): AudioNode[]
```

Gets an array that contains the two external nodes set by [setExternalNodes](https://api.playcanvas.com/engine/classes/SoundSlot.md#setexternalnodes).

**Returns** `AudioNode[]`: An array of 2 elements that contains the first and last nodes set by
[setExternalNodes](https://api.playcanvas.com/engine/classes/SoundSlot.md#setexternalnodes).

### load

```ts
load(): void
```

Loads the asset assigned to this slot.

### pause

```ts
pause(): boolean
```

Pauses all sound instances. To continue playback call [resume](https://api.playcanvas.com/engine/classes/SoundSlot.md#resume).

**Returns** `boolean`: True if the sound instances paused successfully, false otherwise.

### play

```ts
play(): SoundInstance
```

Plays a sound. If [overlap](https://api.playcanvas.com/engine/classes/SoundSlot.md#overlap) is true the new sound instance will be played
independently of any other instances already playing. Otherwise existing sound instances
will stop before playing the new sound.

**Returns** [`SoundInstance`](https://api.playcanvas.com/engine/classes/SoundInstance.md): The new sound instance.

### resume

```ts
resume(): boolean
```

Resumes playback of all paused sound instances.

**Returns** `boolean`: True if any instances were resumed.

### setExternalNodes

```ts
setExternalNodes(firstNode: AudioNode, lastNode?: AudioNode): void
```

Connect external Web Audio API nodes. Any sound played by this slot will automatically
attach the specified nodes to the source that plays the sound. You need to pass the first
node of the node graph that you created externally and the last node of that graph. The
first node will be connected to the audio source and the last node will be connected to the
destination of the AudioContext (e.g. speakers).

**Parameters**

- `firstNode` (`AudioNode`): The first node that will be connected to the audio source of
  sound instances.
- `lastNode` (`AudioNode`, optional): The last node that will be connected to the destination of
  the AudioContext. If unspecified then the firstNode will be connected to the destination
  instead.

**Example**

```ts
const context = app.systems.sound.context;
const analyzer = context.createAnalyzer();
const distortion = context.createWaveShaper();
const filter = context.createBiquadFilter();
analyzer.connect(distortion);
distortion.connect(filter);
slot.setExternalNodes(analyzer, filter);
```

### stop

```ts
stop(): boolean
```

Stops playback of all sound instances.

**Returns** `boolean`: True if any instances were stopped.

## Events

### EVENT_END

```ts
static EVENT_END: string = 'end'
```

Fired when a sound instance stops playing because it reached its end. The handler is passed
the [SoundInstance](https://api.playcanvas.com/engine/classes/SoundInstance.md) that ended.

**Example**

```ts
slot.on('end', (instance) => {
    console.log('Sound instance playback ended');
});
```

### EVENT_LOAD

```ts
static EVENT_LOAD: string = 'load'
```

Fired when the sound [Asset](https://api.playcanvas.com/engine/classes/Asset.md) assigned to the slot is loaded. The handler is passed the
loaded [Sound](https://api.playcanvas.com/engine/classes/Sound.md) resource.

**Example**

```ts
slot.on('load', (sound) => {
    console.log('Sound resource loaded');
});
```

### EVENT_PAUSE

```ts
static EVENT_PAUSE: string = 'pause'
```

Fired when a [SoundInstance](https://api.playcanvas.com/engine/classes/SoundInstance.md) is paused on a slot. The handler is passed the sound
instance that is paused.

**Example**

```ts
slot.on('pause', (instance) => {
    console.log('Sound instance paused');
});
```

### EVENT_PLAY

```ts
static EVENT_PLAY: string = 'play'
```

Fired when a [SoundInstance](https://api.playcanvas.com/engine/classes/SoundInstance.md) starts playing on a slot. The handler is passed the sound
instance that started playing.

**Example**

```ts
slot.on('play', (instance) => {
    console.log('Sound instance started playing');
});
```

### EVENT_RESUME

```ts
static EVENT_RESUME: string = 'resume'
```

Fired when a [SoundInstance](https://api.playcanvas.com/engine/classes/SoundInstance.md) is resumed on a slot. The handler is passed the sound
instance that is resumed.

**Example**

```ts
slot.on('resume', (instance) => {
    console.log('Sound instance resumed');
});
```

### EVENT_STOP

```ts
static EVENT_STOP: string = 'stop'
```

Fired when a [SoundInstance](https://api.playcanvas.com/engine/classes/SoundInstance.md) is stopped on a slot. The handler is passed the sound
instance that is stopped.

**Example**

```ts
slot.on('stop', (instance) => {
    console.log('Sound instance stopped');
});
```

## Inherited from [EventHandler](https://api.playcanvas.com/engine/classes/EventHandler.md)

- `fire(name: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any, arg8?: any): EventHandler`
- `hasEvent(name: string): boolean`
- `off(name?: string, callback?: HandleEventCallback, scope?: any): EventHandler`
- `on(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
- `once(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
