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()
添加回答
舉報