# Audio

> Audio layers — music, voice-over and sound effects with `trimStart`, `playbackRate`, `loop`, animated `volume`, `fadeIn`/`fadeOut`, how they are mixed, and how to check them in the timeline.

An `audio` layer plays sound and draws nothing. Use it for music, voice-over and sound effects; it is
timed like any other layer, so a whoosh can start exactly when a title slides in.

```json
{ "id": "music", "type": "audio", "src": "assets/music.mp3", "volume": 0.8, "fadeIn": "1s", "fadeOut": "2s" }
```

| field | default | notes |
|---|---|---|
| `src` | required | Any audio file ffmpeg can read (mp3, wav, m4a, aac, ogg, flac...), or a video file for its soundtrack. |
| `start`, `duration`, `end` | `0`, media length | When it plays. See [Duration](#duration). |
| `trimStart` | `0` | Where in the file to start, as a time (`"12s"`). |
| `playbackRate` | `1` | Speed, pitch preserved. Must be > 0. A plain number. |
| `loop` | `false` | Start the file over when it ends. |
| `volume` | `1` | Linear gain. A number, keyframes or an expression. |
| `fadeIn`, `fadeOut` | `0` | Fade lengths at the layer's start and end (times). |
| `muted` | `false` | Silence the layer without deleting it. |

Visual fields (position, size, opacity, effects) have no effect: an audio layer draws nothing. A file
with no audio stream is an error.

## Duration

Without `duration` or `end`, an audio layer lasts for the rest of its file:
`(file length − trimStart) ÷ playbackRate`. That fixed length counts toward a composition's
`"duration": "auto"`, so a project whose length should match its soundtrack can leave `duration` out.

- `"loop": true` without a `duration` lasts as long as the parent, repeating the file. This is the usual
  way to lay a short music bed under a longer video.
- With an explicit `duration` the sound is cut there, and `fadeOut` ends at the cut.
- Inside a [group](https://vidmake.app/docs/groups.md), `start` is relative to the group and the sound stops when the group
  ends.

## Volume automation

`volume` is a gain multiplier: `0` is silent, `1` the file's level, `2` twice as loud. Like any numeric
field it can be animated, and times are local to the layer:

```json title="fade up, hold, duck under a voice-over, fade out"
"volume": [[0, 0], ["1s", 0.8], ["4s", 0.8], ["4.5s", 0.25, "easeInOut"], ["9s", 0.25], ["10s", 0]]
```

Expressions work too, e.g. `"volume": "=0.8 * smoothstep(0, 2, t)"`. Animated volume is sampled ten
times per second and interpolated, which is plenty for fades and ducking but not meant for
sample-accurate effects.

`fadeIn` and `fadeOut` multiply on top of `volume`, so a constant `volume` plus two fades is the
shortest way to write the common case.

**Demo: An audio layer with fades; the waveform and the loudness meter show what you hear** (rendered video: https://vidmake.app/media/audio-music.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "music", "description": "An audio layer with fades; the waveform and the loudness meter show what you hear",
    "width": 1280, "height": 720, "fps": 30, "duration": "6s", "background": "#f4f1ea",
    "layers": [
      { "id": "music", "type": "audio", "src": "assets/music.mp3", "trimStart": "2s", "duration": "6s",
        "volume": 0.8, "fadeIn": "1s", "fadeOut": "1.5s" },
      { "id": "title", "type": "text", "text": "music.mp3", "y": "24%", "font": "Schibsted Grotesk", "fontWeight": 860,
        "fontSize": 104, "letterSpacing": -4.5, "fill": "#161412", "scale": "=1 + 0.06 * beat" },
      { "id": "scope", "type": "waveform", "src": "assets/music.mp3", "trimStart": "2s", "mode": "line", "y": "52%",
        "width": 900, "height": 180, "stroke": "#161412", "strokeWidth": 3, "scaleY": "=min(1, 1.8 * amp)" },
      { "id": "meter-track", "type": "rect", "anchor": "left", "x": 340, "y": "78%", "width": 600, "height": 8,
        "fill": "#e9e4d8" },
      { "id": "meter", "type": "rect", "anchor": "left", "x": 340, "y": "78%", "width": "=600 * amp", "height": 8,
        "fill": "#c92a26" },
      { "id": "meter-label", "type": "text", "y": "86%", "font": "JetBrains Mono", "fontSize": 20, "letterSpacing": 1.6,
        "fill": "#8a857a", "text": "amp ${amp:2}  ·  fadeIn 1s  ·  fadeOut 1.5s" }
    ]
  }]
}
```

In the demo, the loudness meter reads [`amp`](https://vidmake.app/docs/audio-reactive.md), the soundtrack's level after
volume and fades, so you can see the fade-in and fade-out. The scope is a [waveform](https://vidmake.app/docs/waveform.md),
which draws the file itself and is not affected by the layer's volume.

## Mixing

Every audio layer and every unmuted [video](https://vidmake.app/docs/video.md#sound) is mixed into the output at its own
volume. The mix is a plain sum (no normalization or automatic ducking), so:

- keep music around `0.3`–`0.6` under speech, and
- check the overall level when many sounds overlap; several loud sources at `1` can clip.

The result is 48 kHz stereo, encoded as AAC in mp4, Opus in webm and PCM in mov. `vidmake render out.mp3`
(or `.wav`, `.m4a`) renders only the soundtrack; `--muted` renders a silent video. GIF and PNG
sequences have no sound. See [Rendering](https://vidmake.app/docs/rendering.md).

## Checking audio in the timeline

Sound is invisible in a storyboard, so check it with `vidmake timeline`. Layers that make sound get a ♪,
and an **Audio** section lists every clip that will be mixed, with where it plays, its source offset
and its rate:

```console
$ vidmake timeline audio-music.json
Composition `music` — 1280x720 @ 30fps, 180 frames (6.00s)

