adpcm: saving raw pcm before conversion

This commit is contained in:
Trek 2019-02-01 01:07:24 +10:30 committed by Trek H
parent 00cb9bf3f6
commit 6beefc536b
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)
}