This commit is contained in:
clf3 2023-12-28 23:33:51 -10:00
parent c728bd3cfd
commit d910b69e92
1 changed files with 57 additions and 52 deletions

View File

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