av/exp/pcm/resample-pcm.go

49 lines
1.3 KiB
Go

package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"bitbucket.org/ausocean/av/audio/pcm"
)
// This program accepts an input pcm file and outputs a resampled pcm file.
// Input and output file names, to and from sample rates, channels and bit-depth can be specified as arguments.
func main() {
var inPath string
var outPath string
var from int
var to int
var channels int
var bitDepth int
flag.StringVar(&inPath, "in", "data.pcm", "file path of input data")
flag.StringVar(&outPath, "out", "resampled.pcm", "file path of output")
flag.IntVar(&from, "from", 48000, "sample rate of input file")
flag.IntVar(&to, "to", 8000, "sample rate of output file")
flag.IntVar(&channels, "ch", 1, "number of channels in input file")
flag.IntVar(&bitDepth, "bd", 16, "bit depth of input file")
flag.Parse()
// Read pcm.
inPcm, err := ioutil.ReadFile(inPath)
if err != nil {
log.Fatal(err)
}
fmt.Println("Read", len(inPcm), "bytes from file", inPath)
// Resample pcm.
resampled, err := pcm.Resample(inPcm, from, to, channels, bitDepth)
if err != nil {
log.Fatal(err)
}
// Save resampled to file.
err = ioutil.WriteFile(outPath, resampled, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println("Encoded and wrote", len(resampled), "bytes to file", outPath)
}