我想编写一个程序,在系统的基本声音播放任何类型的声音时执行特定的命令。就像您在Facebook上收到消息一样,您会听到一些警报声。我想认识这个“高峰”。在python中怎么可能?
最佳答案
嘿>
获取音频数据
我认为您正在寻找的是某种方式回送系统输出,以便您可以像操作系统认为其输入一样读取它。有不同的方法(取决于您的操作系统)。
但是,由于您在注释中提到的操作系统是Windows 8.1,因此可以使用PyAudio的分支-> PyAudio_portaudio:这是正常的PyAudio,但扩展为使用WASAPI将Windows系统的输出环回为可以在Python中恢复的内容。
如果我错过了任何事情,请感谢this other SO post使用Python记录系统输出,并感谢@mate将链接发布到PyAudio fork。
这是一个简短的解释:
The official PyAudio build isn't able to record the output. BUT with Windows Vista and above, a new API, WASAPI was introduced, which includes the ability to open a stream to an output device in loopback mode. In this mode the stream will behave like an input stream, with the ability to record the outgoing audio stream.
To set the mode, one has to set a special flag (AUDCLNT_STREAMFLAGS_LOOPBACK, https://msdn.microsoft.com/de-de/library/windows/desktop/dd316551(v=vs.85).aspx ). Since this flag is not supported in the official build one needs to edit PortAudio as well as PyAudio, to add loopback support.
New option: "as_loopback":(true|false)
分析数据
这将为您提供数据块(按您指定的块大小)。从那里,您可以执行所需的任何DSP /峰值分析,以计算已播放的声音/具有任何属性。
这是一个快速的示例,可以帮助您开始使用peak detection in Python。为了获得更准确的结果,您可以存储要识别的.wav文件并执行cross correlation以查看是否播放了相同的.wav文件。
关于python - 如何在实时系统声音中识别声音 ‘peak’?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51273367/