# Audio-reactive animation

> The `amp`, `bass`, `beat` and `beats` expression variables follow the soundtrack, so any property can pulse, grow or change color with the music. `vidmake analyze` shows what they will see before you design around them.

Four [expression](https://vidmake.app/docs/expressions.md) variables describe the composition's soundtrack at the current
frame. Use them in any expression or `${...}` text and the property moves with the music, with no
manual beat-matching:

| variable | range | meaning |
|---|---|---|
| `amp` | 0..1 | overall loudness |
| `bass` | 0..1 | loudness of the low end (kick drum, bass line) |
| `beat` | 0..1 | 1 on the frame of each detected beat, then decays quickly towards 0 |
| `beats` | 0, 1, 2, ... | number of beats detected so far |

```json
"scale": "=1 + 0.2 * beat"
"height": "=40 + 300 * bass"
"fill": "=pick(beats, ['#4a4640', '#1f5fb0', '#c92a26'])"
"text": "${beats}"
```

**Demo: The four audio variables, live: amp, bass and beat as bars, beats as a counter** (rendered video: https://vidmake.app/media/audio-reactive-meters.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "meters", "description": "The four audio variables, live: amp, bass and beat as bars, beats as a counter",
    "width": 1280, "height": 720, "fps": 30, "duration": "6s", "background": "#f4f1ea",
    "layers": [
      { "id": "music", "type": "audio", "src": "assets/music.mp3", "fadeOut": "0.5s" },
      { "id": "well", "type": "rect", "anchor": "bottom", "x": 520, "y": 560, "width": 110, "height": 380,
        "fill": "#e9e4d8", "repeat": { "count": 3, "layout": "row", "spacing": 170 } },
      { "id": "meter", "type": "rect", "anchor": "bottom", "x": 520, "y": 560, "width": 110,
        "repeat": { "count": 3, "layout": "row", "spacing": 170 },
        "height": "=380 * pick(i, [amp, bass, beat])", "fill": "=pick(i, ['#4a4640', '#161412', '#c92a26'])" },
      { "id": "name", "type": "text", "font": "JetBrains Mono", "fontSize": 22, "letterSpacing": 1, "fill": "#8a857a", "x": 520, "y": 610,
        "repeat": { "count": 3, "layout": "row", "spacing": 170 },
        "text": "${pick(i, ['amp ', 'bass ', 'beat '])}${round(pick(i, [amp, bass, beat]), 2)}" },
      { "id": "count", "type": "text", "text": "${beats}", "font": "Schibsted Grotesk", "fontWeight": 860, "fontSize": 160,
        "letterSpacing": -7, "fill": "#161412", "x": 1030, "y": 330, "scale": "=1 + 0.15 * beat" },
      { "id": "count-label", "type": "text", "text": "beats", "font": "JetBrains Mono", "fontSize": 22, "letterSpacing": 1, "fill": "#8a857a", "x": 1030, "y": 610 }
    ]
  }]
}
```

That demo draws the four values directly: three bars with `height: "=380 * pick(i, [amp, bass, beat])"`
and a counter that prints `${beats}` and scales with `beat`.

## Where the sound comes from

The values are computed from **all audio in the composition you render**, mixed together the way it
will be heard:

- every [audio](https://vidmake.app/docs/audio.md) layer and the sound of every [video](https://vidmake.app/docs/video.md) layer that isn't `muted`,
- including those inside [groups](https://vidmake.app/docs/groups.md) and [components](https://vidmake.app/docs/components.md),
- after `volume`, `fadeIn`/`fadeOut`, `trimStart`, `playbackRate` and `loop` are applied.

The values are sampled at the **global** frame, not the layer's local time, so every layer sees the
same `beat` at the same moment no matter when it starts. A layer that starts at 3 s and reads `beats`
gets the count since the start of the video.

The analysis only runs when an expression or text template somewhere in the project mentions `amp`,
`bass`, `beat` or `beats`, so projects that don't use them pay nothing.

## How the values are computed

vidmake decodes the mix to mono and analyzes it once per frame. Knowing the method makes the values
easier to use:

- **`amp`** is the RMS loudness of each frame's audio. It is **normalized per composition**: it is scaled so
  that the loudest 2% of the soundtrack's frames (the 98th percentile) read 1, and clamped there. A quietly
  mastered track still uses the whole 0..1 range, and a loud, compressed section can sit at 1 for seconds:
  `1` means "as loud as this soundtrack gets", not an absolute level.
- **`bass`** is the same measurement on a low-passed copy of the mix (two filters at about 150 Hz),
  normalized the same way. Kick drums and bass lines dominate it; hi-hats and vocals barely move it.
- Both are smoothed with an instant attack and a release of about 0.1 s, so they jump up immediately on a
  hit and fall back smoothly instead of flickering frame to frame.
- **Beats** are onsets in a bass-weighted energy curve (70% `bass`, 30% `amp`): a frame counts as a beat
  when it is a local peak, is above 0.25, and clearly exceeds the average of the previous 0.4 s. Two beats
  are always at least 0.18 s apart.
- **`beat`** is `1` on the beat's frame and decays exponentially with a time constant of 0.15 s:
  about 0.5 after 0.1 s, 0.26 after 0.2 s and 0.14 after 0.3 s. It never jumps back up until the next beat.
- **`beats`** counts the beats at or before the current frame. On the frame of the first beat it is 1.

This is what the counter in the demo above does over the first beats of `assets/music.mp3`: `scale` is
`1 + 0.15 * beat`, and the notes column shows `${beats}`:

```console
$ vidmake trace audio-reactive-meters.json -l count --every 3 --to 1.6s
 FRAME    TIME        X        Y       W       H    ROT  SCALE  OPAC  NOTES
     0   0.00s   1030.0    330.0    70.9   192.0    0.0   1.15  1.00   "1"
     3   0.10s   1030.0    330.0    70.9   192.0    0.0   1.08  1.00   "1"
     6   0.20s   1030.0    330.0    70.9   192.0    0.0   1.04  1.00   "1"
     9   0.30s   1030.0    330.0    70.9   192.0    0.0   1.02  1.00   "1"
    12   0.40s   1030.0    330.0    70.9   192.0    0.0   1.01  1.00   "1"
    15   0.50s   1030.0    330.0    70.9   192.0    0.0   1.01  1.00   "1"
    18   0.60s   1030.0    330.0    70.9   192.0    0.0   1.00  1.00   "1"
    21   0.70s   1030.0    330.0    70.9   192.0    0.0   1.00  1.00   "1"
    24   0.80s   1030.0    330.0    70.9   192.0    0.0   1.00  1.00   "1"
    27   0.90s   1030.0    330.0    70.9   192.0    0.0   1.00  1.00   "1"
    30   1.00s   1030.0    330.0    97.9   192.0    0.0   1.15  1.00   "2"
    33   1.10s   1030.0    330.0    97.9   192.0    0.0   1.08  1.00   "2"
    36   1.20s   1030.0    330.0    97.9   192.0    0.0   1.04  1.00   "2"
    39   1.30s   1030.0    330.0    97.9   192.0    0.0   1.02  1.00   "2"
    42   1.40s   1030.0    330.0    97.9   192.0    0.0   1.01  1.00   "2"
    45   1.50s   1030.0    330.0    99.2   192.0    0.0   1.15  1.00   "3"
    48   1.60s   1030.0    330.0    99.2   192.0    0.0   1.08  1.00   "3"
