我怎么能暂停/阻塞/睡眠的RawInputStream的声音设备,而我的其他功能的处理。(等待其他任务执行),因为我有一个speak元素(语音助手说话),当它说话时,输入流也记录了这一点,使程序成为噩梦。目标很简单,让python api声音设备等待,直到所有其他函数执行完毕。下面是代码片段:
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)
您需要使用stream.stop_stream()
然后stream.start_stream()
例如:
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()
我先用stream.stop_stream()
,然后用stream.start_stream()
,因为我不想让Vosk听到自己的声音。
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()