python声音识别代码_用python检测并录制声音

你可以试试这样的:# 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包来查找样本的均方根(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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值