[LAU] Audio File to Graphic Thumbnail in Command Line?

Tito Latini tito.01beta at gmail.com
Wed Sep 3 15:55:40 UTC 2014


On Tue, Sep 02, 2014 at 04:12:29PM -0700, Kevin Cosgrove wrote:
> 
> I dug into this a bit more.
> 
> Getting the time & amplitude data out of the WAV file was easy with
> 
>   sox file.wav -t dat - | tail -n +3 > file.dat
> 
> My beloved xgraph is not available on Fedora anymore.  That made me 
> relearn gnuplot, a good move regardless.  I wrote a script to build
> a gnuplot input file, file.gpl, which contains something like
> 
>   set terminal png notransparent nocrop
>   set output file.png
>   plot [] [-1:1] "file.dat" with lines
> 
> Then I run gnuplot on its input file like so
> 
>   gnuplot file.gpl > file.png
> 
> I left out a lot of housekeeping related to my scripting.  But, the
> above does all of the work needed to produce a nice graph of my audio 
> files.
> 
> Of course, this isn't fast.  It takes about 4 minutes on my
> 3.4GHz i7 with 16GB of RAM to create a plot from a 0.5GB mono WAV
> file.  Audacity and Ardour are faster than sox + gnuplot at creating
> their waveform views.  But, I can script with sox + gnuplot.
> 
> I hope this is useful for someone trolling the archive someday.

If the speed is important, Praat [1] also is a possible alternative.
There is a script in this message as a proof of concept.

Usage: ./sf2png [options] SNDFILE...
Write the images of the waveform of one or more soundfiles.

  -h n          heigth of the viewport [1.2].
  -n            suppress the plot annotations.
  -s fontsize   size of the font [4].
  -w n          width of the viewport [1.6].

Examples:

./sf2png /path/to/sndfiles/*.wav

The png files are in the same directory of the related soundfiles.

./sf2png -n -w 0.8 -h 0.5 /path/to/sndfiles/*.wav

./sf2png -w 4 -h 3 -s 5 /path/to/sndfiles/*.wav


[1] http://www.fon.hum.uva.nl/praat/

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

#!/bin/bash
#
# Write the images of the waveform of one or more soundfiles.

usage() {
        cat <<EOF
Usage: $0 [options] SNDFILE...
Write the images of the waveform of one or more soundfiles.

  -h n          heigth of the viewport [${height}].
  -n            suppress the plot annotations.
  -s fontsize   size of the font [${fontsize}].
  -w n          width of the viewport [${width}].

EOF
        exit 1
}

write_img() {
        local sndfile=$(realpath "$1")
        local imgfile="${sndfile%%.*}.png"

        echo "${sndfile} -> ${imgfile}"
        #
        # Warning: if there are trailing spaces after the copy-and-paste
        # of this script, remember to remove at least the spaces after
        # ${sndfile} and ${imgfile} in the follow praat script.
        #
        praat /dev/stdin <<EOF
Read from file... ${sndfile}
Select inner viewport... 0 $2 0 $3
Font size... $5
${inner_box}
Draw... 0 0 0 0 $4
Save as 300-dpi PNG file... ${imgfile}
EOF
}

main() {
        local width="1.6"
        local height="1.2"
        local has_text="1"
        local fontsize="4"
        local inner_box=""

        [ $# -lt 1 ] && usage

        while getopts "h:ns:w:" arg; do
                case "${arg}" in
                h) height="${OPTARG}" ;;
                n) has_text="0"
                   fontsize="1"
                   inner_box="Draw inner box"
                   ;;
                s) fontsize="${OPTARG}" ;;
                w) width="${OPTARG}" ;;
                *) usage ;;
                esac
        done
        shift $(expr "${OPTIND}" - 1)
        for sf in "$@"; do
                if [[ ! -f "${sf}" ]]; then
                        echo "WARN: file ${sf} not found."
                else
                        write_img "${sf}" "${width}" "${height}" "${has_text}" \
                                  "${fontsize}"
                fi
        done
}

main "$@"


tito


More information about the Linux-audio-user mailing list