From 035e51b36e56cf44f758f864f1ce3ae3f3e83965 Mon Sep 17 00:00:00 2001 From: vaguesyntax Date: Mon, 27 Oct 2025 20:39:45 +0300 Subject: [PATCH] add interval to settings, update the script (it was not working as intented) --- .../quickshell/ii/modules/common/Config.qml | 1 + .../ii/modules/settings/ServicesConfig.qml | 15 +++++++- .../androidStyle/AndroidMusicRecognition.qml | 3 +- .../musicRecognition/musicRecognition.sh | 38 ++++++++++--------- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/dots/.config/quickshell/ii/modules/common/Config.qml b/dots/.config/quickshell/ii/modules/common/Config.qml index 70cca4275..7a66d90f6 100644 --- a/dots/.config/quickshell/ii/modules/common/Config.qml +++ b/dots/.config/quickshell/ii/modules/common/Config.qml @@ -378,6 +378,7 @@ Singleton { property JsonObject musicRecognition: JsonObject { property int timeout: 16 + property int interval: 4 } property JsonObject search: JsonObject { diff --git a/dots/.config/quickshell/ii/modules/settings/ServicesConfig.qml b/dots/.config/quickshell/ii/modules/settings/ServicesConfig.qml index d1fb137c5..11fc82966 100644 --- a/dots/.config/quickshell/ii/modules/settings/ServicesConfig.qml +++ b/dots/.config/quickshell/ii/modules/settings/ServicesConfig.qml @@ -63,15 +63,26 @@ ContentPage { ConfigSpinBox { icon: "timer_off" - text: Translation.tr("Music recognition timeout (s)") + text: Translation.tr("Total duration timeout (s)") value: Config.options.musicRecognition.timeout - from: 2 + from: 10 to: 100 stepSize: 2 onValueChanged: { Config.options.musicRecognition.timeout = value; } } + ConfigSpinBox { + icon: "av_timer" + text: Translation.tr("Polling interval (s)") + value: Config.options.musicRecognition.interval + from: 2 + to: 10 + stepSize: 1 + onValueChanged: { + Config.options.musicRecognition.interval = value; + } + } } ContentSection { diff --git a/dots/.config/quickshell/ii/modules/sidebarRight/quickToggles/androidStyle/AndroidMusicRecognition.qml b/dots/.config/quickshell/ii/modules/sidebarRight/quickToggles/androidStyle/AndroidMusicRecognition.qml index 585b784f7..f7e475547 100644 --- a/dots/.config/quickshell/ii/modules/sidebarRight/quickToggles/androidStyle/AndroidMusicRecognition.qml +++ b/dots/.config/quickshell/ii/modules/sidebarRight/quickToggles/androidStyle/AndroidMusicRecognition.qml @@ -10,9 +10,10 @@ import qs.services AndroidQuickToggleButton { id: root - property int timeoutInterval: 5 + property int timeoutInterval: Config.options.musicRecognition.interval property int timeoutDuration: Config.options.musicRecognition.timeout + property string monitorSource: "monitor" // "monitor" (system sound) , "input" (microphone) name: Translation.tr("Identify Music") diff --git a/dots/.config/quickshell/ii/scripts/musicRecognition/musicRecognition.sh b/dots/.config/quickshell/ii/scripts/musicRecognition/musicRecognition.sh index f0b7c20a3..4f129f29d 100755 --- a/dots/.config/quickshell/ii/scripts/musicRecognition/musicRecognition.sh +++ b/dots/.config/quickshell/ii/scripts/musicRecognition/musicRecognition.sh @@ -1,51 +1,55 @@ #!/bin/bash -INTERVAL=5 +INTERVAL=2 TOTAL_DURATION=30 MIN_VALID_RESULT_LENGTH=300 -SOURCE_TYPE="monitor" # default | "monitor" : system_sound , "input" : microphone +SOURCE_TYPE="monitor" # monitor | input +TMP_RAW="/tmp/songrec_recording.raw" +TMP_MP3="/tmp/songrec_recording.mp3" while getopts "i:t:s:" opt; do case $opt in i) INTERVAL=$OPTARG ;; t) TOTAL_DURATION=$OPTARG ;; s) SOURCE_TYPE=$OPTARG ;; - *) echo "Usage: $0 [-i interval_seconds] [-t total_duration_seconds] [-s monitor|input]" - exit 1 ;; + *) exit 1 ;; esac done - -# Kaynağı belirle if [ "$SOURCE_TYPE" = "monitor" ]; then MONITOR_SOURCE=$(pactl list short sources 2>/dev/null | grep -m1 monitor | awk '{print $2}' || true) elif [ "$SOURCE_TYPE" = "input" ]; then MONITOR_SOURCE=$(pactl info | grep "Default Source:" | awk '{print $3}' || true) else - echo "Invalid source type: $SOURCE_TYPE. Use 'monitor' or 'input'." exit 1 fi +if [ -z "$MONITOR_SOURCE" ]; then + exit 1 +fi + +cleanup() { + rm -f "$TMP_RAW" "$TMP_MP3" + pkill -P $$ parec >/dev/null 2>&1 || true +} +trap cleanup EXIT + +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 - echo "Total duration reached, no music recognized." exit 0 fi - TMP_FILE=$(mktemp /tmp/recording.XXXXXX.wav) + ffmpeg -f s16le -ar 44100 -ac 2 -i "$TMP_RAW" -acodec libmp3lame -y -hide_banner -loglevel error "$TMP_MP3" 2>/dev/null - parec --device="$MONITOR_SOURCE" --format=s16le --rate=44100 --channels=2 \ - > >(ffmpeg -f s16le -ar 44100 -ac 2 -i - -t $INTERVAL -acodec libmp3lame "$TMP_FILE" -y -hide_banner -loglevel error) \ - 2>/dev/null + RESULT=$(songrec audio-file-to-recognized-song "$TMP_MP3" 2>/dev/null || true) - RESULT=$(songrec audio-file-to-recognized-song "$TMP_FILE" 2>/dev/null || true) - rm -f "$TMP_FILE" - - if [ -n "$RESULT" ] && [ ${#RESULT} -gt $MIN_VALID_RESULT_LENGTH ]; then + if echo "$RESULT" | grep -q '"matches": \[' && [ ${#RESULT} -gt $MIN_VALID_RESULT_LENGTH ]; then echo "$RESULT" exit 0 fi