dsp: zikichombo.org/dsp/resample Index | Files

package resample

import "zikichombo.org/dsp/resample"

Package resample implements resampling/changes in resolution.

Package resample uses interpolation for resampling which provides easy control over the quality/cost tradeoff and can produce very high quality resampling. Other resampling methods may be more appropriate for a given calling context, package resample doesn't yet provide other mechanisms.

When resampling audio, any decrease in sample rate from rate S to a rate R must be applied to a signal which does not contain frequencies at or above R/2, or aliasing will produce strange results.

This is often achieved by first applying a low pass filter and then resampling. As ZikiChombo does not yet have filter design support, we recommend in the meantime simply taking a moving average of the signal with a window size W = ceil(S/R) before decreasing the sample rate if you do not have access to or knowledge about low pass filtering design.

Package resample is part of http://zikichombo.org

BUG(wsc) the shift size, effecting interpolation order limits and latency of implementations is constant (64 frames).

Index

Package Files

ct.go doc.go itp.go

func Resample Uses

func Resample(src sound.Source, r freq.T, itp Itper) sound.Source

Resample takes a sound.Source src, a desired samplerate r, and an interpolator itp.

If itp is nil, it will default to a high quality interpolator (order 10 Blackman windowed sinc interpolation).

Resample returns a sound.Source whose SampleRate() is equal to r.

After a call to Resample, either the Receive method of src should not be called, or the Receive method of the result should not be called. Clearly, the former is the usual use case.

type C Uses

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

Type C holds state for giving a continuous time representation of a sound.Source.

func NewC Uses

func NewC(src sound.Source, itp Itper) *C

NewC creates a new continuous time representation of the source src using an interpolator itp.

if itp is nil, then a default interpolator of high quality will be used (order 10 Blackman windowed sinc interpolation).

NewC calls src.Receive in this process, so src.Receive should not be called if the resulting continuous time interface is used.

func (*C) At Uses

func (c *C) At(i float64) (float64, error)

At returns a continuous time interpolated sample at index i. It is the equivalent of

var buf [1]float64
if err := c.FrameAt(buf[:], i); err != nil {
	return 0.0, err
}
return buf[0], nil

func (*C) Channels Uses

func (c *C) Channels() int

Channels returns the number of channels of the source to which c provides continuous time access.

func (*C) Close Uses

func (c *C) Close() error

Close closes the underlying source and returns the resulting error.

func (*C) FrameAt Uses

func (c *C) FrameAt(dst []float64, i float64) error

FrameAt places a continuous time interpolated frame at index i in dst.

FrameAt should be called with i increasing monotonically to guarantee that c does not need to go back in time arbitrarily in its underlying source.

If i is not increasing monotonically, the behavior of FrameAt is undefined.

FrameAt returns a non-nil error if i >= the number of samples available in the underlying source without returning an error. The returned error is that returned from the underlying source.

At the edges, where insufficient or no neighbors are available, the interpolation is truncated symmetrically.

FrameAt returns sound.ErrChannelAlignment if len(dst) != c.Channels().

type DynResampler Uses

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

DynResampler is used to dynamically resample a source. It does not implement sound.Source, since the sample rate is fixed.

func NewDynResampler Uses

func NewDynResampler(c *C, conv SampleRateConverter) *DynResampler

NewDynResampler creates a new Dynamic Resampler from a continuous time representation and a sample rate converter.

func (*DynResampler) Channels Uses

func (r *DynResampler) Channels() int

DynResampler returns the number of channels.

func (*DynResampler) Close Uses

func (r *DynResampler) Close() error

Close implements sound.Close

func (*DynResampler) Receive Uses

func (r *DynResampler) Receive(d []float64) (int, error)

Receive is as in sound.Source.Receive.

type Itper Uses

type Itper interface {
    // Order returns the maximum number of discrete neighbors on either
    // side of the point to be interpolated.
    Order() int

    // Itp performs the interpolation for point i.  i must be in
    // the range [0..2*Itper.Order()).  Interpolation neighborhood
    // is truncated according to the bounds of neighbors.
    Itp(neighbors []float64, i float64) float64

    // ItpCirc interpolates for point i in neighbors supposing
    // neighbors is circularly wrapped.  This is useful for
    // when neighbors is in a circular buffer and the context
    // guarantees there are sufficient neighbors around index i.
    CircItp(neighbors []float64, i float64) float64
}

Itper provides an interface for an interpolator.

func LinItp Uses

func LinItp() Itper

LinItp returns a linear interpolator.

func NewFnItp Uses

func NewFnItp(o int, fn func(dist float64) float64) Itper

NewFnItp returns a new interpolator from a weighting function fn.

The function fn should accept values in the range (-o..o) giving the (signed) distance to the point to be interpolated. It should return an appropriate weight for input point at the specified distance.

func NewLanczos Uses

func NewLanczos(order, stretch int) Itper

NewLanczos returns a new Lanczos interpolator with stretch "stretch" of order order.

func NewSincItp Uses

func NewSincItp(o int) Itper

NewSincItp returns a new Sinc interpolator from the Shannon interpolation theorem.

func NewWinSinc Uses

func NewWinSinc(o int, wf func(float64) float64) Itper

NewWinSinc returns a new windowed sinc interpolator where the interpolation weighting function is a windowed sinc windowed by wf.

type SampleRateConverter Uses

type SampleRateConverter interface {
    // Convert is called by the resampling methods in this package to determine
    // the result frequency in a conversion.
    //
    // It is called once for every output sample except the first sample, which
    // is taken to be at the same point in time as the first input sample.
    //
    // The return value should provide the ratio of the output rate to the input
    // rate.  It is assumed the input rate is fixed and determined by calling
    // context.
    Convert() float64
}

SampleRateConverter provides an interface to a dynamic resample rate conversion.

Bugs

the shift size, effecting interpolation order limits and latency of implementations is constant (64 frames).

Package resample imports 7 packages (graph). Updated 2018-09-01. Refresh now. Tools for package owners.