# aego/audio

> Aego 0.12.5 · development; docs build `4a32b816933e02930662f6a443cb25abe6050d090c55e237cbff5c44ae9b25f8`. [Build manifest](/docs-build.json).

Sounds are presentation: they never change the game state, and a rewind or replay is silent. Wait for game logic with a timer, not with a `Sound`. The engine ships four clips for tests and sketches, `audio.Builtin("Beep")`, `Click`, `Hum` and `Melody`; the last two loop without a seam. The example plays a hum and music, then fades both out. See [Audio](/audio.md).

## Example

```aes run
package demo

import (
    "aego/audio"
    "aego/log"
)

script Chime {
    var tone audio.Sound
    var frames int

    func Ready() {
        tone = audio.PlayWith(audio.Builtin("Hum"), audio.PlayOptions{Volume: 0.5, Pitch: 1.5, Loop: true})
        audio.Music(audio.Builtin("Melody"), 0.5)
        audio.SetBusVolume(audio.Bus.Music, 0.6)
        log.Info("the hum plays: " + string(tone.Playing()))
    }

    func Update(dt float) {
        frames++
        if frames == 60 {
            tone.StopFade(0.2)
            audio.StopMusic(0.5)
            log.Info("the hum fades out: " + string(!tone.Playing()))
        }
    }
}
```

## Builtin

```
func Builtin(name string) AssetRef<audio>
```

Returns a clip that ships with the engine: Beep, Click, Hum (loopable) or Melody (loopable).

## Bus

```
enum Bus
```

Bus is a mixer bus: Master, Music, Sfx, UI, Ambience or Voice.

## BusVolume

```
func BusVolume(bus aego/audio.Bus) float
```

Returns the session volume of a bus set by SetBusVolume; the player's settings are separate.

## ListenerPosition

```
func ListenerPosition() Vec2
```

Returns the last effective listener position in world space, or zero outside Play. It includes the camera fallback when no listener is active.

## Music

```
func Music(clip AssetRef<audio>, seconds float) Handle<aego/audio.Sound>
```

Loops a clip on the music bus, crossfading from the music that played over seconds.

## Mute

```
func Mute(bus aego/audio.Bus, on bool)
```

Silences a bus for this session.

## Pause

```
func Pause(on bool)
```

Pauses or resumes every sound of this world.

## PauseWith

```
enum PauseWith
```

PauseWith decides whether a sound pauses with the game or keeps playing.

## Play

```
func Play(clip AssetRef<audio>) Handle<aego/audio.Sound>
```

Plays a clip on its bus. The sound stops when the node of the script is destroyed.

## PlayAt

```
func PlayAt(clip AssetRef<audio>, position Vec2) Handle<aego/audio.Sound>
```

Plays a clip at a world position, fading and panning with the distance to the listener.

## PlayEvent

```
func PlayEvent(event AssetRef<data:AudioEvent>) Handle<aego/audio.Sound>
```

Plays an audio event; a spatial event follows the node of the script.

## PlayEventAt

```
func PlayEventAt(event AssetRef<data:AudioEvent>, position Vec2) Handle<aego/audio.Sound>
```

Plays an audio event at a world position.

## PlayWith

```
func PlayWith(clip AssetRef<audio>, options aego/audio.PlayOptions) Handle<aego/audio.Sound>
```

Plays a clip with options.

## SetBusVolume

```
func SetBusVolume(bus aego/audio.Bus, volume float)
```

Sets the session volume of a bus, on top of the player's settings; it is not saved.

## Steal

```
enum Steal
```

Steal is what an audio event does at Max Voices: stop the oldest voice, stop the quietest, or skip the new play.

## StopMusic

```
func StopMusic(seconds float)
```

Fades the music out over seconds.

## WorldBusGain

```
func WorldBusGain(bus aego/audio.Bus) float
```

Returns the last zone-selected world-layer target for a bus, or 1 outside Play. It is not the ramped mixer output or the player's settings.
