Detecting volume level

How can I work my app to detect microphone sound stream’s volume?
For example let’s say I want to build a security surveillance app which is supposed to record sound whenever there is a loud enough (specified) sound/noise/voice.
And do not record anything whenever there is no loud enough sounds around?

The surveillance was an example, it can be used in a lot of apps,
Personally I want to record my sleep talking, but I don’t like to listen to 8 hours long audio clips.
I want the app to only record the moments I’m talking in sleep.

I think the answer will be the same as for synthesizing audio (… get MBS portAudio plugin.

I’ve been using declares to the mac Audio Queue framework for recording and synthesizing but apparently it’s not safe. Here’s where I’m at with recording anyways, just plots the mics input

I have a similar project (OSX) using commandline tool rec from sox
you must have sox installed

maybe it works on Windows if you change the path to ‘rec’

it makes an audio recording and stops when there are 3 seconds of silence.
when again a volume level is present, it starts a new audio recording.