diff --git a/main_baidu.py b/main_baidu.py index f053176..d7a6364 100644 --- a/main_baidu.py +++ b/main_baidu.py @@ -290,66 +290,71 @@ def speech2text(wsParam): if __name__ == "__main__": # 测试时候在此处正确填写相关信息即可运行 - CHUNK = 512 - FORMAT = pyaudio.paInt16 - CHANNELS = 1 - RATE = 16000 - RECORD_SECONDS = 5 - WAVE_OUTPUT_FILENAME = "output.wav" + while 1: + global_text="" + CHUNK = 512 + FORMAT = pyaudio.paInt16 + CHANNELS = 1 + RATE = 16000 + RECORD_SECONDS = 5 + WAVE_OUTPUT_FILENAME = "output.wav" - p = pyaudio.PyAudio() + p = pyaudio.PyAudio() - stream = p.open(format=FORMAT, - channels=CHANNELS, - rate=RATE, - input=True, - frames_per_buffer=CHUNK) + stream = p.open(format=FORMAT, + channels=CHANNELS, + rate=RATE, + input=True, + frames_per_buffer=CHUNK) - print("recording...") + print("recording...") - frames = [] + frames = [] - for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): - data = stream.read(CHUNK) - frames.append(data) + for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): + data = stream.read(CHUNK) + frames.append(data) - print("done") + print("done") - stream.stop_stream() - stream.close() - p.terminate() + stream.stop_stream() + stream.close() + p.terminate() - wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') - wf.setnchannels(CHANNELS) - wf.setsampwidth(p.get_sample_size(FORMAT)) - wf.setframerate(RATE) - wf.writeframes(b''.join(frames)) - wf.close() + wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') + wf.setnchannels(CHANNELS) + wf.setsampwidth(p.get_sample_size(FORMAT)) + wf.setframerate(RATE) + wf.writeframes(b''.join(frames)) + wf.close() - f = open("output.wav",'rb') - f.seek(0) - f.read(44) - data = np.fromfile(f, dtype=np.int16) - data.tofile("output.pcm") + f = open("output.wav",'rb') + f.seek(0) + f.read(44) + data = np.fromfile(f, dtype=np.int16) + data.tofile("output.pcm") - wsParam_stt = Ws_Param_stt(APPID='44385032', APISecret='YWNkNTZhMTkwZWFiZjhjNjU4MDg2MzY4', - APIKey='83f2198f66ed394e7a73deb57c1c1b44', - AudioFile=r'./output.pcm') - speech2text(wsParam_stt) + wsParam_stt = Ws_Param_stt(APPID='44385032', APISecret='YWNkNTZhMTkwZWFiZjhjNjU4MDg2MzY4', + APIKey='83f2198f66ed394e7a73deb57c1c1b44', + AudioFile=r'./output.pcm') + speech2text(wsParam_stt) - text = bdgettext(global_text) - text = text.replace('\n', '').replace('\\n', '').replace('**', '') - wsParam = Ws_Param(APPID='44385032', APISecret='YWNkNTZhMTkwZWFiZjhjNjU4MDg2MzY4', - APIKey='83f2198f66ed394e7a73deb57c1c1b44', - Text=text) - websocket.enableTrace(False) - wsUrl = wsParam.create_url() - ws = websocket.WebSocketApp( - wsUrl, on_message=on_message, on_error=on_error, on_close=on_close) - ws.on_open = on_open - ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) - file = "demo.mp3" - p=vlc.MediaPlayer(file) - p.play() - input("playing") - #os.system(file) + text = bdgettext(global_text) + text = text.replace('\n', '').replace('\\n', '').replace('**', '') + wsParam = Ws_Param(APPID='44385032', APISecret='YWNkNTZhMTkwZWFiZjhjNjU4MDg2MzY4', + APIKey='83f2198f66ed394e7a73deb57c1c1b44', + Text=text) + + websocket.enableTrace(False) + wsUrl = wsParam.create_url() + ws = websocket.WebSocketApp( + wsUrl, on_message=on_message, on_error=on_error, on_close=on_close) + ws.on_open = on_open + ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) + file = "demo.mp3" + p=vlc.MediaPlayer(file) + p.play() + input("playing") + p.stop() + + #os.system(file)