audio

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2025 License: MIT 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.

README

Go package for audio processing

This package makes use of Golang's generics for processing audio. Don't be initimidated by their appearance :-)

package main

import (
    "encoding/binary"
    "io"
    "os"

    "github.com/BeatGlow/audio"
)

func main() {
    f, err := os.Open("/tmp/mpd.fifo")
    if err != nil {
        panic(err)
    }
    defer func() { _ = f.Close() }()

    // Allocate a buffer of 1024 flaot64 samples.
    samples := make(audio.Samples[float64], 1024)

    r := audio.NewReader[float64](f, binary.BigEndian)
    for {
        n, err := r.ReadSamples(samples)
        if err == io.EOF {
            break
        } else if err != nil {
            panic(err)
        }
        println("Read", n, "samples")
    }
}

Documentation

Overview

Package audio ...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Sample](x T) T

Abs returns the absolute value of x.

func Clip

func Clip[T Sample](min, max T, samples Samples[T])

Clip samples between min and max.

func Max

func Max[T Sample](values Samples[T]) T

Max returns the largest value.

Example:

Max([]int8{-127, -63, 0, 63, 128}) = 128

func Mean

func Mean[T Sample](samples Samples[T]) T

Mean of values.

func Min

func Min[T Sample](values Samples[T]) T

Min returns the smallest value.

Example:

Min([]int8{-127, -63, 0, 63, 128}) = -127

func Normalize

func Normalize[T Sample](samples Samples[T])

Normalize samples to fit in [-1..1].

It doesn't make a whole lot of sense to call this function for non-float values, but you could.

NB: this function may change to bias around the centerpoint of math.MaxUint* for unsigned types.

func Pop

func Pop[T Sample](buffer *Samples[T]) T

Pop removes one sample from the end of the slice.

func Push

func Push[T Sample](buffer *Samples[T], samples ...T)

Push adds samples to the slice.

func RMS

func RMS[T Sample](samples Samples[T]) T

RMS returns the root mean square of values.

func Unshift

func Unshift[T Sample](buffer *Samples[T], samples ...T)

Unshift adds samples to the start of the slice.

Types

type Buffer

type Buffer[T Sample] [][]T

Buffer represents a multi channel audio buffer.

func Deinterleave

func Deinterleave[T Sample](dst Buffer[T], src Samples[T], channels int) Buffer[T]

Deinterleave splits interleaved samples into a multi channel buffer.

The provided buffer dst should be nil, or adequately sized to fit channels of samples.

func (Buffer[T]) BitsPerSample

func (b Buffer[T]) BitsPerSample() int

BitsPerSample is the number of bits required to store one sample.

func (Buffer[T]) Channels

func (b Buffer[T]) Channels() int

Channels is the number of channels in the buffer.

func (Buffer[T]) Samples

func (b Buffer[T]) Samples() int

Samples is the number of samples per channel.

func (Buffer[T]) ToFloat

func (b Buffer[T]) ToFloat(dst [][]float64) [][]float64

ToFloat converts the buffers in b to [][]float64 in dst.

The provided buffer dst should be nil, or adequately sized to fit all samples in all channels.

func (Buffer[T]) ToMono

func (b Buffer[T]) ToMono(dst Samples[T]) int

ToMono converts a multi channel buffer to a mono channel by averaging the sounds per channel.

type Format

type Format interface {
	// Channels is the number of channels.
	Channels() int

	// BitsPerSample is the number of bits required to store one sample.
	BitsPerSample() int
}

Format describes the audio format.

type Reader

type Reader[T Sample] interface {
	ReadSamples(Samples[T]) (int, error)
}

Reader can read samples.

func NewReader

func NewReader[T Sample](r io.Reader, order binary.ByteOrder) Reader[T]

NewReader returns a Reader that can read from any io.Reader.

type Sample

type Sample interface {
	constraints.Integer | constraints.Float
}

Sample is an audio sample, it can be any of the native Go numeric types.

type Samples

type Samples[T Sample] []T

Samples represents a mono audio buffer, or a single channel.

func Interleave

func Interleave[T Sample](dst Samples[T], src Buffer[T]) Samples[T]

Interleave a buffer into a single samples slice.

The provided samples dst should be nil, or adequately sized to fit channels of samples.

func Shift

func Shift[T Sample](buffer *Samples[T], n int) Samples[T]

Shift removes n samples from the start of the slice.

func (Samples[T]) BitsPerSample

func (s Samples[T]) BitsPerSample() int

BitsPerSample is the number of bits required to store one sample.

func (Samples[T]) Decode

func (s Samples[T]) Decode(b []byte, order binary.ByteOrder)

Decodes values encoded in b using the specified byte order.

func (Samples[T]) DecodeFrom

func (s Samples[T]) DecodeFrom(r io.Reader, order binary.ByteOrder) (n int, err error)

DecodeFrom reads samples from r to s.

func (Samples[T]) DecodeFromChunked

func (s Samples[T]) DecodeFromChunked(r io.Reader, order binary.ByteOrder, chunkSize int) (n int, err error)

DecodeFromChunked reads samples from r to s.

If s doesn't contain a multiple of chunkSize samples, an additional smaller chunk will be read to complete to read.

func (Samples[T]) Encode

func (s Samples[T]) Encode(b []byte, order binary.ByteOrder)

Encode the sample slice as bytes in b.

func (Samples[T]) EncodeTo

func (s Samples[T]) EncodeTo(w io.Writer, order binary.ByteOrder) (n int, err error)

Write samples contained in src to w.

func (Samples[T]) EncodeToChunked

func (s Samples[T]) EncodeToChunked(w io.Writer, order binary.ByteOrder, chunkSize int) (n int, err error)

WriteChunked writes samples contained in src to w.

func (Samples[T]) ToComplex

func (s Samples[T]) ToComplex(dst []complex128) []complex128

ToComplex converts the samples in s to []complex128 in dst.

func (Samples[T]) ToFloat

func (s Samples[T]) ToFloat(dst []float64) []float64

ToFloat converts samples in s to []float64 in dst.

The provided buffer dst should be nil, or adequately sized to fit all samples.

func (Samples[T]) ToInt16

func (s Samples[T]) ToInt16(dst []int16) []int16

ToInt16 converts samples in s to []int16 in dst.

The provided buffer dst should be nil, or adequately sized to fit all samples.

type Writer

type Writer[T Sample] interface {
	WriteSamples(Samples[T]) (int, error)
}

Writer can write samples.

func NewWriter

func NewWriter[T Sample](w io.Writer, order binary.ByteOrder) Writer[T]

NewWriter returns a Writer that can write to any io.Writer.

Directories

Path Synopsis
driver
dsp
internal

Jump to

Keyboard shortcuts

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