```

## When the values are 0

- **No audio at all**: when the composition has no audio layer and no video with sound, all four stay 0.
  `validate` warns about it: `expressions use amp/bass/beat/beats but the composition has no audio; they stay 0`.
- **Before any sound and during silence**: `amp` and `bass` are 0 (after the sound stops they fall smoothly,
  to about 5% within 0.3 s). `beat` is 0 until the first beat. `beats` keeps its count.
- **Muted video**: `"muted": true` removes a video's sound from the mix, and from the analysis.

> [!TIP]
> Because the values are normalized to the loudest part of the composition, a long quiet intro followed by
> a loud drop gives small values during the intro, and the drop may sit at 1 the whole time. If you need a
> reaction to the intro, scale it up there: `"=1 + 0.4 * amp * (gt < 8 ? 2 : 1)"`. [`analyze`](#check-the-soundtrack-with-analyze)
> shows where each part of the track lands before you pick the numbers.

## Check the soundtrack with analyze

You can't hear the music, but `vidmake analyze` can tell you what the four variables will do with it. Give it an
audio or video file:

```console
$ vidmake analyze assets/music.mp3
12.00s at 30.0 fps (360 frames)
beats: 18, tempo ≈120 BPM
  0.00 1.00 1.50 2.00 3.00 3.50 4.00 5.00 5.50 6.00 7.00 7.50 8.00 9.00 9.50 10.00 11.00 11.50s
onsets: 25 (TIME  RISE  PEAK  BAND)
    0.499s  +12.3 dB   -8.9 dBFS  mid
    1.008s  +12.5 dB   -8.8 dBFS  mid
    1.507s  +12.4 dB   -8.7 dBFS  mid
    2.005s  +12.8 dB   -8.4 dBFS  mid
    2.504s  +12.4 dB   -8.8 dBFS  mid
    3.003s  +12.5 dB   -8.7 dBFS  mid
    3.502s  +12.4 dB   -8.6 dBFS  mid
    4.001s  +12.7 dB   -8.3 dBFS  mid
    4.500s  +12.6 dB   -8.6 dBFS  mid
    4.590s  +10.6 dB  -10.6 dBFS  mid
    5.009s  +12.5 dB   -8.8 dBFS  mid
    5.507s  +12.5 dB   -8.7 dBFS  mid
    6.016s  +12.4 dB   -8.8 dBFS  mid
    6.505s  +12.5 dB   -8.6 dBFS  mid
    7.004s  +12.5 dB   -8.7 dBFS  mid
    7.503s  +12.4 dB   -8.6 dBFS  mid
    8.012s  +12.4 dB   -8.6 dBFS  mid
    8.501s  +12.4 dB   -8.6 dBFS  mid
    9.010s  +12.3 dB   -8.8 dBFS  mid
    9.508s  +12.6 dB   -8.7 dBFS  mid
   10.007s  +12.7 dB   -8.5 dBFS  mid
   10.087s  +10.7 dB  -10.5 dBFS  mid
   10.506s  +12.8 dB   -8.6 dBFS  mid
   11.005s  +12.4 dB   -8.6 dBFS  mid
   11.504s  +12.5 dB   -8.7 dBFS  mid

        MEAN    P10 MEDIAN    P90 AT CEILING
amp     0.54   0.27   0.49   0.90         7%
bass    0.31   0.10   0.24   0.61         3%

   TIME  AMP mean/peak                   BASS mean/peak                  BEATS   (bar = mean, ▏ = peak)
  0.00s  0.54/1.00 ███████████        ▏ 0.59/1.00 ████████████       ▏   1
  0.50s  0.54/1.00 ███████████        ▏ 0.32/0.61 ██████     ▏           0
  1.00s  0.54/1.00 ███████████        ▏ 0.20/0.38 ████   ▏               1
  1.50s  0.54/1.00 ███████████        ▏ 0.13/0.26 ███ ▏                  1
  2.00s  0.54/1.00 ███████████        ▏ 0.59/1.00 ████████████       ▏   1
  2.50s  0.54/1.00 ███████████        ▏ 0.32/0.61 ██████     ▏           0
  3.00s  0.54/1.00 ███████████        ▏ 0.20/0.38 ████   ▏               1
  3.50s  0.54/1.00 ███████████        ▏ 0.13/0.26 ███ ▏                  1
  4.00s  0.54/1.00 ███████████        ▏ 0.59/1.00 ████████████       ▏   1
  4.50s  0.54/1.00 ███████████        ▏ 0.32/0.61 ██████     ▏           0
  5.00s  0.54/1.00 ███████████        ▏ 0.20/0.38 ████   ▏               1
  5.50s  0.54/1.00 ███████████        ▏ 0.14/0.27 ███ ▏                  1
  6.00s  0.54/1.00 ███████████        ▏ 0.59/1.00 ████████████       ▏   1
  6.50s  0.54/1.00 ███████████        ▏ 0.32/0.61 ██████     ▏           0
  7.00s  0.54/1.00 ███████████        ▏ 0.20/0.38 ████   ▏               1
  7.50s  0.54/1.00 ███████████        ▏ 0.13/0.26 ███ ▏                  1
  8.00s  0.54/1.00 ███████████        ▏ 0.59/1.00 ████████████       ▏   1
  8.50s  0.54/1.00 ███████████        ▏ 0.32/0.60 ██████     ▏           0
  9.00s  0.54/1.00 ███████████        ▏ 0.20/0.38 ████   ▏               1
  9.50s  0.54/1.00 ███████████        ▏ 0.13/0.26 ███ ▏                  1
 10.00s  0.54/1.00 ███████████        ▏ 0.59/1.00 ████████████       ▏   1
 10.50s  0.54/1.00 ███████████        ▏ 0.32/0.60 ██████     ▏           0
 11.00s  0.54/1.00 ███████████        ▏ 0.20/0.38 ████   ▏               1
 11.50s  0.54/1.00 ███████████        ▏ 0.13/0.26 ███ ▏                  1
