adpcm: saving raw pcm before conversion

This commit is contained in:
Trek 2019-02-01 01:07:24 +10:30
parent b1d8c3888c
commit 2f24c84a13
1 changed files with 30 additions and 4 deletions

View File

@ -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)
}