flac

package module
v0.0.0-...-d5a7441 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

README

flac

GoDoc Go Report Card Coverage Status

This package provides access to FLAC (Free Lossless Audio Codec) streams.

  • flac: provides access to FLAC (Free Lossless Audio Codec) streams.
    • frame: implements access to FLAC audio frames.
    • meta: implements access to FLAC metadata blocks.

Documentation

Overview

Package flac provides access to FLAC (Free Lossless Audio Codec) streams.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNoSeektable = errors.New("stream.searchFromStart: no seektable exists")      // seektable has not been created (search in the thread is impossible)
	ErrNoSeeker    = errors.New("stream.Seek: reader does not implement io.Seeker") // flac.NewSeek was called using io.Reader, which does not implement io.Seeker
)

Functions

This section is empty.

Types

type Encoder

type Encoder struct {
	// FLAC stream of encoder.
	*Stream
	// contains filtered or unexported fields
}

Encoder represents a FLAC encoder.

func NewEncoder

func NewEncoder(w io.Writer, info *meta.StreamInfo, blocks ...*meta.Block) (*Encoder, error)

NewEncoder returns a new FLAC encoder for the given metadata StreamInfo block and optional metadata blocks.

func (*Encoder) Close

func (enc *Encoder) Close() error

Close closes the underlying io.Writer of the encoder and flushes any pending writes. If the io.Writer implements io.Seeker, the encoder will update the StreamInfo metadata block with the MD5 checksum of the unencoded audio samples, the number of samples, and the minimum and maximum frame size and block size.

func (*Encoder) WriteFrame

func (enc *Encoder) WriteFrame(f *frame.Frame) error

WriteFrame encodes the given audio frame to the output stream. The Num field of the frame header is automatically calculated by the encoder.

type Stream

type Stream struct {
	// The StreamInfo metadata block describes
	// the basic properties of the FLAC audio stream.
	Info *meta.StreamInfo
	// Zero or more metadata blocks.
	Blocks []*meta.Block
	// contains filtered or unexported fields
}

Stream contains the metadata blocks and provides access to the audio frames of a FLAC stream.

func New

func New(r io.Reader) (stream *Stream, err error)

New creates a new Stream for accessing the audio samples of r. It reads and parses the FLAC signature and the StreamInfo metadata block, but skips all other metadata blocks.

Call Stream.Next to parse the frame header of the next audio frame, and call Stream.ParseNext to parse the entire next frame including audio samples.

func NewSeek

func NewSeek(rs io.ReadSeeker) (stream *Stream, err error)

NewSeek returns a Stream that has seeking enabled. The incoming io.ReadSeeker will not be buffered, which might result in performance issues. Using an in-memory buffer like *bytes.Reader should work well.

func Open

func Open(path string) (stream *Stream, err error)

Open creates a new Stream for accessing the audio samples of path. It reads and parses the FLAC signature and the StreamInfo metadata block, but skips all other metadata blocks.

Call Stream.Next to parse the frame header of the next audio frame, and call Stream.ParseNext to parse the entire next frame including audio samples.

Note: The Close method of the stream must be called when finished using it.

Example
package main

import (
	"bytes"
	"crypto/md5"
	"fmt"
	"io"
	"log"

	"github.com/pchchv/flac"
)

func main() {
	// open love.flac for audio streaming without parsing metadata
	stream, err := flac.Open("testdata/love.flac")
	if err != nil {
		log.Fatal(err)
	}
	defer stream.Close()

	// parse audio samples and verify the
	// MD5 signature of the decoded audio samples
	md5sum := md5.New()
	for {
		// parse one frame of audio samples at the time,
		// each frame containing one subframe per audio channel
		frame, err := stream.ParseNext()
		if err != nil {
			if err == io.EOF {
				break
			}
			log.Fatal(err)
		}
		frame.Hash(md5sum)

		// print first three samples from each channel of the first five frames
		if frame.Num < 5 {
			fmt.Printf("frame %d\n", frame.Num)
			for i, subframe := range frame.Subframes {
				fmt.Printf("  subframe %d\n", i)
				for j, sample := range subframe.Samples {
					if j >= 3 {
						break
					}
					fmt.Printf("    sample %d: %v\n", j, sample)
				}
			}
		}
	}

	fmt.Println()

	got, want := md5sum.Sum(nil), stream.Info.MD5sum[:]
	fmt.Println("decoded audio md5sum valid:", bytes.Equal(got, want))
}
Output:
frame 0
  subframe 0
    sample 0: 126
    sample 1: 126
    sample 2: 126
  subframe 1
    sample 0: 126
    sample 1: 126
    sample 2: 126
frame 1
  subframe 0
    sample 0: 126
    sample 1: 126
    sample 2: 126
  subframe 1
    sample 0: 126
    sample 1: 126
    sample 2: 126
frame 2
  subframe 0
    sample 0: 121
    sample 1: 130
    sample 2: 137
  subframe 1
    sample 0: 121
    sample 1: 130
    sample 2: 137
frame 3
  subframe 0
    sample 0: -9501
    sample 1: -6912
    sample 2: -3916
  subframe 1
    sample 0: -9501
    sample 1: -6912
    sample 2: -3916
frame 4
  subframe 0
    sample 0: 513
    sample 1: 206
    sample 2: 152
  subframe 1
    sample 0: 513
    sample 1: 206
    sample 2: 152

decoded audio md5sum valid: true

func Parse

func Parse(r io.Reader) (stream *Stream, err error)

Parse creates a new Stream for accessing the metadata blocks and audio samples of r. It reads and parses the FLAC signature and all metadata blocks.

Call Stream.Next to parse the frame header of the next audio frame, and call Stream.ParseNext to parse the entire next frame including audio samples.

func ParseFile

func ParseFile(path string) (stream *Stream, err error)

ParseFile creates a new Stream for accessing the metadata blocks and audio samples of path. It reads and parses the FLAC signature and all metadata blocks.

Call Stream.Next to parse the frame header of the next audio frame, and call Stream.ParseNext to parse the entire next frame including audio samples.

Note: Close method of the stream must be called when finished using it.

Example
package main

import (
	"fmt"
	"log"

	"github.com/pchchv/flac"
)

func main() {
	// parse metadata of love.flac
	stream, err := flac.ParseFile("testdata/love.flac")
	if err != nil {
		log.Fatal(err)
	}
	defer stream.Close()

	fmt.Printf("unencoded audio md5sum: %032x\n", stream.Info.MD5sum[:])
	for i, block := range stream.Blocks {
		fmt.Printf("block %d: %v\n", i, block.Type)
	}
}
Output:
unencoded audio md5sum: bdf6f7d31f77cb696a02b2192d192a89
block 0: seek table
block 1: vorbis comment
block 2: padding

func (*Stream) Close

func (stream *Stream) Close() error

Close closes the stream gracefully if the underlying io.Reader also implements the io.Closer interface.

func (*Stream) Next

func (stream *Stream) Next() (f *frame.Frame, err error)

Next parses the frame header of the next audio frame. It returns io.EOF to signal a graceful end of FLAC stream.

Call Frame.Parse to parse the audio samples of its subframes.

func (*Stream) ParseNext

func (stream *Stream) ParseNext() (f *frame.Frame, err error)

ParseNext parses the entire next frame including audio samples. Returns io.EOF to signal a graceful end of FLAC stream.

func (*Stream) Seek

func (stream *Stream) Seek(sampleNum uint64) (uint64, error)

Seek seeks to the frame containing the given absolute sample number. The return value specifies the first sample number of the frame containing sampleNum.

Directories

Path Synopsis
Package frame implements access to FLAC audio frames.
Package frame implements access to FLAC audio frames.
internal
bits
Package bits provides bit access operations and binary decoding algorithms.
Package bits provides bit access operations and binary decoding algorithms.
hashutil
Package hashutil provides utility interfaces for hash functions.
Package hashutil provides utility interfaces for hash functions.
ioutilx
Package ioutilx implements extended input/output utility functions.
Package ioutilx implements extended input/output utility functions.
utf8
Package utf8 implements encoding and decoding of UTF-8 coded numbers.
Package utf8 implements encoding and decoding of UTF-8 coded numbers.
Package meta package implements access to FLAC metadata blocks.
Package meta package implements access to FLAC metadata blocks.

Jump to

Keyboard shortcuts

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