【问题标题】:How can I detect specific sound from live application?如何从现场应用程序中检测特定声音?
【发布时间】:2020-07-01 04:38:46
【问题描述】:

有没有办法从应用程序(如 chrome、mozilla)收集实时音频并执行一些代码 什么时候在网站上播放特定的声音?

【问题讨论】:

  • 我确定有,但是如果您没有向我们展示您所做的努力并就您遇到的问题提出更具体的问题,这不是一个合适的 SO 问题。
  • 请参阅How to Askhelp center

标签: python audio automation


【解决方案1】:

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

当然,这会使它很容易受到背景噪音的影响,因此您必须以某种方式将其过滤掉。

这是一个使用 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多