2019-02-13 04:23:06 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
encode-pcm.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2019-02-25 04:25:13 +03:00
|
|
|
encode-pcm.go is a program for encoding/compressing a pcm file to an adpcm file.
|
2019-02-13 04:23:06 +03:00
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
encode-pcm.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2019-02-27 09:16:15 +03:00
|
|
|
for more details.
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-27 09:21:06 +03:00
|
|
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
|
|
|
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
2019-02-13 04:23:06 +03:00
|
|
|
*/
|
|
|
|
|
2019-02-11 04:41:26 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-02-28 06:02:57 +03:00
|
|
|
"bytes"
|
2019-02-11 04:41:26 +03:00
|
|
|
"flag"
|
2019-02-13 09:23:25 +03:00
|
|
|
"fmt"
|
2019-02-11 04:41:26 +03:00
|
|
|
"io/ioutil"
|
2019-02-13 10:00:11 +03:00
|
|
|
"log"
|
2019-02-11 04:41:26 +03:00
|
|
|
|
2019-02-11 07:55:10 +03:00
|
|
|
"bitbucket.org/ausocean/av/stream/adpcm"
|
2019-02-11 04:41:26 +03:00
|
|
|
)
|
|
|
|
|
2019-02-13 09:23:25 +03:00
|
|
|
// This program accepts an input pcm file and outputs an encoded adpcm file.
|
|
|
|
// Input and output file names can be specified as arguments.
|
2019-02-11 04:41:26 +03:00
|
|
|
func main() {
|
|
|
|
var inPath string
|
|
|
|
var adpcmPath string
|
|
|
|
flag.StringVar(&inPath, "in", "data.pcm", "file path of input data")
|
|
|
|
flag.StringVar(&adpcmPath, "out", "encoded.adpcm", "file path of output")
|
|
|
|
flag.Parse()
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Read pcm.
|
2019-02-11 04:41:26 +03:00
|
|
|
pcm, err := ioutil.ReadFile(inPath)
|
|
|
|
if err != nil {
|
2019-02-13 10:00:11 +03:00
|
|
|
log.Fatal(err)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
2019-02-13 09:23:25 +03:00
|
|
|
fmt.Println("Read", len(pcm), "bytes from file", inPath)
|
2019-02-11 04:41:26 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Encode adpcm.
|
2019-02-13 09:23:25 +03:00
|
|
|
inBSize := 1010
|
2019-02-25 07:13:26 +03:00
|
|
|
numBlocks := len(pcm) / inBSize
|
2019-03-07 08:38:00 +03:00
|
|
|
outBSize := int(float64(inBSize)/4 + 3.5) // Compression is 4:1 and 3.5 bytes of info are added to each block.
|
2019-02-28 06:02:57 +03:00
|
|
|
comp := bytes.NewBuffer(make([]byte, 0, outBSize*numBlocks))
|
|
|
|
enc := adpcm.NewEncoder(comp)
|
2019-02-13 10:00:11 +03:00
|
|
|
for i := 0; i < numBlocks; i++ {
|
|
|
|
block := pcm[inBSize*i : inBSize*(i+1)]
|
2019-02-28 06:02:57 +03:00
|
|
|
err := enc.EncodeBlock(block)
|
2019-02-13 09:23:25 +03:00
|
|
|
if err != nil {
|
2019-02-13 10:00:11 +03:00
|
|
|
log.Fatal(err)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-03-07 08:38:00 +03:00
|
|
|
// Save adpcm to file.
|
2019-02-28 06:02:57 +03:00
|
|
|
err = ioutil.WriteFile(adpcmPath, comp.Bytes(), 0644)
|
2019-02-11 04:41:26 +03:00
|
|
|
if err != nil {
|
2019-02-13 10:00:11 +03:00
|
|
|
log.Fatal(err)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
2019-02-28 06:02:57 +03:00
|
|
|
fmt.Println("Encoded and wrote", len(comp.Bytes()), "bytes to file", adpcmPath)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|