#!/bin/sh # # snd2img converter # by pasp # # last update: Sun Aug 5 01:13:11 CEST 2007 # # default settings outputformat=eps skip=1 title=false extendedtitle=false amplitudelabel=false normalize=false # ------------------------------------------------------------------------ # functions check_tools () { tools_paths=( /usr/bin/sox /usr/bin/gnuplot ) # change paths if needed for tool in ${tools[@]} do if [ ! -e $tool ]; then error "$tool not found." fi done } header () { echo "snd2img - sound file to image converter" echo "by pasp " echo "" } error () { echo "" echo "ERROR: $1" exit -1 } set_title () { if [ $title == "true" ]; then echo "set title '"${1%.*}"'" >> $script else if [ $extendedtitle == "true" ]; then echo "set title '$1 ($channels_str, $rate_str)'" >> $script fi fi } # ------------------------------------------------------------------------ # MAIN check_tools if [ $# -lt "1" ] # filename passed to script ? then header echo "usage: `basename $0` [options] filename" echo "" echo "options:" echo " -d : draw every n-th sample" echo " -s : generate SVG figure (default is EPS)" echo " -a : enable amplitude label" echo " -t : use filename without extension as title" echo " -e : use filename with extension," echo " number of channels and sample rate as title" echo " -n : set amplitude axis to <-1,1> range" exit fi set -- `getopt d:snate "$@"` counter=$# while [ $counter -gt 1 ] do case "$1" in -d) skip=$2; shift; let counter=counter-1;; -s) outputformat=svg;; -a) amplitudelabel=true;; -t) title=true;; -e) extendedtitle=true;; -n) normalize=true;; esac shift let counter=counter-1 done if [ $skip -lt "1" ] # skip > 0 ? then error "n must be greater then zero." fi if [ -e "$@" ]; then tmpfile=`mktemp` script=$tmpfile.gnu header echo -n "processing..." sox "$1" $tmpfile.dat 2>/dev/null if [ ! -e $tmpfile.dat ]; then error "Wrong file type." fi grep -v '^;' $tmpfile.dat > $tmpfile.txt rate=`awk 'NR==1 {print $4}' $tmpfile.dat` # sample rate -> 4th column channels=`awk 'NR==2 {print $3}' $tmpfile.dat` # channels -> 3rd column n=${#channels} n=$(($n-1)) channels=`echo $channels | cut -c 1-$n` # convert to integer rate_str=`echo $rate | tr -d [:space:]`Hz if [ $channels -eq 1 ]; then channels_str="1 channel" else channels_str="$channels channels" fi if [ $outputformat == "eps" ]; then echo "set terminal postscript enhanced 'Helvetica' 14" > $script else echo "set terminal svg fname 'Helvetica' fsize 14" > $script fi if [ $normalize == "true" ]; then echo "set yrange [-1:1]" >> $script fi if [ $amplitudelabel == "true" ]; then echo "set ylabel 'amplitude'" >> $script fi echo "set nokey" >> $script # hide legend echo "set xzeroaxis" >> $script echo "set grid" >> $script echo "set xlabel 'time [s]'" >> $script echo "set output '"${@%.*}.$outputformat"'" >> $script if [ $channels -eq 1 ]; then set_title $@ echo "plot '$tmpfile.txt' every $skip with lines" >> $script else echo "set size 1.0, 1.0" >> $script echo "set origin 0.0, 0.0" >> $script echo "set multiplot" >> $script echo "set size 1.0,0.5" >> $script echo "set origin 0.0,0.0" >> $script echo "plot '$tmpfile.txt' every $skip using 1:3 with lines" >> $script echo "set size 1.0,0.5" >> $script echo "set origin 0.0,0.5" >> $script set_title $@ echo "set noxlabel" >> $script echo "set format x ''" >> $script echo "plot '$tmpfile.txt' every $skip using 1:2 with lines" >> $script echo "set nomultiplot" >> $script fi gnuplot < $script rm -rf $tmpfile $tmpfile.dat $tmpfile.txt $script echo "done" else error "File not found." fi