From 496225ab9d4767e78b3d96b8b847db42c7ece2a9 Mon Sep 17 00:00:00 2001 From: vaguesyntax Date: Sun, 26 Oct 2025 14:04:52 +0300 Subject: [PATCH] refactor code --- .../androidStyle/AndroidMusicRecognition.qml | 75 +++++++++---------- .../musicRecognition/musicRecognition.sh | 9 ++- 2 files changed, 40 insertions(+), 44 deletions(-) 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 924ec936c..c3aeff1a6 100644 --- a/dots/.config/quickshell/ii/modules/sidebarRight/quickToggles/androidStyle/AndroidMusicRecognition.qml +++ b/dots/.config/quickshell/ii/modules/sidebarRight/quickToggles/androidStyle/AndroidMusicRecognition.qml @@ -12,24 +12,37 @@ AndroidQuickToggleButton { property int timeoutInterval: 5 property int timeoutDuration: Config.options.resources.musicRecognitionTimeout - property string resultsJSON - - property string recognizedTrackTitle - property string recognizedTrackSubtitle - property string recognizedTrackURL - name: Translation.tr("Identify Music") - statusText: toggled ? Translation.tr("Listening...") : Translation.tr("Inactive") + statusText: toggled ? Translation.tr("Listening...") : Translation.tr("Inactive") toggled: false buttonIcon: toggled ? "cadence" : "graphic_eq" - onClicked: { - if (!toggled){ - recognizeMusicProc.running = true - } else { - recognizeMusicProc.running = false + + property var recognizedTrack: ({ title:"", subtitle:"", url:""}) + + function handleRecognition(jsonText) { + try { + var obj = JSON.parse(jsonText) + root.recognizedTrack = { + title: obj.track.title, + subtitle: obj.track.subtitle, + url: obj.track.url + } + musicReconizedProc.running = true + } catch(e) { + Quickshell.execDetached(["notify-send", "Unable to recognize music", "Please make sure your music is playing and try again", "-a", "Shell"]) + } finally { + root.toggled = false } - + } + + StyledToolTip { + text: Translation.tr("Identifies the song that’s currently playing") + } + + onClicked: { root.toggled = !root.toggled + recognizeMusicProc.running = root.toggled + musicReconizedProc.running = false } Process { @@ -38,45 +51,27 @@ AndroidQuickToggleButton { command: [`${Directories.scriptPath}/musicRecognition/musicRecognition.sh`, "-i", root.timeoutInterval, "-t", root.timeoutDuration] stdout: StdioCollector { onStreamFinished: { - root.resultsJSON = this.text - if (this.text.length < 100) { - Quickshell.execDetached(["notify-send", "Unable to recognize music", "Please make sure your music is playing and try again", "-a", "Shell"]) - toggled = false - return - } - var obj = JSON.parse(root.resultsJSON) - root.recognizedTrackTitle = obj.track.title - root.recognizedTrackSubtitle = obj.track.subtitle - root.recognizedTrackURL = obj.track.url - musicReconizedProc.running = true - recognizedMusicKiller.running = true - toggled = false + handleRecognition(this.text) } } } - Process { id: musicReconizedProc running: false - command: [ "notify-send" , "Music Recognized" , root.recognizedTrackTitle + " by " + root.recognizedTrackSubtitle , "-A" , "Shazam Link" , "-a" , "Shell"] + command: [ + "notify-send", + Translation.tr("Music Recognized"), + root.recognizedTrack.title + " - " + root.recognizedTrack.subtitle, + "-A", "Shazam", + "-a", "Shell" + ] stdout: StdioCollector { onStreamFinished: { if (this.text !== ""){ - Qt.openUrlExternally(root.recognizedTrackURL); + Qt.openUrlExternally(root.recognizedTrack.url); } } } } - - Timer { - id: recognizedMusicKiller - running: false; repeat: false - interval: 5000 - onTriggered: musicReconizedProc.running = false - } - - StyledToolTip { - text: Translation.tr("Identifies the song that’s playing right now") - } } diff --git a/dots/.config/quickshell/ii/scripts/musicRecognition/musicRecognition.sh b/dots/.config/quickshell/ii/scripts/musicRecognition/musicRecognition.sh index 671688cbd..810635814 100755 --- a/dots/.config/quickshell/ii/scripts/musicRecognition/musicRecognition.sh +++ b/dots/.config/quickshell/ii/scripts/musicRecognition/musicRecognition.sh @@ -1,10 +1,11 @@ #!/bin/bash -MONITOR_SOURCE="alsa_output.pci-0000_00_1f.3.analog-stereo.monitor" +## can be added manually if not chosen automatically with running this on terminal 'pw-cli list-objects | grep node.name' +MONITOR_SOURCE=$(pactl list short sources 2>/dev/null | grep -m1 monitor | awk '{print $2}' || true) -# Default değerler INTERVAL=5 TOTAL_DURATION=30 +MIN_VALID_RESULT_LENGTH=300 while getopts "i:t:" opt; do case $opt in @@ -22,7 +23,7 @@ while true; do ELAPSED=$((CURRENT_TIME - START_TIME)) if (( ELAPSED >= TOTAL_DURATION )); then - echo "Total duration reached. Exiting." + echo "Total duration reached, no music recognized." exit 0 fi @@ -35,7 +36,7 @@ while true; do RESULT=$(songrec audio-file-to-recognized-song "$TMP_FILE" 2>/dev/null || true) rm -f "$TMP_FILE" - if [ -n "$RESULT" ] && [ ${#RESULT} -gt 300 ]; then + if [ -n "$RESULT" ] && [ ${#RESULT} -gt $MIN_VALID_RESULT_LENGTH ]; then echo "$RESULT" exit 0 fi