-
Notifications
You must be signed in to change notification settings - Fork 0
/
freq.sh
executable file
·101 lines (91 loc) · 2.81 KB
/
freq.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/sh
# this script's dir (and location of the other tools)
scriptpath=$0
scriptname=`basename "$0"`
scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cwd=`pwd`
# options:
start_in_seconds=0
duration_in_seconds=1
nocr=0
wavs=()
VERBOSE=false
################################
# scan command line args:
function usage
{
echo "$scriptname output the rough frequency found in the audio file"
echo "Usage: "
echo " $scriptname (default)"
echo " $scriptname --help (this help)"
echo " $scriptname --verbose (output verbose information)"
echo " $scriptname --seconds same as --duration"
echo " $scriptname --duration (number of seconds to scan, default $duration_in_seconds)"
echo " $scriptname --start (starting offset in seconds, default $start_in_seconds)"
echo " $scriptname --nocr (return the single result with no carriage return, default $nocr)"
echo ""
}
ARGC=$#
ARGV=("$@")
# no arguments given, output help
if [ $ARGC -eq 0 ]; then
usage
exit -1
fi
for ((i = 0; i < ARGC; i++)); do
if [[ $ARGC -ge 1 && ${ARGV[$i]} == "--help" ]]; then
usage
exit -1
fi
if [[ $ARGC -ge 1 && ${ARGV[$i]} == "--verbose" ]]; then
VERBOSE=true
continue
fi
if [[ $ARGC -ge 1 && ${ARGV[$i]} == "--start" ]]; then
((i+=1))
start_in_seconds=${ARGV[$i]}
$VERBOSE && echo "Parsing Args: Changing to $start_in_seconds seconds in, as the start"
continue
fi
if [[ $ARGC -ge 1 && ${ARGV[$i]} == "--seconds" ]]; then
((i+=1))
duration_in_seconds=${ARGV[$i]}
$VERBOSE && echo "Parsing Args: Changing to $duration_in_seconds seconds to scan"
continue
fi
if [[ $ARGC -ge 1 && ${ARGV[$i]} == "--duration" ]]; then
((i+=1))
duration_in_seconds=${ARGV[$i]}
$VERBOSE && echo "Parsing Args: Changing to $duration_in_seconds seconds to scan"
continue
fi
if [[ $ARGC -ge 1 && ${ARGV[$i]} == "--nocr" ]]; then
nocr=1
$VERBOSE && echo "Parsing Args: Will not output New Line after single result"
continue
fi
if [[ $ARGC -ge 1 && ${ARGV[$i]:0:2} == "--" ]]; then
echo "Unknown option ${ARGV[$i]}"
exit -1
fi
wavs+=("${ARGV[$i]}")
$VERBOSE && echo "Parsing Args: Audio: \"${ARGV[$i]}\""
done
################################
# for each wav given:
for f in "${wavs[@]}"; do
db=`sox "$f" -n trim "$start_in_seconds" "$duration_in_seconds" remix 1 stat 2>&1 | grep "Rough frequency:" | sed -E "s|^Rough frequency:[[:space:]]*([-.0-9]*).*$|\1|g"`
db_padded="$db"
#db_padded=`printf "%07.3f" $db`
## db is always negative unless it clipped, then 000.000
#if [ $db_padded == "000.000" ]; then
# db_padded="-00.000"
#fi
if [ ${#wavs[@]} -gt 1 ]; then
echo "$f\t->\t$db_padded"
elif [ $nocr -eq 1 ]; then
printf "%s" $db_padded
else
echo "$db_padded"
fi
done