Metadata-Version: 2.5
Name: audio-bed-check
Version: 0.2.0
Summary: Check an audio bed for loops and level steps, and a voiceover mix for speech-over-bed separation.
Project-URL: Homepage, https://github.com/oo-pibe/audio-bed-check
Project-URL: Issues, https://github.com/oo-pibe/audio-bed-check/issues
Project-URL: Security, https://github.com/oo-pibe/audio-bed-check/blob/main/SECURITY.md
License-Expression: MIT
License-File: LICENSE
Keywords: audio,bs1770,ducking,ebu-r128,ffmpeg,loop-detection,loudness,qa,video,voiceover
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Multimedia :: Sound/Audio :: Analysis
Classifier: Topic :: Multimedia :: Video
Requires-Python: >=3.10
Requires-Dist: numpy>=1.26
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Description-Content-Type: text/markdown

# audio-bed-check

If you render video or audio from a script, this tells you when the background audio is broken
before anyone hears it.

![A terminal: a Remotion render passes, then audio-bed-check fails its music bed (a 6.5 s loop, a 7.03 dB jump) and the mix (voice 0.74 LU above the music), and passes the fixed versions with exit 0](https://raw.githubusercontent.com/oo-pibe/audio-bed-check/main/docs/demo.gif)

```
pip install audio-bed-check
audio-bed-check bed ambience.wav                      # does the bed loop? does its level jump?
audio-bed-check separation final.mp4 --vo read.wav    # is the voice far enough above the bed?
```

Exit 0 means fine. Exit 1 means one of the three faults below, with a line saying which and where.

For Node and Remotion projects the same tool is on npm with no dependencies:
`npx audio-bed-check bed music.wav`. See [js/](js/README.md).

## The problem

In a pipeline (Remotion, ffmpeg, a render farm, a script that stitches clips), the background audio
is assembled by code rather than by someone listening. That audio is called the bed: the crowd,
the room tone, the music under a voiceover. Three things go wrong with it, and none of them fails
a render or trips a loudness meter.

- The clip was shorter than the cut, so it was repeated to fill the time. The same crowd roar comes
  round every 12 seconds.
- Two recordings were joined and the level jumps at the seam. The pipeline this tool came out of
  shipped a bed with a 7 dB jump at two joins; every automated check passed, and a person caught it
  by ear.
- The voiceover was mixed too close to the bed. Every word is there if you listen for it, and a
  viewer will not.

By the time someone notices, the file has been posted.

## What this does

It measures the rendered file and gives a verdict a build can act on.

`bed` runs two checks on the bed alone. The loop check looks for the same material appearing twice
and reports how often it repeats. The steps check looks for a jump in level that stays, as opposed
to a crowd surge that comes back, and reports its size and position.

`separation` takes the rendered mix and the voiceover file on its own, finds where the voice is
speaking, and measures how far above the bed it sits, in loudness units, against published floors:
10 LU over music, 15 over ambience, 20 for WCAG.

```
ambience.wav
  FAIL  loop        1.00 (repeats every 12.0s, at or above 0.90)
  ok    step        0.59 dB at 38.15s (max 6.00)
  info  range       3.0 dB   transient 2.7 dB   peak -5.1 dBTP
```

Exit 0 when every check passes, 1 when any fails, 2 when it could not run (a bad flag, an
unreadable file, a bed under 6.5 seconds, no speech found in the voiceover). A `warn` line, such as a file peaking at or
under -60 dBTP or a voiceover that could not be located in the mix, never changes
the exit code. `--json` prints the numbers as a list of objects; the field list is in the command
reference. `python -m audio_bed_check` is the same command.

When the file list comes from a glob, put `--` before it: `audio-bed-check bed -- out/*`. Without
it, a file whose name starts with `-` (say `--max-step=100`) is read as an option.

## What it needs

Python 3.10 or later and ffmpeg, either on PATH or named with `--ffmpeg` or
`AUDIO_BED_CHECK_FFMPEG`. It reads anything ffmpeg reads, video included. Files keep their
channels: mono stays mono, stereo is measured per channel and summed the way BS.1770 does, more
than two channels are downmixed to two by ffmpeg; only the first audio track is read.

Check the bed before it is encoded. ffmpeg's built-in AAC at 128k rebuilt exact loops of steady
noise to scores between 0.88 and 0.97, some under the threshold; textured loops stayed at 1.00 after
AAC and MP3.

On an Apple A18 Pro laptop with 8 GB of memory, `bed` on a 10-minute mono file takes about 5 s of
processor time and 1 GB of memory; an hour takes about 50 s and 3 GB.

## The checks

`loop`: the frame-to-frame changes of the loudness envelope, correlated with themselves at every
lag over the best-matching 10 s window. A copied clip copies its texture, so a loop scores near 1.0
even when only part of the file repeats, or it fades out, or each repeat sits at a different gain.
Among the peaks that reach the threshold (`--loop-threshold`, 0.9), it reports the shortest one
scoring within 0.05 of the strongest. That is the repeat length rather than a multiple of it. A
repeat of 6 seconds or longer (`--min-period`) fails. A shorter one is reported and passes, because
music repeats at bar length. A file under 12 s at the defaults (twice `--min-period`) cannot play
a 6 s repeat twice, so it reports ok with a note saying so.

`steps`: half-second loudness blocks every 0.1 s. The sustained step is the mean level over two
seconds after a half-second gap against the two seconds before; it fails above 6 dB (`--max-step`).
A bad join moves the level and it stays moved. A crowd surge spikes and comes back, so the largest
half-second transient and the range are printed but never fail the file. A change held about two
seconds or longer does fail, even if it comes back: a +7 dB plateau reads 3.9 dB held one second
and 6.7 held two. The steps check needs 6.5 s at the default edge; a shorter file reports ok with a
note.

`separation`: speech windows are found in the voiceover file, then compared with the bed-only gaps
between them. Each window's level is its 90th percentile of 50 ms K-weighted blocks. Both kinds of
window are measured in the mix, so the figure is voice plus bed against bed, which is closer to what
a listener hears than the gain on each stem. Profiles set the minimum: `music` 10 LU (the default),
`ambience` 15 LU, `wcag` 20 LU. The read needs pauses of 0.9 seconds or more. Where the
voiceover starts in the mix is found from the two envelopes, so it does not have to start at zero;
`--vo-offset` sets it by hand. When no start can be trusted (a voice far under the bed, or a bed
with a speech-like rhythm of its own), the mix is measured from zero and at the closest match. If
both agree, that is the verdict, with a warning; if one passes and the other fails, the check
reports it could not tell (exit 2) and asks for `--vo-offset`. A mix whose start was not found
passes only when it passes from both starts tried, and carries the warning that the start was
not found. A mix whose bed-only windows are silent (the voiceover passed as the mix) is refused.

The check compares speech windows with the bed in the gaps between them, so a bed that is ducked
under speech barely changes the figure. Torcoli et al. measured speech against the ducked
background, so treat the floors as approximate for ducked mixes.

| Check | Fails when | Default | Source |
|---|---|---|---|
| loop | repeat score at or above | 0.9, repeats of 6 s or longer | validation below |
| steps | sustained step above | 6 dB | chosen in one production pipeline where a 7 dB join was audible; no published source |
| separation, music | voice less than | 10 LU above bed | Torcoli et al., JAES 2019 |
| separation, ambience | voice less than | 15 LU above bed | Torcoli et al., JAES 2019 |
| separation, wcag | voice less than | 20 LU above bed | WCAG 2 technique G56 (stated there in dB(A) SPL) |

Every default can be changed with a flag; the full list is in
[skills/audio-bed-check/references/cli.md](https://github.com/oo-pibe/audio-bed-check/blob/main/skills/audio-bed-check/references/cli.md).

## Where the numbers come from

The loop check is validated on generated signals. The figures here are from one run of
[scripts/loop_sweep.py](https://github.com/oo-pibe/audio-bed-check/blob/main/scripts/loop_sweep.py),
which prints them. In that run, 400 unlooped beds of 30 to 600 seconds produced no failures
and the worst scored 0.75. Other seeds reach about 0.78, and longer files score higher by chance.
Loops under fades, in part of a file, with crossfaded joins or at changing gain all failed with the
right period, the lowest at 0.96. On one other seed a 10 s loop at gains up to 6 dB apart was named
at 20 s; it still failed. Beds with a 6 dB swell every 8 or 20 seconds passed, the highest at 0.85.

A repeat is seen only when the repeating stretch is at least one period plus the window long. The
window is 10 s, or a third of a file under 30 s. So a 6 to 8 second clip played twice inside a
longer bed usually is not seen, while a file that is just that clip played twice is. The longest repeat it can see is the file
length minus the window: about 10.5 s less than the file, or a third less under 30 s. A gain cycle
with nothing under it, such as a steady tremolo or the same swell twice on room tone, reads as a
loop; the details are in
[references/checks.md](https://github.com/oo-pibe/audio-bed-check/blob/main/skills/audio-bed-check/references/checks.md).

The loudness code is ITU-R BS.1770-4 K-weighting written in numpy. The test suite compares its
momentary loudness with ffmpeg's `ebur128` filter on the same file (median difference under 0.1 LU,
95th percentile under 0.2 LU), and its true peak on a test tone (within 0.2 dB). These tests skip
when no ffmpeg is found, or when the one found was built without `ebur128`.

## From Python

```python
from audio_bed_check import decode, loop_score, level_steps, separation

samples, sr = decode("ambience.wav")
loop_score(samples, sr).passed        # False if it repeats
level_steps(samples, sr).step_db      # the worst sustained step, dB

vo, _ = decode("read.wav")
mix, _ = decode("final.mp4")
separation(vo, mix, sr, min_lu=15).separation_lu
```

The check functions are pure functions of a float array at 48 kHz; `decode` always returns that.

## For coding agents

The package ships an [Agent Skill](https://github.com/oo-pibe/audio-bed-check/tree/main/skills/audio-bed-check)
describing when to run which check and how to read the result. Claude Code users can install it as a
plugin:

```
/plugin marketplace add oo-pibe/audio-bed-check
/plugin install audio-bed-check@audio-bed-check
```

## Prior art

- ITU-R BS.1770-4, the loudness algorithm.
- Rafii and Pardo, REPET, IEEE TASLP 21(1), 2013: the repeating-period idea behind the loop check.
- Torcoli, Freke-Morin, Paulus, Simon and Shirley, "Preferred Levels for Background Ducking to
  Produce Esthetically Pleasing Audio for TV with Clear Speech", JAES 67(12), 2019,
  doi:10.17743/jaes.2019.0052: the source of the 10 LU (music) and 15 LU (ambience) floors. The
  same study found non-expert listeners wanted about 4 LU more than experts.
- WCAG 2 technique G56, the 20 dB speech-over-background rule, stated there in dB(A) SPL.
- ffmpeg's `ebur128` filter, the reference the loudness tests compare against.

## Origin

Extracted from the video pipeline at [Road to Kickoff](https://roadtokickoff.com). A bed there
stepped 7 dB at two joins, got past every automated check, and was caught by ear. The 6 dB step
limit comes from that bed.

## License

MIT.
