speech

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 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 speech is offline speech recognition on the controller: the sherpa-onnx C API bound through purego, a SenseVoice recognizer and the resampler that brings recorder audio to the model's rate. It has no Node, transport or UI dependency, and audio never leaves the process.

Index

Constants

View Source
const (
	// SampleRate is the rate the recognizer decodes, in Hz.
	SampleRate = 16_000
	// MaxSamples bounds one recording to five minutes.
	MaxSamples = SampleRate * 300
)
View Source
const Version = "1.13.8"

Version is the only sherpa-onnx release this binding accepts.

Variables

View Source
var (
	// ErrUnavailable reports that the packaged speech runtime is absent or
	// is not the pinned build.
	ErrUnavailable = errors.New("speech runtime is unavailable")
	// ErrInvalidModel reports model files the recognizer cannot load.
	ErrInvalidModel = errors.New("speech model validation failed")
	// ErrRecognition reports a failed decode.
	ErrRecognition = errors.New("speech recognition failed")
	// ErrInvalidAudio reports audio outside the accepted bounds.
	ErrInvalidAudio = errors.New("invalid speech audio")
)
View Source
var Languages = [...]string{"auto", "chinese", "english"}

Languages are the values of the dictation.language preference, in the order of the constants.

Functions

func Initialize

func Initialize() error

Initialize loads the runtime packaged beside the executable, the only place it is loaded from.

func Load

func Load(directory string) error

Load opens the pinned sherpa-onnx C API library in directory. The pinned onnxruntime of the same directory is opened first, so that sherpa-onnx binds to the image every other consumer uses. The process keeps one runtime: a second directory is refused.

func Normalize

func Normalize(samples []float32, rate uint32) ([]float32, error)

Normalize accepts mono PCM from a platform recorder and returns it at SampleRate. The rate and the duration are bounded and every sample must be finite.

func Silent

func Silent(samples []float32) bool

Silent reports audio the recognizer returns no text for: shorter than a fifth of a second, or without any sample above the noise floor.

Types

type Language

type Language int

Language is the language the recognizer assumes.

const (
	Auto Language = iota
	Chinese
	English
)

func ParseLanguage

func ParseLanguage(preference string) Language

ParseLanguage maps a dictation.language preference; anything else is Auto.

func (Language) Code

func (l Language) Code() string

Code is the SenseVoice language code.

type Recognizer

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

Recognizer is one loaded SenseVoice model, reused for the successive decodes of a recording. It is not safe for concurrent use.

func NewRecognizer

func NewRecognizer(model, tokens string, language Language) (*Recognizer, error)

NewRecognizer loads a SenseVoice graph and its vocabulary after Load. A path that cannot be a C string, or files sherpa-onnx rejects, are ErrInvalidModel.

func (*Recognizer) Close

func (r *Recognizer) Close()

Close releases the model. The recognizer must not be used afterwards.

func (*Recognizer) Recognize

func (r *Recognizer) Recognize(samples []float32) (string, error)

Recognize decodes cumulative mono audio at SampleRate; each result replaces the previous hypothesis. Silence is the empty text.

type Sampler

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

Sampler converts a stream at another rate to SampleRate by averaging the input samples that fall into each output period. Upsampling repeats the average.

func NewSampler

func NewSampler(rate uint32) *Sampler

NewSampler converts from rate, in Hz.

func (*Sampler) Push

func (s *Sampler) Push(value float32, emit func(float32))

Push takes one input sample and emits the output samples it completes. A sample that is not finite counts as silence; others are clamped to ±1.

Directories

Path Synopsis
Package capture records a microphone through a purego binding of the miniaudio shim built from native/capture.
Package capture records a microphone through a purego binding of the miniaudio shim built from native/capture.
Package models is the controller's own speech-model installations: the recommended SenseVoice model and the models registered in the index models/speech.json of the desktop data directory, with their files under models/speech/<id>.
Package models is the controller's own speech-model installations: the recommended SenseVoice model and the models registered in the index models/speech.json of the desktop data directory, with their files under models/speech/<id>.

Jump to

Keyboard shortcuts

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