import threading
import queue
import json

audio_queue = queue.Queue()
SAMPLE_RATE = 16000

def vosk_listener():
    import vosk
    import pyaudio

    VOSK_MODEL_PATH = "/home/tino/Documents/_Documents/Schule/4 Wilhelm Maybach Schule/2BKI Jahr 2/Abschlussprojekt/test assistant/assistant_all_in_one/vosk-model-de-0.21/"

    model = vosk.Model(VOSK_MODEL_PATH)
    rec = vosk.KaldiRecognizer(model, SAMPLE_RATE)

    p = pyaudio.PyAudio()
    stream = p.open(
        format=pyaudio.paInt16,
        channels=1,
        rate=SAMPLE_RATE,
        input=True,
        frames_per_buffer=4000
    )
    stream.start_stream()

    while True:
        data = stream.read(4000, exception_on_overflow=False)
        if rec.AcceptWaveform(data):
            result = json.loads(rec.Result())
            text = result.get("text", "")
            if text:
                audio_queue.put(text)

def main():
    threading.Thread(target=vosk_listener, daemon=True).start()

    while True:
        try:
            text = audio_queue.get(timeout=0.1)
            print("[SST]", text)
        except queue.Empty:
            pass

if __name__ == "__main__":
    main()
