From 2f24c84a1305481dcd845dcdd87410d53e8b5ebd Mon Sep 17 00:00:00 2001 From: Trek Date: Fri, 1 Feb 2019 01:07:24 +1030 Subject: [PATCH] adpcm: saving raw pcm before conversion --- cmd/adpcm/main.go | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/cmd/adpcm/main.go b/cmd/adpcm/main.go index 1c4b05bf..e3b852b0 100644 --- a/cmd/adpcm/main.go +++ b/cmd/adpcm/main.go @@ -1,6 +1,8 @@ package main import ( + "encoding/binary" + "flag" "fmt" "io/ioutil" "os" @@ -16,7 +18,9 @@ import ( func main() { //open wav - wavPath := "out.wav" + var wavPath string + flag.StringVar(&wavPath, "path", "before_compression.wav", "file path of input data") + flag.Parse() f, err := os.Open(wavPath) if err != nil { panic(err) @@ -32,12 +36,34 @@ func main() { if err != nil { panic(err) } + fmt.Println("Decoded wav. First 8 samples: ") + for i := 0; i < len(buf.Data) && i < 8; i++ { + fmt.Print(buf.Data[i], ", ") + } + fmt.Println() + // save pcm to file + var pcmBytes []byte + for _, sample := range buf.Data { + bs := make([]byte, 2) + binary.LittleEndian.PutUint16(bs, uint16(sample)) + pcmBytes = append(pcmBytes, bs[0], bs[1]) + } + pcmPath := "out.pcm" + err = ioutil.WriteFile(pcmPath, pcmBytes, 0644) + if err != nil { + panic(err) + } + fmt.Println("Saved raw PCM to " + pcmPath + ". First 8 bytes: ") + for i := 0; i < len(pcmBytes) && i < 8; i++ { + fmt.Print(pcmBytes[i], ", ") + } + fmt.Println() //encode adpcm var comp []byte adpcm.Encode(buf.Data, &comp) - // save to file - pcmPath := "out.adpcm" - err = ioutil.WriteFile(pcmPath, comp, 0644) + // save adpcm to file + adpcmPath := "out.adpcm" + err = ioutil.WriteFile(adpcmPath, comp, 0644) if err != nil { panic(err) }