Search code examples
pythonspeech-recognitionspeech-to-textspeechpyaudio

How can I do real-time voice activity detection in Python?


I am performing a voice activity detection on the recorded audio file to detect speech vs non-speech portions in the waveform.

The output of the classifier looks like (highlighted green regions indicate speech):

enter image description here

The only issue I face here is making it work for a stream of audio input (for eg: from a microphone) and do real-time analysis for a stipulated time-frame.

I know PyAudio can be used to record speech from the microphone dynamically and there a couple of real-time visualization examples of a waveform, spectrum, spectrogram, etc, but could not find anything relevant to carrying out feature extraction in a near real-time manner.


Solution

  • You should try using Python bindings to webRTC VAD from Google. It's lightweight, fast and provides very reasonable results, based on GMM modelling. As the decision is provided per frame, the latency is minimal.

    # Run the VAD on 10 ms of silence. The result should be False.
    import webrtcvad
    vad = webrtcvad.Vad(2)
    
    sample_rate = 16000
    frame_duration = 10  # ms
    frame = b'\x00\x00' * int(sample_rate * frame_duration / 1000)
    print('Contains speech: %s' % (vad.is_speech(frame, sample_rate))
    

    Also, this article might be useful for you.

    UPDATE December 2022

    As the topic still draws attention, I'd like to update my answer. SileroVAD is very fast and very accurate VAD that was released recently under MIT license.