# core/soundeffectpool (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/soundeffectpool/

Sound effect pool.

## SoundEffectPool extends Object

Sound effect pool.
- Plays by rotating (round robin) through several AudioPlayers so the same sound effect
  is not cut off when it overlaps in quick succession. (what several games each implemented as a 6-channel pool)
- Usage:
    const pool = new SoundEffectPool(engine.getAudioManager(), 6);
    pool.play(hitAudioAsset);
    pool.play(hitAudioAsset, 0.5);   // half volume for this playback only.
    pool.setVolume(0.8);             // volume of the whole pool. (also multiplied into those playing)

Example: Play the same sound effect overlapping

```js
import { Engine, EngineConfiguration, AudioAsset, SoundEffectPool } from "./build/vanilla.js";

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const audioManager = engine.getAudioManager();
const audioAsset = new AudioAsset();
await audioAsset.load("examples/assets/beep.wav");

const soundEffectPool = new SoundEffectPool(audioManager, 4);
soundEffectPool.setVolume(0.8);
soundEffectPool.play(audioAsset);
soundEffectPool.play(audioAsset, 0.5);
console.log(soundEffectPool.getVoiceCount(), soundEffectPool.getVolume());
soundEffectPool.stopAll();
```

- `constructor(audioManager, voiceCount = 6)` (constructor, since 0.3.0): Constructor.
  - `audioManager` AudioManager: The engine's AudioManager. (engine.getAudioManager(), makes the players)
  - `voiceCount` number: Maximum number of playbacks that can overlap at once.
- `play(audioAsset, volume = 1)` (method, since 0.3.0): Plays a sound effect.
  - Loads the asset into the next player in turn and plays it. If that player was playing, stops it and plays the new one.
  - The volume is this playback's volume (0 ~ 1) multiplied by the pool volume.
  - `audioAsset` AudioAsset: Audio asset to play. (not played if missing or muted)
  - `volume` number: Volume of this playback. (0 ~ 1, default 1)
- `stopAll()` (method, since 0.3.0): Stops all.
- `setMuted(isMuted)` (method, since 0.3.0): Sets mute.
  - `isMuted` boolean: If true, mutes all players and does not start new playback.
- `setVolume(volume)` (method, since 0.3.2): Sets the volume of the whole pool. (0 ~ 1, applied immediately to those playing too)
  - `volume` number: Volume of the whole pool. (clamped to 0 ~ 1, 1 if not a finite number)
- `getVolume()` (method, since 0.3.2): Volume of the whole pool.
  - returns number: Volume of the whole pool. (0 ~ 1, default 1)
- `isMuted()` (method, since 0.3.0): Whether muted.
  - returns boolean: True if muted.
- `getVoiceCount()` (method, since 0.3.0): Returns the number of voices.
  - returns number: Player count. (the maximum number of overlapping playbacks)
