av/stream/flac: using writerseeker to pass to wav.NewEncoder because I don't want to give it a file, but it's not working

This commit is contained in:
saxon 2019-01-21 17:34:15 +10:30
parent 631b3a27f2
commit 05f5a967fe
2 changed files with 14 additions and 56 deletions

View File

@ -4,66 +4,16 @@ import (
"bytes" "bytes"
"errors" "errors"
"io" "io"
"io/ioutil"
"github.com/go-audio/audio" "github.com/go-audio/audio"
"github.com/go-audio/wav" "github.com/go-audio/wav"
"github.com/mewkiz/flac" "github.com/mewkiz/flac"
"github.com/orcaman/writerseeker"
) )
const wavAudioFormat = 1 const wavAudioFormat = 1
type buffer struct {
Buffer bytes.Buffer
Index int64
}
func (b *buffer) Bytes() []byte {
return b.Buffer.Bytes()
}
func (b *buffer) Read(p []byte) (int, error) {
n, err := bytes.NewBuffer(b.Buffer.Bytes()[b.Index:]).Read(p)
if err == nil {
if b.Index+int64(len(p)) < int64(b.Buffer.Len()) {
b.Index += int64(len(p))
} else {
b.Index = int64(b.Buffer.Len())
}
}
return n, err
}
func (b *buffer) Write(p []byte) (int, error) {
n, err := b.Buffer.Write(p)
if err == nil {
b.Index = int64(b.Buffer.Len())
}
return n, err
}
func (b *buffer) Seek(offset int64, whence int) (int64, error) {
var err error
var Index int64 = 0
switch whence {
case 0:
if offset >= int64(b.Buffer.Len()) || offset < 0 {
err = errors.New("Invalid Offset.")
} else {
b.Index = offset
Index = offset
}
default:
err = errors.New("Unsupported Seek Method.")
}
return Index, err
}
// Decode takes a slice of flac and decodes to wav // Decode takes a slice of flac and decodes to wav
func Decode(buf []byte) ([]byte, error) { func Decode(buf []byte) ([]byte, error) {
r := bytes.NewReader(buf) r := bytes.NewReader(buf)
@ -71,10 +21,12 @@ func Decode(buf []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, errors.New("Could not parse FLAC") return nil, errors.New("Could not parse FLAC")
} }
fb := &buffer{} ws := &writerseeker.WriterSeeker{}
enc := wav.NewEncoder(fb, int(stream.Info.SampleRate), int(stream.Info.BitsPerSample), int(stream.Info.NChannels), wavAudioFormat) enc := wav.NewEncoder(ws, int(stream.Info.SampleRate), int(stream.Info.BitsPerSample), int(stream.Info.NChannels), wavAudioFormat)
defer enc.Close() defer enc.Close()
var data []int var data []int
var out []byte
var d []byte
for { for {
// Decode FLAC audio samples. // Decode FLAC audio samples.
frame, err := stream.ParseNext() frame, err := stream.ParseNext()
@ -103,6 +55,12 @@ func Decode(buf []byte) ([]byte, error) {
if err := enc.Write(buf); err != nil { if err := enc.Write(buf); err != nil {
return nil, err return nil, err
} }
d, err = ioutil.ReadAll(ws.Reader())
if err != nil {
return nil, err
} }
return fb.Bytes(), nil out = append(out, d...)
}
return d, nil
} }

View File

@ -8,7 +8,7 @@ import (
const ( const (
testFile = "/home/saxon/Desktop/robot.flac" testFile = "/home/saxon/Desktop/robot.flac"
outFile = "out.wav" outFile = "testOut.wav"
) )
func TestDecodeFlac(t *testing.T) { func TestDecodeFlac(t *testing.T) {