为了账号安全,请及时绑定邮箱和手机立即绑定

如何从现场应用程序中检测特定声音?

如何从现场应用程序中检测特定声音?

森林海 2022-09-06 16:50:45
有没有办法从应用程序(如chrome,mozilla)收集实时音频,并在网站上播放特定声音时执行一些代码?
查看完整描述

1 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

如果您在使用的任何设备上都有麦克风,则可以使用它来读取从计算机发出的任何声音。然后,您可以将要录制的音频帧与要查找的声音的声音文件进行比较


当然,这使得它非常容易受到背景噪音的影响,因此不知何故,您将不得不将其过滤掉。


下面是使用 PyAudio 和 wave 库的示例:


import pyaudio

import wave


wf = wave.open("websitSound.wav", "rb")

amountFrames = 100 # just an arbitrary number; could be anything

sframes = wf.readframes(amountFrames)


currentSoundFrame = 0


chunk = 1024  # Record in chunks of 1024 samples

sample_format = pyaudio.paInt16  # 16 bits per sample

channels = 2

fs = 44100  # Record at 44100 samples per second

seconds = 3


p = pyaudio.PyAudio()  # Create an interface to PortAudio



stream = p.open(format=sample_format,

                channels=channels,

                rate=fs,

                frames_per_buffer=chunk,

                input=True)



# Store data in chunks for 3 seconds

for i in range(0, int(fs / chunk * seconds)):

    data = stream.read(chunk)

    if data == sframes[currentSoundFrame]:

        currentSoundFrame += 1

        if currentSoundFrame == len(sframes): #the whole entire sound was played

            print("Sound was played!")

    frames.append(data)


# Stop and close the stream 

stream.stop_stream()

stream.close()

# Terminate the PortAudio interface

p.terminate()


查看完整回答
反对 回复 2022-09-06
  • 1 回答
  • 0 关注
  • 95 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号