/go-webrtcvad

WebRTC Voice Activity Detection for Golang

Primary LanguageC++

go-webrtcvad

Build Status

This project rewrite from maxhawkins/go-webrtcvad. The WebRTC source code download form WebRTC lkgr. The Abseil Common Libraries (C++) source code download from abseil/abseil-cpp. Need C++ compiler with support for C++11.

Installation

go get github.com/baabaaox/go-webrtcvad

Example

package main

import (
    "io"
    "log"
    "os"
    "github.com/baabaaox/go-webrtcvad"
)

const (
    // VadMode vad mode
    VadMode = 0
    // SampleRate sample rate
    SampleRate = 16000
    // BitDepth bit depth
    BitDepth = 16
    // FrameDuration frame duration
    FrameDuration = 20
)

var (
    audioFile   *os.File
    frameIndex  = 0
    frameSize   = SampleRate / 1000 * FrameDuration
    frameBuffer = make([]byte, SampleRate/1000*FrameDuration*BitDepth/8)
    frameActive = false
)

func main() {
    // ffmpeg -y -i test.mp4 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 test.pcm
    audioFile, err := os.Open("test.pcm")
    if err != nil {
        log.Fatal(err)
    }
    defer audioFile.Close()
    vadInst := webrtcvad.Create()
    defer webrtcvad.Free(vadInst)
    webrtcvad.Init(vadInst)
    if err != nil {
        log.Fatal(err)
    }
    err = webrtcvad.SetMode(vadInst, VadMode)
    if err != nil {
        log.Fatal(err)
    }
    for {
        _, err = audioFile.Read(frameBuffer)
        if err == io.EOF {
            break
        }
        if err != nil {
            return
        }
        frameActive, err = webrtcvad.Process(vadInst, SampleRate, frameBuffer, frameSize)
        if err != nil {
            log.Fatal(err)
        }
        log.Printf("Frame: %v, Active: %v", frameIndex, frameActive)
        frameIndex++
    }
}