我正在使用这个程序在 python 中录制声音:
Detect & Record Audio in Python
我想更改程序以在声卡输入检测到声音时开始录音。可能应该以 block 为单位比较输入声级,但如何做到这一点?
最佳答案
你可以尝试这样的事情:
# 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
您可能想要调整 block 大小和阈值,直到获得所需的行为。
编辑:
您可以使用内置的 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
关于python - 用python检测并记录声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2668442/