Documentation
¶
Overview ¶
Package segmenter provides audio segmentation functionality.
The segmenter reads audio from any supported format (using the reader package) and outputs segments of mono float32 samples at a chosen sample rate. It supports:
- Fixed segment sizes (e.g., 30 seconds)
- Silence-based segmentation (break on silence boundaries)
- Sample rate conversion
- Mono output (single channel)
Use cases include:
- Speech-to-text preprocessing (splitting audio into segments)
- Audio analysis (processing chunks at a time)
- Streaming audio processing
Index ¶
Constants ¶
const ( DefaultSilenceThreshold = 0.01 // Default silence threshold (RMS) DefaultSilenceDuration = time.Millisecond * 500 // Default silence duration MinSegmentDuration = time.Millisecond * 100 // Minimum segment/silence duration MinBoundaryInterval = 2 * boundaryTolerance // Minimum time between the times to cut segments at )
const ClassifyWindow = 6 * time.Second
ClassifyWindow is the minimum duration of samples a segment is classified with. A shorter segment is classified with the samples before it as well, so it isn't output any later.
const VADFrameDuration = 20 * time.Millisecond
VADFrameDuration is the duration of each frame of samples which is analysed for voice activity
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Class ¶
type Class int
Class is the kind of audio in a segment
const ( ClassUnknown Class = iota // Not silence, and not classified further ClassSilence // Silence ClassSpeech // Mostly speech ClassMusic // Tonal sound which isn't speech, such as music ClassOther // Other sound, such as noise )
Classes of audio. A segment is silence when its RMS energy is below the silence threshold, or DefaultSilenceThreshold when there's none. Otherwise, it's classified further only with a ModeVAD mode.
type Mode ¶
type Mode int
Mode sets how silence is detected, to break segments at silence boundaries
const ( ModeNone Mode = iota // No silence detection ModeSilence // Silence is where the RMS energy is below the threshold ModeVADQuality // Silence is where there's no speech, or the RMS energy is below the threshold ModeVADLowBitrate // See ModeVADQuality ModeVADAggressive // See ModeVADQuality ModeVADVeryAggressive // See ModeVADQuality )
Silence detection modes. The voice activity detection modes are in increasing order of aggressiveness: a more aggressive mode is more likely to be right when it reports speech, but misses more of it.
type Opt ¶
type Opt func(*opts) error
Opt is a function which applies options to a Segmenter
func WithMode ¶
WithMode sets how silence is detected, to break segments where silence has lasted at least the silence duration (minimum 100ms, or zero for DefaultSilenceDuration). The threshold is the RMS energy level (0.0-1.0) below which audio is silence. With ModeSilence, zero is DefaultSilenceThreshold. With the voice activity detection modes, which also classify segments as speech, music or other sound, zero is no threshold.
func WithReaderOpt ¶
WithReaderOpt passes a reader.Opt to the reader, such as reader.WithInput to set the input format of raw audio.
func WithSegments ¶
WithSegments sets how segments are cut. With one duration, it's the target segment duration, so segments are output when they reach approximately this duration (minimum 100ms). With more than one, they're the times from the start to cut segments at, in increasing order, not including the start or the end, at least MinBoundaryInterval apart. Each cut is at a silence within a second of the time if there is one, so one segment is output for each part of the audio between the times.
func WithStream ¶
WithStream sets the index of the audio stream to decode. Without it, the audio stream with the lowest index is decoded.
type Segment ¶
type Segment struct {
Start time.Duration // Timestamp of the first sample
Duration time.Duration // Duration of the samples
Samples []float32 // Samples, in the range [-1.0, 1.0]
Class Class // Kind of audio
Stats Stats // Measurements of the samples
}
Segment is a segment of mono audio samples, passed to a SegmentFn. It's only valid during the call, as its samples are reused for the next one.
type SegmentFn ¶
SegmentFn is called for each segment. Return nil to continue, io.EOF to stop early, or any other error to abort.
type Segmenter ¶
type Segmenter struct {
// contains filtered or unexported fields
}
Segmenter reads audio samples from a reader and segments them into chunks. It can be used to process audio samples in chunks for speech recognition, audio analysis, etc.
func NewReader ¶
NewReader creates a new segmenter from an io.Reader. See Open for parameter documentation.
func Open ¶
Open creates a new segmenter from a file path or URL. The sampleRate is the output sample rate in Hz (e.g., 16000 for speech). The output is always mono (single channel).
Options:
- WithSegments(duration) - output segments of approximately this duration
- WithSegments(time, time, ...) - output segments cut near these times
- WithMode(mode, silence, threshold) - also break segments where silence has lasted at least the silence duration, detected by RMS energy (ModeSilence) or voice activity detection (ModeVAD...)
- WithStream(index) - the audio stream to decode
- WithReaderOpt(opt) - options for the reader
func (*Segmenter) Decode ¶
Decode decodes the audio stream into mono float32 samples at the configured sample rate, and calls fn for each segment. When the context is cancelled, such as to stop recording from a device, the samples so far are output as the last segment, and the context's error is returned.
func (*Segmenter) Duration ¶
Duration returns the total duration of the media stream. Returns zero if duration is unknown.
func (*Segmenter) SampleRate ¶
SampleRate returns the configured output sample rate.
type Stats ¶
type Stats struct {
Speech float64 // Fraction of frames with speech (0.0-1.0), with a ModeVAD mode
RMS float64 // RMS energy of the samples (0.0-1.0)
Peak float64 // Largest absolute sample value (0.0-1.0)
Flatness float64 // Mean spectral flatness, from tonal (0.0) to noise (1.0)
ZCR float64 // Mean zero-crossing rate, in crossings per sample
ZCRVariation float64 // Standard deviation of the zero-crossing rate, divided by its mean
LowEnergy float64 // Fraction of frames with less than half the mean RMS energy
Modulation float64 // Fraction of the energy's modulation at the syllable rate of speech (2-8Hz)
Centroid float64 // Mean spectral centroid, in Hz
Flux float64 // Mean spectral flux, how much the spectrum changes between frames (0.0-2.0)
FluxVariation float64 // Standard deviation of the spectral flux, divided by its mean
Rhythm float64 // Strength of a steady beat of 60-180 BPM in the energy onsets (0.0-1.0)
Harmonicity float64 // Mean harmonicity, from noise (0.0) to a periodic sound with a pitch of 100-1000Hz (1.0)
Voiced float64 // Fraction of frames with a harmonicity of at least 0.5
}
Stats are measurements of a segment's samples. Except for RMS and Peak, they're measured over 20ms frames