拉丁的传说
如果您在使用的任何设备上都有麦克风,则可以使用它来读取从计算机发出的任何声音。然后,您可以将要录制的音频帧与要查找的声音的声音文件进行比较当然,这使得它非常容易受到背景噪音的影响,因此不知何故,您将不得不将其过滤掉。下面是使用 PyAudio 和 wave 库的示例:import pyaudioimport wavewf = wave.open("websitSound.wav", "rb")amountFrames = 100 # just an arbitrary number; could be anythingsframes = wf.readframes(amountFrames)currentSoundFrame = 0chunk = 1024 # Record in chunks of 1024 samplessample_format = pyaudio.paInt16 # 16 bits per samplechannels = 2fs = 44100 # Record at 44100 samples per secondseconds = 3p = pyaudio.PyAudio() # Create an interface to PortAudiostream = p.open(format=sample_format, channels=channels, rate=fs, frames_per_buffer=chunk, input=True)# Store data in chunks for 3 secondsfor 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 interfacep.terminate()