#!/usr/bin/env bash
# test-run.sh: test for the guide "Voice recording too quiet: raise it, then check the hiss".
# 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; : > work/build-notes.txt
bash "$HERE/build.sh" "$SRC"
echo "# BUILD NOTES" | tee -a $R; cat work/build-notes.txt | tee -a $R
echo "# CONTROL A: 1 kHz sine blocks of 0.5 s at -26 dBFS RMS alternating with pink noise blocks at -60 dBFS RMS. Known answer: speech -26, floor -60, gap 34" | tee -a $R
ffmpeg -hide_banner -loglevel error -y -i work/hiss60.wav -t 0.5 -c:a pcm_s16le work/n.wav
ffmpeg -hide_banner -loglevel error -y -f lavfi -i "aevalsrc=0.07079*sin(2*PI*1000*t):d=0.5:s=48000:c=mono" -c:a pcm_s16le work/s.wav
ffmpeg -hide_banner -loglevel error -y -f concat -safe 0 -i <(for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do echo "file '$PWD/work/s.wav'"; echo "file '$PWD/work/n.wav'"; done) -c:a pcm_s16le in/ctl-sine-noise.wav
bash "$HERE/check-level.sh" in/ctl-sine-noise.wav | tee -a $R
echo "# CONTROL B: the same file turned down 20 dB, no filter: speech, floor and I must all drop by exactly 20 and the gap must not move" | tee -a $R
ffmpeg -hide_banner -loglevel error -y -i in/ctl-sine-noise.wav -af volume=-20dB -c:a pcm_s16le in/ctl-sine-noise-m20.wav
bash "$HERE/check-level.sh" in/ctl-sine-noise-m20.wav | tee -a $R
echo "# INPUTS (before)" | tee -a $R
for f in in/q*.wav; do bash "$HERE/check-level.sh" $f | tee -a $R; done
echo "# THE TOOL'S FILTER loudnorm=I=-16:TP=-1.5:LRA=11, run in ffmpeg 6.1.1, MP3 out" | tee -a $R
for f in in/q*.wav in/ctl-sine-noise-m20.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-ln.mp3; bash "$HERE/check-level.sh" out/$b-ln.mp3 | tee -a $R; done
echo "# PLAIN GAIN to -16 LUFS (volume= only, no limiter), WAV out; clipped = samples at full scale" | tee -a $R
for f in in/q*.wav; do b=$(basename $f .wav); I=$(bash "$HERE/check-level.sh" $f | sed -n 's/.*I=\(-[0-9.]*\) LUFS.*/\1/p'); g=$(awk -v i="$I" 'BEGIN{printf "%.2f", -16-i}'); ffmpeg -hide_banner -loglevel error -y -i $f -af "volume=${g}dB" -c:a pcm_s16le out/$b-gain.wav; printf "%s gain=%s dB\t" $b $g | tee -a $R; bash "$HERE/check-level.sh" out/$b-gain.wav | tee -a $R; ffmpeg -hide_banner -nostats -i out/$b-gain.wav -af astats=metadata=0 -f null - 2>&1 | awk -v b=$b '/Overall/ {o=1} o && /Number of samples/ {s=$NF} o && /Peak count/ {p=$NF} END{print "  " b " peak-count-of-max-sample=" p}' | tee -a $R; done
echo "# PEAK-SAFE GAIN: volume= with gain = -1.5 minus the true peak read before (no limiter, no clipping), WAV out" | tee -a $R
for f in in/q*.wav; do b=$(basename $f .wav); TP=$(bash "$HERE/check-level.sh" $f | sed -n 's/.*TP=\(-[0-9.]*\) dBTP.*/\1/p'); g=$(awk -v t="$TP" 'BEGIN{printf "%.1f", -1.5-t}'); ffmpeg -hide_banner -loglevel error -y -i $f -af "volume=${g}dB" -c:a pcm_s16le out/$b-safe.wav; printf "%s gain=%s dB\t" $b $g | tee -a $R; bash "$HERE/check-level.sh" out/$b-safe.wav | tee -a $R; done
echo "# GAIN TO -16 LUFS THEN A LIMITER at -1.5 dBFS (alimiter, 5 ms attack, 50 ms release), WAV out" | tee -a $R
for f in in/q*.wav; do b=$(basename $f .wav); g=$(awk -v i="$(bash "$HERE/check-level.sh" $f | sed -n 's/.*I=\(-[0-9.]*\) LUFS.*/\1/p')" 'BEGIN{printf "%.2f", -16-i}'); ffmpeg -hide_banner -loglevel error -y -i $f -af "volume=${g}dB,alimiter=limit=0.841:attack=5:release=50:level=disabled" -c:a pcm_s16le out/$b-lim.wav; printf "%s gain=%s dB\t" $b $g | tee -a $R; bash "$HERE/check-level.sh" out/$b-lim.wav | tee -a $R; done
