flacgo/enc_test.go

106 lines
2.4 KiB
Go
Raw Permalink Normal View History

2022-07-16 22:07:10 +00:00
//go:build ignore
// +build ignore
flac: add Encoder API to encode audio samples and metadata blocks (#32) * flac: encode frame header * flac: calculate CRC-8 when encoding frame headers * flac: fix encoding of frame header * flac: add preliminary subframe encoder * flac: fix UTF-8 encoding of frame number * frame: add sanity check for sample count in decodeLPC Updates #31. * flac: update flac encoding API, depricate flac.Encode Encode has been removed in favour of using NewEncoder. The Encode function was temporarily added to support re-encoding FLAC streams to update the metadata, but it had no support for encoding audio samples. The added flac.Encoder has support for encoding both metadata and audio samples. It also does not require that you first decode a FLAC file to later re-encode it by calling Encode (as was the previous behaviour). * flac: add MD5 running hash of unencoded audio samples to StreamInfo * flac: remove unused encodePadding Reported by golangci * flac: fix golangci lint issues frame/utf8.go:57:6: `decodeUTF8Int` is unused (deadcode) func decodeUTF8Int(r io.Reader) (n uint64, err error) { ^ internal/utf8/encode.go:32:16: unnecessary conversion (unconvert) bits = uint64(t2 | (x>>6)&mask2) ^ internal/utf8/encode.go:37:16: unnecessary conversion (unconvert) bits = uint64(t3 | (x>>(6*2))&mask3) ^ internal/utf8/encode.go:42:16: unnecessary conversion (unconvert) bits = uint64(t4 | (x>>(6*3))&mask4) ^ * flac: fix golangci lint issues encode_frame.go:89:1: cyclomatic complexity 52 of func `(*Encoder).encodeFrameHeader` is high (> 30) (gocyclo) func (enc *Encoder) encodeFrameHeader(w io.Writer, hdr frame.Header) error { ^ internal/utf8/encode.go:66:17: unnecessary conversion (unconvert) bits := uint64(tx | (x>>uint(6*i))&maskx) ^ encode_subframe.go:105:46: unnecessary conversion (unconvert) if err := bw.WriteBits(uint64(sample), byte(hdr.BitsPerSample)); err != nil { ^ * flac: clarify that frame.Header.Num is calculated by the encoder * flac: minor re-phrasing
2018-08-18 18:18:12 +00:00
package flac_test
import (
"bytes"
"io/ioutil"
"testing"
2022-07-16 22:07:10 +00:00
"git.gammaspectra.live/S.O.N.G/flacgo"
"git.gammaspectra.live/S.O.N.G/flacgo/meta"
)
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/8297-275156-0011.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 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
}
}
}
}