vad

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2026 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package vad provides voice activity detection: it tells the pipeline when a user is speaking. The Analyzer interface is the contract a detector implements; the package ships Silero, an ONNX-based detector, as the default.

An analyzer is fed raw mono 16-bit PCM and runs a confidence model over fixed-size frames, smoothing the result through a small state machine (quiet → starting → speaking → stopping → quiet) so brief dips or spikes do not flip the speaking decision. The turntaking package drives an analyzer and turns its state transitions into speaking and interruption frames.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Analyzer

type Analyzer interface {
	// SetSampleRate sets the stream sample rate in Hz and resets state. It is
	// called once before analysis begins and returns an error if the rate is
	// unsupported.
	SetSampleRate(sampleRate int) error
	// AnalyzeAudio feeds a chunk of mono 16-bit PCM and returns the resulting
	// state. Chunks need not align to the analyzer's frame size; audio is
	// buffered internally.
	AnalyzeAudio(buffer []byte) State
	// Params returns the detection parameters.
	Params() Params
	// Reset clears all internal state.
	Reset()
	// Close releases any resources (for example a model session).
	Close() error
}

Analyzer detects voice activity in a mono 16-bit PCM stream.

type Option

type Option func(*config)

Option configures a Silero analyzer.

func WithParams

func WithParams(p Params) Option

WithParams sets the detection parameters.

type Params

type Params struct {
	// Confidence is the minimum model confidence in [0,1] for a frame to count
	// as speech.
	Confidence float64
	// StartSecs is how long speech must persist before the state is confirmed
	// as speaking.
	StartSecs float64
	// StopSecs is how long silence must persist before the state is confirmed
	// as quiet.
	StopSecs float64
}

Params configures voice activity detection.

func DefaultParams

func DefaultParams() Params

DefaultParams returns the default detection parameters.

type Silero

type Silero struct {
	// contains filtered or unexported fields
}

Silero is a voice activity Analyzer backed by the Silero VAD ONNX model. It supports 8 kHz and 16 kHz mono input and manages the model's recurrent state and input context across calls.

func NewSilero

func NewSilero(opts ...Option) (*Silero, error)

NewSilero loads the embedded Silero VAD model and returns an analyzer. It requires the ONNX runtime to be locatable (see the onnxrt package); the returned error explains how to configure it when it is not.

func (Silero) AnalyzeAudio

func (m Silero) AnalyzeAudio(buffer []byte) State

AnalyzeAudio buffers the chunk and advances the state machine over every complete frame it now holds, returning the resulting state.

func (*Silero) Close

func (s *Silero) Close() error

Close releases the model session.

func (Silero) Params

func (m Silero) Params() Params

Params implements part of Analyzer.

func (*Silero) Reset

func (s *Silero) Reset()

Reset clears the model state, the input context and the detection state machine.

func (*Silero) SetSampleRate

func (s *Silero) SetSampleRate(sampleRate int) error

SetSampleRate sets the input sample rate. Silero supports only 8 kHz and 16 kHz.

type State

type State int

State is the voice-activity state of the audio stream.

const (
	// StateQuiet means no voice activity is detected.
	StateQuiet State = iota + 1
	// StateStarting means voice activity has begun but is not yet confirmed.
	StateStarting
	// StateSpeaking means voice activity is confirmed and ongoing.
	StateSpeaking
	// StateStopping means voice activity is ending but not yet confirmed quiet.
	StateStopping
)

func (State) String

func (s State) String() string

String returns the lowercase state name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL