flac

package module
v1.0.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

README

Saprobe FLAC

Pure Go FLAC streaming decoder and encoder.

Thin layer over a fork of mewkiz/flac, providing our standard PCM API, along with a raft of optimizations.

An example cli decoder is provided.

For a full-blown cli, see Saprobe.

Support

  • Bit depths: 4, 8, 12, 16, 20, 24, 32
  • Channels: 1-8 (mono through 7.1 surround)
  • Sample rates: any valid uint32; tested at 8000-192000 Hz (11 rates)
  • Output: interleaved little-endian signed PCM
Bit Depth Bytes/Sample Notes
4 1 Signed LE
8 1 Signed LE
12 2 Signed LE
16 2 Signed LE
20 3 Signed LE
24 3 Signed LE
32 4 Signed LE

API

func NewDecoder(rs io.ReadSeeker) (*Decoder, error)
func (d *Decoder) Read(p []byte) (int, error)
func (d *Decoder) Format() PCMFormat
func (d *Decoder) Close() error

func Encode(writer io.Writer, pcm []byte, format PCMFormat) error

Dependencies

github.com/mewkiz/flac (via mycophonic fork)

Other dependencies (agar) are purely for test tooling.

Detailed documentation

Documentation

Overview

Package flac decodes FLAC audio streams to raw interleaved PCM at native bit depth.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBitDepth is returned when a FLAC stream has an unsupported bit depth.
	ErrBitDepth = errors.New("unsupported bit depth")

	// ErrReadFailure is returned when reading from the FLAC stream fails.
	ErrReadFailure = errors.New("read failure")
)

Functions

func Encode

func Encode(writer io.Writer, pcm []byte, format PCMFormat) error

Encode writes interleaved little-endian signed PCM bytes as a FLAC stream to writer. It is the inverse of Decode.

Types

type BitDepth

type BitDepth uint

BitDepth represents the bit depth of PCM audio samples.

const (
	Depth4  BitDepth = 4
	Depth8  BitDepth = 8
	Depth12 BitDepth = 12
	Depth16 BitDepth = 16
	Depth20 BitDepth = 20
	Depth24 BitDepth = 24
	Depth32 BitDepth = 32
)

Standard PCM bit depths.

func (BitDepth) BytesPerSample

func (d BitDepth) BytesPerSample() int

BytesPerSample returns the number of bytes needed to store one sample. Sub-byte depths (4-bit) are stored in 1 byte (sign-extended). 12-bit samples are stored in 2 bytes (sign-extended to 16-bit). 20-bit samples are stored in 3 bytes (sign-extended to 24-bit).

type Decoder

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

Decoder streams decoded PCM from a FLAC source.

func NewDecoder

func NewDecoder(rs io.ReadSeeker) (*Decoder, error)

NewDecoder opens a FLAC stream and returns a streaming decoder. The caller should call Close when done.

func (*Decoder) Close

func (d *Decoder) Close() error

Close releases resources held by the FLAC stream.

func (*Decoder) Format

func (d *Decoder) Format() PCMFormat

Format returns the PCM output format.

func (*Decoder) Read

func (d *Decoder) Read(p []byte) (int, error)

Read reads decoded PCM bytes from the FLAC stream.

type PCMFormat

type PCMFormat struct {
	SampleRate int
	BitDepth   BitDepth
	Channels   uint
}

PCMFormat describes the format of raw PCM audio data.

Directories

Path Synopsis
cmd
flac-example-decoder command
flac-example-decoder decodes a FLAC file to WAV or raw PCM on stdout.
flac-example-decoder decodes a FLAC file to WAV or raw PCM on stdout.
Package version holds build-time metadata injected via ldflags.
Package version holds build-time metadata injected via ldflags.

Jump to

Keyboard shortcuts

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