-
-
Save paskozdilar/6095fe73c80ad21fda3f518177699149 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash | |
set -euo pipefail | |
function main() { | |
# Check for argument | |
if [ $# -ne 1 ] | |
then | |
echo "usage: $0 INFILE" | |
exit 1 | |
fi | |
# Define bitrates to check | |
local INFILE="$1" | |
local BITRATES="320 256 224 192 160 128 112 96 80 64 56 48 40 32" | |
# Check if file exists | |
if ! [ -f "$INFILE" ] | |
then | |
echo "file not found: $INFILE" | |
exit 1 | |
fi | |
# Remove temporary files on exit | |
trap 'rm -f .tmp*.wav .tmp*.mp3' EXIT | |
# Check if lame and sox commands exist | |
for cmd in lame sox | |
do | |
if ! which "$cmd" >/dev/null | |
then | |
echo "command not found: $cmd" | |
exit 1 | |
fi | |
done | |
# Decode original file to wav and invert amplitude | |
decode "$INFILE" .tmp.src.wav -1 | |
# Decode file to bitrate and compare differences | |
for BITRATE in $BITRATES | |
do | |
# compress "$INFILE" .tmp.mp3 "$bitrate" | |
compress "$INFILE" .tmp.mp3 "$BITRATE" | |
decode .tmp.mp3 .tmp.wav | |
printf "%3s: %s\n" "$BITRATE" \ | |
"$(compare .tmp.src.wav .tmp.wav \ | |
2>&1 \ | |
| grep 'RMS.*amplitude' \ | |
| awk '{print $3}')" | |
done | |
} | |
# Compress mp3 file with given constant bit rate | |
function compress() { | |
local INFILE="$1" | |
local OUTFILE="$2" | |
local BITRATE="$3" | |
lame \ | |
--quiet \ | |
-q 0 \ | |
"$INFILE" \ | |
-b "$BITRATE" \ | |
"$OUTFILE" | |
} | |
# Decode mp3 file into wav | |
function decode() { | |
local INFILE="$1" | |
local OUTFILE="$2" | |
local VOLUME="${3-1}" # set to -1 to invert signal | |
lame \ | |
--quiet \ | |
-q 0 \ | |
"$INFILE" \ | |
--decode \ | |
.tmp.decode.wav | |
# resample to avoid compare issues | |
sox \ | |
--volume "${VOLUME}" \ | |
.tmp.decode.wav \ | |
--rate 44100 \ | |
"$OUTFILE" >/dev/null 2>&1 | |
} | |
# Compare two wav files, assume one is inverted | |
function compare() { | |
local FILE1="$1" | |
local FILE2="$2" | |
sox \ | |
--combine mix \ | |
"$FILE1" "$FILE2" \ | |
--null \ | |
stat | |
} | |
main "$@" |
I suppose that this isn't a good metric then.
There will always be some loss, and linear increase is expected in case of genuine bitrate.
In case of upscaled files, the loss should be lesser until the target bitrate is reached, then it should jump higher.
The question is - lesser than what?
It might be required to gather some data on what a typical sound loss looks like, so that we may compare the difference to that curve.
I guess that's too much for Bash. I might try doing that in Python some time later.
I'll keep this gist updated :)
I plotted the spectrogram of the 320k and the "320k->downsampled to 56k -> upsampled to 320k" files, took a screenshot and subtracted the images from each other. I'm looking for discernable patterns. The "blocks" are misleading, that's just me croaking scales. The faint, pink, belt in the middle is low-frequency noise (<100 Hz) on the left channel - that seems to be more present in the 320k file than in the upsampled one.
Thanks. Ran it, doesn't seem to change much - there's still a noticeable increase at 56k.