python麦克风监听_python–检测tap与pyaudio从现场麦克风

更新时间:2023-07-12 16:35:53 阅读: 评论:0

python麦克风监听_python–检测tap与pyaudio从现场麦克风⼀种⽅式我做到了:
>⼀次读取⼀个样本块,
说0.05秒的价值
>计算足球梦
块的RMS幅度(平⽅
根的平⽅的平⽅
个体样本)
>如果块的RMS幅度⼤于阈值,则它是“噪声块”,否则它是“安静块”
>突然的抽头将是⼀个安静的块,随后是少量的噪声块,随后是⼀个安静的块
>如果你从来没有得到⼀个安静的块,你的阈值太低
>如果你从来没有得到⼀个嘈杂的块,你的阈值太⾼
我的应⽤程序录⾳“有趣”的噪⾳⽆⼈值守,所以它会记录只要有嘈杂的块。如果存在15秒的噪声时段(“覆盖其⽿朵”),则将阈值乘以1.1,并且如果存在15分钟的静默时段(“更倾听”),将阈值乘以0.9。您的应⽤程序将有不同的需求。
什么闹钟此外,只是注意到我的代码中有关观察RMS值的⼀些意见。在Macbook Pro上的内置麦克风上,使⽤/ – 1.0标准化⾳频数据范围,将输⼊⾳量设置为最⼤,⼀些数据点:
> 0.003-0.006(-50dB到-44dB)⼀个令⼈讨厌的中央暖风扇在我家
> 0.010-0.40(-40dB⾄-8dB)在同⼀台笔记本电脑上打字
> 0.10(-20dB)在1’距离处轻柔地攫取⼿指
> 0.60(-4.4dB)弹响指尖在1′
更新:这⾥是⼀个例⼦,让你开始。
#!/usr/bin/python
# open a microphone in pyAudio and listen for taps
import pyaudio
import struct
import math
INITIAL_TAP_THRESHOLD = 0.010
FORMAT = pyaudio.paInt16
SHORT_NORMALIZE = (1.0/32768.0)
CHANNELS = 2
RATE = 44100
INPUT_BLOCK_TIME = 0.05
INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME)
# if we get this many noisy blocks in a row, increa the threshold
OVERSENSITIVE = 15.0/INPUT_BLOCK_TIME
# if we get this many quiet blocks in a row, decrea the threshold UNDERSENSITIVE = 120.0/INPUT_BLOCK_TIME
# if the noi was longer than this many blocks, it's not a 'tap' MAX_TAP_BLOCKS = 0.15/INPUT_BLOCK_TIME
def get_rms( block ):
# RMS amplitude is defined as the square root of the
# mean over time of the square of the amplitude.
# so we need to convert this string of bytes into
# a string of
点灯的人# we will get one short out for each
# two chars in the string.
count = len(block)/2
泡菜饼的做法format = "%dh"%(count)
shorts = struct.unpack( format, block )
# iterate over the block.
sum_squares = 0.0
for sample in shorts:
# sample is a signed short in +/- 32768.
# normalize it to 1.0
n = sample * SHORT_NORMALIZE
sum_squares += n*n
return math.sqrt( sum_squares / count )
class TapTester(object):
def __init__(lf):
lf.pa = pyaudio.PyAudio()
lf.stream = lf.open_mic_stream()
lf.tap_threshold = INITIAL_TAP_THRESHOLD
lf.quietcount = 0
def stop(lf):
lf.stream.clo()
def find_input_device(lf):
长毛金丝熊
device_index = None
for i in range( _device_count() ):
devinfo = _device_info_by_index(i)
print( "Device %d: %s"%(i,devinfo["name"]) )
for keyword in ["mic","input"]:
if keyword in devinfo["name"].lower():
print( "Found an input: device %d - %s"%(i,devinfo["name"]) ) device_index = i
return device_index
if device_index == None:
print( "No preferred input found; using default input device." ) return device_index
def open_mic_stream( lf ):
device_index = lf.find_input_device()
stream = lf.pa.open( format = FORMAT,
channels = CHANNELS,
rate = RATE,苦心孤诣的拼音
重生之做个乖孩子
input = True,
input_device_index = device_index,
frames_per_buffer = INPUT_FRAMES_PER_BLOCK)
return stream
def tapDetected(lf):
print "Tap!"
def listen(lf):
try:
block = ad(INPUT_FRAMES_PER_BLOCK) except IOError, e:
# dammit.
print( "(%d) Error recording: %s"%(lf.errorcount,e) )
return
amplitude = get_rms( block )
if amplitude > lf.tap_threshold:
# noisy block
lf.quietcount = 0
isycount > OVERSENSITIVE:
# turn down the nsitivity
lf.tap_threshold *= 1.1
el:
# quiet block.
if 1 <= lf.noisycount <= MAX_TAP_BLOCKS: lf.tapDetected()
lf.quietcount += 1
if lf.quietcount > UNDERSENSITIVE:
# turn up the nsitivity
lf.tap_threshold *= 0.9
if __name__ == "__main__":
tt = TapTester()
for i in range(1000):
tt.listen()
>踟蹰前行

本文发布于:2023-07-12 16:35:53,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1078729.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:阈值   样本   时段
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图