pythonpython-3.xspeech-recognitionpython-sounddevice

How to pause or sleep or block the raw input stream of sound device


How can I pause/block/sleep the RawInputStream of sound device while my other function are processed..( wait for other tasks to execute), cause i have a speak element( voice assistant speaking) and when it speaks the input stream records that also, making the program a nightmare. The goal is simple make the python api sound device wait until all other functions are executed. Here is the code snippet:

with sd.RawInputStream(samplerate=args.samplerate, blocksize = 8000, device=args.device,
    dtype='int16', channels=1, callback=callback):
        rec = vosk.KaldiRecognizer(model, args.samplerate)
        while True:
            data = q.get()
            if rec.AcceptWaveform(data):
                vc=rec.FinalResult()   #produces raw output of what the user said
                vc=json.loads(vc)
                text=vc['text']    #converts the user speech to text format
                evaluale(text)

Solution

  • you need to use stream.stop_stream() then stream.start_stream() for example:

    if rec.AcceptWaveform(data):
        result=rec.Result()
        result=json.loads(result)
    
        if "hello" in result['text']:
            stream.stop_stream()
            speak('hello my boss, how can I help you?')
            stream.start_stream()
        if "hi" in result['text']:
            stream.stop_stream()
            speak('hello my boss, how can I help you?')
            stream.start_stream()
    

    I use stream.stop_stream() then stream.start_stream() because I don't want Vosk hears itself.