```

- **Beats**: how many, the tempo (from the median gap between beats, folded into 70..180 BPM) and every beat time
  in seconds. These are exactly the moments `beat` jumps to 1 and `beats` counts up. This track has a beat at
  0, 1 and 1.5 s and then repeats every two seconds.
- **The table**: mean, 10th percentile, median and 90th percentile of `amp` and `bass` over the whole track, and
  **AT CEILING**, the share of frames at 0.98 or more. A high share means anything driven by that variable spends
  that much of the video at its maximum.
- **The curve**: one row per `--every` interval (default `0.5s`): the mean and peak of `amp` and `bass` in that
  interval, drawn as a bar (█ = mean, ▏ = peak), and how many beats start in it.

`--fps` sets the frame rate for a media file (default 30; match your composition's), `--every` takes
frames or a time, and `--json` returns the same report as data: `fps`, `frames`, `duration`,
`beats: { count, tempo, times }`, `amp` and `bass` as `{ mean, p10, median, p90, atCeiling, longestAtCeiling }`
(`longestAtCeiling` is `{ from, to }` in seconds, or `null`), `warnings`, and `curve` rows of
`{ time, frame, amp, ampPeak, bass, bassPeak, beats }`.

### Read the numbers before picking multipliers

A few examples of turning the report into expressions for this track:

- `bass` has a median of 0.24 and a 90th percentile of 0.61: a meter that should reach the top on the kicks can
  use `"=min(1, bass / 0.6)"` instead of plain `bass`.
- `amp` peaks at 1 in every half-second row and its median is about 0.5, so `"=1 + 0.1 * amp"` breathes around
  1.05 rather than resting at 1. Squaring it (`pow(amp, 2)`) pushes the quiet frames down and keeps the hits.
- The tempo is 120 BPM and the beat pattern (0, 1, 1.5 s) repeats every 2 s, so cuts every 2 s, or every 3
  `beats` on this track, land on the downbeat.

When a variable is at its ceiling for 10% of the frames or more, `analyze` warns, names the longest stretch and
suggests a fix. The test clip's soundtrack is a steady tone, so both variables are pinned at 1:

```console
$ vidmake analyze assets/clip.mp4 --every 1s
6.00s at 30.0 fps (180 frames)
beats: 1
  0.00s
onsets: 0 (TIME  RISE  PEAK  BAND)

        MEAN    P10 MEDIAN    P90 AT CEILING
amp     0.98   0.97   0.98   1.00        63%
bass    0.98   0.97   0.98   0.99        61%

   TIME  AMP mean/peak                   BASS mean/peak                  BEATS   (bar = mean, ▏ = peak)
  0.00s  0.98/1.00 ████████████████████ 0.98/1.00 ████████████████████   1
  1.00s  0.98/1.00 ████████████████████ 0.98/1.00 ████████████████████   0
  2.00s  0.98/1.00 ████████████████████ 0.98/1.00 ████████████████████   0
  3.00s  0.98/1.00 ████████████████████ 0.98/0.99 ████████████████████   0
  4.00s  0.99/1.00 ████████████████████ 0.98/1.00 ████████████████████   0
  5.00s  0.98/1.00 ████████████████████ 0.98/1.00 ████████████████████   0
