我正在使用这个程序在 python 中录制声音:
在 Python 中检测和录制音频 https://stackoverflow.com/questions/892199/detect-record-audio-in-python/892293#892293
我想更改程序以在声卡输入检测到声音时开始录音。可能应该以块为单位比较输入声级,但是如何做到这一点呢?
你可以尝试这样的事情:
基于这个问题/答案 https://stackoverflow.com/questions/892199/detect-record-audio-in-python/892293#892293
# 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
您可能需要调整块大小和阈值,直到获得所需的行为。
Edit:
您可以使用内置的audioop http://docs.python.org/library/audioop.html#audioop.rms包来查找样本的均方根 (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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)