mirror of https://bitbucket.org/ausocean/av.git
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
|
||
|
"bitbucket.org/ausocean/av/audio/pcm"
|
||
|
"github.com/yobert/alsa"
|
||
|
)
|
||
|
|
||
|
// 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 sample format can be specified as arguments.
|
||
|
func main() {
|
||
|
var inPath string
|
||
|
var outPath string
|
||
|
var sf string
|
||
|
flag.StringVar(&inPath, "in", "data.pcm", "file path of input data")
|
||
|
flag.StringVar(&outPath, "out", "mono.pcm", "file path of output")
|
||
|
flag.StringVar(&sf, "sf", "S16_LE", "sample format of input audio, eg. S16_LE")
|
||
|
flag.Parse()
|
||
|
|
||
|
// Read pcm.
|
||
|
inPcm, err := ioutil.ReadFile(inPath)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
fmt.Println("Read", len(inPcm), "bytes from file", inPath)
|
||
|
|
||
|
var sampleFormat alsa.FormatType
|
||
|
switch sf {
|
||
|
case "S32_LE":
|
||
|
sampleFormat = alsa.S32_LE
|
||
|
case "S16_LE":
|
||
|
sampleFormat = alsa.S16_LE
|
||
|
default:
|
||
|
log.Fatalf("Unhandled ALSA format: %v", sf)
|
||
|
}
|
||
|
|
||
|
format := alsa.BufferFormat{
|
||
|
Channels: 2,
|
||
|
SampleFormat: sampleFormat,
|
||
|
}
|
||
|
|
||
|
buf := alsa.Buffer{
|
||
|
Format: format,
|
||
|
Data: inPcm,
|
||
|
}
|
||
|
|
||
|
// Convert audio.
|
||
|
mono, err := pcm.StereoToMono(buf)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// Save mono to file.
|
||
|
err = ioutil.WriteFile(outPath, mono, 0644)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
fmt.Println("Encoded and wrote", len(mono), "bytes to file", outPath)
|
||
|
}
|