38 lines
910 B
Go
38 lines
910 B
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
lame "github.com/sunicy/go-lame"
|
|
)
|
|
|
|
func PcmToMp3(audioBuffer *bytes.Buffer, mp3FileName string) {
|
|
mp3File, _ := os.OpenFile(mp3FileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
|
|
defer mp3File.Close()
|
|
wr, err := lame.NewWriter(mp3File)
|
|
if err != nil {
|
|
panic("cannot create lame writer, err: " + err.Error())
|
|
|
|
}
|
|
wr.InSampleRate = 48000 // input sample rate
|
|
wr.InNumChannels = 1 // number of channels: 1
|
|
wr.OutMode = lame.MODE_STEREO // common, 2 channels
|
|
wr.OutQuality = 0 // 0: highest; 9: lowest
|
|
wr.OutSampleRate = 44100 // output sample rate
|
|
io.Copy(wr, audioBuffer)
|
|
wr.Close()
|
|
}
|
|
|
|
func bufWriter(stuff []byte) {
|
|
buf := new(bytes.Buffer)
|
|
err := binary.Write(buf, binary.LittleEndian, stuff)
|
|
if err != nil {
|
|
fmt.Println("binary.Write failed:", err)
|
|
}
|
|
fmt.Printf("% x", buf.Bytes())
|
|
}
|