python - 用python检测并记录声音

标签 python audio detect record

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

Detect & Record Audio in Python

我想更改程序以在声卡输入检测到声音时开始录音。可能应该以 block 为单位比较输入声级,但如何做到这一点?

最佳答案

你可以尝试这样的事情:

基于 this question/answer

# 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/

相关文章:

python - 在 Django 或 PostgreSQL 中生成唯一的随机字段

python - 为什么 Python 为短于文件系统限制的文件名给出 "OSError: [Errno 36] File name too long"?

python - 获取 int 对象不是可迭代错误

objective-c - 如何检测字符是大写还是小写?

c# - 在 C# 中检测网络连接速度和带宽使用情况

python - 如何从单独的键和值列表中制作字典(dict)?

audio - Verizon SongID - 它是如何编程的?

flash - 播放音频时使闪光灯按钮消失

audio - Scenekit Xcode 7 位置音频

java - 检测未初始化对象的类型