resample

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: BSD-2-Clause Imports: 4 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 resample converts interleaved S16LE PCM audio between sample rates, preserving the channel count. It reconciles service sample rates (for example 24 kHz TTS audio) with the 48 kHz the WebRTC/Opus output path needs.

There are two ways to convert, and which one is right depends on whether the audio keeps coming:

  • A Resampler is for a stream. It is stateful, carrying filter state across calls so a continuous stream converts cleanly across chunk boundaries, and it holds back the filter delay at the end of every call because more audio is expected. Create one per stream with New; it is not safe for concurrent use, and Close must be called when finished.
  • Resample is for a buffer that is complete on its own: a sound effect, a recorded utterance, one turn of audio handed to a model. It converts in a single pass and flushes the filter delay, so nothing is clipped off the end. Running a complete buffer through a Resampler instead loses a millisecond or two of its tail.

The conversion is pure Go (see resampler.go), a no-cgo converter from github.com/gojargo/go-resample. It cross-compiles and links into static binaries with no native dependency.

Index

Constants

View Source
const DefaultClearAfter = 200 * time.Millisecond

DefaultClearAfter is how long a Resampler may sit idle before the next chunk is treated as the start of a fresh signal rather than the continuation of the last one.

View Source
const NeverClear = -1 * time.Nanosecond

NeverClear is the ClearAfter that turns the idle window off, for a caller that marks the boundaries of a stream itself with Flush and Clear. Any negative duration does the same; this one names the intent.

Variables

This section is empty.

Functions

func Resample added in v0.1.0

func Resample(pcm []byte, inRate, outRate, channels int) ([]byte, error)

Resample converts a complete buffer of interleaved S16LE PCM from inRate to outRate at the default quality, in a single pass. Unlike Resampler.Process it flushes the converter's filter delay, so the returned audio holds the whole signal rather than stopping a filter length short of it. Use it for audio that arrives whole; use a Resampler for audio that keeps coming.

It returns the input unchanged when the rates match, and nil for an empty buffer.

func ResampleQuality added in v0.1.0

func ResampleQuality(pcm []byte, inRate, outRate, channels int, q Quality) ([]byte, error)

ResampleQuality is Resample at a chosen quality.

Types

type Config added in v0.1.0

type Config struct {
	// Quality selects the conversion filter; the zero value is QualityVHQ.
	Quality Quality
	// ClearAfter is how long the resampler may sit idle before its filter
	// history is discarded. A resampler carries the tail of the audio it last
	// saw so that a continuous stream converts cleanly across chunk boundaries,
	// but after a gap that tail is no longer what came before: it is the end of
	// the previous utterance bleeding into the start of the next one, which is
	// heard as a click. 0 uses DefaultClearAfter; NeverClear (or any negative
	// value) never clears.
	//
	// Leave it off for a caller that knows where its streams end, and say so
	// with Flush and Clear. The window only ever guesses, and it guesses wrong
	// in both directions: a telephony leg delivers a continuous stream in bursts
	// that look like gaps, and a stream running ahead of playback pauses
	// mid-utterance whenever what feeds it does.
	ClearAfter time.Duration
}

Config configures a Resampler.

type Quality added in v0.1.0

type Quality int

Quality selects the conversion filter, trading audio quality against CPU cost. The zero value is the highest quality, which is what a pipeline gets unless it asks for something cheaper.

The names are the five standard SoX Resampler recipes, mapped onto the converters the pure-Go library offers (see converterFor).

const (
	// QualityVHQ is very high quality. It is the zero value, and so the default.
	QualityVHQ Quality = iota
	// QualityHQ is high quality: a shorter filter than VHQ, and audibly
	// transparent for speech.
	QualityHQ
	// QualityMQ is medium quality.
	QualityMQ
	// QualityLQ is low quality.
	QualityLQ
	// QualityQQ is "quick": interpolation rather than a windowed filter, for
	// paths that care more about cost than about the stopband.
	QualityQQ
)

func (Quality) String added in v0.1.0

func (q Quality) String() string

String returns the recipe name.

type Resampler

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

Resampler converts a stream of interleaved S16LE PCM from one sample rate to another using the pure-Go github.com/gojargo/go-resample converter (no cgo). Create one per audio stream with New; it is not safe for concurrent use. Close releases it; the pure-Go converter holds no native resource, so it is a no-op, but callers should still call it so the contract stays honest.

func New

func New(inRate, outRate, channels int) (*Resampler, error)

New returns a Resampler from inRate to outRate for the given channel count, at the default quality and idle window. When inRate equals outRate the Resampler passes audio through unchanged and allocates no converter.

func NewWithConfig added in v0.1.0

func NewWithConfig(inRate, outRate, channels int, cfg Config) (*Resampler, error)

NewWithConfig returns a Resampler configured by cfg. See New.

func (*Resampler) Clear added in v0.1.0

func (r *Resampler) Clear()

Clear discards the filter history, so the next chunk is converted as the start of a fresh signal rather than the continuation of the last one. The rate and quality are unchanged.

It is the counterpart of Flush: use Clear for a stream that was abandoned, so its leftover tail does not bleed into the next one, and Flush for one that ended, so the tail is heard before the next one starts.

func (*Resampler) Close

func (r *Resampler) Close()

Close releases converter resources. The pure-Go converter holds none, so this is a no-op and is safe to call more than once. Callers should still call it, so the stream lifecycle stays explicit at every call site.

func (*Resampler) Flush added in v0.2.0

func (r *Resampler) Flush() []byte

Flush emits the audio the converter is still holding and clears it, so the next chunk starts a fresh stream.

A streaming converter keeps the most recent input in its filter, which is what lets a continuous stream convert cleanly across chunk boundaries: the tail of one call is only emitted once the next call supplies the audio that follows it. At the end of a stream there is no next call, so without this the tail is never heard. Call it when a stream has ended; the returned audio belongs at the end of that stream.

It returns nothing when the rates match, since a passthrough buffers nothing.

func (*Resampler) Process

func (r *Resampler) Process(in []byte) []byte

Process resamples one buffer of interleaved S16LE PCM and returns the resampled audio. When the input and output rates match it returns the input unchanged; otherwise the returned slice is freshly allocated and owned by the caller. Because the converter has filter delay, the first calls of a stream emit slightly fewer frames than the rate ratio implies, which later calls make up. Use Resample instead for a buffer that is complete on its own.

A gap longer than the configured idle window clears the filter history first, so the tail of what came before is not mixed into what comes next.

Jump to

Keyboard shortcuts

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