Skip to content

WebRTC Voice Activity Detection for Golang

Notifications You must be signed in to change notification settings

Stilch/go-webrtcvad

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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++
    }
}

About

WebRTC Voice Activity Detection for Golang

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 88.0%
  • C 8.1%
  • Starlark 1.4%
  • NASL 1.1%
  • CMake 0.9%
  • Python 0.2%
  • Other 0.3%