Documentation
¶
Overview ¶
Package microwakeword is a pure Go streaming wake-word detector compatible with microWakeWord v2 TFLite models (https://github.com/kahrendt/microWakeWord).
It implements the full detection pipeline: audio feature extraction (mel-spectrogram), quantised TFLite model inference, and sliding-window probability averaging. No CGo or external C/C++ dependencies.
Basic usage:
det, err := microwakeword.NewDetector(microwakeword.Config{
ModelPath: "hey_jarvis.tflite",
ProbabilityCutoff: 0.85,
SlidingWindowSize: 5,
FeaturesStepMs: 10,
})
if err != nil { log.Fatal(err) }
for chunk := range audioSource {
if det.ProcessAudio(chunk) {
fmt.Println("wake word detected")
}
}
Example ¶
package main
import (
"encoding/binary"
"fmt"
"log"
"os"
"github.com/zserge/microwakeword"
)
func main() {
det, err := microwakeword.NewDetector(microwakeword.Config{
ModelPath: "testdata/hey_jarvis.tflite",
ProbabilityCutoff: 0.85,
SlidingWindowSize: 5,
FeaturesStepMs: 10,
})
if err != nil {
log.Fatal(err)
}
// Load a WAV file (skip 44-byte header).
data, err := os.ReadFile("testdata/hey_jarvis.wav")
if err != nil {
log.Fatal(err)
}
pcm := data[44:]
samples := make([]int16, len(pcm)/2)
for i := range samples {
samples[i] = int16(binary.LittleEndian.Uint16(pcm[i*2:]))
}
// Feed 10 ms chunks (160 samples at 16 kHz).
const chunk = 160
for i := 0; i+chunk <= len(samples); i += chunk {
if det.ProcessAudio(samples[i : i+chunk]) {
fmt.Printf("detected at %.2fs\n", float64(i)/16000)
return
}
}
fmt.Println("not detected")
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// ModelPath is the path to a .tflite model file.
ModelPath string
// ProbabilityCutoff is the sliding-window average threshold (0-1).
// A higher value reduces false positives at the cost of sensitivity.
ProbabilityCutoff float64
// SlidingWindowSize is the number of recent inference results averaged
// for the detection decision.
SlidingWindowSize int
// FeaturesStepMs is the audio frontend step size in milliseconds.
// Typical values are 10 or 20. Zero defaults to 20.
FeaturesStepMs int
}
Config holds the parameters for creating a Detector.
type Detector ¶
type Detector struct {
// OnInference, if non-nil, is called after each model inference with
// the invocation count, the raw uint8 probability (0-255), and the
// current sliding-window average (0-1). Useful for debugging.
OnInference func(invocation int, rawProb uint8, slidingAvg float64)
// contains filtered or unexported fields
}
Detector is a streaming wake-word detector. It is not safe for concurrent use; protect with a mutex if shared across goroutines.
func NewDetector ¶
NewDetector loads a TFLite model and creates a ready-to-use detector.
func (*Detector) ProcessAudio ¶
ProcessAudio feeds PCM audio samples into the detector and returns true when the wake word is detected. Samples must be 16 kHz, 16-bit signed, mono. Any chunk size is accepted; the detector buffers internally.
func (*Detector) ProcessFeatures ¶
ProcessFeatures feeds pre-extracted int8 features (40 per frame) directly into the model, bypassing the audio frontend.
func (*Detector) Reset ¶
func (d *Detector) Reset()
Reset clears all internal state so the detector can be reused on a new audio stream. The loaded model is kept.
func (*Detector) SlidingAverage ¶
SlidingAverage returns the current sliding-window average probability (0-1). Useful for debugging and visualisation.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package audiofrontend implements the audio feature extraction pipeline used by microWakeWord models.
|
Package audiofrontend implements the audio feature extraction pipeline used by microWakeWord models. |
|
cmd
|
|
|
detect
command
Command detect loads a microWakeWord v2 model and a WAV file, runs detection frame-by-frame, and prints each inference result.
|
Command detect loads a microWakeWord v2 model and a WAV file, runs detection frame-by-frame, and prints each inference result. |
|
Package model parses TFLite FlatBuffer (.tflite) model files into Go structures.
|
Package model parses TFLite FlatBuffer (.tflite) model files into Go structures. |