2011-02-13

Visual feedback for your moodbar scanner:

moodbar-preview-cli

This is a pseudo-graphical application of AWK. I know it’s weird.

#!/usr/bin/env awk -f

# Setup
BEGIN { chunksize = int(1000 / barlength + 0.5); n = 0; rmax = 1; gmax = 1; bmax = 1; }

# Sum the samples in each chunk and find the max value for each color
!(NR % chunksize - 1) {
r[n] = $1; g[n] = $2; b[n] = $3;
for (i = 1; i < chunksize; i++) { getline; r[n] += $1; g[n] += $2; b[n] += $3 };
if (r[n] > rmax) rmax = r[n]; if (g[n] > gmax) gmax = g[n]; if (b[n] > bmax) bmax = b[n];
n++;
}

# Draw the moodbar
END {
ORS=""; shade[0] = "β–‘"; shade[1] = "β–’"; shade[2] = "β–“"; print "[";
for (i = 1; i <= barlength; i++) {
r[i] /= rmax * 1.25; g[i] /= gmax; b[i] /= bmax;
if (r[i] + g[i] + b[i] < 0.10) print "\033[30m" shade[0];
else if ((r[i] > g[i]) && (r[i] > b[i])) print "\033[31m" shade[int(r[i] * 2.99)];
else if ((g[i] > r[i]) && (g[i] > b[i])) print "\033[32m" shade[int(g[i] * 2.99)];
else print "\033[34m" shade[int(b[i] * 2.99)];
}
print "\033[0m]\n"
}
#!/usr/bin/env bash
# Adapted from example at http://amarok.kde.org/wiki/Moodbar

cpu_count="$(grep --count '^processor' /proc/cpuinfo)"

find . -type 'f' -regextype 'posix-awk' -iregex '.*\.(mp3|ogg|flac|wma|m4a)' |
while read -r path; do (( "$(jobs -p | wc -l)" <= "$cpu_count" )) || wait; {
mood="${path%/*}/.${path##*/}.mood"

[[ -e "$mood" ]] || moodbar -o "$mood" "$path" > /dev/null

basename "$path"
hexdump -v -e '3/1 "%03u "' -e '"\n"' "$mood" \
| ./render.awk -v barlength="$(($(tput cols) - 2))"
} & done; wait

echo 'Done scanning.'