warning: `amp` is at its ceiling (≥0.98) for 63% of the frames, longest stretch 4.033s–4.467s; anything driven by `amp` will sit at its maximum there. Scale it down (`amp * 0.6`), compress it (`pow(amp, 2)`) or use `beat` for punctuation
warning: `bass` is at its ceiling (≥0.98) for 61% of the frames, longest stretch 4.033s–4.467s; anything driven by `bass` will sit at its maximum there. Scale it down (`bass * 0.6`), compress it (`pow(bass, 2)`) or use `beat` for punctuation
```

`no beats detected` is the other warning: `beat` and `beats` will stay 0, so use `amp`/`bass` or time-based
animation instead.

### Analyze the project, not just the file

Given a project file, `analyze` mixes the composition's soundtrack exactly as the expressions hear it: every
audio layer and unmuted video, after `trimStart`, `volume`, fades, `playbackRate` and `loop`, normalized for that
composition (`-c <id>` picks one; a composition without sound fails with ``error: composition `main` has no audio``).
Here the music starts 1 s into the file and ducks to 30% under a voice-over between 2.5 and 4.5 s:

**Demo: Music trimmed by 1 s and ducked under a voice-over: amp and beat follow the mix, not the file** (rendered video: https://vidmake.app/media/audio-reactive-analyze.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk-Italic.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "compositions": [{
    "id": "ducked", "description": "Music trimmed by 1 s and ducked under a voice-over: amp and beat follow the mix, not the file",
    "width": 1280, "height": 720, "fps": 30, "duration": "6s", "background": "#f4f1ea",
    "layers": [
      { "id": "music", "type": "audio", "src": "assets/music.mp3", "trimStart": "1s", "fadeOut": "0.5s",
        "volume": [[0, 1], ["2.4s", 1], ["2.6s", 0.3], ["4.4s", 0.3], ["4.6s", 1]] },
      { "id": "pulse", "type": "ellipse", "x": 330, "y": 300, "width": 150, "fill": "#161412", "scale": "=1 + 0.35 * beat" },
      { "id": "track", "type": "rect", "anchor": "left", "x": 540, "y": 300, "width": 440, "height": 28, "fill": "#e9e4d8" },
      { "id": "level", "type": "rect", "anchor": "left", "x": 540, "y": 300, "width": "=28 + 412 * amp", "height": 28, "fill": "#c92a26" },
      { "id": "beats", "type": "text", "x": 330, "y": 440, "font": "JetBrains Mono", "fontSize": 22, "letterSpacing": 2.6, "uppercase": true, "fill": "#8a857a", "text": "beats ${beats}" },
      { "id": "amp", "type": "text", "x": 760, "y": 440, "font": "JetBrains Mono", "fontSize": 22, "letterSpacing": 2.6, "uppercase": true, "fill": "#8a857a", "text": "amp ${amp:2}" },
      { "id": "voice", "type": "text", "y": 590, "start": "2.5s", "duration": "2s", "enter": "fade", "exit": "fade",
        "font": "Schibsted Grotesk", "italic": true, "fontSize": 48, "letterSpacing": -1, "fill": "#161412", "text": "“…and the music steps back.”" }
    ]
  }]
}
```

