flac

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2023 License: Apache-2.0 Imports: 6 Imported by: 29

README

go-flac

Documentation Build Status Coverage Status

go library for manipulating FLAC metadata

Introduction

A FLAC(Free Lossless Audio Codec) stream generally consists of 3 parts: a "fLaC" header to mark the stream as an FLAC stream, followed by one or more metadata blocks which stores metadata and information regarding the stream, followed by one or more audio frames. This package encapsulated the operations that extract and split FLAC metadata blocks from a FLAC stream file and assembles them back after modification. this package only implemented parsing the essential StreamInfo metadata block by File#GetStreamInfo, other metadata block operations should be provided by other packages.

Usage

go-flac provided three APIs for reading FLAC files and returns a File struct:

  • ParseBytes for consuming a whole FLAC stream from an io.Reader.
  • ParseFile for consuming a whole FLAC stream from a file.
  • ParseMetadata for consuming only metadata from am io.Reader.

The File struct has two exported fields, Meta and Frames, the Frames consisted of raw stream data and the Meta field was a slice of all MetaDataBlocks present in the file. Other packages could parse/construct a MetadataBlock by inspecting its Type field and apply proper decoding/encoding on the Data field of the MetadataBlock. You can modify the elements in the Meta field of a File as you like, as long as the StreamInfo metadata block is the first element in Meta field, according to the specs of FLAC format.

Examples

The following example extracts the sample rate of a FLAC file.

package example

import (
    "github.com/go-flac/go-flac"
)

func getSampleRate(fileName string) int {
	f, err := flac.ParseFile(fileName)
	if err != nil {
		panic(err)
	}
	data, err := f.GetStreamInfo()
	if err != nil {
		panic(err)
	}
	return data.SampleRate
}

The following example adds a jpeg image as front cover to the FLAC metadata using flacpicture.

package example

import (
    "github.com/go-flac/flacpicture"
    "github.com/go-flac/go-flac"
)

func addFLACCover(fileName string, imgData []byte) {
	f, err := flac.ParseFile(fileName)
	if err != nil {
		panic(err)
	}
	picture, err := flacpicture.NewFromImageData(flacpicture.PictureTypeFrontCover, "Front cover", imgData, "image/jpeg")
	if err != nil {
		panic(err)
	}
	picturemeta := picture.Marshal()
	f.Meta = append(f.Meta, &picturemeta)
	f.Save(fileName)
}

Documentation

Overview

Package flac encapsulated the operations that extract and split FLAC metadata blocks from a FLAC stream file and assembles them back after modifications provided by other packages.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorNoFLACHeader indicates that "fLaC" marker not found at the beginning of the file
	ErrorNoFLACHeader = errors.New("fLaC head incorrect")
	// ErrorNoStreamInfo indicates that StreamInfo Metablock not present or is not the first Metablock
	ErrorNoStreamInfo = errors.New("stream info not present")
	// ErrorStreamInfoEarlyEOF indicates that an unexpected EOF is hit while reading StreamInfo Metablock
	ErrorStreamInfoEarlyEOF = errors.New("unexpected end of stream while reading stream info")
	// ErrorNoSyncCode indicates that the frames are malformed as the sync code is not present after the last Metablock
	ErrorNoSyncCode = errors.New("frames do not begin with sync code")
)

Functions

This section is empty.

Types

type BlockData

type BlockData []byte

BlockData data in a FLAC Metadata Block. Custom Metadata decoders and modifiers should accept/modify whole MetaDataBlock instead.

type BlockType

type BlockType int

BlockType representation of types of FLAC Metadata Block

const (
	// StreamInfo METADATA_BLOCK_STREAMINFO
	// This block has information about the whole stream, like sample rate, number of channels, total number of samples, etc. It must be present as the first metadata block in the stream. Other metadata blocks may follow, and ones that the decoder doesn't understand, it will skip.
	StreamInfo BlockType = iota
	// Padding METADATA_BLOCK_PADDING
	// This block allows for an arbitrary amount of padding. The contents of a PADDING block have no meaning. This block is useful when it is known that metadata will be edited after encoding; the user can instruct the encoder to reserve a PADDING block of sufficient size so that when metadata is added, it will simply overwrite the padding (which is relatively quick) instead of having to insert it into the right place in the existing file (which would normally require rewriting the entire file).
	Padding
	// Application METADATA_BLOCK_APPLICATION
	// This block is for use by third-party applications. The only mandatory field is a 32-bit identifier. This ID is granted upon request to an application by the FLAC maintainers. The remainder is of the block is defined by the registered application. Visit the registration page if you would like to register an ID for your application with FLAC.
	Application
	// SeekTable METADATA_BLOCK_SEEKTABLE
	// This is an optional block for storing seek points. It is possible to seek to any given sample in a FLAC stream without a seek table, but the delay can be unpredictable since the bitrate may vary widely within a stream. By adding seek points to a stream, this delay can be significantly reduced. Each seek point takes 18 bytes, so 1% resolution within a stream adds less than 2k. There can be only one SEEKTABLE in a stream, but the table can have any number of seek points. There is also a special 'placeholder' seekpoint which will be ignored by decoders but which can be used to reserve space for future seek point insertion.
	SeekTable
	// VorbisComment METADATA_BLOCK_VORBIS_COMMENT
	// This block is for storing a list of human-readable name/value pairs. Values are encoded using UTF-8. It is an implementation of the Vorbis comment specification (without the framing bit). This is the only officially supported tagging mechanism in FLAC. There may be only one VORBIS_COMMENT block in a stream. In some external documentation, Vorbis comments are called FLAC tags to lessen confusion.
	VorbisComment
	// CueSheet METADATA_BLOCK_CUESHEET
	// This block is for storing various information that can be used in a cue sheet. It supports track and index points, compatible with Red Book CD digital audio discs, as well as other CD-DA metadata such as media catalog number and track ISRCs. The CUESHEET block is especially useful for backing up CD-DA discs, but it can be used as a general purpose cueing mechanism for playback.
	CueSheet
	// Picture METADATA_BLOCK_PICTURE
	// This block is for storing pictures associated with the file, most commonly cover art from CDs. There may be more than one PICTURE block in a file. The picture format is similar to the APIC frame in ID3v2. The PICTURE block has a type, MIME type, and UTF-8 description like ID3v2, and supports external linking via URL (though this is discouraged). The differences are that there is no uniqueness constraint on the description field, and the MIME type is mandatory. The FLAC PICTURE block also includes the resolution, color depth, and palette size so that the client can search for a suitable picture without having to scan them all.
	Picture
	// Reserved Reserved Metadata Block Types
	Reserved
	// Invalid Invalid Metadata Block Type
	Invalid BlockType = 127
)

type File

type File struct {
	Meta   []*MetaDataBlock
	Frames FrameData
}

File represents a handler of FLAC file

func ParseBytes

func ParseBytes(f io.Reader) (*File, error)

ParseBytes accepts a reader to a FLAC stream and returns the final file

func ParseFile

func ParseFile(filename string) (*File, error)

ParseFile parses a FLAC file

func ParseMetadata added in v0.3.0

func ParseMetadata(f io.Reader) (*File, error)

ParseMetadata accepts a reader to a FLAC stream and consumes only FLAC metadata Frames is always nil

func (*File) GetStreamInfo added in v0.2.0

func (c *File) GetStreamInfo() (*StreamInfoBlock, error)

GetStreamInfo parses the first metadata block of the File which should always be StreamInfo and returns a StreamInfoBlock containing the decoded StreamInfo data.

func (*File) Marshal

func (c *File) Marshal() []byte

Marshal encodes all meta tags and returns the content of the resulting whole FLAC file

func (*File) Save

func (c *File) Save(fn string) error

Save encapsulates Marshal and save the file to the file system

type FrameData

type FrameData []byte

FrameData FLAC stream data

type MetaDataBlock

type MetaDataBlock struct {
	Type BlockType
	Data BlockData
}

MetaDataBlock is the struct representation of a FLAC Metadata Block

func (*MetaDataBlock) Marshal

func (c *MetaDataBlock) Marshal(isfinal bool) []byte

Marshal encodes this MetaDataBlock without touching block data isfinal defines whether this is the last metadata block of the FLAC file

type StreamInfoBlock added in v0.2.0

type StreamInfoBlock struct {
	// BlockSizeMin The minimum block size (in samples) used in the stream.
	BlockSizeMin int
	// BlockSizeMax The maximum block size (in samples) used in the stream. (Minimum blocksize == maximum blocksize) implies a fixed-blocksize stream.
	BlockSizeMax int
	// FrameSizeMin The minimum frame size (in bytes) used in the stream. May be 0 to imply the value is not known.
	FrameSizeMin int
	// FrameSizeMax The maximum frame size (in bytes) used in the stream. May be 0 to imply the value is not known.
	FrameSizeMax int
	// SampleRate Sample rate in Hz
	SampleRate int
	// ChannelCount Number of channels
	ChannelCount int
	// BitDepth  Bits per sample
	BitDepth int
	// SampleCount Total samples in stream.  'Samples' means inter-channel sample, i.e. one second of 44.1Khz audio will have 44100 samples regardless of the number of channels. A value of zero here means the number of total samples is unknown.
	SampleCount int64
	// AudioMD5 MD5 signature of the unencoded audio data
	AudioMD5 []byte
}

StreamInfoBlock represents the undecoded data of StreamInfo block

Jump to

Keyboard shortcuts

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