有没有办法从应用程序(例如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/