用python检测并录制声音 [英] Detect and record a sound with python

查看:56
本文介绍了用python检测并录制声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

检测&用 Python 录制音频

我想更改程序以在声卡输入检测到声音时开始录制.大概应该比较输入声级的chunk,但是怎么做呢?

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?

推荐答案

你可以试试这样的:

基于这个问题/答案em>

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

您可以使用内置的 audioop 包来查找样本的均方根 (rms),这通常是您获得水平的方式.

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

这篇关于用python检测并录制声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