mirror of https://bitbucket.org/ausocean/av.git
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
|
/*
|
||
|
NAME
|
||
|
stereo-to-mono.go
|
||
|
|
||
|
AUTHOR
|
||
|
Trek Hopton <trek@ausocean.org>
|
||
|
|
||
|
LICENSE
|
||
|
stereo-to-mono.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).
|
||
|
*/
|
||
|
|
||
|
// Package stereo-to-mono is a command-line program for converting a mono pcm file to a stereo pcm file.
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
|
||
|
"bitbucket.org/ausocean/av/codec/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 sample format can be specified as arguments.
|
||
|
func main() {
|
||
|
var inPath = *flag.String("in", "data.pcm", "file path of input data")
|
||
|
var outPath = *flag.String("out", "mono.pcm", "file path of output")
|
||
|
var SFString = *flag.String("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 sf pcm.SampleFormat
|
||
|
switch SFString {
|
||
|
case "S32_LE":
|
||
|
sf = pcm.S32_LE
|
||
|
case "S16_LE":
|
||
|
sf = pcm.S16_LE
|
||
|
default:
|
||
|
log.Fatalf("Unhandled sample format: %v", SFString)
|
||
|
}
|
||
|
|
||
|
format := pcm.BufferFormat{
|
||
|
Channels: 2,
|
||
|
SFormat: sf,
|
||
|
}
|
||
|
|
||
|
buf := pcm.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.Data, 0644)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
fmt.Println("Encoded and wrote", len(mono.Data), "bytes to file", outPath)
|
||
|
}
|