refactor code

This commit is contained in:
vaguesyntax
2025-10-26 14:04:52 +03:00
parent b137feac16
commit 496225ab9d
2 changed files with 40 additions and 44 deletions
@@ -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 thats 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 thats playing right now")
}
}
@@ -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