如何手动停止python中的音频录制?

时间:2019-07-12 14:19:14

标签: python speech-recognition speech-to-text pyaudio

我想停止键盘输入处的音频录制,并希望将其录制为wav文件。 如您所知,我正在运行刻录机一个小时。基本上,我想在在线面试中分析候选人记录的回答。我的要求是稍微更改代码,以便我可以在特定的键盘输入处停止并检索记录。请告诉我该怎么做(可能的代码说明)。

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 3600
WAVE_OUTPUT_FILENAME = "voice.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int((RATE / CHUNK) * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)


print("* done recording")

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

0 个答案:

没有答案