diff options
author | Botond Hende <nettingman@gmail.com> | 2024-11-20 13:37:51 +0100 |
---|---|---|
committer | Botond Hende <nettingman@gmail.com> | 2024-11-20 13:37:51 +0100 |
commit | 93eb7479e83494690e4eb51f2fb662c651a9fd5e (patch) | |
tree | e73548e7df148a66590b23c4a42d2c6dbe6db739 /modules/input_handlers/pipewire_record.py | |
parent | 528d65c6bd9954cfd96de57de05498dc4c563e0f (diff) |
voice control with whisper
Diffstat (limited to 'modules/input_handlers/pipewire_record.py')
-rw-r--r-- | modules/input_handlers/pipewire_record.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/modules/input_handlers/pipewire_record.py b/modules/input_handlers/pipewire_record.py new file mode 100644 index 0000000..8584ad3 --- /dev/null +++ b/modules/input_handlers/pipewire_record.py @@ -0,0 +1,66 @@ +import subprocess +import os.path +import signal +import sys +from time import sleep + +import whisper + +FIFO_PATH = "/tmp/hestia-listening" +RECORD_PATH = "/tmp/hestia-record.mp3" + +def get_input_pw_record(): + device = get_device() + + if os.path.exists(FIFO_PATH): + os.remove(FIFO_PATH) + + os.mkfifo(FIFO_PATH) + + while True: + with open(FIFO_PATH): + pass + # TODO "I'm listening" + + try: + ps = subprocess.Popen((f"pw-record --target {device} {RECORD_PATH}",), shell=True) + with open(FIFO_PATH): + print("finished") + ps.send_signal(signal.SIGINT) + # TODO "acknowledged" + except: + if "ps" in locals(): + ps.kill() + # TODO "error" + # TODO exit gracefully or try to recover + sys.exit() + + model = whisper.load_model("base") + + audio = whisper.load_audio(RECORD_PATH) + audio = whisper.pad_or_trim(audio) + + mel = whisper.log_mel_spectrogram(audio).to(model.device) + options = whisper.DecodingOptions(language="en", fp16=False) + result = whisper.decode(model, mel, options) + result_text = result.text.replace(",", "").replace(".", "").lower() + + print(result_text) + + yield result_text + +def get_device() -> str: + already_warned = False + + while True: + ps = subprocess.Popen(('pw-cli ls | \\grep -Poi "(?<=node.name = \\").*mic.*(?=\\")"',), shell=True, stdout=subprocess.PIPE) + ps.wait() + + if ps.returncode == 0: + return ps.stdout.read().decode().strip() + + elif not already_warned: + already_warned = True + # TODO warn about device not found + + sleep(3)
\ No newline at end of file |