用python检测并记录声音

发布于 2024-08-29 13:39:27 字数 111 浏览 7 评论 0原文

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

:在Python中录制音频

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

I'm using this program to record a sound in python:

Detect & Record Audio in Python

I want to change the program to start recording when sound is detected by the sound card input. Probably should compare the input sound level in chunk, but how do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜是你 2024-09-05 13:39:33

如何做到这一点在您给出的链接中指出:

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

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

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

how to do it is indicated in the link you give:

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

You have to set the threshold variable and compare with the average value (the amplitude) or other related parameter in data each time it is read in the loop.

You could have two nested loops, the first one to trigger the recording and the other to continously save sound data chuncks after that.

薯片软お妹 2024-09-05 13:39:32

您可以尝试这样的操作:

基于这个问题/答案

# 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< /a> 包来查找样本的均方根 (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

You could try something like this:

based on 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

You'll probably want to play with your chunk size and threshold values until you get the desired behavior.

Edit:

You can use the built-in audioop package to find the root-mean-square (rms) of a sample, which is generally how you would get the level.

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
如歌彻婉言 2024-09-05 13:39:32

检测何时没有静音通常是通过使用某些参数的均方根(RMS) 来完成的声音的一部分,并将其与您设置的某个阈值进行比较(该值将取决于您的麦克风的灵敏度和其他因素,因此您必须对其进行调整)。此外,根据您希望麦克风检测要录制的声音的速度,您可能需要降低块大小,或计算重叠数据块的 RMS。

Detecting when there isn't silence is usually done by using the root mean square(RMS) of some chunk of the sound and comparing it with some threshold value that you set (the value will depend on how sensitive your mic is and other things so you'll have to adjust it). Also, depending on how quickly you want the mic to detect sound to be recorded, you might want to lower the chunk size, or compute the RMS for overlapping chunks of data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文