```console
$ vidmake analyze audio-reactive-analyze.json
6.00s at 30.0 fps (180 frames)
beats: 7, tempo ≈120 BPM
  0.00 0.50 1.00 2.00 3.00 4.57 5.00s
onsets: 11 (TIME  RISE  PEAK  BAND)
    0.509s  +12.4 dB   -8.8 dBFS  mid
    1.008s  +12.8 dB   -8.5 dBFS  mid
    1.507s  +12.6 dB   -8.6 dBFS  mid
    2.005s  +12.4 dB   -8.7 dBFS  mid
    2.504s  +11.5 dB  -12.8 dBFS  mid
    3.003s  +12.8 dB  -18.8 dBFS  mid
    3.502s  +12.7 dB  -18.8 dBFS  mid
    4.001s  +12.3 dB  -19.1 dBFS  mid
    4.580s  +14.4 dB  -11.1 dBFS  mid
    5.019s  +12.5 dB   -8.8 dBFS  mid
    5.507s  +12.1 dB   -9.0 dBFS  mid

        MEAN    P10 MEDIAN    P90 AT CEILING
amp     0.40   0.11   0.33   0.82         3%
bass    0.23   0.05   0.15   0.54         2%

   TIME  AMP mean/peak                   BASS mean/peak                  BEATS   (bar = mean, ▏ = peak)
  0.00s  0.54/1.00 ███████████        ▏ 0.20/0.40 ████   ▏               1
  0.50s  0.54/1.00 ███████████        ▏ 0.13/0.26 ███ ▏                  1
  1.00s  0.54/1.00 ███████████        ▏ 0.59/1.00 ████████████       ▏   1
  1.50s  0.54/1.00 ███████████        ▏ 0.32/0.61 ██████     ▏           0
  2.00s  0.54/1.00 ███████████        ▏ 0.19/0.38 ████   ▏               1
  2.50s  0.20/0.59 ████       ▏         0.05/0.15 █ ▏                    0
  3.00s  0.16/0.30 ███  ▏               0.18/0.34 ████  ▏                1
  3.50s  0.16/0.30 ███  ▏               0.10/0.18 ██ ▏                   0
  4.00s  0.17/0.30 ███  ▏               0.06/0.11 █▏                     0
  4.50s  0.51/0.77 ██████████    ▏      0.13/0.19 ███▏                   1
  5.00s  0.54/1.00 ███████████        ▏ 0.59/1.00 ████████████       ▏   1
  5.50s  0.34/0.97 ███████           ▏  0.20/0.59 ████       ▏           0
```

Compare it with the file's report above. The beat times moved 1 s earlier because of `trimStart`. Under the
duck, `amp` drops to a mean of about 0.16 and only the strongest beat, the downbeat at 3 s, is still loud enough
to count: the beats at 2.5 s and 4 s are gone. The one at 4.5 s falls on the volume ramp back up and is detected
late, at 4.57 s. If the pulse should keep going under the voice-over, duck less, or animate it from time with the
beat times in the file's report (muted audio is not analyzed, so a silent copy of the music won't help).

> [!AGENT]
> Run `vidmake analyze music.mp3` before writing any audio-reactive expression, and
> `vidmake analyze project.json` after adding fades, trims or volume changes. Choose multipliers from the
> percentiles, place cuts on the reported beat times, and treat a ceiling warning as a bug to fix with a smaller
> multiplier, `pow(...)` or `beat`.

## Examples

### Pulse on the beat

