Julia 中的声音激活录音

Sound activated recording in Julia

我正在使用 Julia 录制音频,希望能够在音频信号超过特定音量后触发 5 秒的录制。这是我目前的记录脚本:

using PortAudio, SampledSignals, LibSndFile, FileIO, Dates

stream = PortAudioStream("HDA Intel PCH: ALC285 Analog (hw:0,0)")

buf = read(stream, 5s)

close(stream)

save(string("recording_", Dates.format(now(), "yyyymmdd_HHMMSS"), ".wav"), buf, Fs = 48000)

我对 Julia 和一般的信号处理不熟悉。我怎样才能告诉它仅在音频超过指定音量阈值时才开始录制?

您需要测试您捕获的声音的平均振幅并据此采取行动。如果声音足够大就保存,否则冲洗并重复。

using PortAudio, SampledSignals, LibSndFile, FileIO

const hassound = 10   # choose this to fit

suprathreshold(buf, thresh = hassound) = norm(buf) / sqrt(length(buf)) > thresh  # power over threshold
    
stream = PortAudioStream("HDA Intel PCH: ALC285 Analog (hw:0,0)")

while true
    buf = read(stream, 5s)
    close(stream)
    if suprathreshold(buf)
        save("recording.wav", buf, Fs = 48000) # should really append here maybe???
    end
end