Skip to content

Instantly share code, notes, and snippets.

@turbulentie
Created September 29, 2024 14:11
Show Gist options
  • Save turbulentie/ab62f888d0b244703ac91e10d8024bff to your computer and use it in GitHub Desktop.
Save turbulentie/ab62f888d0b244703ac91e10d8024bff to your computer and use it in GitHub Desktop.
Plotting waveform of audiofile data in gnuplot using stats from sox
#!/bin/bash
# -----------------------------------------------------------------------------
# file: gnuplot-waveform.sh
# -----------------------------------------------------------------------------
#
# author: turbulentie
# created: 2024-09-29 (09:48:25)
# updated: 2024-09-29 (17:02:03)
# revision: 1.0
#
# -- Description --------------------------------------------------------------
#
# Plotting waveform of audiofile data in gnuplot using stats from sox
#
# -----------------------------------------------------------------------------
req_bins="sox gnuplot"
for binary in $req_bins; do
if [[ ! -x "$(command -v $binary)" ]]; then
printf "[\e[0;31m+\e[0m] - This script requires $binary\n"
exit 1
fi
done
if [[ "$#" -ge 1 ]]; then
if [[ -e "$1" ]]; then
datafile="$1"
else
printf "[\e[0;31m+\e[0m] - File $1 not found! Exiting...\n"
exit 2
fi
if [[ -n "$2" ]]; then
outpic="$2"
else
outpic="$1.png"
printf "[\e[0;31m+\e[0m] - Output file not defined. Using name from file: $outpic ...\n"
fi
setfont="Helvetica"
fontsize="10"
bgcolor="#1f1f1f"
brlcolor="#00bfff"
tltcolor="#a0a0b0"
xlbltcolor="#80c0ff"
ylbltcolor="#80c0ff"
p1lcolor="#73db78"
p2lcolor="#73db78"
gnuplot << EOF
set datafile commentschars ";"
set terminal png enhanced background rgbcolor "$bgcolor" enhanced font "$setfont,$fontsize"
set output "$outpic"
set border lc rgbcolor "$brlcolor"
set title "file: $1" tc rgbcolor "$tltcolor"
set multiplot layout 2,1
set ylabel "sample value" tc rgbcolor "$ylbltcolor"
set bmargin 0
set format x ""
set ytics -0.8,0.2
set key bottom
plot "< sox $datafile -t dat -" using 1:2 every 100 with lines lc rgb "$p1lcolor" title "left channel"
set xlabel "time (s)" tc rgbcolor "$xlbltcolor"
set bmargin
set tmargin 0
set format x "%g"
set ytics -1.0,0.2,0.8
set key top
unset title
plot "" using 1:3 every 100 with lines lc rgb "$p2lcolor" title "right channel"
unset multiplot
EOF
if [[ "$?" -eq 0 ]]; then
printf "[\e[0;31m+\e[0m] - Date: $(date +%d-%m-%Y) | $(date +%H:%M:%S)\n"
printf "[\e[0;31m+\e[0m] - Processed file: $datafile \n[\e[0;31m+\e[0m] - Output file: $outpic\n"
[[ -n DISPLAY && -n $(which display) ]] && exec display "$outpic" || printf "[\e[0;31m+\e[0m] - Skipping attempt to open file: not X11 session or ImageMagick not found!"
exit 0
fi
else
printf "Usage: $(basename $0) <datafile> <wavepic>\n"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment