func TestParseBlocks(t *testing.T) { for i, g := range golden { stream, err := flac.ParseFile(g.name) if err != nil { t.Fatal(err) } defer stream.Close() blocks := stream.Blocks if len(blocks) != len(g.blocks) { t.Errorf("i=%d: invalid number of metadata blocks; expected %d, got %d", i, len(g.blocks), len(blocks)) continue } got := stream.Info want := g.info if !reflect.DeepEqual(got, want) { t.Errorf("i=%d: metadata StreamInfo block bodies differ; expected %#v, got %#v", i, want, got) } for j, got := range blocks { want := g.blocks[j] if !reflect.DeepEqual(got.Header, want.Header) { t.Errorf("i=%d, j=%d: metadata block headers differ; expected %#v, got %#v", i, j, want.Header, got.Header) } if !reflect.DeepEqual(got.Body, want.Body) { t.Errorf("i=%d, j=%d: metadata block bodies differ; expected %#v, got %#v", i, j, want.Body, got.Body) } } } }
func list(path string) error { var blockNums []int if flagBlockNum != "" { // Parse "--block-number" command line flag. rawBlockNums := strings.Split(flagBlockNum, ",") for _, rawBlockNum := range rawBlockNums { blockNum, err := strconv.Atoi(rawBlockNum) if err != nil { return err } blockNums = append(blockNums, blockNum) } } // Open FLAC stream. stream, err := flac.ParseFile(path) if err != nil { return err } if blockNums != nil { // Only list blocks specified in the "--block-number" command line flag. for _, blockNum := range blockNums { if blockNum == 0 { listStreamInfo(stream.Info) } else { // strea.Blocks doesn't contain StreamInfo, therefore the blockNum // is one less. blockNum-- } if blockNum < len(stream.Blocks) { listBlock(stream.Blocks[blockNum], blockNum) } } } else { // List all blocks. var isLast bool if len(stream.Blocks) == 0 { isLast = true } listStreamInfoHeader(isLast) listStreamInfo(stream.Info) for blockNum, block := range stream.Blocks { // strea.Blocks doesn't contain StreamInfo, therefore the blockNum // is one less. blockNum-- listBlock(block, blockNum) } } return nil }
func TestEncode(t *testing.T) { paths := []string{ "meta/testdata/input-SCPAP.flac", "meta/testdata/input-SCVA.flac", "meta/testdata/input-SCVPAP.flac", "meta/testdata/input-VA.flac", "meta/testdata/silence.flac", "testdata/19875.flac", "testdata/44127.flac", "testdata/59996.flac", "testdata/80574.flac", "testdata/172960.flac", "testdata/189983.flac", "testdata/191885.flac", "testdata/212768.flac", "testdata/220014.flac", "testdata/243749.flac", "testdata/256529.flac", "testdata/257344.flac", "testdata/love.flac", } for _, path := range paths { // Decode source file. stream, err := flac.ParseFile(path) if err != nil { t.Errorf("%q: unable to parse FLAC file; %v", path, err) continue } defer stream.Close() // Encode FLAC stream. out := new(bytes.Buffer) if err := flac.Encode(out, stream); err != nil { t.Errorf("%q: unable to encode FLAC stream; %v", path, err) continue } // Compare source and destination FLAC streams. want, err := ioutil.ReadFile(path) if err != nil { t.Errorf("%q: unable to read file; %v", path, err) continue } got := out.Bytes() if !bytes.Equal(got, want) { t.Errorf("%q: content mismatch; expected % X, got % X", path, want, got) continue } } }
func ExampleParseFile() { // 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 TestEncodeComment(t *testing.T) { // Decode FLAC file. src, err := flac.ParseFile("testdata/love.flac") if err != nil { t.Fatalf("unable to parse input FLAC file; %v", err) } defer src.Close() // Add custom vorbis comment. const want = "FLAC encoding test case" for _, block := range src.Blocks { if comment, ok := block.Body.(*meta.VorbisComment); ok { comment.Vendor = want } } // Encode FLAC file. out := new(bytes.Buffer) if err := flac.Encode(out, src); err != nil { t.Fatalf("unable to encode FLAC file; %v", err) } stream, err := flac.Parse(out) if err != nil { t.Fatalf("unable to parse output FLAC file; %v", err) } defer stream.Close() // Add custom vorbis comment. for _, block := range stream.Blocks { if comment, ok := block.Body.(*meta.VorbisComment); ok { got := comment.Vendor if got != want { t.Errorf("Vorbis comment mismatch; expected %q, got %q", want, got) continue } } } }
func TestParsePicture(t *testing.T) { stream, err := flac.ParseFile("testdata/silence.flac") if err != nil { t.Fatal(err) } defer stream.Close() want, err := ioutil.ReadFile("testdata/silence.jpg") if err != nil { t.Fatal(err) } for _, block := range stream.Blocks { if block.Type == meta.TypePicture { pic := block.Body.(*meta.Picture) got := pic.Data if !bytes.Equal(got, want) { t.Errorf("picture data differ; expected %v, got %v", want, got) } break } } }
// TODO: better error verification than string-based comparisons. func TestMissingValue(t *testing.T) { _, err := flac.ParseFile("testdata/missing-value.flac") if err.Error() != `meta.Block.parseVorbisComment: unable to locate '=' in vector "title 2"` { t.Fatal(err) } }