---
title: Game audio
order: 10
summary: A real time mixer, loops, variations and audio sprites.
---

<!-- Generated by tools/generate-docs.bxs from the BIF classes. Do not edit; change the Javadoc and run it again. -->

A real time mixer, loops, variations and audio sprites.

## audioMixer

Open a real time mixer that plays many sounds at once with volume, pan, pitch, loops and buses, the way a game needs

```js
audioMixer( sampleRate=44100, channels=2, output="speakers", buses )
```

| Argument | Type | Default | |
|---|---|---|---|
| `sampleRate` | integer | `44100` | Output sample rate. Defaults to 44100. |
| `channels` | integer | `2` | 1 or 2. Defaults to 2. |
| `output` | string | `"speakers"` | speakers plays to the sound card. null keeps time with no sound, for servers. memory runs nothing: call render(seconds) on the mixer to get the audio. Defaults to speakers. |
| `buses` | struct | optional | Starting volumes of named buses, for example { music: 0.7, sfx: 1 }. A sound goes on a bus with the bus option of play(). |

**Returns** A mixer. Its methods: play(sound, options) returns a playback; options are bus, volume, pan, pitch, loop, loopStart, loopEnd, start and fadeIn. A playback has stop(fadeSeconds), pause(), resume(), setVolume(v), setPan(p), setPitch(p), fadeTo(level, seconds), isPlaying() and getPosition(). The mixer has crossfade(playback, sound, seconds, options), setVolume(v), setVolume(bus, v), stopAll(), stopAll(bus, fade), getActiveCount(), waitUntilSilent(seconds), render(seconds) for memory mixers, and close().

## audioPlay

Play audio through the speakers

```js
audioPlay( audio, wait=true, volume=1, output="speakers" )
```

| Argument | Type | Default | |
|---|---|---|---|
| `audio` | any | required | The audio buffer. |
| `wait` | boolean | `true` | True to return when the sound has finished. False to return at once and let it play on a background mixer. Defaults to true. |
| `volume` | double | `1` | Volume, 1 is the sound as it is. Defaults to 1. |
| `output` | string | `"speakers"` | speakers, null (keep time, no sound) or memory is not allowed here. Defaults to speakers. |

**Returns** The playback, which can be stopped with stop().

## audioLoopify

Make a sound loop seamlessly by blending its end into its start

```js
audioLoopify( audio, crossfade=0.25 )
```

| Argument | Type | Default | |
|---|---|---|---|
| `audio` | any | required | The audio buffer. |
| `crossfade` | double | `0.25` | Seconds of overlap, at most half the sound. Defaults to 0.25. |

**Returns** A new buffer, shorter by the crossfade, whose end runs straight into its start.

## audioVariations

Make several slightly different copies of a sound, so repeated effects do not sound the same

```js
audioVariations( audio, count=5, pitchRange=1, gainRangeDb=2, seed=1 )
```

| Argument | Type | Default | |
|---|---|---|---|
| `audio` | any | required | The audio buffer. |
| `count` | integer | `5` | How many variations. Defaults to 5. |
| `pitchRange` | double | `1` | Random change of pitch, plus or minus this many semitones; the speed changes with it, like a tape. Defaults to 1. |
| `gainRangeDb` | double | `2` | Random change of level, plus or minus this many dB. Defaults to 2. |
| `seed` | long | `1` | Seed of the random numbers, so the same call gives the same variations. Defaults to 1. |

**Returns** An array of audio buffers.

## audioSprite

Join named sounds into one audio sprite with a map of where each starts, the way Howler.js and game engines load effects

```js
audioSprite( sounds, gap=0.1, path="" )
```

| Argument | Type | Default | |
|---|---|---|---|
| `sounds` | struct | required | A struct of name to audio buffer. They are joined in the order of the struct, so use an ordered struct, written with square brackets [ jump: a, coin: b ], when the order matters. |
| `gap` | double | `0.1` | Seconds of silence between sounds. Defaults to 0.1. |
| `path` | string | `""` | A file to write the sprite audio to; a .json map is written next to it in the Howler.js format. Empty to only return it. |

**Returns** A struct with audio (the joined buffer) and sprites, a struct of name to { start, end, duration } in seconds. When a path is given, also path and mapPath.

