From 0725edd35db5bda3ff41d9a8c9cc0b4fe7fe23fb Mon Sep 17 00:00:00 2001 From: Aman Date: Sun, 1 Mar 2026 15:46:02 +0530 Subject: [PATCH 1/2] refactor(musicRecognition): replace parec/ffmpeg pipeline with songrec listen fix:#3057 Use `songrec listen --audio-device` instead of manually capturing audio with parec, converting with ffmpeg, and passing to audio-file-to-recognized-song. Drops temp file handling and simplifies the recognition loop. --- .../musicRecognition/recognize-music.sh | 50 +++++++------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/dots/.config/quickshell/ii/scripts/musicRecognition/recognize-music.sh b/dots/.config/quickshell/ii/scripts/musicRecognition/recognize-music.sh index 183432b97..c0d88e7f8 100755 --- a/dots/.config/quickshell/ii/scripts/musicRecognition/recognize-music.sh +++ b/dots/.config/quickshell/ii/scripts/musicRecognition/recognize-music.sh @@ -1,12 +1,8 @@ #!/bin/bash -INTERVAL=2 +INTERVAL=10 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 @@ -16,47 +12,37 @@ while getopts "i:t:s:" opt; do *) exit 1 ;; esac done + +if ! command -v songrec >/dev/null 2>&1; then + exit 1 +fi + if [ "$SOURCE_TYPE" = "monitor" ]; then - MONITOR_SOURCE=$(pactl get-default-sink).monitor + AUDIO_DEVICE=$(pactl get-default-sink).monitor elif [ "$SOURCE_TYPE" = "input" ]; then - MONITOR_SOURCE=$(pactl info | grep "Default Source:" | awk '{print $3}' || true) + AUDIO_DEVICE=$(pactl info | grep "Default Source:" | awk '{print $3}' || true) else echo "Invalid source type" exit 1 fi -if ! command -v songrec >/dev/null 2>&1 || ! command -v parec >/dev/null 2>&1 || ! command -v ffmpeg >/dev/null 2>&1; then - exit 1 -fi - -if [ -z "$MONITOR_SOURCE" ] || ! pactl list short sources | grep -q "$MONITOR_SOURCE"; then +if [ -z "$AUDIO_DEVICE" ] || ! pactl list short sources | grep -q "$AUDIO_DEVICE"; then exit 1 fi cleanup() { - rm -f "$TMP_RAW" "$TMP_MP3" - pkill -P $$ parec >/dev/null 2>&1 || true + kill "$SONGREC_PID" 2>/dev/null || true } trap cleanup EXIT -mkdir -p "$TMP_PATH" -parec --device="$MONITOR_SOURCE" --format=s16le --rate=44100 --channels=2 > "$TMP_RAW" & -START_TIME=$(date +%s) - -while true; do - sleep "$INTERVAL" - CURRENT_TIME=$(date +%s) - ELAPSED=$((CURRENT_TIME - START_TIME)) - - if (( ELAPSED >= TOTAL_DURATION )); then +songrec listen --audio-device "$AUDIO_DEVICE" --request-interval "$INTERVAL" --json --disable-mpris | while IFS= read -r line; do + if echo "$line" | grep -q '"matches": \['; then + echo "$line" + kill "$SONGREC_PID" 2>/dev/null || true exit 0 fi +done & +SONGREC_PID=$! - ffmpeg -f s16le -ar 44100 -ac 2 -i "$TMP_RAW" -acodec libmp3lame -y -hide_banner -loglevel error "$TMP_MP3" 2>/dev/null - RESULT=$(songrec audio-file-to-recognized-song "$TMP_MP3" 2>/dev/null || true) - - if echo "$RESULT" | grep -q '"matches": \[' && [ ${#RESULT} -gt $MIN_VALID_RESULT_LENGTH ]; then - echo "$RESULT" - exit 0 - fi -done +sleep "$TOTAL_DURATION" +exit 0 From 8c66dc7aa7545b4f43bf42168fcd2e0ef06ebefd Mon Sep 17 00:00:00 2001 From: Aman Date: Wed, 4 Mar 2026 09:21:29 +0530 Subject: [PATCH 2/2] fix(musicRecognition): restore 2s interval and fix songrec process leak --- .../musicRecognition/recognize-music.sh | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/dots/.config/quickshell/ii/scripts/musicRecognition/recognize-music.sh b/dots/.config/quickshell/ii/scripts/musicRecognition/recognize-music.sh index c0d88e7f8..456ed963d 100755 --- a/dots/.config/quickshell/ii/scripts/musicRecognition/recognize-music.sh +++ b/dots/.config/quickshell/ii/scripts/musicRecognition/recognize-music.sh @@ -1,8 +1,9 @@ #!/bin/bash -INTERVAL=10 +INTERVAL=2 TOTAL_DURATION=30 SOURCE_TYPE="monitor" # monitor | input +FIFO=$(mktemp -u /tmp/songrec_out_XXXXXX) while getopts "i:t:s:" opt; do case $opt in @@ -30,19 +31,25 @@ if [ -z "$AUDIO_DEVICE" ] || ! pactl list short sources | grep -q "$AUDIO_DEVICE exit 1 fi +mkfifo "$FIFO" + cleanup() { kill "$SONGREC_PID" 2>/dev/null || true + wait "$SONGREC_PID" 2>/dev/null + rm -f "$FIFO" } trap cleanup EXIT -songrec listen --audio-device "$AUDIO_DEVICE" --request-interval "$INTERVAL" --json --disable-mpris | while IFS= read -r line; do - if echo "$line" | grep -q '"matches": \['; then - echo "$line" - kill "$SONGREC_PID" 2>/dev/null || true - exit 0 - fi -done & +songrec listen --audio-device "$AUDIO_DEVICE" --request-interval "$INTERVAL" --json --disable-mpris > "$FIFO" & SONGREC_PID=$! -sleep "$TOTAL_DURATION" +( sleep "$TOTAL_DURATION" && kill "$SONGREC_PID" 2>/dev/null ) & + +while IFS= read -r line; do + if echo "$line" | grep -q '"matches": \['; then + echo "$line" + exit 0 + fi +done < "$FIFO" + exit 0