def word()

in TSA-demo/TSA_voice_assistant/raspberry_voice_assistant.py [0:0]


def word():
    try:
        with noalsaerr():
            porcupine = pvporcupine.create(keywords=["picovoice", "blueberry"])
            #porcupine = pvporcupine.create(keyword_paths=['picovoice/hey-t-s-a-bot__en_mac_2021-10-21-utc_v1_9_0.ppn'])
    
            pa = pyaudio.PyAudio()
    
            audio_stream = pa.open(
                            rate=porcupine.sample_rate,
                            channels=1,
                            format=pyaudio.paInt16,
                            input=True,
                            #input_device_index=2,
                            frames_per_buffer=porcupine.frame_length)
         
            print('[Listening...]')
    
            while True:
                pcm = audio_stream.read(porcupine.frame_length)
                pcm = struct.unpack_from("h" * porcupine.frame_length, pcm)

                keyword_index = porcupine.process(pcm)

                if keyword_index >= 0:
                    print("Hotword '%s' Detected" % keywords[keyword_index])
                    if platform_sys != 'Darwin':
                        set_color(COLORS_RGB[KEYWORDS_COLOR[keywords[keyword_index]]])
                    return "hotword"

    except Exception as e: print('word exception: ',e)

    finally:
            if porcupine is not None:
                porcupine.delete()

            if audio_stream is not None:
                audio_stream.close()
                print('audio stream closed')

            if pa is not None:
                pa.terminate()
                print("pa terminated")