av/exp/adpcm/encode-pcm/encode-pcm.go

69 lines
1.8 KiB
Go
Raw Permalink Normal View History

/*
NAME
encode-pcm.go
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.
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).
*/
2020-03-12 08:30:40 +03:00
// Package encode-pcm is a command-line program for encoding/compressing a pcm file to an adpcm file.
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
2019-03-28 04:05:32 +03:00
"bitbucket.org/ausocean/av/codec/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()
2019-03-07 08:38:00 +03:00
// Read pcm.
pcm, err := ioutil.ReadFile(inPath)
if err != nil {
log.Fatal(err)
}
fmt.Println("Read", len(pcm), "bytes from file", inPath)
2019-03-07 08:38:00 +03:00
// Encode adpcm.
comp := bytes.NewBuffer(make([]byte, 0, adpcm.EncBytes(len(pcm))))
enc := adpcm.NewEncoder(comp)
_, err = enc.Write(pcm)
if err != nil {
log.Fatal(err)
}
2019-03-07 08:38:00 +03:00
// Save adpcm to file.
err = ioutil.WriteFile(adpcmPath, comp.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println("Encoded and wrote", len(comp.Bytes()), "bytes to file", adpcmPath)
}