使用Python检测和记录声音

12

我正在使用这个程序在Python中录制声音:

检测和记录Python中的音频

我想更改程序,使其在声卡输入检测到声音时开始录制。 可能应该按块比较输入声音级别,但是如何做到这一点呢?

3个回答

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

5
检测非静音状态通常使用一些声音的均方根(RMS)并将其与您设置的某个阈值进行比较(该值取决于麦克风的灵敏度和其他因素,因此您需要进行调整)。此外,根据您希望麦克风检测录制声音的速度快慢,您可能需要降低块大小或计算重叠数据的RMS。

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

您需要设置阈值变量,并在每次循环中读取数据时与平均值(振幅)或其他相关参数进行比较。

您可以有两个嵌套的循环,第一个用于触发录音,另一个用于在此之后连续保存声音数据块。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接