使用Python检测并录制声音
3 个回答
1
你给的链接里说明了怎么做:
print "* recording"
for i in range(0, 44100 / chunk * RECORD_SECONDS):
data = stream.read(chunk)
# check for silence here by comparing the level with 0 (or some threshold) for
# the contents of data.
# then write data or not to a file
你需要设置一个阈值变量,然后在每次读取数据的时候,把它和平均值(也就是音量)或者其他相关参数进行比较。
你可以使用两个嵌套的循环,第一个循环用来开始录音,第二个循环则是不断地保存声音数据的小块。
5
检测声音不是静音的情况,通常是通过计算一段声音的均方根(RMS)来实现的,然后把这个值和你设定的一个阈值进行比较(这个阈值会根据你的麦克风灵敏度和其他因素而有所不同,所以你需要自己调整一下)。另外,如果你希望麦克风快速检测到声音并进行录音,可能需要减小每段声音的大小,或者对重叠的数据段计算均方根。
14
你可以试试这样的做法:
参考了这个问题/答案
# this is the threshold that determines whether or not sound is detected
THRESHOLD = 0
#open your audio stream
# wait until the sound data breaks some level threshold
while True:
data = stream.read(chunk)
# check level against threshold, you'll have to write getLevel()
if getLevel(data) > THRESHOLD:
break
# record for however long you want
# close the stream
你可能需要调整一下你的数据块大小和阈值,直到达到你想要的效果。
编辑:
你可以使用内置的audioop包来计算一个样本的均方根(rms),这通常是获取音量水平的方法。
import audioop
import pyaudio
chunk = 1024
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=chunk)
data = stream.read(chunk)
rms = audioop.rms(data, 2) #width=2 for format=paInt16