resample

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: BSD-2-Clause Imports: 6 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. Its output keeps pace with its input, so equal chunks in give equal chunks out, the first one included; the filter delays the audio by a few milliseconds, which Flush emits where the stream ends. Create one per stream with New; it is not safe for concurrent use.
  • 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, taking the clip to be silent before and after, and its output lines up with its input and keeps the clip's length.

Both run one polyphase windowed-sinc filter (see sinc.go), flat to 90% of the lower rate's band, with about 90 dB of aliasing and image rejection. It is pure Go, so it cross-compiles and links into static binaries with no native dependency.

Index

Constants

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

View Source
var (
	// ErrBadRate reports a sample rate that is not positive.
	ErrBadRate = errors.New("resample: sample rates must be positive")
	// ErrTooLarge reports a pair of rates whose filter would be too large.
	ErrTooLarge = errors.New("resample: the filter for these rates is too large")
)

Errors a filter is refused with.

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 in a single pass. The output lines up with the input and holds the clip's length at the output rate, rounded to the nearest frame. 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.

Types

type Config added in v0.1.0

type Config struct {
	// 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. Zero, the default, never clears, and neither does
	// NeverClear (or any negative value); a positive value clears after that
	// long idle.
	//
	// 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 Resampler

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

Resampler converts a stream of interleaved S16LE PCM from one sample rate to another with a polyphase windowed-sinc filter that keeps the lower rate's band flat to 90% of its Nyquist frequency and attenuates aliasing and images by about 90 dB.

Its output keeps pace with its input: every call returns exactly the audio its input amounts to at the output rate, so equal input chunks come out as equal output chunks, the first one included. The filter delays the audio by a few milliseconds instead of holding output back; Flush emits those milliseconds where a stream ends, and Clear drops them where a stream is abandoned.

Create one per audio stream with New; it is not safe for concurrent use.

func New

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

New returns a Resampler from inRate to outRate for the given channel count. When inRate equals outRate the Resampler passes audio through unchanged.

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 audio held in the filter, so the next chunk is converted as the start of a fresh signal rather than the continuation of the last one.

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 the stream. The filter it used stays shared with the other streams between the same rates. It is safe to call more than once.

func (*Resampler) Delay added in v0.5.0

func (r *Resampler) Delay() int

Delay is how many input frames it takes for the last input to reach the output: the audio the filter is holding, which Flush emits.

func (*Resampler) Flush added in v0.2.0

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

Flush emits the audio the filter is still delaying and resets for the next stream. Call it when a stream has ended; the returned audio belongs at the end of that stream. It returns nothing when no audio has gone in since the resampler was created, flushed or cleared, and when the rates match.

func (*Resampler) Process

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

Process resamples one buffer of interleaved S16LE PCM and returns exactly the audio it amounts to at the output rate, carrying any fraction of a frame into the next call. When the input and output rates match it returns the input unchanged; otherwise the returned slice is freshly allocated and owned by the caller. Use Resample instead for a buffer that is complete on its own.

A gap longer than a configured idle window clears the audio held in the filter 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