-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathwriter.go
172 lines (145 loc) · 3.93 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
package wave
import (
"encoding/binary"
"io"
"math"
"os"
)
// Consts that appear in the .WAVE file format
var (
ChunkID = []byte{0x52, 0x49, 0x46, 0x46} // RIFF
BigEndianChunkID = []byte{0x52, 0x49, 0x46, 0x58} // RIFX
WaveID = []byte{0x57, 0x41, 0x56, 0x45} // WAVE
Format = []byte{0x66, 0x6d, 0x74, 0x20} // FMT
Subchunk2ID = []byte{0x64, 0x61, 0x74, 0x61} // DATA
)
type intsToBytesFunc func(i int) []byte
var (
// intsToBytesFm to map X-bit int to byte functions
intsToBytesFm = map[int]intsToBytesFunc{
16: int16ToBytes,
32: int32ToBytes,
}
)
// WriteFrames writes the slice to disk as a .wav file
// the WaveFmt metadata needs to be correct
// WaveData and WaveHeader are inferred from the samples however..
func WriteFrames(samples []Frame, wfmt WaveFmt, file string) error {
return WriteWaveFile(samples, wfmt, file)
}
func WriteWaveFile(samples []Frame, wfmt WaveFmt, file string) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
return WriteWaveToWriter(samples, wfmt, f)
}
func WriteWaveToWriter(samples []Frame, wfmt WaveFmt, writer io.Writer) error {
wfb := fmtToBytes(wfmt)
data, databits := framesToData(samples, wfmt)
hdr := createHeader(data)
_, err := writer.Write(hdr)
if err != nil {
return err
}
_, err = writer.Write(wfb)
if err != nil {
return err
}
_, err = writer.Write(databits)
if err != nil {
return err
}
return nil
}
func int16ToBytes(i int) []byte {
b := make([]byte, 2)
in := uint16(i)
binary.LittleEndian.PutUint16(b, in)
return b
}
func int32ToBytes(i int) []byte {
b := make([]byte, 4)
in := uint32(i)
binary.LittleEndian.PutUint32(b, in)
return b
}
func framesToData(frames []Frame, wfmt WaveFmt) (WaveData, []byte) {
b := []byte{}
raw := samplesToRawData(frames, wfmt)
// We receive frames but have to store the size of the samples
// The size of the samples is frames / channels..
subchunksize := (len(frames) * wfmt.NumChannels * wfmt.BitsPerSample) / 8
subBytes := int32ToBytes(subchunksize)
// construct the data part..
b = append(b, Subchunk2ID...)
b = append(b, subBytes...)
b = append(b, raw...)
wd := WaveData{
Subchunk2ID: Subchunk2ID,
Subchunk2Size: subchunksize,
RawData: raw,
Frames: frames,
}
return wd, b
}
func floatToBytes(f float64, nBytes int) []byte {
bits := math.Float64bits(f)
bs := make([]byte, 8)
binary.LittleEndian.PutUint64(bs, bits)
// trim padding
switch nBytes {
case 2:
return bs[:2]
case 4:
return bs[:4]
}
return bs
}
// Turn the samples into raw data...
func samplesToRawData(samples []Frame, props WaveFmt) []byte {
raw := []byte{}
for _, s := range samples {
// the samples are scaled - rescale them?
rescaled := rescaleFrame(s, props.BitsPerSample)
bits := intsToBytesFm[props.BitsPerSample](rescaled)
raw = append(raw, bits...)
}
return raw
}
// rescale frames back to the original values..
func rescaleFrame(s Frame, bits int) int {
rescaled := float64(s) * float64(maxValues[bits])
return int(rescaled)
}
func fmtToBytes(wfmt WaveFmt) []byte {
b := []byte{}
subchunksize := int32ToBytes(wfmt.Subchunk1Size)
audioformat := int16ToBytes(wfmt.AudioFormat)
numchans := int16ToBytes(wfmt.NumChannels)
sr := int32ToBytes(wfmt.SampleRate)
br := int32ToBytes(wfmt.ByteRate)
blockalign := int16ToBytes(wfmt.BlockAlign)
bitsPerSample := int16ToBytes(wfmt.BitsPerSample)
b = append(b, wfmt.Subchunk1ID...)
b = append(b, subchunksize...)
b = append(b, audioformat...)
b = append(b, numchans...)
b = append(b, sr...)
b = append(b, br...)
b = append(b, blockalign...)
b = append(b, bitsPerSample...)
return b
}
// turn the sample to a valid header
func createHeader(wd WaveData) []byte {
// write chunkID
bits := []byte{}
chunksize := 36 + wd.Subchunk2Size
cb := int32ToBytes(chunksize)
bits = append(bits, ChunkID...) // in theory switch on endianness..
bits = append(bits, cb...)
bits = append(bits, WaveID...)
return bits
}