annotate iterative-chroma/chromacompare.sh @ 7:7cdaaf6f64f1

More tests
author Chris Cannam
date Tue, 03 Feb 2015 13:43:02 +0000
parents 1924df3245f4
children
rev   line source
Chris@6 1 #!/bin/bash
Chris@6 2
Chris@6 3 set -e
Chris@6 4
Chris@6 5 mypath=`dirname $0`
Chris@6 6
Chris@6 7 sonic-annotator --minversion 1.1 || exit 1
Chris@6 8
Chris@6 9 reference="$1"
Chris@6 10 other="$2"
Chris@6 11
Chris@6 12 usage() {
Chris@6 13 echo "Usage: $0 reference.wav other.wav" 1>&2
Chris@6 14 exit 2
Chris@6 15 }
Chris@6 16
Chris@6 17 test -n "$reference" || usage
Chris@6 18 test -n "$other" || usage
Chris@6 19
Chris@6 20 if ! test -f "$reference" ; then
Chris@6 21 echo "Reference file $reference not found" 1>&2
Chris@6 22 exit 1
Chris@6 23 fi
Chris@6 24
Chris@6 25 if ! test -f "$other" ; then
Chris@6 26 echo "Other file $other not found" 1>&2
Chris@6 27 exit 1
Chris@6 28 fi
Chris@6 29
Chris@6 30 refchroma="/tmp/$$.ref"
Chris@6 31 otherchroma="/tmp/$$.other"
Chris@6 32 tmpscript="/tmp/$$.dc"
Chris@6 33 transform="/tmp/$$.ttl"
Chris@6 34 scores="/tmp/$$.scores.txt"
Chris@6 35 trap "rm $refchroma $otherchroma $tmpscript $transform $scores" 0
Chris@6 36
Chris@6 37 extract() {
Chris@6 38 hz="$1"
Chris@6 39 file="$2"
Chris@6 40 cat chroma-excerpt.ttl | sed 's,"440","'"$hz"'",' > "$transform"
Chris@7 41 sonic-annotator -t "$transform" --summary mean --summary-only -w csv --csv-stdout --csv-omit-filename "$file" 2>/dev/null | cut -d, -f4-63 | sed 's/,/\n/g'
Chris@6 42 }
Chris@6 43
Chris@6 44 extract 440 "$reference" | cat -n > "$refchroma"
Chris@6 45
Chris@6 46 sox "$other" -r 44100 -c 1 b.wav
Chris@6 47
Chris@6 48 for cents in $(seq -400 10 400) ; do
Chris@6 49
Chris@6 50 hz=$(perl -e "print 440.0 * (2.0 ** ($cents / 1200.0))")
Chris@6 51
Chris@6 52 extract "$hz" b.wav | cat -n > "$otherchroma"
Chris@6 53 (
Chris@6 54 echo "6 k 0"
Chris@6 55 join "$refchroma" "$otherchroma" | \
Chris@6 56 awk '{ print $2, $3, "- d * +" }' # square difference
Chris@6 57 echo "p"
Chris@6 58 ) > "$tmpscript"
Chris@6 59
Chris@6 60 dist=$( cat "$tmpscript" | dc )
Chris@6 61 echo "$dist $hz"
Chris@7 62
Chris@6 63 done | tee "$scores"
Chris@6 64
Chris@6 65 echo
Chris@6 66 echo Scores:
Chris@6 67 sort -n "$scores"
Chris@6 68
Chris@6 69 echo
Chris@6 70 bestfreq=$(sort -n "$scores" | head -1 | awk '{ print $2; }')
Chris@6 71 echo "I reckon the tuning frequency is $bestfreq"
Chris@6 72
Chris@6 73