python - Raspberry Pi 上的声音激活录音和高级过滤

标签 python audio raspbian

我正在使用 USB-powered ultrasonic microphone 制作一个 Raspberry Pi bat 探测器.我希望能够在记录 bat 的同时排除昆虫和其他非 bat 的噪音。录音需要声音触发,以避免过快地填充 SD 卡并帮助分析。 This website解释了如何使用 SoX 做到这一点:

rec - c1 -r 192000 record.wav sinc 10k silence 1 0.1 1% trim 0 5

这会在至少 0.1 秒的触发声音后记录 5 秒,并包括一个 10kHz 高通滤波器。这是一个好的开始,但我真正想要的是一个高级过滤器,可以排除蟋蟀和其他非 bat 的噪音。昆虫和 bat 的叫声在频率上重叠,因此高通或带通滤波器不起作用。

Elekon Batlogger使用分析过零的周期触发器来执行此操作。来自 Batlogger 网站:

The difference in sound production of bats (vocal cords) and insects (stridulation) affects the period continuity. The period trigger takes advantage of this: enter image description here

The trigger fires when ProdVal and DivVal are lower than the set limits, so if the values ​​are within the yellow range. (Values mean default values): ProdVal = 8, higher values ​​trigger easier DivVal = 20, higher values ​​trigger easier

图片中的翻译文本:

Bat: Tonal signal

Period constant => zero crossings / time = stable

Insects: scratching

Period constant => zero crossings / time = differs

MN => mean value of the number of periods per measurement interval

SD => standard deviation of the number of periods

Higher values trigger better even at low frequencies (also insects!) And vice versa

有没有办法在 Raspberry Pi OS 中实现这个(或具有相同效果的东西)?我最熟悉的语言是 R。基于对 this question 的回答看起来 R 似乎适合这个问题,但如果 R​​ 不是最佳选择,那么我愿意接受其他建议。

我真的很感激上面描述的用于录制音频和过滤的一些工作代码。我想要的输出是包含 bat 叫声的 5 秒文件,而不是昆虫或噪音。需要在 CPU/电源使用方面高效,并且需要即时工作。

bat 和昆虫的录音示例 here .


更新:

我有一个在 Python 中运行的基本声音激活脚本 (based on this answer),但我不确定如何在其中包含一个高级过滤器:

import pyaudio
import wave
from array import array
 import time
 
FORMAT=pyaudio.paInt16
CHANNELS=1
RATE=44100
CHUNK=1024
RECORD_SECONDS=5

audio=pyaudio.PyAudio() 

stream=audio.open(format=FORMAT,channels=CHANNELS, 
                  rate=RATE,
                  input=True,
                  frames_per_buffer=CHUNK)

nighttime=True # I will expand this later

while nighttime:
     data=stream.read(CHUNK)
     data_chunk=array('h',data)
     vol=max(data_chunk)
     if(vol>=3000):
         print("recording triggered")
         frames=[]
         for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
             data = stream.read(CHUNK)
             frames.append(data)
         print("recording saved")
         # write to file
         words = ["RECORDING-", time.strftime("%Y%m%d-%H%M%S"), ".wav"]
         FILE_NAME= "".join(words) 
         wavfile=wave.open(FILE_NAME,'wb')
         wavfile.setnchannels(CHANNELS)
         wavfile.setsampwidth(audio.get_sample_size(FORMAT))
         wavfile.setframerate(RATE)
         wavfile.writeframes(b''.join(frames))
         wavfile.close()
     # check if still nighttime
     nighttime=True # I will expand this later
 
 stream.stop_stream()
 stream.close()
 audio.terminate()

最佳答案

长话短说

R 应该能够在后处理中执行此操作。如果您想在现场录音/流媒体上完成此操作,我建议您寻找其他工具。

更长的答案

R 能够通过多个包处理音频文件(最值得注意的似乎是 tuneR ),但我相当确定这将仅限于收集后处理,即分析您已经收集的文件,而不是而不是流式音频输入的“实时”过滤。

您可以采用多种方法“实时”过滤昆虫/不需要的声音。一种是只记录上面列出的文件,然后编写 R 代码来处理它们(例如,您可以使用 cron 按计划自动执行此操作)并丢弃不符合您标准的部分或文件。如果您担心 SD 卡空间,您也可以在处理后将这些文件卸载到另一个位置(即上传到某个地方的另一个驱动器)。您可以将此设置为相当短的时间范围(冒着 Pi 上 CPU 使用率的风险),以获得“几乎实时”的处理方法。

另一种方法是更多地查看 sox documentation看看那里是否有选项可以根据流式音频输入实现您想要的效果,或者看看是否有其他工具可以将输入流式传输到,这将允许进行那种过滤。

维尔·格吕克!

关于python - Raspberry Pi 上的声音激活录音和高级过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68212491/

相关文章:

javascript - 通过 javascript 和 Android 浏览器播放音频

linux - 在 RPI 上使用 cec-client 关闭电视

python - Selenium Chrome 驱动程序无法在 Pi Zero 上运行,错误代码为 -4

python - 改变 turtle 图形的操作

python - 线性搜索打印列表中存在的值的找到和未找到的值

java - 如何将声音融入到程序中

android - UBS电缆中断了音频插孔的麦克风输入

python - 为什么我的 Amazon S3 key 权限不固定?

python - Tkinter Text Widget Ctrl+A + DEL 删除标签

raspberry-pi - Windows 10 IoT Internet 连接共享