mirror of https://bitbucket.org/ausocean/av.git
76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
/*
|
|
NAME
|
|
encode-pcm.go
|
|
|
|
DESCRIPTION
|
|
encode-pcm.go is a program for encoding/compressing a pcm file to an adpcm file.
|
|
|
|
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
|
|
for more details.
|
|
|
|
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).
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
|
|
"bitbucket.org/ausocean/av/stream/adpcm"
|
|
)
|
|
|
|
// This program accepts an input pcm file and outputs an encoded adpcm file.
|
|
// Input and output file names can be specified as arguments.
|
|
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()
|
|
|
|
//read pcm
|
|
pcm, err := ioutil.ReadFile(inPath)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println("Read", len(pcm), "bytes from file", inPath)
|
|
|
|
//encode adpcm
|
|
inBSize := 1010
|
|
numBlocks := len(pcm) / inBSize
|
|
outBSize := int(float64(inBSize)/4 + 3.5) // compression is 4:1 and 3.5 bytes of info are added to each block
|
|
comp := make([]byte, 0, outBSize*numBlocks)
|
|
for i := 0; i < numBlocks; i++ {
|
|
block := pcm[inBSize*i : inBSize*(i+1)]
|
|
encBlock, err := adpcm.EncodeBlock(block)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
comp = append(comp, encBlock...)
|
|
}
|
|
|
|
// save adpcm to file
|
|
err = ioutil.WriteFile(adpcmPath, comp, 0644)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println("Encoded and wrote", len(comp), "bytes to file", adpcmPath)
|
|
}
|