# core/audiobus (vanilla-engine 0.4.0)

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

Audio bus. (AudioBus in the original core/audiobus.js)
- Gathers the sound of several players into one path and applies volume and mute to all of them at once. (SoundSystem's music bus and sound effect bus)
- Created with AudioManager::create_audio_bus() or AudioBus::new(manager). (the original new AudioBus(audioContext)) Players that play through the bus are created with create_audio_player().
- The backend fills the bus slot. (AudioBusBackend in platform/audio)
  Desktop: one GainNode connected to the output in the engine mixer. Players created from the bus send their sound here. (the math matches Chrome WebAudio)
  Web: one GainNode connected to the output, as in the original. The GainNodes of players created from the bus connect to it.
- A bus created without an AudioContext is silent, and players created from it make no sound either. (same as the original)
- Volume and mute follow the same rules as AudioPlayer. (clamped to 0 ~ 1, unmuting returns to the set volume)
- Differences from the original:
  - State is shared through `Rc<RefCell>`. So that the volume can be changed and players created while SoundSystem lends its bus through get_music_bus,
    all methods take &self. Cloning makes one more handle to the same bus.
  - A player created from a bus holds one bus handle. When all handles are dropped the bus is disconnected from the output. (in the original the browser keeps connected nodes alive)
  - get_gain_node (the original getGainNode) returns an AudioGainNode. On the web it is a GainNode, on desktop an `Rc<MixerGainNode>` sharing the mixer's bus node.
    Either way, its as_deref() is the output destination for AudioPlayer::new. (AudioPlayer::new(&audio_manager, audio_bus.get_gain_node().as_deref()), same on both platforms)

## AudioBus (struct) (Clone)

Audio bus.
- Gathers the sound of players created from the bus and applies volume and mute to all of them at once. A cloned handle refers to the same bus.

Example: Lower the sound effect bus volume and mute it

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

fn play_effect(engine: &Engine) {
	let audio_manager: &AudioManager = &engine.get_audio_manager();
	let effect_bus = audio_manager.create_audio_bus();
	effect_bus.set_volume(0.5);
	let mut effect_player = effect_bus.create_audio_player();
	let audio_asset = engine.load_audio_asset("tests/assets/audio/wav_s16_stereo.wav").unwrap();
	effect_player.set_audio_asset(audio_asset);
	effect_player.play(false);
	effect_bus.mute();
	assert_eq!(effect_bus.is_muted(), effect_bus.is_available());
	effect_bus.unmute();
	assert_eq!(effect_bus.get_volume(), 0.5);
}
```

Example: Create a player by passing the bus node as the output destination, as in the original

```rust
use vanilla::{AudioBus, AudioManager, AudioPlayer};

let audio_manager = AudioManager::new();
let audio_bus = AudioBus::new(&audio_manager);
let audio_player = AudioPlayer::new(&audio_manager, audio_bus.get_gain_node().as_deref());
assert!(!audio_player.is_playing());
```

- `pub fn new(audio_manager: &AudioManager) -> AudioBus` (constructor, since 0.4.0): Constructor. (the original new AudioBus(audioContext), creates the manager's AudioContext if absent, a silent bus if it cannot be created)
  - `audio_manager` &AudioManager: Manager holding the AudioContext. (usually engine.get_audio_manager())
  - returns AudioBus: New bus connected to the output. (volume 1, not muted)
- `pub fn create_audio_player(&self) -> AudioPlayer` (method, since 0.4.0): Creates an audio player that plays through this bus. (a silent player if the bus is silent)
  - returns AudioPlayer: New player that sends its sound through this bus. (holds one bus handle to keep the bus alive)
- `pub fn mute(&self)` (method, since 0.4.0): Mute. (does nothing on a silent bus)
- `pub fn unmute(&self)` (method, since 0.4.0): Unmute. (returns to the set volume)
- `pub fn set_volume(&self, volume: f64)` (method, since 0.4.0): Sets the volume. (0 ~ 1, if muted it applies when unmuted)
  - `volume` f64: Bus volume. (clamped to 0 ~ 1, 1 if not finite)
- `pub fn get_volume(&self) -> f64` (method, since 0.4.0): Returns the volume.
  - returns f64: The set volume. (0 ~ 1, does not change when muted)
- `pub fn is_muted(&self) -> bool` (method, since 0.4.0): Returns whether muted.
  - returns bool: true if muted. (false for a silent bus even if mute is called)
- `pub fn is_available(&self) -> bool` (method, since 0.4.0): Whether it can gather sound. (a bus created while an AudioContext exists)
  - returns bool: true if there is a backend node. (false for a bus created when the AudioContext could not be created)
- `pub fn get_gain_node(&self) -> Option<AudioGainNode>` (method, since 0.4.0): The bus's GainNode. (the original getGainNode; on the web a GainNode, on desktop an `Rc<MixerGainNode>` sharing the bus's mixer gain node; None for a bus created without an AudioContext)
  - A gain set directly on the node does not change the volume field or the mute field. (same as the original)
  - returns Option<AudioGainNode>: Handle sharing the bus node. (its as_deref() is the output destination for AudioPlayer::new, None for a silent bus)
