-
Notifications
You must be signed in to change notification settings - Fork 0
/
audd
executable file
·125 lines (106 loc) · 3.04 KB
/
audd
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
# Use https://audd.io/ to detect audio currently playing through audio source.
# Requires curl, ffmpeg.
TMP_DIR="/tmp/audd"
[ ! -d "$TMP_DIR" ] && mkdir "$TMP_DIR"
AUDIO_FILE="$TMP_DIR/recording.mp3"
PID_FILE="$TMP_DIR/recordingpid"
API_TOKEN_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/audd/api_token"
[ -f "$API_TOKEN_FILE" ] && API_TOKEN="$(cat "$API_TOKEN_FILE")"
err_msg_exit() {
# Print an error to stderr and exit.
printf "%s\n" "$*" >&2
exit 1
}
type curl >/dev/null || err_msg_exit "'curl' not found."
type ffmpeg >/dev/null || err_msg_exit "'ffmpeg' not found."
usage() {
printf "Usage: audd [OPTION]... [FILE]
Query the AudD music recognition API.
Get a free API token at: https://audd.io/
The API token can be read from file:
$ echo \"api-token\" > \"%s\"
If no FILE is provided, a recording is made using the AUDIO_SOURCE.
Usage:
-h Show this message and exit.
-a API_KEY AudD API token.
-s AUDIO_SOURCE ffmpeg audio input source, (default: \"default\").
-t RECORDING_TIME Length of recording time, in seconds, (default: 3).
-r API_RETURN AudD API return parameter, see https://docs.audd.io/,
(default: \"apple_music,spotify\").
-o Use the \"recognizeWithOffset\" endpoint.
" "$API_TOKEN_FILE"
}
start_recording() {
ffmpeg \
-hide_banner \
-f pulse -i "$AUDIO_SOURCE" -ac 1 \
-c:a mp3 \
-y "$AUDIO_FILE" >/dev/null 2>&1 &
printf "%s\n" "$!" >"$PID_FILE"
}
kill_recording() {
# kill with SIGTERM, allowing finishing touches.
local pid
pid="$(cat "$PID_FILE")"
kill "$pid"
rm -f "$PID_FILE"
# even after SIGTERM, ffmpeg may still run, so wait till it's done.
while kill -0 "$pid" 2>/dev/null; do
sleep 0.1
done
}
record() {
start_recording && sleep "$RECORDING_TIME" && kill_recording
}
query() {
curl --silent "https://api.audd.io/$ENDPOINT" \
-F api_token="$API_TOKEN" \
-F file=@"$AUDIO_FILE" \
-F return="$API_RETURN"
}
# Defaults
RECORDING_TIME=3 # in seconds
AUDIO_SOURCE="default"
API_RETURN="apple_music,spotify"
ENDPOINT=""
# Parse options
while getopts ":hoa:s:t:r:" opt; do
case $opt in
"h")
usage
exit 0
;;
"a")
API_TOKEN="$OPTARG"
;;
"s")
AUDIO_SOURCE="$OPTARG"
;;
"t")
RECORDING_TIME="$OPTARG"
;;
"r")
API_RETURN="$OPTARG"
;;
"o")
ENDPOINT="recognizeWithOffset/"
;;
"?")
usage >&2
err_msg_exit "Invalid Option: -$OPTARG"
;;
esac
done
shift $((OPTIND - 1))
[ -z "$API_TOKEN" ] && err_msg_exit "No API_TOKEN provided. Use the -a option or write it to \"$API_TOKEN_FILE\""
if [ -z "$1" ]; then
record || exit
else
if [ -f "$1" ]; then
AUDIO_FILE="$1"
else
err_msg_exit "audd: File \"$1\" not found."
fi
fi
[ -f "$AUDIO_FILE" ] && query