-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathrecognize-music.sh
More file actions
executable file
·71 lines (59 loc) · 1.94 KB
/
Copy pathrecognize-music.sh
File metadata and controls
executable file
·71 lines (59 loc) · 1.94 KB
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
#!/usr/bin/env bash
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
INTERVAL=2
TOTAL_DURATION=30
MIN_VALID_RESULT_LENGTH=300
SOURCE_TYPE="monitor" # monitor | input
TMP_PATH="/tmp/quickshell/media/songrec"
TMP_RAW="$TMP_PATH/recording.raw"
TMP_MP3="$TMP_PATH/recording.mp3"
while getopts "i:t:s:" opt; do
case $opt in
i) INTERVAL=$OPTARG ;;
t) TOTAL_DURATION=$OPTARG ;;
s) SOURCE_TYPE=$OPTARG ;;
*) exit 1 ;;
esac
done
if [ "$SOURCE_TYPE" = "monitor" ]; then
MONITOR_SOURCE=$(/usr/bin/pactl get-default-sink).monitor
elif [ "$SOURCE_TYPE" = "input" ]; then
MONITOR_SOURCE=$(/usr/bin/pactl info | /usr/bin/grep "Default Source:" | /usr/bin/awk '{print $3}' || true)
else
echo "Invalid source type"
exit 1
fi
if [ ! -x /usr/bin/songrec ] || [ ! -x /usr/bin/parec ] || [ ! -x /usr/bin/ffmpeg ]; then
exit 1
fi
if [ -z "$MONITOR_SOURCE" ] || ! /usr/bin/pactl list short sources | /usr/bin/grep -q "$MONITOR_SOURCE"; then
exit 1
fi
cleanup() {
/usr/bin/rm -f "$TMP_RAW" "$TMP_MP3"
/usr/bin/pkill -P $$ parec >/dev/null 2>&1 || true
}
trap cleanup EXIT
/usr/bin/mkdir -p "$TMP_PATH"
/usr/bin/parec --device="$MONITOR_SOURCE" --format=s16le --rate=44100 --channels=2 > "$TMP_RAW" &
PAREC_PID=$!
START_TIME=$(/usr/bin/date +%s)
while true; do
/usr/bin/sleep "$INTERVAL"
CURRENT_TIME=$(/usr/bin/date +%s)
ELAPSED=$((CURRENT_TIME - START_TIME))
if (( ELAPSED >= TOTAL_DURATION )); then
exit 0
fi
# stop immediately if parec dont find the correct audio source
if ! kill -0 $PAREC_PID 2>/dev/null; then
exit 1
fi
/usr/bin/ffmpeg -f s16le -ar 44100 -ac 2 -i "$TMP_RAW" -acodec libmp3lame -y -hide_banner -loglevel error "$TMP_MP3" 2>/dev/null
RESULT=$(/usr/bin/songrec recognize -j "$TMP_MP3" 2>/dev/null || true)
# better if shazam api change
if echo "$RESULT" | /usr/bin/grep -q '"track"'; then
echo "$RESULT"
exit 0
fi
done