成功的热门词检测;但后来处理音频会出错

时间:2017-10-15 05:43:49

标签: pyaudio wit.ai

#Global Declarations

CHUNK_SIZE = 1024
MIN_VOLUME = 500
BUF_MAX_SIZE = 1024 * 10
RECORD_TIME_SECONDS = 7

   def wakeword():
       agent = snowboydecoder.HotwordDetector("res/bella.pmdl", 
       sensitivity=0.5)
       agent.start(detected_callback=listenIntent, sleep_time=0.03)
       agent.terminate()

成功检测。当它进入listenIntent函数时它会给我一个错误

   def listenIntent():
       print("Hotword")
       stream = pyaudio.PyAudio().open(
               format = pyaudio.paInt16,
               channels = 1,
               rate = 44100,
               input = True,
               frames_per_buffer = 1024)


while True:
    for i in range(0,int(44100/1024*RECORD_TIME_SECONDS)):
        data = array('h',stream.read(CHUNK_SIZE))
        stream_rec = pyaudio.PyAudio.open(format= pyaudio.paInt16,
                                          channels = 2,
                                          rate = 44100,
                                          input = True,
                                          frames_per_buffer = 1024)
        threshold = max(stream_rec)
        if threshold >= MIN_VOLUME:
            print("Recording....")
            frames = []
            data_rec = data.read(CHUNK_SIZE)
            frames.append(data_rec)
            stream.stop_stream()
            stream.close()
            pyaudio.PyAudio.terminate()
            _wavefile = wave.open("temp.wav",'wb')
            _wavefile.setnchannels(1)
            _wavefile.setsampwidth(pyaudio.PyAudio.get_sample_size(pyaudio.paInt16))
            _wavefile.setframerate(44100)
            _wavefile.writeframes(b''.join(frames))
            _wavefile.close()
            processAction()

def processAction():
     access_token = ""
     client = wit(access_token = access_token)
     with open("temp.wav",'rb') as f:
        response = client.speech(f, None,{'Content-Type': 'audio/wav',
                                 'encoding':'encode',
                                 'bits':'1024',
                                 'rate':'44100',
                                 'endian': 'big'})
print("Response" + str(response))

if __name__ == "__main__":
    wakeword()

过程 - 第一个唤醒代理触发器 - 开始录音7秒钟 - 处理音频文件it wit.ai以理解意图

一旦我运行该程序,它就会成功进行热门词检测,但后来部分失败了。即录音。我收到了这个错误。

Hotword
INFO:snowboy:Keyword 1 detected at time: 2017-10-15 10:10:53
||PaMacCore (AUHAL)|| Error on line 2490: err='-10863', msg=Audio 
Unit: cannot do in current context
||PaMacCore (AUHAL)|| Error on line 2490: err='-10863', msg=Audio 
Unit: cannot do in current context

0 个答案:

没有答案