0

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)
Benji
  • 15
  • 5

2 Answers2

0
tream = sd.RawInputStream(samplerate=args.samplerate, blocksize=8000, 
        device=args.device, dtype='int16',channels=1, callback=callback)
# to start listening
tream.start()
# to stop listen
tream.stop()
0

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.

VLAZ
  • 26,331
  • 9
  • 49
  • 67