#!/usr/bin/env bash
# test-run.sh: loudness test for the guide "Podcast loudness: measure, set -16 LUFS, re-measure".
# Needs ffmpeg 6.x. Input: adventuresholmes_01_doyle_64kb.mp3 (LibriVox, public domain, https://archive.org/details/adventures_sherlock_holmes_rg_librivox).
# Usage: bash test-run.sh <dir containing that mp3> ; writes in/ out/ work/ and test-results.txt in the current directory.
set -euo pipefail
SRC="${1:?usage: bash test-run.sh <dir with the mp3>}/adventuresholmes_01_doyle_64kb.mp3"
[ -s "$SRC" ] || { echo "missing $SRC" >&2; exit 2; }
HERE="$(cd "$(dirname "$0")" && pwd)"
mkdir -p in out work
R=test-results.txt; : > $R
bash "$HERE/build.sh" "$SRC"
echo "# CONTROL: 1 kHz sine at -20 dBFS peak. Known answer (BS.1770): mono -23.0 LUFS, the same sine on both channels -20.0 LUFS, true peak -20.0" | tee -a $R
ffmpeg -hide_banner -loglevel error -y -f lavfi -i "aevalsrc=0.1*sin(2*PI*1000*t):d=20:s=48000:c=mono" -c:a pcm_s16le in/ctl-sine-mono.wav
ffmpeg -hide_banner -loglevel error -y -i in/ctl-sine-mono.wav -af "pan=stereo|c0=c0|c1=c0" in/ctl-sine-stereo.wav
for f in in/ctl-sine-mono.wav in/ctl-sine-stereo.wav; do bash "$HERE/measure.sh" $f | tee -a $R; done
echo "# INPUTS (before)" | tee -a $R
for f in in/[a-g]-*.wav; do bash "$HERE/measure.sh" $f | tee -a $R; done
echo "# ONE PASS, the Koldflux tool's filter, run in ffmpeg 6.1.1 (cli1)" | tee -a $R
for f in in/[a-g]-*.wav; do b=$(basename $f .wav); ffmpeg -hide_banner -loglevel error -y -i $f -af loudnorm=I=-16:TP=-1.5:LRA=11 -ar 48000 -c:a libmp3lame -q:a 2 out/$b-cli1.mp3; bash "$HERE/measure.sh" out/$b-cli1.mp3 | tee -a $R; done
echo "# TWO PASS, linear requested (cli2), output MP3" | tee -a $R
for f in in/[a-g]-*.wav; do b=$(basename $f .wav); bash "$HERE/normalize-two-pass.sh" $f out/$b-cli2.mp3 | tee -a $R >/dev/null; bash "$HERE/measure.sh" out/$b-cli2.mp3 | tee -a $R; done
