-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
64 lines (53 loc) · 1.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// © 2014 the flac Authors under the MIT license. See AUTHORS for the list of authors.
// +build ignore
package main
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"os"
"github.com/eaburns/flac"
)
func main() {
data, meta, err := flac.Decode(bufio.NewReader(os.Stdin))
if err != nil {
fmt.Println(err.Error())
return
}
writeWAV(data, meta)
}
type wavFmt struct {
format int16
channels int16
sampleRate int32
dataRate int32
dataBlockSize int16
bitsPerSample int16
}
const pcmFormat = 1
func writeWAV(data []byte, meta flac.MetaData) {
wdata := bytes.NewBuffer(nil)
wdata.WriteString("WAVE")
wdata.WriteString("fmt ")
binary.Write(wdata, binary.LittleEndian, uint32(16))
binary.Write(wdata, binary.LittleEndian, wavFmt{
format: pcmFormat,
channels: int16(meta.NChannels),
sampleRate: int32(meta.SampleRate),
dataRate: int32(meta.NChannels * meta.SampleRate * (meta.BitsPerSample / 8)),
dataBlockSize: int16(meta.NChannels * (meta.BitsPerSample / 8)),
bitsPerSample: int16(meta.BitsPerSample),
})
wdata.WriteString("data")
binary.Write(wdata, binary.LittleEndian, uint32(len(data)))
wdata.Write(data)
wav, err := os.Create("out.wav")
if err != nil {
panic(err)
}
defer wav.Close()
wav.WriteString("RIFF")
binary.Write(wav, binary.LittleEndian, uint32(len(wdata.Bytes())))
wav.Write(wdata.Bytes())
}