我正在编写一个识别语音的程序。它所做的是记录来自麦克风的音频并使用Sphinx将其转换为文本。我的问题是我只想在用户说出某些内容时才开始录制音频。Python中的语音检测或无语音检测
我通过从麦克风读取音频电平并仅在电平高于特定值时进行记录来进行试验。但它不是那么有效。程序每当检测到任何响亮的声音时就开始录制。这是我使用的代码
import audioop
import pyaudio as pa
import wav
class speech():
def __init__(self):
# soundtrack properties
self.format = pa.paInt16
self.rate = 16000
self.channel = 1
self.chunk = 1024
self.threshold = 150
self.file = 'audio.wav'
# intialise microphone stream
self.audio = pa.PyAudio()
self.stream = self.audio.open(format=self.format,
channels=self.channel,
rate=self.rate,
input=True,
frames_per_buffer=self.chunk)
def record(self)
while True:
data = self.stream.read(self.chunk)
rms = audioop.rms(data,2) #get input volume
if rms>self.threshold: #if input volume greater than threshold
break
# array to store frames
frames = []
# record upto silence only
while rms>threshold:
data = self.stream.read(self.chunk)
rms = audioop.rms(data,2)
frames.append(data)
print 'finished recording.... writing file....'
write_frames = wav.open(self.file, 'wb')
write_frames.setnchannels(self.channel)
write_frames.setsampwidth(self.audio.get_sample_size(self.format))
write_frames.setframerate(self.rate)
write_frames.writeframes(''.join(frames))
write_frames.close()
有没有一种方法可以区分人类语音和Python中的其他噪声?希望有人能找到我一个解决方案。
+0
我正在测试你的代码在ubuntu, 从哪里wav包下载&测试? –