From b1d8c3888c2087d04b0cd78b5872c151fd124389 Mon Sep 17 00:00:00 2001 From: Trek H Date: Wed, 23 Jan 2019 16:33:28 +1030 Subject: [PATCH] adpcm test working with wav files --- cmd/adpcm/main.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 cmd/adpcm/main.go diff --git a/cmd/adpcm/main.go b/cmd/adpcm/main.go new file mode 100644 index 00000000..1c4b05bf --- /dev/null +++ b/cmd/adpcm/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + + "github.com/bovarysme/adpcm" + "github.com/go-audio/audio" + "github.com/go-audio/wav" + "github.com/pkg/errors" +) + +// This program is for compare data size of pcm and adpcm, it takes a wav file and compresses and encodes it to adpcm, +// the adpcm byte array is stored as a file and then the adpcm is decoded and the result saved as final.wav + +func main() { + //open wav + wavPath := "out.wav" + f, err := os.Open(wavPath) + if err != nil { + panic(err) + } + defer f.Close() + //decode wav + dec := wav.NewDecoder(f) + if !dec.IsValidFile() { + panic(errors.Errorf("invalid WAV file %q", wavPath)) + } + sampleRate, nchannels, bps := int(dec.SampleRate), int(dec.NumChans), int(dec.BitDepth) + buf, err := dec.FullPCMBuffer() + if err != nil { + panic(err) + } + //encode adpcm + var comp []byte + adpcm.Encode(buf.Data, &comp) + // save to file + pcmPath := "out.adpcm" + err = ioutil.WriteFile(pcmPath, comp, 0644) + if err != nil { + panic(err) + } + //decode adpcm + var decoded []int + deco := adpcm.NewDecoder(nchannels) + deco.Decode(comp, &decoded) + + //encode wav and save to file + finalWavPath := "final.wav" + of, err := os.Create(finalWavPath) + if err != nil { + panic(err) + } + defer of.Close() + + sampleBytes := bps / 8 + + // normal uncompressed WAV format + wavformat := 1 + + enc := wav.NewEncoder(of, sampleRate, bps, nchannels, wavformat) + + format := &audio.Format{ + NumChannels: nchannels, + SampleRate: sampleRate, + } + + intBuf := &audio.IntBuffer{Data: decoded, Format: format, SourceBitDepth: sampleBytes * 8} + + if err := enc.Write(intBuf); err != nil { + panic(err) + } + + if err := enc.Close(); err != nil { + panic(err) + } + fmt.Printf("Saved audio to %s\n", finalWavPath) + +}