Skip to content

Instantly share code, notes, and snippets.

@hollychen503
Forked from dstotijn/pcm2wav.go
Created February 19, 2021 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hollychen503/a671f570e546ff431492a02309a5d2c6 to your computer and use it in GitHub Desktop.
Save hollychen503/a671f570e546ff431492a02309a5d2c6 to your computer and use it in GitHub Desktop.
pcm2wav
package main
import (
"encoding/binary"
"io"
"log"
"os"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
)
func main() {
// Read raw PCM data from input file.
in, err := os.Open("audio.pcm")
if err != nil {
log.Fatal(err)
}
// Output file.
out, err := os.Create("output.wav")
if err != nil {
log.Fatal(err)
}
defer out.Close()
// 8 kHz, 16 bit, 1 channel, WAV.
e := wav.NewEncoder(out, 8000, 16, 1, 1)
// Create new audio.IntBuffer.
audioBuf, err := newAudioIntBuffer(in)
if err != nil {
log.Fatal(err)
}
// Write buffer to output file. This writes a RIFF header and the PCM chunks from the audio.IntBuffer.
if err := e.Write(audioBuf); err != nil {
log.Fatal(err)
}
if err := e.Close(); err != nil {
log.Fatal(err)
}
}
func newAudioIntBuffer(r io.Reader) (*audio.IntBuffer, error) {
buf := audio.IntBuffer{
Format: &audio.Format{
NumChannels: 1,
SampleRate: 8000,
},
}
for {
var sample int16
err := binary.Read(r, binary.LittleEndian, &sample)
switch {
case err == io.EOF:
return &buf, nil
case err != nil:
return nil, err
}
buf.Data = append(buf.Data, int(sample))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment