# Audio

Play sound effects and music from your scripts, and check what a game plays without
speakers. Sound is output: a script cannot read what was heard, and a request to play is
not in the world, the checksum or a snapshot. What a recorded run asks to hear is still
the same every time, and the command line can list it or write it to a file. Every call
is in the [sound reference](../reference/sound.md).

## The sample

The sounds sample loops a music track and blips each
time a ball bounces, to the side the ball is on. Its two WAV files are written by its
`sounds/make.py`.

```toml
# example: sounds/game.toml
# The sound guide's game: a looping music track, and a blip each time the ball bounces,
# panned to where the ball is and a little different in pitch each time. Sound is output:
# nothing here changes the world, and `frost run --sounds` lists what was asked for.
#
#   frost dev game.toml
#   frost run --headless --steps 240 --sounds game.toml
#   frost run --headless --steps 240 --wav out.wav game.toml
scene = "scene.toml"
scripts = ["bounce.luau"]
seed = 1
sounds = ["sounds/blip.wav", "sounds/hum.wav"]
replay = { steps = 240, checksum = "00d88d25c7211edd", engine = "0.0.1 r280" }
# The run's checksum on the way there, written by `frost test --write`: a test that
# fails says between which two of these steps the game first went another way.
checkpoints = []

[window]
size = [320, 180]
background = 0x15171c
```

```lua
-- example: sounds/bounce.luau
-- A ball crosses the frame and back. Each bounce plays the blip at the ball's place
-- (left or right of the camera) with its pitch varied by up to a tenth; the varied pitch
-- comes from the game's seeded random, so a recording sounds the same every time.
state.vx = state.vx or 120

function on_scene(name)
    music.play("sounds/hum.wav", { volume = 0.4 })
end

function step(dt)
    local ball = find("ball")
    local x = get(ball, "Transform", "x") + state.vx * dt
    if x < 8 or x > 312 then
        state.vx = -state.vx
        x = math.clamp(x, 8, 312)
        sound.play("sounds/blip.wav", { volume = 0.8, vary = 0.1, at = ball })
    end
    set(ball, "Transform", "x", x)
end
```

## Files

A game lists the sounds its scripts play under `sounds` in the project file: WAV files,
or Ogg Vorbis. An Ogg file of ten seconds or less is decoded whole when the game loads; a
longer one is decoded as it plays and can only be the music (`sound.play` of one is
`E0430`). On the web every Ogg file is decoded when the game starts, so every browser
plays the same samples a desktop does; `frost check` warns of a sound over three minutes in
a game meant for a browser (`W0437`).

## Effects

`sound.play("sounds/blip.wav", { volume = 0.8, pitch = 1, vary = 0.1, at = ball })`
plays a sound once.

- `volume` is 0 to 1 and `pitch` a speed, 0.25 to 4; outside the range is an error.
- `vary = 0.1` draws each play's pitch within a tenth of `pitch` from the game's seeded
  random, so a recording sounds the same every time (and the `math.random` numbers after
  it move).
- `at = entity`, or `x = 40`, pans a 2D play left or right by where it is against the
  camera. In 3D, `at` pans by the thing's side of the camera and fades it with distance:
  full within `near` metres, silent past `far` (`[sound] near` and `far`, 2 and 30 when
  left out).
- At most 4 plays of one sound and 32 in all are heard; one more eases out the quietest.

A playing sound has no handle: it cannot be stopped, looped or moved once asked for, and
there is no positioning beyond the pan and the distance fade.

## Music

`music.play("sounds/hum.wav", { volume = 0.4 })` loops one track, in place of the one
before: there is one music track at a time. `music.stop(1)` fades it out over a second,
`music.pause()` and `music.resume()` hold it and let it go on, and `music.volume(0.2, 2)`
moves its own volume over two seconds. To cross from one track to the next, stop the
first with a fade and play the second when it is done.

## Volumes

`sound.volume(level)` is the gain of everything heard and `sound.effects(level)` of all
that `sound.play` plays; volumes multiply. A settings screen's sliders write
`music.volume` and `sound.effects`.

## Hearing a run without speakers

```sh
frost run --headless --steps 240 --sounds game.toml
```

lists every request with its step, for the sample:

```text
sound: step 0 music sounds/hum.wav 0.4 1
sound: step 76 play sounds/blip.wav 0.8 1.0133123 0.9375
sound: step 228 play sounds/blip.wav 0.8 1.0491563 -0.9375
```

(the volume, the pitch as varied, and the pan: right at the right wall, left at the
left). `--wav out.wav` writes what the run would sound like: 16-bit stereo at 48,000 a
second, the same bytes every run, and prints its peak (`peak: 0.488` here). A peak over 1
would be cut off by a real device, and `frost run` warns of it (`W0438`). `frost bench
--sound` mixes each step's sound as it measures. Over the protocol, `audio.requests`
answers the same list.

## Checks

```sh
frost check game.toml
frost test game.toml
frost run --headless --steps 240 --sounds game.toml
```