The classic: `scale` with `beat` for a punch on each hit, `amp` for a continuous breathing ring, and
`bass` driving a [glow](https://vidmake.app/docs/effects.md#glow). The rays rotate by a fixed step per beat with
`beats * 7.5`, so they tick round in time with the music:

**Demo: A beat-synced pulse: scale and glow follow beat, a ring follows amp, the rays turn on every beat** (rendered video: https://vidmake.app/media/audio-reactive-pulse.mp4)

```json
{
  "fonts": [{ "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" }],
  "compositions": [{
    "id": "pulse", "description": "A beat-synced pulse: scale and glow follow beat, a ring follows amp, the rays turn on every beat",
    "width": 1280, "height": 720, "fps": 30, "duration": "6s", "background": "#131210",
    "layers": [
      { "id": "music", "type": "audio", "src": "assets/music.mp3", "fadeOut": "0.5s" },
      { "id": "ray", "type": "rect", "width": 4, "height": 40, "fill": "#8f8a7f", "x": 640, "y": 360,
        "repeat": { "count": 24, "layout": "radial", "radius": 250, "rotate": true },
        "rotation": "=beats * 7.5", "opacity": "=0.5 + 0.5 * beat" },
      { "id": "ring", "type": "ellipse", "fill": null, "stroke": "#efebe2", "strokeWidth": 2, "width": 300, "height": 300,
        "scale": "=0.9 + 0.35 * amp", "opacity": "=0.3 + 0.7 * amp" },
      { "id": "core", "type": "ellipse", "width": 220, "fill": "#f05a4e", "scale": "=1 + 0.18 * beat",
        "glow": { "radius": 40, "strength": "=0.2 + 1.2 * bass", "color": "#f05a4e" } },
      { "id": "word", "type": "text", "text": "on the beat", "font": "Schibsted Grotesk", "fontWeight": 860, "fontSize": 32,
        "letterSpacing": -1.3, "fill": "#131210", "scale": "=1 + 0.18 * beat" }
    ]
  }]
}
```

Keep the multipliers small. `1 + 0.2 * beat` is a clear pulse; `1 + beat` doubles the size and reads as a
glitch.

### Color cycling on beats

`beats` is an integer that goes up by one on every beat, so `pick(beats, [...])` switches to the next
color on each hit. Adding `i` gives every [repeated](https://vidmake.app/docs/repeat.md) copy its own offset into the palette:

**Demo: Color cycling on beats: pick(beats + i, palette) shifts every tile one color per beat** (rendered video: https://vidmake.app/media/audio-reactive-colors.mp4)

```json
{
  "fonts": [{ "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" }],
  "compositions": [{
    "id": "colors", "description": "Color cycling on beats: pick(beats + i, palette) shifts every tile one color per beat",
    "width": 1280, "height": 720, "fps": 30, "duration": "6s", "background": "#f4f1ea",
    "layers": [
      { "id": "music", "type": "audio", "src": "assets/music.mp3", "fadeOut": "0.5s" },
      { "id": "tile", "type": "rect", "width": 150, "height": 150, "cornerRadius": 4, "x": 640, "y": 290,
        "repeat": { "count": 12, "layout": "grid", "cols": 6, "spacing": 175 },
        "fill": "=pick(beats + i, ['#e9e4d8', '#161412', '#d8d1c1', '#1f5fb0', '#e9e4d8', '#c92a26', '#4a4640'])",
        "scale": "=1 - 0.08 * beat" },
      { "id": "title", "type": "text", "text": "beat ${pad(beats, 2)}", "font": "Schibsted Grotesk", "fontWeight": 860,
        "fontSize": 88, "letterSpacing": -4, "fill": "#161412", "y": 590 }
    ]
  }]
}
```

### Bars and meters

For an equalizer-style bar or a meter, use `bass` or `amp` directly on a size:

```json
{ "id": "bar", "type": "rect", "anchor": "bottom", "x": 640, "y": 600, "width": 40, "cornerRadius": 4,
  "repeat": { "count": 9, "layout": "row", "spacing": 60 },
  "height": "=30 + 280 * bass * (0.6 + 0.4 * noise(t * 3, i))", "fill": "#161412" }
```

Every bar here follows the same `bass` value, varied a little per bar with `noise`. For a real
frequency spectrum (each bar a different band), use the [waveform](https://vidmake.app/docs/waveform.md) layer instead.

### More ideas

| effect | expression |
|---|---|
| flash the background on each beat | `"opacity": "=0.6 * beat"` on a full-frame rect |
| shake on hits | `"x": "=640 + 12 * beat * wiggle(20, 1)"` |
| RGB split on the kick | `"chromaticAberration": "=10 * beat"` |
| zoom in on the bass | `"scale": "=1 + 0.05 * bass"` on a group holding the whole scene |
| a different word every 4 beats | `"text": "${pick(floor(beats / 4), ['make', 'it', 'move'])}"` |
| alternate left and right | `"x": "=beats % 2 == 0 ? 400 : 880"` |
| brightness follows loudness | `"brightness": "=0.7 + 0.5 * amp"` |

> [!AGENT]
> `analyze` gives the overview; for exact values at exact frames, put
> `"text": "${round(amp, 2)} ${round(bass, 2)} ${beats}"` on a temporary text layer and run
> `vidmake trace project.json -l <id> --every 3`. The notes column shows the values frame by frame.
