pcm: added to exp a program that resamples pcm files

This commit is contained in:
Trek H 2019-03-12 18:53:08 +10:30
parent ad1e11ea51
commit e9d4fb47fc
1 changed files with 48 additions and 0 deletions

48
exp/pcm/resample-pcm.go Normal file
View File

@ -0,0 +1,48 @@
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)
}