'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 1:[1]

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()

Solution 2:[2]

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.

Solution 3:[3]

To compose two functions means that the result of one will be the input to the other. Therefore, if you have two functions that have the same input type as each other and the same output type as each other but the input and output types differ, then they are not composable in either order.

In your particular case, you are requesting a composed function that applies f1() to the result of f2(), but the result of f2() is an Integer, whereas f1() requires an input of type String. One way to approach the problem would be to modify f1() so that it operates on Integers:

        Function<Integer, Integer> f1 = i -> i + 1;
        Function<String, Integer> f2 = s -> Integer.valueOf(s) * 2;
        Function<String, Integer> f3 = f1.compose(f2);

If you cannot modify f1(), then you could insert an intermediate conversion back to String into your composition chain:

        Function<String, Integer> f1=s->Integer.valueOf(s)+1;
        Function<String, Integer> f2 = s -> Integer.valueOf(s) * 2;
        Function<String, Integer> f3 = f1.compose(f2.andThen(Integer::toString));

But of course, all those conversion back and forth to String are expensive.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MCA2004
Solution 2 VLAZ
Solution 3 John Bollinger