python - 如何从实时应用程序中检测特定声音?

标签 python audio automation

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

最佳答案

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

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

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

关于python - 如何从实时应用程序中检测特定声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60767435/

相关文章:

Python正则表达式提取 token

python - 远程连接到 MS SQL - 使用 pyodbc 时出错与使用 SQL Server Management Studio 时成功

java - 当我的应用程序打开时,单击三星设备上的内置菜单按钮会发出两次声音

将 8 位无符号 PCM 转换为 8 位有符号 PCM

python - 枚举器的默认值或无效值

Python - 循环浏览大列表并快速下载图像

audio - 如何确定音轨是否为杜比定向逻辑II(Dolby Pro Logic II)混音

azure - 命令 'Get-AzFunctionApp ' 在 Azure 自动化 Runbook 中不起作用

python - 更改单选按钮,然后使用 mechanize (python) 读取()

testing - 用于端到端集成测试的测试自动化框架