# core/audioplayer (vanilla-engine 0.4.0)

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

Audio player.
- The original core/audioplayer.js. Plays the audio buffer loaded in an AudioAsset.
- Volume, mute, loop and playback position are held here, and making the sound is done by the backend (AudioPlayerBackend in platform/audio).
  In place of the original's GainNode: one GainNode on both platforms, as in the original. (on desktop a node of the engine mixer, the math matches Chrome WebAudio)
- Created with AudioManager::create_audio_player() or AudioPlayer::new(manager, output destination). Without an AudioContext it becomes a player that makes no sound. (same as the original)
  new stands in for the original new AudioPlayer(audioContext, outputNode). The first argument is the manager holding the AudioContext (it creates the AudioContext if absent),
  and the output destination (AudioOutputNode) is as_deref() of the bus's get_gain_node() (AudioGainNode). None means the output. The code is the same on both platforms.
    AudioPlayer::new(&audio_manager, audio_bus.get_gain_node().as_deref())
  (on the web it is &AudioNode so other AudioNodes can be passed too, on desktop it is the mixer bus's &MixerGainNode)
  On desktop, a player created by passing the output destination directly also keeps that bus node alive while connected, like the original WebAudio.
- As in the original, play wakes the AudioContext first if it is suspended, then plays. (the web waits for resume(), desktop does it immediately)
- Dropping the player does not stop what it is playing; like a WebAudio source node in the original, the sound plays to the end. (on desktop the mixer holds the node until the source ends, on the web the source node plays)
  A looping sound does not stop, as in the original, so call stop before dropping the player.
- Differences from the original:
  The asset is shared as `Rc<AudioAsset>`.
  On desktop, is_playing() reads whether the mixer has finished the source instead of using the onended callback. (false when finished)
  get_audio_context lends the AudioContext of the manager that created it. (the WebAudio AudioContext on the web, the engine mixer's AudioContext on desktop, None for a player created without a manager)
  set_playback_rate was not in the original and was made first in the Rust version. (it was added to the original too)
  A player created with AudioBus::create_audio_player sends its sound to that bus instead of the output. (the original AudioPlayer's output node argument) It holds one bus handle to keep the bus alive.
- get_time, as in the original, is the current position while playing: the start position plus the time elapsed on the audio context clock. (web currentTime, desktop mixer clock)

## AudioPlayer (struct)

Audio player.
- Loads one asset and plays it, and holds volume, mute, loop and playback position. Owned by its creator, dropping it also stops the sound.

Example: Load an asset and play it in a loop from the 1 second mark

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

let engine = Engine::new(EngineConfiguration::new());
let audio_asset = engine.load_audio_asset("tests/assets/audio/vorbis_stereo.ogg").unwrap();
let mut audio_player = engine.get_audio_manager().create_audio_player();
audio_player.set_audio_asset(audio_asset);
audio_player.set_volume(0.8);
audio_player.set_time(1.0);
audio_player.play(true);
assert!(audio_player.get_duration() > 1.0);
audio_player.mute();
audio_player.stop();
```

- `pub fn new(audio_manager: &AudioManager, output_node: Option<AudioOutputNode<'_>>) -> AudioPlayer` (constructor, since 0.3.2): Constructor. (the original new AudioPlayer(audioContext, outputNode), creates the manager's AudioContext if absent, the output if the output destination is None)
  - `audio_manager` &AudioManager: Manager holding the AudioContext. (usually engine.get_audio_manager())
  - `output_node` Option<AudioOutputNode<'_>>: Node to send the sound to. (the bus's get_gain_node().as_deref()) The output if None.
  - returns AudioPlayer: New player. (volume 1, no asset, a silent player if the AudioContext could not be created)

  Example: A player that sends its sound to a bus node

  ```rust
  use vanilla::{AudioManager, AudioPlayer};
  
  let audio_manager = AudioManager::new();
  let audio_bus = audio_manager.create_audio_bus();
  let mut audio_player = AudioPlayer::new(&audio_manager, audio_bus.get_gain_node().as_deref());
  audio_player.set_volume(0.5);
  assert_eq!(audio_player.get_volume(), 0.5);
  ```

- `pub fn play(&mut self, is_loop: bool)` (method, since 0.3.2): Plays. (if playing, stops and plays again from the playback position)
  - If the audio context is suspended, resumes it and then plays. (the web waits for resume(), desktop does it immediately)
  - Does nothing for a silent player, if there is no asset, or if the asset is not loaded yet.
  - `is_loop` bool: If true, goes back to the start at the end and repeats.
- `pub fn set_playback_rate(&mut self, playback_rate: f64)` (method, since 0.4.0): Sets the playback speed. (1 is unchanged, the pitch changes too, from the next play, the original setPlaybackRate)
  - `playback_rate` f64: Playback speed multiplier. (2 is twice as fast, 1 if 0 or less or not finite)
- `pub fn stop(&mut self)` (method, since 0.3.2): Stop.
- `pub fn mute(&mut self)` (method, since 0.3.2): Mute. (does nothing if there is no backend)
- `pub fn unmute(&mut self)` (method, since 0.3.2): Unmute. (returns to the set volume)
- `pub fn set_volume(&mut self, volume: f64)` (method, since 0.3.2): Sets the volume. (0 ~ 1, if muted it applies when unmuted)
  - `volume` f64: Volume. (clamped to 0 ~ 1, 1 if not finite)
- `pub fn get_volume(&self) -> f64` (method, since 0.3.2): Returns the volume.
  - returns f64: The set volume. (0 ~ 1, does not change when muted)
- `pub fn set_time(&mut self, time: f64)` (method, since 0.3.2): Sets the playback position. (seconds, if playing, plays again from that position right away)
  - `time` f64: Playback position. (seconds, from the start of the asset)
- `pub fn set_audio_asset(&mut self, audio_asset: Rc<AudioAsset>)` (method, since 0.3.2): Sets the audio asset.
  - Does not change the sound being played. This asset plays from the next play.
  - `audio_asset` Rc<AudioAsset>: Asset to play. (loaded with engine.load_audio_asset)
- `pub fn get_audio_asset(&self) -> Option<&Rc<AudioAsset>>` (method, since 0.3.2): Returns the audio asset.
  - returns Option<&Rc<AudioAsset>>: The loaded asset. (None if none)
- `pub fn is_playing(&self) -> bool` (method, since 0.3.2): Returns whether playing.
  - returns bool: true if currently making sound. (false if finished or stopped)
- `pub fn is_muted(&self) -> bool` (method, since 0.3.2): Returns whether muted.
  - returns bool: true if muted. (false for a silent player even if mute is called)
- `pub fn get_time(&self) -> f64` (method, since 0.3.2): Returns the playback position. (seconds; while playing, the current moving position; if stopped or ended, the set_time position where the next play starts)
  - While playing, adds the time elapsed on the audio context clock multiplied by the playback rate to the start position. (it stops too while the context is suspended)
  - If looping, wraps around by the length; otherwise stops at the length.
  - returns f64: Playback position. (seconds, default 0)
- `pub fn get_audio_context(&self) -> Option<Ref<'_, AudioContext>>` (method, since 0.4.0): AudioContext. (the original getAudioContext, lends the one of the manager that created it, None if none)
  - returns Option<Ref<'_, AudioContext>>: The borrowed AudioContext. (None if the manager has no AudioContext)
- `pub fn get_duration(&self) -> f64` (method, since 0.3.2): Returns the playback duration. (seconds, 0 if there is no asset)
  - returns f64: Length of the loaded asset. (seconds, 0 if there is no asset or it is not loaded yet)
