2019-03-13 06:29:21 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
resample.go
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
resample.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).
|
|
|
|
*/
|
2019-05-08 13:34:40 +03:00
|
|
|
|
2020-03-12 08:30:40 +03:00
|
|
|
// Package resample is a command-line program for resampling a pcm file.
|
2019-03-12 11:23:08 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
|
2019-04-09 09:18:54 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/pcm"
|
2019-03-12 11:23:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// This program accepts an input pcm file and outputs a resampled pcm file.
|
2019-03-13 05:49:53 +03:00
|
|
|
// Input and output file names, to and from sample rates, channels and sample format can be specified as arguments.
|
2019-03-12 11:23:08 +03:00
|
|
|
func main() {
|
2019-03-26 08:13:11 +03:00
|
|
|
var inPath = *flag.String("in", "data.pcm", "file path of input data")
|
|
|
|
var outPath = *flag.String("out", "resampled.pcm", "file path of output")
|
|
|
|
var from = *flag.Int("from", 48000, "sample rate of input file")
|
|
|
|
var to = *flag.Int("to", 8000, "sample rate of output file")
|
|
|
|
var channels = *flag.Int("ch", 1, "number of channels in input file")
|
2019-11-12 13:54:08 +03:00
|
|
|
var SFString = *flag.String("sf", "S16_LE", "sample format of input audio, eg. S16_LE")
|
2019-03-12 11:23:08 +03:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Read pcm.
|
|
|
|
inPcm, err := ioutil.ReadFile(inPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Read", len(inPcm), "bytes from file", inPath)
|
|
|
|
|
2019-11-12 13:54:08 +03:00
|
|
|
var sf pcm.SampleFormat
|
|
|
|
switch SFString {
|
2019-03-13 05:49:53 +03:00
|
|
|
case "S32_LE":
|
2019-11-12 13:54:08 +03:00
|
|
|
sf = pcm.S32_LE
|
2019-03-13 05:49:53 +03:00
|
|
|
case "S16_LE":
|
2019-11-12 13:54:08 +03:00
|
|
|
sf = pcm.S16_LE
|
2019-03-13 05:49:53 +03:00
|
|
|
default:
|
2019-11-12 13:54:08 +03:00
|
|
|
log.Fatalf("Unhandled ALSA format: %v", SFString)
|
2019-03-13 05:49:53 +03:00
|
|
|
}
|
|
|
|
|
2019-11-13 09:24:41 +03:00
|
|
|
format := pcm.BufferFormat{
|
2019-11-12 13:54:08 +03:00
|
|
|
Channels: channels,
|
|
|
|
Rate: from,
|
|
|
|
SFormat: sf,
|
2019-03-13 05:49:53 +03:00
|
|
|
}
|
|
|
|
|
2019-11-13 09:24:41 +03:00
|
|
|
buf := pcm.Buffer{
|
2019-03-13 05:49:53 +03:00
|
|
|
Format: format,
|
|
|
|
Data: inPcm,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resample audio.
|
2019-11-13 09:24:41 +03:00
|
|
|
resampled, err := pcm.Resample(buf, to)
|
2019-03-12 11:23:08 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save resampled to file.
|
2019-06-13 18:21:26 +03:00
|
|
|
err = ioutil.WriteFile(outPath, resampled.Data, 0644)
|
2019-03-12 11:23:08 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-06-13 18:21:26 +03:00
|
|
|
fmt.Println("Encoded and wrote", len(resampled.Data), "bytes to file", outPath)
|
2019-03-12 11:23:08 +03:00
|
|
|
}
|