#!/usr/bin/env bash
# Part 3: does the volume box of the Koldflux tool do what the percent says? Music-only window 10-14 s of the mixed Reel file, tool vs ffmpeg volume filter.
set -u
export PATH=$HOME/.nvm/versions/node/v24.21.0/bin:$PATH
cd "$(dirname "$0")"
win() { ffmpeg -nostats -ss $2 -t $3 -i "$1" -map 0:a -af volumedetect -f null - 2>&1 | awk -F': ' '/mean_volume/ {print $2}'; }
echo "## part 3: volume box, music-only window 10-14 s (the 6.4 s voice is over), mean_volume dB; reference = music at 100% = $(win out/B-replace.mp4 10 4)"
for v in 100 50 25 10; do
  node tool.cjs in/reel-voice.mp4 in/music.mp3 mix $v out/L$v.mp4 > /dev/null 2>&1
  echo "tool mix at $v%: $(win out/L$v.mp4 10 4) | voice window 1.8-6.0 s: $(win out/L$v.mp4 1.8 4.2)"
done
for v in 100 50 25 10; do
  case $v in 100) f=1.0;; 50) f=0.5;; 25) f=0.25;; 10) f=0.1;; esac
  ffmpeg -v error -y -i in/music.mp3 -af "volume=$f" -t 20 work/ctl$v.wav
  echo "control: ffmpeg volume=$f on the music file alone: $(win work/ctl$v.wav 10 4) (arithmetic: 20*log10($f) = $(python3 -c "import math;print(round(20*math.log10($f),1))") dB from the 100% reference)"
done