ID           TYPE         START      END      DUR  DETAILS
music        audio        0.00s    6.00s    6.00s  src:assets/music.mp3 ♪
title        text         0.00s    6.00s    6.00s  "music.mp3"
scope        waveform     0.00s    6.00s    6.00s  src:assets/music.mp3
meter-track  rect         0.00s    6.00s    6.00s  
meter        rect         0.00s    6.00s    6.00s  
meter-label  text         0.00s    6.00s    6.00s  "amp 0.00  ·  fadeIn 1s  ·  fadeOut 1.5s"

             |0s        1s        2s        3s        4s        5s        |
music        |████████████████████████████████████████████████████████████|
title        |████████████████████████████████████████████████████████████|
scope        |████████████████████████████████████████████████████████████|
meter-track  |████████████████████████████████████████████████████████████|
meter        |████████████████████████████████████████████████████████████|
meter-label  |████████████████████████████████████████████████████████████|

Audio:
  music                   0.00s →    6.00s  src:assets/music.mp3  trim:2.00s rate:1
```

`vidmake inspect` lists an audio layer while it plays (with a placeholder box, since it draws nothing),
which confirms it is active at that time. To see the level over time, put `amp` in a text layer or a
meter as in the demo above.

## Audio-reactive visuals

The soundtrack also drives expressions: `amp` (loudness), `bass`, `beat` (a pulse on each detected
beat) and `beats` (a count). `"scale": "=1 + 0.06 * beat"` makes a title pulse with the music. See
[Audio-reactive](https://vidmake.app/docs/audio-reactive.md).

## Tips

- Sound effects are short audio layers with a `start`: `{ "id": "whoosh", "type": "audio",
  "src": "sfx/whoosh.wav", "start": "1.2s", "volume": 0.6 }`.
- To skip a slow intro, use `trimStart`: `"trimStart": "8.5s"` starts the music at its first downbeat.
- To use the soundtrack of a video without its picture, point an audio layer at the video file.
- `vidmake probe music.mp3` tells you the length before you plan the edit.
