Stop recording once volume is below threshold with python-sounddevice

1.5k Views Asked by At

I currently have some python code that constantly records 4-second chunks of audio:

#!/usr/bin/env python3
import sounddevice as sd

fs = 16000

while True:
    print('Started listening')
    myrecording = sd.rec(int(4 * fs), dtype='int16', channels=1, blocking=True)

However, instead of having a fixed 4-second chunk, I would like sounddevice to record until the volume of the drops below a threshold of audio (ie, when the person with the microphone has stopped talking), and then start listening again.

Essentially, I want to mimic the behaviour of a command like sox's rec recording.wav silence 1 0.1 3% 1 3.0 3%, which does exactly this.

Is there an easy way to do this with sounddevice?

1

There are 1 best solutions below

0
On

It seems that the libraries that do this are called VAD (voice audio detection). For python, a good one seems to be py-webrtcvad.