#!/usr/bin/env bash
# make-songs-the-same-volume: tests behind the guide. Run from the work folder; writes test-results.txt and commands.log.
# Edge inputs are made in e/. Tracks: Wikimedia Commons (CC0 / public domain, see PROVENANCE). Tool runs: live https://koldflux.com/tools/audio/normalize via tool-run.cjs (Playwright).
set -u
cd "$(dirname "$0")/.."
L=pub/levels.py; LOG=pub/commands.log; : > "$LOG"
run() { # published command, then the real run, exit code
  local pub="$1"; shift; local edge="${EDGE:-}"
  { echo "published: $pub"; [ -n "$edge" ] && echo "edge: $edge"; echo "run: $*"; "$@" 2>&1; echo "exit=$?"; echo; } >> "$LOG"
}
T="chopin-waltz softly anarchobliss shoepop monster-parade vivaldi-autumn"
{
echo "== controls (known answers): 1 kHz sine, amplitude 0.1, mono -> -23.0 LUFS, TP -20.0; same tone as bed at -12 dB -> 12.0 LU; at 0 dB -> 0.0 LU, in-phase sum +6.0 (mix -17.0)"
python3 $L ctl/sine-1k.wav
python3 $L --bed ctl/sine-1k.wav ctl/sine-1k-7s.wav -12
python3 $L --bed ctl/sine-1k.wav ctl/sine-1k-7s.wav 0
echo "== 1. six tracks as downloaded"
python3 $L $(for t in $T; do echo raw/$t.*; done)
echo "== 2. after one pass through the Normalize tool (loudnorm=I=-16:TP=-1.5:LRA=11, MP3 out)"
python3 $L $(for t in $T; do echo out/$t-tool.mp3; done)
echo "== 3. the two tracks more than 1 LU off, through the tool once more"
python3 $L out/shoepop-tool2.mp3 out/vivaldi-autumn-tool2.mp3
echo "== 4. final set (one pass, second pass for the two outliers)"
python3 $L out/chopin-waltz-tool.mp3 out/softly-tool.mp3 out/anarchobliss-tool.mp3 out/shoepop-tool2.mp3 out/monster-parade-tool.mp3 out/vivaldi-autumn-tool2.mp3
echo "== 4b. second pass on all six (for comparison only)"
python3 $L $(for t in $T; do echo out/$t-tool2.mp3; done)
echo "== 5. voice (LibriVox, 60 s) before and after the tool"
python3 $L in/voice.wav out/voice-tool.mp3
echo "== 6. music bed under the normalized voice: monster-parade after the tool, three fader settings"
for g in -12 -18 -24; do python3 $L --bed out/voice-tool.mp3 out/monster-parade-tool.mp3 $g; done
echo "== 7. same -18 dB fader, loudest and quietest track, before and after the tool"
for f in raw/anarchobliss.ogg raw/vivaldi-autumn.mp3 out/anarchobliss-tool.mp3 out/vivaldi-autumn-tool2.mp3; do printf "%s\t" "$f"; python3 $L --bed out/voice-tool.mp3 "$f" -18; done
echo "== 7b. how the mono voice sits in a stereo edit: the same voice (mono file) at full level on both channels, same -18 dB fader"
ffmpeg -v error -y -i out/voice-tool.mp3 -af "pan=stereo|c0=c0|c1=c0" e/voice-stereo.wav
printf "voice channels: out/voice-tool.mp3 %s, e/voice-stereo.wav %s\n" "$(ffprobe -v error -show_entries stream=channels -of csv=p=0 out/voice-tool.mp3)" "$(ffprobe -v error -show_entries stream=channels -of csv=p=0 e/voice-stereo.wav)"
for f in out/voice-tool.mp3 e/voice-stereo.wav; do printf "%s\t" "$f"; python3 $L --bed "$f" out/monster-parade-tool.mp3 -18; done
} > pub/test-results.txt 2>&1
{ echo "== 8. computed from sections 6 and 7"; python3 pub/summary.py pub/test-results.txt; } >> pub/test-results.txt 2>&1
# published commands with edge cases
P1="python3 levels.py track1.mp3 track2.mp3 track3.mp3"
run "$P1" python3 $L out/chopin-waltz-tool.mp3 out/softly-tool.mp3 out/anarchobliss-tool.mp3
printf "chopin-waltz-tool.mp3\r\nsoftly-tool.mp3\r\n\r\n# comment\r\n" > out/tracks-crlf.txt
EDGE="list file with Windows line breaks, a blank line and a comment" run "python3 levels.py --list tracks.txt" python3 $L --list out/tracks-crlf.txt
cp out/monster-parade-tool.mp3 "out/Chanson d’été (mix).mp3"
EDGE="non-ASCII file name (accents and a curly quote)" run "$P1" python3 $L "out/Chanson d’été (mix).mp3"
EDGE="missing file" run "$P1" python3 $L out/nofile.mp3
: > out/empty.mp3; EDGE="empty file" run "$P1" python3 $L out/empty.mp3
echo "not audio" > out/text.mp3; EDGE="text file named .mp3" run "$P1" python3 $L out/text.mp3
P2="python3 levels.py --bed voice.mp3 music.mp3 -18"
run "$P2" python3 $L --bed out/voice-tool.mp3 out/monster-parade-tool.mp3 -18
EDGE="gain typed as text" run "$P2" python3 $L --bed out/voice-tool.mp3 out/monster-parade-tool.mp3 minus18
EDGE="target changed" run "python3 levels.py --target -14 track1.mp3" python3 $L --target -14 out/monster-parade-tool.mp3
EDGE="no arguments" run "$P1" python3 $L
mkdir -p e/a e/b; cp out/monster-parade-tool.mp3 e/a/track.mp3; cp out/softly-tool.mp3 e/b/track.mp3
EDGE="same file name in two folders (path shown instead of name)" run "$P1" python3 $L e/a/track.mp3 e/b/track.mp3
ffmpeg -v error -y -f lavfi -i anullsrc=r=48000:cl=mono -t 5 e/silent.wav; ffmpeg -v error -y -f lavfi -i sine=1000:d=0.2 e/short.wav
EDGE="5 s of silence" run "$P1" python3 $L e/silent.wav
EDGE="0.2 s tone, shorter than one 400 ms block" run "$P1" python3 $L e/short.wav
EDGE="no ffmpeg on PATH" run "$P1" env PATH=/nonexistent "$(command -v python3)" $L out/softly-tool.mp3
EDGE="fractional gain" run "$P2" python3 $L --bed out/voice-tool.mp3 out/monster-parade-tool.mp3 -17.5
EDGE="gain nan" run "$P2" python3 $L --bed out/voice-tool.mp3 out/monster-parade-tool.mp3 nan
EDGE="silent voice" run "$P2" python3 $L --bed e/silent.wav out/monster-parade-tool.mp3 -18
ffmpeg -v error -y -f lavfi -i testsrc=d=2 -an e/videoonly.mp4
EDGE="music file is a video with no sound" run "$P2" python3 $L --bed out/voice-tool.mp3 e/videoonly.mp4 -18
{ echo "== 9. tool runs: time from click to Download MP3 (Playwright log), input and output format"
for t in $T; do i=$(ls raw/$t.*); printf "%s\tpass1 %s\tpass2 %s\tin: %s\tout: %s\n" "$t" "$(grep -o "[0-9.]* s" out/$t-run.log | tail -1)" "$(grep -o "[0-9.]* s" out/$t-run2.log | tail -1)" "$(ffprobe -v error -show_entries stream=codec_name,sample_rate,channels:format=duration -of csv=p=0 "$i" | tr "\n" " ")" "$(ffprobe -v error -show_entries stream=codec_name,sample_rate,channels:format=duration,size -of csv=p=0 out/$t-tool.mp3 | tr "\n" " ")"; done
printf "voice (LibriVox)\tpass1 one run, not timed\tin: %s\tout: %s\n" "$(ffprobe -v error -show_entries stream=codec_name,sample_rate,channels:format=duration -of csv=p=0 in/voice.wav | tr "\n" " ")" "$(ffprobe -v error -show_entries stream=codec_name,sample_rate,channels:format=duration,size -of csv=p=0 out/voice-tool.mp3 | tr "\n" " ")"; } >> pub/test-results.txt 2>&1
echo done
