-
Notifications
You must be signed in to change notification settings - Fork 5
/
writer.go
176 lines (142 loc) · 3.41 KB
/
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package wav
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
type WriterParam struct {
SampleRate uint32
BitsPerSample uint16
NumChannels uint16
AudioFormat AudioFormat
}
type Writer struct {
r io.WriteSeeker
p WriterParam
dataSize int
}
func NewWriter(r io.WriteSeeker, p WriterParam) (w *Writer, err error) {
if p.SampleRate == 0 || p.BitsPerSample == 0 || p.NumChannels == 0 {
return nil, ErrInvalidFmt
}
w = &Writer{r: r, p: p}
w.writeHeader()
w.dataSize = 0
return w, nil
}
func (w *Writer) Close() {
buf := new(bytes.Buffer)
u, err := w.r.Seek(4, 0)
if err != nil {
fmt.Printf("error: %v\n", u)
}
err = binary.Write(buf, binary.LittleEndian, int32(w.dataSize+32))
if err != nil {
}
w.r.Write(buf.Bytes())
u, err = w.r.Seek(40, 0)
if err != nil {
fmt.Printf("error: %v\n", u)
}
buf.Reset()
err = binary.Write(buf, binary.LittleEndian, int32(w.dataSize))
if err != nil {
}
w.r.Write(buf.Bytes())
}
func (w *Writer) writeRiffChunk(buf *bytes.Buffer) error {
header := fmt.Sprintf("RIFF")
err := binary.Write(buf, binary.LittleEndian, []byte(header))
if err != nil {
return ErrFailedWrite
}
// Chunk Size
err = binary.Write(buf, binary.LittleEndian, float32(0))
if err != nil {
return ErrFailedWrite
}
header = fmt.Sprintf("WAVE")
err = binary.Write(buf, binary.LittleEndian, []byte(header))
if err != nil {
return ErrFailedWrite
}
return nil
}
func (w *Writer) writeFmtSubChunk(buf *bytes.Buffer) error {
header := fmt.Sprintf("fmt ")
err := binary.Write(buf, binary.LittleEndian, []byte(header))
if err != nil {
return ErrFailedWrite
}
// SubChunk1Size
err = binary.Write(buf, binary.LittleEndian, int32(16))
if err != nil {
return ErrFailedWrite
}
// AudioFormat
err = binary.Write(buf, binary.LittleEndian, int16(1))
if err != nil {
return ErrFailedWrite
}
// Num Channels
err = binary.Write(buf, binary.LittleEndian, int16(w.p.NumChannels))
if err != nil {
return ErrFailedWrite
}
// Sample Rate
err = binary.Write(buf, binary.LittleEndian, int32(w.p.SampleRate))
if err != nil {
return ErrFailedWrite
}
// Byte Rate
byteRate := w.p.SampleRate * uint32(w.p.NumChannels) * uint32(w.p.BitsPerSample) / 8
err = binary.Write(buf, binary.LittleEndian, int32(byteRate))
if err != nil {
return ErrFailedWrite
}
// Block Align
blockAlign := uint32(w.p.NumChannels) * uint32(w.p.BitsPerSample) / 8
err = binary.Write(buf, binary.LittleEndian, int16(blockAlign))
if err != nil {
return ErrFailedWrite
}
// Bits Per Sample
err = binary.Write(buf, binary.LittleEndian, int16(w.p.BitsPerSample))
if err != nil {
return ErrFailedWrite
}
// data
header = fmt.Sprintf("data")
err = binary.Write(buf, binary.LittleEndian, []byte(header))
if err != nil {
return ErrFailedWrite
}
// SubChunk2Size
err = binary.Write(buf, binary.LittleEndian, int32(256*2))
if err != nil {
return ErrFailedWrite
}
return nil
}
func (w *Writer) writeHeader() {
buf := new(bytes.Buffer)
err := w.writeRiffChunk(buf)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
err = w.writeFmtSubChunk(buf)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
w.r.Write(buf.Bytes())
}
func (w *Writer) WriteSamples(samples []int16) {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, (samples))
if err != nil {
fmt.Println("binary.Write failed:", err)
}
w.dataSize += len(buf.Bytes())
w.r.Write(buf.Bytes())
}