Thomas
Thomas

Reputation: 402

Sound activated recording in Julia

I'm recording audio with Julia and want to be able to trigger a 5 second recording after the audio signal exceeds a certain volume. This is my record script so far:

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)

I'm new to Julia and signal processing in general. How can I tell this only to start recording once the audio exceeds a specified volume threshold?

Upvotes: 3

Views: 188

Answers (1)

Bill
Bill

Reputation: 6086

You need to test the sound you capture for average amplitude and act on that. Save if loud enough, otherwise rinse and repeat.

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
    

Upvotes: 3

Related Questions