pcm

package
v4.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package pcm provides a interface for interacting with PCM audio streams

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedBits = fmt.Errorf("unsupported bits in pcm format")

ErrUnsupportedBits represents that the Bits value for a Format was not supported for some operation.

Functions

This section is empty.

Types

type Format

type Format struct {
	// SampleRate defines how many times per second a consumer should read a single value. An example
	// of a common value for this is 44100 or 44.1khz.
	SampleRate uint32
	// Channels defines how many concurrent audio channels are present in audio data. Common values are
	// 1 for mono and 2 for stereo.
	Channels uint16
	// Bits determines how many bits a single sample value takes up. 8, 16, and 32 are common values.
	// TODO: Do we need LE vs BE, float vs int representation?
	Bits uint16
}

Format is a PCM format; it defines how binary audio data should be converted into real audio.

func (Format) BytesPerSecond

func (f Format) BytesPerSecond() uint32

BytesPerSecond returns how many bytes this format would be encoded into per second in an audio stream.

func (Format) PCMFormat

func (f Format) PCMFormat() Format

PCMFormat returns this format.

func (Format) SampleFloat

func (f Format) SampleFloat(b []byte) (values []float64, read int, err error)

ReadFloat reads a single sample from an audio stream, respecting bits and channels: f.Bits / 8 bytes * f.Channels bytes will be read from b, and this count will be returned as 'read'. the length of values will be equal to f.Channels, if no error is returned. If an error is returned, it will be io.ErrUnexpectedEOF or ErrUnsupportedBits

func (Format) SampleSize

func (f Format) SampleSize() int

type Formatted

type Formatted interface {
	// PCMFormat will return the Format used by an encoded audio or expected by an audio consumer.
	// Implementations can embed a Format struct to simplify this.
	PCMFormat() Format
}

The Formatted interface represents types that are aware of a PCM Format they expect or provide.

type IOReader

type IOReader struct {
	Format
	io.Reader
}

An IOReader converts an io.Reader into a pcm.Reader

func (*IOReader) ReadPCM

func (ior *IOReader) ReadPCM(p []byte) (n int, err error)

type Reader

type Reader interface {
	Formatted
	ReadPCM(b []byte) (n int, err error)
}

A Reader mimics io.Reader for pcm data.

type Writer

type Writer interface {
	io.Closer
	Formatted
	// WritePCM expects PCM bytes matching this Writer's format.
	// WritePCM will block until all of the bytes are consumed.
	WritePCM([]byte) (n int, err error)
}

A Writer can have PCM formatted audio data written to it. It mimics io.Writer.

Jump to

Keyboard shortcuts

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