检测和记录与Python声音 [英] Detect and record a sound with python
问题描述
我使用该程序记录在python声音:
I'm using this program to record a sound in python:
<一个href=\"http://stackoverflow.com/questions/892199/detect-record-audio-in-python/892293#892293\">http://stackoverflow.com/questions/892199/detect-record-audio-in-python/892293#892293
我想改变程序启动时由声卡输入检测录音。或许应该比较块的输入声音电平,但如何做到这一点?
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?
推荐答案
您可以尝试这样的事:
的根据这个问题/回答
# 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屋!