# core/soundeffectpool (vanilla-engine 0.4.0)

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

Sound effect pool.
- The original core/soundeffectpool.js. So the same sound effect is not cut off when it overlaps in quick succession,
  it plays by rotating through several AudioPlayers (round robin).
- Does not call the backend directly. On top of AudioPlayer it only tracks the turn and volume.
- Usage:
  ```text
    let mut sound_effect_pool = SoundEffectPool::new(&engine.get_audio_manager(), 6);
    sound_effect_pool.play(&hitAudioAsset, 1.0);
    sound_effect_pool.play(&hitAudioAsset, 0.5);   // Half volume for this playback only.
    sound_effect_pool.set_volume(0.8);              // Volume of the whole pool. (also multiplied into ones already playing)
  ```
- If the voice count is 0, play does nothing, as in the original.
- Differences from the original:
  play's volume has no default, so it is always passed. (the original defaults to 1)

## SoundEffectPool (struct)

Sound effect pool.
- Rotates through several players in turn so overlapping copies of the same sound effect do not cut off the earlier sound. (if more overlap than the voice count, the oldest is cut)

Example: Play overlapping sound effects with a 6-voice pool

```rust
use vanilla::{Engine, EngineConfiguration, SoundEffectPool};

let engine = Engine::new(EngineConfiguration::new());
let hit_audio_asset = engine.load_audio_asset("tests/assets/audio/wav_s16_stereo.wav").unwrap();
let mut sound_effect_pool = SoundEffectPool::new(&engine.get_audio_manager(), 6);
sound_effect_pool.play(&hit_audio_asset, 1.0);
sound_effect_pool.play(&hit_audio_asset, 0.5);
sound_effect_pool.set_volume(0.8);
assert_eq!(sound_effect_pool.get_voice_count(), 6);
```

Example: A pool with no voices does not play and only holds the volume

```rust
use vanilla::{AudioManager, SoundEffectPool};

let audio_manager = AudioManager::new();
let mut sound_effect_pool = SoundEffectPool::new(&audio_manager, 0);
assert_eq!(sound_effect_pool.get_voice_count(), 0);
sound_effect_pool.set_volume(1.5);
assert_eq!(sound_effect_pool.get_volume(), 1.0);
sound_effect_pool.set_muted(true);
assert!(sound_effect_pool.is_muted());
```

- `pub fn new(audio_manager: &AudioManager, voice_count: usize) -> SoundEffectPool` (constructor, since 0.3.2): Creates. (voice_count: maximum number of playbacks that can overlap at once)
  - `audio_manager` &AudioManager: Manager to create the players with. (usually engine.get_audio_manager())
  - `voice_count` usize: Maximum number of playbacks that can overlap at once. (the original default 6, DEFAULT_VOICE_COUNT) If 0, play does nothing.
  - returns SoundEffectPool: A new pool holding voice_count players. (volume 1, not muted)
- `pub fn play(&mut self, audio_asset: &Rc<AudioAsset>, volume: f64)` (method, since 0.3.2): Plays a sound effect.
  - Loads the asset into the next player in turn and plays it. If that player was playing, cuts it and plays anew.
  - The volume is this playback's volume (0 ~ 1) multiplied by the pool's volume.
  - While muted or without voices, does nothing and does not advance the turn.
  - `audio_asset` &Rc<AudioAsset>: Sound effect asset to play.
  - `volume` f64: Volume of this playback. (0 ~ 1, multiplied by the pool's volume then clamped to 0 ~ 1)
- `pub fn stop_all(&mut self)` (method, since 0.3.2): Stops all.
- `pub fn set_muted(&mut self, is_muted: bool)` (method, since 0.3.2): Sets mute.
  - Mutes or unmutes all players, and skips play while muted.
  - `is_muted` bool: True to mute, false to unmute.
- `pub fn set_volume(&mut self, volume: f64)` (method, since 0.3.2): Sets the volume of the whole pool. (0 ~ 1, applied immediately to those playing too)
  - `volume` f64: Volume of the whole pool. (clamped to 0 ~ 1, 1 if not finite)
- `pub fn get_volume(&self) -> f64` (method, since 0.3.2): Volume of the whole pool.
  - returns f64: Volume of the whole pool. (0 ~ 1, default 1)
- `pub fn is_muted(&self) -> bool` (method, since 0.3.2): Whether muted.
  - returns bool: True after set_muted(true).
- `pub fn get_voice_count(&self) -> usize` (method, since 0.3.2): Returns the number of voices.
  - returns usize: Number of players the pool holds. (voice_count at creation)

## Functions and constants

- `pub const DEFAULT_VOICE_COUNT: usize = 6` (field, since 0.3.2): Default voice count of the original constructor.
  - returns usize
