# core/audiobus (vanilla.js 0.4.0)

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

Audio bus.

## AudioBus

Audio bus.
Gathers the sound of several players into one path and applies volume and mute to them at once. (SoundSystem's background music bus and sound effect bus)
A single GainNode connected to the output (audioContext.destination), and players playing through the bus (createAudioPlayer) connect their GainNode here instead of to the output.
Created by AudioManager.createAudioBus(). A bus created without an AudioContext is silent, and players made from it play no sound either.
Volume and mute follow the same rules as AudioPlayer. (clamped to 0 ~ 1, unmuting returns to the set volume)
Same shape as AudioBus in the Rust version (vanilla-engine). (a kira sub-track on desktop, this GainNode on the web)

Example: Make a sound effect bus and set the volume all at once

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

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const audioManager = engine.getAudioManager();
const audioBus = audioManager.createAudioBus();
audioBus.setVolume(0.5);

const audioAsset = new AudioAsset();
await audioAsset.load("examples/assets/beep.wav");
const audioPlayer = audioBus.createAudioPlayer();
audioPlayer.setAudioAsset(audioAsset);
audioPlayer.play();
console.log(audioBus.isAvailable(), audioBus.getVolume());
audioBus.mute();
console.log(audioBus.isMuted());
```

- `constructor(audioContext)` (constructor, since 0.4.0): Constructor.
  - `audioContext` AudioContext | null: Audio context to make the bus with. (if null, a silent bus)
- `createAudioPlayer()` (method, since 0.4.0): Creates an audio player that plays through this bus. (a silent player if the bus is silent)
  - returns AudioPlayer: A player that sounds through this bus.
- `mute()` (method, since 0.4.0): Mute. (does nothing on a silent bus)
- `unmute()` (method, since 0.4.0): Unmute. (returns to the set volume)
- `setVolume(volume)` (method, since 0.4.0): Sets the volume. (0 ~ 1, if muted it applies when unmuted)
  - `volume` number: Volume. (clamped to 0 ~ 1, 1 if not a finite number)
- `getVolume()` (method, since 0.4.0): Returns the volume.
  - returns number: Volume. (0 ~ 1, default 1)
- `isMuted()` (method, since 0.4.0): Returns whether muted.
  - returns boolean: True if muted.
- `isAvailable()` (method, since 0.4.0): Whether it can gather sound. (a bus created while an AudioContext exists)
  - returns boolean: True if it can collect sound.
- `getGainNode()` (method, since 0.4.0): The bus's GainNode. (for connecting other WebAudio nodes to this bus, for SoundSystem to apply the volume setting, null if none)
  - Volume set directly on this node does not change the volume field (getVolume) or the mute field. Values set with setVolume and mute last only until then.
  - returns GainNode | null: The bus GainNode. (null if none)
