multichannel

package
v0.0.0-...-f8103e0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2017 License: MIT Imports: 0 Imported by: 13

Documentation

Overview

Package multichannel provides data structure used for manipulating multi-channel audio data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer interface {
	// Channels returns the number of channels of the buffer.
	Channels() int

	// Len returns the number of samples of each channel of the buffer.
	Len() int

	// Sample returns the index-th sample of the channel-th channel of the
	// buffer.
	Sample(channel int, index int) float64

	// SetSample sets the index-th sample of the channel-c channel of the
	// buffer to value.
	SetSample(channel int, index int, value float64)

	// Slice returns a Buffer containing only the audio samples between from
	// (included) and to (excluded) for each channel. It is the equivalent of
	// buffer[from:to], if buffer is a mono-channel buffer represented by a
	// slice.
	Slice(from int, to int) Buffer
}

A Buffer is a representation of a multi-channel audio buffer.

type CBuffer

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

A CBuffer is a fixed-size circular buffer used to store multi-channel audio data.

A CBuffer is divided into two parts : a readable part, that can be read but cannot be modified, and an writable part that can be modified but cannot be read.

func NewCBuffer

func NewCBuffer(channels int, size int) CBuffer

NewCBuffer creates a new empty CBuffer, each channel containing at most size samples.

func (*CBuffer) Add

func (c *CBuffer) Add(buffer TSMBuffer)

Add adds a buffer to the CBuffer element-wise.

The buffer is added in the writable part of the CBuffer, but does not mark the samples as readable, allowing them to be modified again by the Add and Divide methods. SetReadable should be called to mark these samples as readable and to prevent them from being modified.

Add will panic if the two buffer do not have the same number of channels or if there is not enough space in the writable part of the CBuffer.

c := multichannel.NewCBuffer(1, 4)
c.Add(multichannel.Buffer{{1, 2}}
buffer := multichannel.NewBuffer(1, 3)

fmt.Println(c.Len()) // prints 0
fmt.Println(c.Read(buffer)) // prints 0, and leaves buffer unchanged
fmt.Println(buffer) // prints [[0, 0, 0]]

c.SetReadable(2)
fmt.Println(c.Len()) // prints 2
fmt.Println(c.Read(buffer)) // prints 2
fmt.Println(buffer) // prints [[1, 2, 0]]

func (*CBuffer) Divide

func (c *CBuffer) Divide(buffer NormalizeBuffer, n int)

Divide divides each channel of the CBuffer by the first n values of the NormalizeBuffer element-wise.

The CBuffer is divided in its writable part, and the samples are not marked as readable, allowing them to be modified again by the Add and Divide methods. SetReadable should be called to mark these samples as readable and to prevent them from being modified.

The values of the NormalizeBuffer that are lower than 0.0001 are ignored to avoid division by zero.

Divide will panic if there is not enough space in the writable part of the CBuffer.

func (*CBuffer) Len

func (c *CBuffer) Len() int

Len returns the number of samples that each channel contains (i.e. the size of the readable part).

func (*CBuffer) Peek

func (c *CBuffer) Peek(samples Buffer) int

Peek reads as many samples from the CBuffer as possible (min(c.Len(), buffer.Len())) without removing them from the CBuffer, writes them to the buffer, and returns the number of samples that were read.

It panics if the two buffer do not have the same number of channels.

func (*CBuffer) Read

func (c *CBuffer) Read(buffer Buffer) int

Read reads as many samples from the CBuffer as possible (min(c.Len(), buffer.Len()), removes them from the CBuffer, writes them to the buffer, and returns the number of samples that were read.

It panics if the two buffer do not have the same number of channels.

func (*CBuffer) RemainingSpace

func (c *CBuffer) RemainingSpace() int

RemainingSpace returns the number of samples that can be added to each channel (i.e. the size of the writable part).

func (*CBuffer) Remove

func (c *CBuffer) Remove(n int)

Remove removes the first n samples of the buffer, preventing them to be read again, and leaving more space for new samples to be written.

func (*CBuffer) SetReadable

func (c *CBuffer) SetReadable(n int)

SetReadable sets the next n samples as readable.

It panics if there is not enough space in the CBuffer.

func (*CBuffer) Write

func (c *CBuffer) Write(buffer Buffer) int

Write writes as many samples as possible from the buffer to the CBuffer, and returns the number of samples that were written.

It panics if the CBuffer and the buffer do not have the same number of channels.

type NormalizeBuffer

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

NormalizeBuffer is a mono-channel circular buffer, used to normalize audio buffers.

func NewNormalizeBuffer

func NewNormalizeBuffer(n int) NormalizeBuffer

NewNormalizeBuffer returns a new NormalizeBuffer of length n.

func (*NormalizeBuffer) Add

func (b *NormalizeBuffer) Add(window []float64)

Add adds a window element-wise to the buffer.

func (*NormalizeBuffer) Get

func (b *NormalizeBuffer) Get(i int) float64

Get returns the i-th value of the buffer.

func (*NormalizeBuffer) Remove

func (b *NormalizeBuffer) Remove(n int)

Remove removes the first n values of the buffer.

type TSMBuffer

type TSMBuffer [][]float64

A TSMBuffer is a representation of a multi-channel audio buffer which implements the Buffer interface and is used internally by the tsm package.

If buffer is a TSMBuffer, the value at buffer[c][i] is the value of the i-th sample of the c-th channel. Each channel should have the same number of samples. This representation makes it easy to work on each channel individually, which is often required for time-scale modifications.

func NewTSMBuffer

func NewTSMBuffer(channels int, length int) TSMBuffer

NewTSMBuffer creates a new TSMBuffer, each channel containing length samples.

func (TSMBuffer) ApplyWindow

func (b TSMBuffer) ApplyWindow(window []float64)

ApplyWindow applies a window to each channel of the buffer.

A window is a slice of float64 (as returned by the functions of the packages tsm/windows and github.com/mjibson/go-dsp/window), and is applied by multiplying each channel by the window element-wise.

ApplyWindow will panic if the buffer and the window have different lengths.

func (TSMBuffer) Channel

func (b TSMBuffer) Channel(channel int) []float64

Channel returns the channel-th channel of the buffer.

func (TSMBuffer) Channels

func (b TSMBuffer) Channels() int

Channels returns the number of channels of the buffer.

func (TSMBuffer) Len

func (b TSMBuffer) Len() int

Len returns the number of samples of each channel of the buffer.

func (TSMBuffer) Sample

func (b TSMBuffer) Sample(channel int, index int) float64

Sample returns the index-th sample of the channel-th channel.

func (TSMBuffer) SetSample

func (b TSMBuffer) SetSample(channel int, index int, value float64)

SetSample sets the value of the index-th sample of the channel-th channel of the buffer to value.

func (TSMBuffer) Slice

func (b TSMBuffer) Slice(from int, to int) Buffer

Slice returns a TSMBuffer containing only the audio samples between from (included) and to (excluded) for each channel. It is the equivalent of buffer[from:to], if buffer is a mono-channel buffer represented by a slice.

Jump to

Keyboard shortcuts

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