From 308a60888b372e44e1460a0fed339e7b6d0b4d26 Mon Sep 17 00:00:00 2001 From: m3tm3re Date: Mon, 26 Jan 2026 05:16:29 +0100 Subject: [PATCH] fix: stt-ptt use pkill for better process management --- pkgs/stt-ptt/default.nix | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pkgs/stt-ptt/default.nix b/pkgs/stt-ptt/default.nix index 67563db..896b872 100644 --- a/pkgs/stt-ptt/default.nix +++ b/pkgs/stt-ptt/default.nix @@ -6,6 +6,7 @@ wtype, libnotify, pipewire, + procps, busybox, }: let script = writeShellScriptBin "stt-ptt" '' @@ -15,7 +16,6 @@ CACHE_DIR="''${XDG_CACHE_HOME:-$HOME/.cache}/stt-ptt" MODEL_DIR="''${XDG_DATA_HOME:-$HOME/.local/share}/stt-ptt/models" AUDIO="$CACHE_DIR/stt.wav" - PID_FILE="$CACHE_DIR/stt.pid" # Configurable via environment STT_MODEL="''${STT_MODEL:-$MODEL_DIR/ggml-large-v3-turbo.bin}" @@ -26,26 +26,31 @@ PW_RECORD="${pipewire}/bin/pw-record" WHISPER="${whisper-cpp}/bin/whisper-cli" WTYPE="${wtype}/bin/wtype" + PKILL="${procps}/bin/pkill" MKDIR="${busybox}/bin/mkdir" RM="${busybox}/bin/rm" - CAT="${busybox}/bin/cat" - KILL="${busybox}/bin/kill" TR="${busybox}/bin/tr" SED="${busybox}/bin/sed" + SLEEP="${busybox}/bin/sleep" # Ensure cache directory exists "$MKDIR" -p "$CACHE_DIR" + # Kill any existing pw-record for this audio file (prevents orphan nodes) + kill_existing() { + "$PKILL" -f "pw-record.*$AUDIO" 2>/dev/null + "$SLEEP" 0.1 + } + case "''${1:-}" in start) - "$RM" -f "$AUDIO" "$PID_FILE" + kill_existing + "$RM" -f "$AUDIO" "$NOTIFY" -t "$STT_NOTIFY_TIMEOUT" -a "stt-ptt" "Recording..." "$PW_RECORD" --rate=16000 --channels=1 "$AUDIO" & - echo $! > "$PID_FILE" ;; stop) - [[ -f "$PID_FILE" ]] && "$KILL" "$("$CAT" "$PID_FILE")" 2>/dev/null - "$RM" -f "$PID_FILE" + kill_existing if [[ -f "$AUDIO" ]]; then if [[ ! -f "$STT_MODEL" ]]; then