vad

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: BSD-2-Clause Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (

	// DefaultStopSecs is the recommended silence window before a stop is
	// reported. It is exported because the measured STT latencies a turn
	// strategy sizes its safety net against were taken with it, so a strategy
	// can say so when a pipeline runs a different one.
	DefaultStopSecs = 0.2
)

Default detection parameters.

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
	// SetParams applies new detection parameters at runtime and resets the
	// detection state, so a change takes effect from the next chunk rather than
	// part-way through the one being decided.
	SetParams(params Params)
	// Reset clears all internal state.
	Reset()
	// Close releases any resources (for example a model session).
	Close() error
	// contains filtered or unexported methods
}

Analyzer detects voice activity in a mono 16-bit PCM stream. An analyzer is built by embedding *Base, which supplies the detection logic and every method here, and supplying a Scorer.

type Base added in v1.0.0

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

Base is the model-agnostic detection logic shared by analyzers. A concrete analyzer embeds *Base and supplies itself as the Scorer, so the base can score frames without inheritance. A frame counts as speech only when the model is confident enough about it and it is loud enough to be worth hearing, so a confident guess at something barely audible does not open a turn.

func NewBase added in v1.0.0

func NewBase(scorer Scorer, params Params) *Base

NewBase builds the detection logic for an analyzer that scores frames with scorer, usually the analyzer embedding it.

func (*Base) AnalyzeAudio added in v1.0.0

func (m *Base) AnalyzeAudio(buffer []byte) State

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

The analysis runs a model, so a task of a session that calls this lets the session's other tasks run until it returns.

func (*Base) Close added in v1.0.0

func (*Base) Close() error

Close releases nothing: the base holds no resources. An analyzer holding a model session overrides it.

func (*Base) Params added in v1.0.0

func (m *Base) Params() Params

Params implements part of Analyzer.

func (*Base) Reset added in v1.0.0

func (m *Base) Reset()

Reset clears everything the base holds, the buffered audio included. It is for a break in the stream rather than a change of mind about it.

func (*Base) SampleRate added in v1.0.0

func (m *Base) SampleRate() int

SampleRate is the stream sample rate in Hz, zero until SetSampleRate is called.

func (*Base) SetParams added in v1.0.0

func (m *Base) SetParams(params Params)

SetParams implements part of Analyzer. It recomputes the frame counts the parameters size and resets the detection state, so the new values apply from a clean start rather than to a decision already part-way made.

func (*Base) SetSampleRate added in v1.0.0

func (m *Base) SetSampleRate(sampleRate int) error

SetSampleRate sets the stream sample rate, recomputes the frame size and the start and stop frame counts, and resets the detection state. An analyzer that supports only some rates validates the rate in its own SetSampleRate and then calls this one.

type Option

type Option func(*config)

Option configures a Silero analyzer.

func WithModel added in v0.3.0

func WithModel(m *SileroModel) Option

WithModel configures the analyzer to reuse a shared SileroModel session.

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
	// MinVolume is how loud a frame must be, on the 0..1 scale audio/loudness
	// measures, to count as speech however confident the model is. It keeps a
	// confident guess at something barely audible from opening a turn.
	MinVolume float64
}

Params configures voice activity detection.

func DefaultParams

func DefaultParams() Params

DefaultParams returns the default detection parameters.

type Scorer added in v1.0.0

type Scorer interface {
	// NumFramesRequired is the number of samples one analysis frame holds at
	// the current sample rate.
	NumFramesRequired() int
	// VoiceConfidence scores one analysis frame, exactly NumFramesRequired
	// samples of mono 16-bit PCM, as the probability in [0,1] that it is
	// speech.
	VoiceConfidence(buffer []byte) float64
}

Scorer is the model-specific half of an analyzer: it sizes the analysis frame and scores a frame for voice activity. Base drives it.

type Silero

type Silero struct {
	*Base
	// 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.

The model's recurrent state is reset about every two seconds of audio, but only during clear non-speech. Non-speech such as music, or long silence at 8 kHz, leaves the state unable to detect the speech that follows until it is reset. A reset during speech makes the model briefly lose confidence, which can end the user's speech early, so a due reset waits until the user is quiet.

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) Close

func (s *Silero) Close() error

Close releases the model session if it is owned by this analyzer. Analyzers sharing a model session do not close the underlying session.

func (*Silero) NumFramesRequired added in v1.0.0

func (s *Silero) NumFramesRequired() int

NumFramesRequired is the number of samples per analysis frame: 512 at 16 kHz, 256 at 8 kHz.

func (*Silero) Reset

func (s *Silero) Reset()

Reset clears the model state, the input context, the reset schedule and the detection state machine, as for a new stream.

func (*Silero) SetSampleRate

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

SetSampleRate sets the input sample rate. Silero supports only 8 kHz and 16 kHz. The model starts over only when the rate changes: its state belongs to the rate it was fed.

func (*Silero) VoiceConfidence added in v1.0.0

func (s *Silero) VoiceConfidence(buffer []byte) float64

VoiceConfidence runs the model over one frame and returns its speech probability in [0,1]. buffer is exactly numFramesRequired samples of mono 16-bit PCM.

type SileroModel added in v0.3.0

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

SileroModel is a reusable ONNX session holding the loaded Silero VAD model. It must outlive any analyzers built from it. Streams sharing a model run inference sequentially one at a time.

func NewSileroModel added in v0.3.0

func NewSileroModel() (*SileroModel, error)

NewSileroModel loads the embedded Silero VAD model into a reusable session.

func (*SileroModel) Close added in v0.3.0

func (m *SileroModel) Close() error

Close releases the shared model session.

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.

Directories

Path Synopsis
Package controller drives a voice-activity detector and reports what it hears.
Package controller drives a voice-activity detector and reports what it hears.

Jump to

Keyboard shortcuts

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