Documentation ¶
Overview ¶
Package flac provides access to FLAC (Free Lossless Audio Codec) streams.
A brief introduction of the FLAC stream format [1] follows. Each FLAC stream starts with a 32-bit signature ("fLaC"), followed by one or more metadata blocks, and then one or more audio frames. The first metadata block (StreamInfo) describes the basic properties of the audio stream and it is the only mandatory metadata block. Subsequent metadata blocks may appear in an arbitrary order.
Please refer to the documentation of the meta [2] and the frame [3] packages for a brief introduction of their respective formats.
[1]: https://www.xiph.org/flac/format.html#stream [2]: https://godoc.org/github.com/apatterson-cogo/flac/meta [3]: https://godoc.org/github.com/apatterson-cogo/flac/frame
Note: the Encoder API is experimental until the 1.1.x release. As such, it's API is expected to change.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoSeeker reports that flac.NewSeek was called with an io.Reader not // implementing io.Seeker, and thus does not allow for seeking. ErrNoSeeker = errors.New("stream.Seek: reader does not implement io.Seeker") // ErrNoSeektable reports that no seektable has been generated. Therefore, // it is not possible to seek in the stream. ErrNoSeektable = errors.New("stream.searchFromStart: no seektable exists") )
Functions ¶
This section is empty.
Types ¶
type Encoder ¶
type Encoder struct { // FLAC stream of encoder. *Stream // contains filtered or unexported fields }
An Encoder represents a FLAC encoder.
func NewEncoder ¶
NewEncoder returns a new FLAC encoder for the given metadata StreamInfo block and optional metadata blocks.
func (*Encoder) Close ¶
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.
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 }
A Stream contains the metadata blocks and provides access to the audio frames of a FLAC stream.
ref: https://www.xiph.org/flac/format.html#stream
func New ¶
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 ¶
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/apatterson-cogo/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 ¶
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 ¶
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: The Close method of the stream must be called when finished using it.
Example ¶
package main import ( "fmt" "log" "github.com/apatterson-cogo/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 ¶
Close closes the stream if opened through a call to Open or ParseFile, and performs no operation otherwise.
func (*Stream) Next ¶
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.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
flac2wav
The flac2wav tool converts FLAC files to WAV files.
|
The flac2wav tool converts FLAC files to WAV files. |
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. |
hashutil/crc16
Package crc16 implements the 16-bit cyclic redundancy check, or CRC-16, checksum.
|
Package crc16 implements the 16-bit cyclic redundancy check, or CRC-16, checksum. |
hashutil/crc8
Package crc8 implements the 8-bit cyclic redundancy check, or CRC-8, checksum.
|
Package crc8 implements the 8-bit cyclic redundancy check, or CRC-8, checksum. |
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 implements access to FLAC metadata blocks.
|
Package meta implements access to FLAC metadata blocks. |