-
Notifications
You must be signed in to change notification settings - Fork 2
/
wav_writer.go
142 lines (112 loc) · 2.97 KB
/
wav_writer.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package agraph
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
/*
Implementation specific to WAVE
*/
type WaveWriter struct {
Out io.WriteCloser
SamplesWrittenCount int
Riff *Riff
Fmt *Fmt
Data *DataWriterChunk
}
type Options struct {
NumChannels int
SampleRate int
BitsPerSample int
}
type Option func(*Options)
func NumChannels(channels int) Option {
return func(args *Options) {
args.NumChannels = channels
}
}
func SampleRate(sampleRate int) Option {
return func(args *Options) {
args.SampleRate = sampleRate
}
}
func BitsPerSample(bitsPerSample int) Option {
return func(args *Options) {
args.BitsPerSample = bitsPerSample
}
}
func NewWaveWriter(out io.WriteCloser, opts ...Option) (*WaveWriter, error) {
args := &Options{
NumChannels: 1,
SampleRate: 44100,
BitsPerSample: 8,
}
for _, opt := range opts {
opt(args)
}
writer := &WaveWriter{}
writer.Out = out
blockAlign := uint16(args.BitsPerSample*args.NumChannels) / 8
byteRate := uint32(int(blockAlign) * args.SampleRate)
writer.Riff = &Riff{
ChunkID: []byte(riffChunkToken),
Format: []byte(wavFormatToken),
}
writer.Fmt = &Fmt{
ID: []byte(fmtChunkToken),
Size: uint32(fmtChunkSize),
}
writer.Fmt.Data = &WavFmtData{
AudioFormat: uint16(1), // pulse code modulation (PCM)
NumChannels: uint16(args.NumChannels),
SampleRate: uint32(args.SampleRate),
ByteRate: byteRate,
BlockAlign: blockAlign,
BitsPerSample: uint16(args.BitsPerSample),
}
writer.Data = &DataWriterChunk{
ID: []byte(dataChunkToken),
Data: bytes.NewBuffer([]byte{}),
}
return writer, nil
}
func (w *WaveWriter) Write(b []byte) (int, error) {
blockAlign := int(w.Fmt.Data.BlockAlign)
if len(b) < blockAlign {
return 0, fmt.Errorf("Size of b %v < BlockAlign %v", len(b), blockAlign)
}
if len(b)%blockAlign != 0 {
return 0, fmt.Errorf("Sizez of b %v must be a multiple of BlockAlign %v", len(b), blockAlign)
}
numBytesWritten := len(b) / blockAlign
n, err := w.Data.Data.Write(b)
if err != nil {
w.SamplesWrittenCount += numBytesWritten
}
return n, err
}
func (w *WaveWriter) Close() error {
data := w.Data.Data.Bytes()
length := uint32(len(data))
// possibly shouldn't have the (8 + length) on the end
w.Riff.ChunkSize = uint32(len(w.Riff.ChunkID)) + (8 + w.Fmt.Size) + (8 + length)
w.Data.Size = length
// Write the riff chunk
err := binary.Write(w.Out, binary.BigEndian, w.Riff.ChunkID)
err = binary.Write(w.Out, binary.LittleEndian, w.Riff.ChunkSize)
err = binary.Write(w.Out, binary.BigEndian, w.Riff.Format)
// Write the fmt chunk
err = binary.Write(w.Out, binary.BigEndian, w.Fmt.ID)
err = binary.Write(w.Out, binary.LittleEndian, w.Fmt.Size)
err = binary.Write(w.Out, binary.LittleEndian, w.Fmt.Data)
// Write the data chunk
err = binary.Write(w.Out, binary.BigEndian, w.Data.ID)
err = binary.Write(w.Out, binary.LittleEndian, w.Data.Size)
_, err = w.Out.Write(data)
err = w.Out.Close()
if err != nil {
return err
}
return nil
}