我正在編寫一個識別語音的程序。它所做的是記錄來自麥克風的音頻並使用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中的其他噪聲?希望有人能找到我一個解決方案。
我正在測試你的代碼在ubuntu, 從哪裏wav包下載&測試? –