2019-02-13 04:23:06 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
decode-pcm.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
decode-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
|
|
|
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
|
|
|
|
2019-02-11 04:41:26 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// This program accepts an input file encoded in adpcm and outputs a decoded pcm file.
|
|
|
|
// Input and output file names can be specified as arguments.
|
|
|
|
func main() {
|
|
|
|
var inPath string
|
|
|
|
var outPath string
|
|
|
|
flag.StringVar(&inPath, "in", "encoded.adpcm", "file path of input")
|
|
|
|
flag.StringVar(&outPath, "out", "decoded.pcm", "file path of output data")
|
|
|
|
flag.Parse()
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-13 09:23:25 +03:00
|
|
|
// read adpcm
|
2019-02-11 04:41:26 +03:00
|
|
|
comp, 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(comp), "bytes from file", inPath)
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-13 09:23:25 +03:00
|
|
|
// decode adpcm
|
|
|
|
inBSize := 256
|
|
|
|
numBlocks := int(len(comp) / inBSize)
|
|
|
|
outBSize := 2 + (inBSize-4)*4 // 2 bytes are copied, 2 are used as block header info, the remaining bytes are decompressed 1:4
|
|
|
|
decoded := make([]byte, 0, outBSize*numBlocks)
|
2019-02-13 10:00:11 +03:00
|
|
|
for i := 0; i < numBlocks; i++ {
|
|
|
|
block := comp[inBSize*i : inBSize*(i+1)]
|
2019-02-13 09:23:25 +03:00
|
|
|
decBlock, err := adpcm.DecodeBlock(block)
|
|
|
|
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
|
|
|
decoded = append(decoded, decBlock...)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|
2019-02-13 04:23:06 +03:00
|
|
|
|
2019-02-11 04:41:26 +03:00
|
|
|
// save pcm to file
|
|
|
|
err = ioutil.WriteFile(outPath, decoded, 0644)
|
|
|
|
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("Decoded and wrote", len(decoded), "bytes to file", outPath)
|
2019-02-11 04:41:26 +03:00
|
|
|
}
|