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/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"
}
#!/bin/bash
# Adapted from example at http://amarok.kde.org/wiki/Moodbar

cpu_count="$(grep -c processor < /proc/cpuinfo)"
render="$(dirname "$0")/render.awk"

while read -r audio_path ; do
(( "$(jobs -p | wc -l)" >= "$cpu_count" )) && wait; {
moodbar_path="$(echo "${audio_path%.*}.mood" | sed 's#\(.*\)/\([^,]*\)#\1/.\2#')"

if [ ! -e "$moodbar_path" ] ; then
( moodbar -o "$moodbar_path" "$audio_path" > /dev/null ) || echo "Error processing $audio_path"
fi

basename "$audio_path"
hexdump -v -e '3/1 "%03u "' -e '"\n"' "$moodbar_path" | $render -v barlength="$(($(tput cols) - 2))"
} &
done < <(find . -type f -regextype posix-awk -iregex '.*\.(mp3|ogg|flac|wma|m4a)'); wait

echo "Done scanning."