# Audio

> Sound effects and music by name, three volume buses a settings screen can move, and a sound device that opens itself — or is quietly absent.

Source: https://omardev29.github.io/rmp-docs/manual/modules/audio.html

[`rmp::audio`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html) plays a sound by its name and a song by its name, and that is most of it. There is no device to open, nothing to load first, no stream to feed and nothing to unload: the framework does each of those at the moment it has to be done, and on a machine with no sound at all, every call quietly does nothing.

## Playing a sound

```cpp
rmp::audio::play("coin");
```

The name is *logical*: `"coin"` finds `coin.wav`, `coin.ogg`, `coin.mp3` or `coin.qoa` in `resources/`, trying them in that order and taking the first that exists. Re-export a sound in another format and the code does not change. A name that already ends in an audio extension — `"coin.ogg"` — is taken exactly as written.

The first [`play()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#play) of a name loads the file; every later one costs nothing, because the sound stays loaded until the game ends. A sound comes out of the [pack](https://omardev29.github.io/rmp-docs/manual/modules/assets.html#the-pack) in a release and out of the folder while you work, like every other asset.

### How it sounds

A second argument changes one play, and only that play. Its fields, in the order a designated initialiser has to write them:

| Field | Default | Means |
|---|---|---|
| [`volume`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#PlayOptions-volume) | `1` | 0 to 1, for this play; multiplied by the `SFX` bus and by `MASTER`. |
| [`pitch`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#PlayOptions-pitch) | `1` | 2 is an octave up, 0.5 an octave down, from 1/16 to 16: past either end it stops there. Zero, a negative number or a NaN plays at 1. |
| [`pan`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#PlayOptions-pan) | `0` | -1 is the left speaker, 0 the centre, 1 the right; clamped to that range. |

```cpp
rmp::audio::play("hit", { .volume = 0.7f, .pitch = 1.2f });
rmp::audio::play("door", { .pan = -0.8f });   // mostly from the left
rmp::audio::play("step", { .pitch = rmp::random::range(0.9f, 1.1f) });
```

The last line is the cheapest trick in game audio: the same footstep at a slightly different pitch each time stops sounding like a recording. With a fixed [seed](https://omardev29.github.io/rmp-docs/manual/modules/random.html), it is also the same sequence of footsteps every run.

### Sounds overlap

Playing a sound that is still playing starts it again on another voice instead of cutting the first one off, so a machine gun sounds like one and not like a stuck record. Each sound has up to four voices; when all four are busy, the one that has been playing longest is cut. The voices share the loaded samples, so a voice costs a few bytes, not a second copy of the sound.

## Music

```cpp
class LevelScene : public rmp::Scene {
    void _ready() override { rmp::audio::music("level1"); }
};
```

[`music()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#music) starts a track, looping, and replaces whatever was playing. There is one track at a time. The name is logical, the same as for `play()`.

Asking for the track that is already playing is *not* a restart. A scene that asks for its song in `_ready()` can be changed to itself every time the player dies, and the song carries on instead of jumping back to the beginning. The platformer asks for its music in every level's `_ready()` and the music plays straight through from one level to the next.

A track that is not looped plays once and stops, which is what a jingle wants. Asking for it again once it has finished starts it again:

```cpp
rmp::audio::music("victory", false);   // once, then silence
```

[`stop_music()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#stop_music), or `music("")`, stops the music and lets the track go. [`music_playing()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#music_playing) says whether anything is playing right now.

The new track is loaded before the old one stops, so asking for a song that does not exist leaves the current one playing, and says so once. A name that came to nothing is remembered and not searched for again.

### Streamed, from memory

Music is streamed: it is decoded a little at a time while it plays, so the minutes of sound it would decode to never exist in memory at once. The encoded file itself is held in memory while it plays, which is what lets a song come out of the pack — a few megabytes for a few minutes of `.ogg`. The stream is fed once per frame by the framework, so music keeps playing under a pause menu that freezes the scene below it.

Not through raylibraylib's own `LoadMusicStream()` opens the file by itself and never sees the pack, so a song loaded that way is missing from a release that ships one. Play music with `rmp::audio::music()`.

## The files it reads

| Format | As a sound | As music |
|---|---|---|
| `.wav` | yes, found by its logical name, first | yes |
| `.ogg` | yes, second | yes |
| `.mp3` | yes, third | yes |
| `.qoa` | yes, fourth | yes |
| `.xm`, `.mod` | no | yes, by the full name: `music("theme.xm")` |
| `.flac` | no | no: raylib is built without its decoder |

A short effect is best as `.wav`, which needs no decoding; music is best as `.ogg`, which is small.

## Volume: three buses

A settings screen offers three volumes, and those are the three buses of [`rmp::audio::Bus`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#Bus):

`MASTER`

Everything.

`MUSIC`

The music, under `MASTER`.

`SFX`

Every sound effect, under `MASTER`, multiplied by each play's own volume.

Each is a fraction from 0 to 1, and they multiply: music at 0.8 under a master of 0.5 plays at 40 percent. They start at the values in `[audio]`:

```toml
[audio]
master = 1.0
music  = 0.8
sfx    = 1.0
```

A volume is a fraction of full, not a gain, so a value past 1 is refused when the `.toml` is read rather than turned into clipping:

```toml
[audio]
music = 1.5
```

```text
  raylib_multiplatform.toml:2: music = 1.5
                               ^^^^^^^^^^^

  configure: error: [audio] music = 1.5 has to be between 0 and 1.
                    It is a fraction of full volume, not a gain: past 1 is clipping, and a negative volume means nothing. Use 1 for full.
```

At run time, [`set_volume()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#set_volume) moves a bus and [`volume()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#volume) reads it. A new volume takes effect at once on what is already playing — effects still ringing out keep their own volume and are scaled by the new `SFX`. The value is clamped to 0..1, and a NaN is ignored rather than stored, so one bad slider value cannot silence the game.

```cpp
class SoundSettings : public rmp::Scene {
    float _music = rmp::audio::volume(rmp::audio::Bus::MUSIC);
    float _effects = rmp::audio::volume(rmp::audio::Bus::SFX);

    void _draw() override {
        rmp::ui::begin();
        if (rmp::ui::slider("Music", _music, 0, 1))
            rmp::audio::set_volume(rmp::audio::Bus::MUSIC, _music);
        if (rmp::ui::slider("Effects", _effects, 0, 1))
            rmp::audio::set_volume(rmp::audio::Bus::SFX, _effects);
        if (rmp::ui::button("Back")) rmp::Scene::pop();
        rmp::ui::end();
    }
};
```

Neither call opens the sound device, so a settings screen can show and move its sliders in a game that has not made a sound yet. The buses are not saved for you; a game that wants the player's volumes next time keeps them with [rmp::save](https://omardev29.github.io/rmp-docs/manual/modules/saving.html), as [examples/ui/04_settings](https://omardev29.github.io/rmp-docs/examples/ui/04_settings.html) does.

## The sound device

A game does not call `InitAudioDevice()` or `CloseAudioDevice()`. The device opens the first time something needs it — a `play()`, a `music()`, or a [`rmp::assets::load_sound()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_sound) — so a game with no sound never opens it. On the way out, the voices and the music are released before the device closes, in the order that does not crash. A game that opens the device itself — older code, a raylib example pasted in — keeps it: the framework closes only a device it opened.

[`available()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-audio.html#available) says whether the device is open and working. It is false until the first sound, and asking does not open anything.

### A machine with no sound

A CI runner, a server, a laptop with no output: the device fails to open, and that is not an error. The framework says so once —

`AUDIO: no sound device on this machine; sound is off and every rmp::audio call is a silent no-op from here on`

— and the game runs, silent. The open is tried once: a footstep sixty times a second does not pay for a failing device sixty times a second. Because this is a fact about the machine and not a mistake in the game, `[dev] strict` does not abort on it, and a strict debug build still runs on a machine with no speakers.

A game that does not want sound at all can take the audio module out of raylib, which makes the binary smaller. The game still compiles unchanged, and behaves exactly as it does on a machine with no device:

```toml
[raylib]
disabled_modules = ["raudio"]
```

Except on iOS, where the setting does nothing: the iOS build links a prebuilt raylib, built without it, and `configure.py` warns so when iOS is among the targets.

### On the web

Browsers keep a page silent until the player has clicked, touched it or pressed a key. That is the browser's rule, and no game gets round it: music started on the title screen is heard from the player's first click or key press on. A gamepad button does not count, because browsers do not treat it as a gesture, so a player on a gamepad alone hears nothing until then.

## A sound you hold yourself

For raylib's own sound functions there is a handle: `rmp::assets::load_sound()` returns an [`rmp::Sound`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#Sound) that converts to raylib's `Sound`, opening the device first if it has to.

```cpp
rmp::Sound beep = rmp::assets::load_sound("beep.wav");
PlaySound(beep);
```

It and `play()` do not get in each other's way. `play()` never plays the sound it loaded, only voices that share its samples, so a volume, pitch or pan it sets never lands on the `rmp::Sound` you hold.

## When a sound is not there

A name that finds no file, or finds one that does not decode, plays nothing, and says which of the two it was, once:

- `AUDIO: no sound called "…" in resources/ (looked for …)`
- `AUDIO: no music called "…" in resources/ (looked for …)`
- `AUDIO: "…" is in resources/ but could not be decoded (raylib here reads .wav, .ogg, .mp3 and .qoa)`
- `AUDIO: "…" is in resources/ but could not be streamed (raylib here reads .wav, .ogg, .mp3 and .qoa)`

These are mistakes in the game, so under `[dev] strict` the first of them aborts a debug build. Looking a logical name up is not counted as a [failed load](https://omardev29.github.io/rmp-docs/manual/modules/assets.html#when-a-load-fails): trying four extensions to find one file is not three failures, so a game whose sounds are `.ogg` boots with nothing failed. It also means a sound that is missing altogether is caught by the warning, not by the boot count. A file that is found and does not decode is different: the sound or the song is there and broken, and that counts as a failed load, the way a broken picture does.
