-
Notifications
You must be signed in to change notification settings - Fork 27
/
writer.go
72 lines (59 loc) · 1.58 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
package wav
import (
"encoding/binary"
"io"
"math"
"github.com/youpy/go-riff"
)
type Writer struct {
io.Writer
Format *WavFormat
}
func NewWriter(w io.Writer, numSamples uint32, numChannels uint16, sampleRate uint32, bitsPerSample uint16) (writer *Writer) {
blockAlign := numChannels * bitsPerSample / 8
byteRate := sampleRate * uint32(blockAlign)
format := &WavFormat{AudioFormatPCM, numChannels, sampleRate, byteRate, blockAlign, bitsPerSample}
dataSize := numSamples * uint32(format.BlockAlign)
riffSize := 4 + 8 + 16 + 8 + dataSize
riffWriter := riff.NewWriter(w, []byte("WAVE"), riffSize)
writer = &Writer{riffWriter, format}
riffWriter.WriteChunk([]byte("fmt "), 16, func(w io.Writer) {
binary.Write(w, binary.LittleEndian, format)
})
riffWriter.WriteChunk([]byte("data"), dataSize, func(w io.Writer) {})
return writer
}
func (w *Writer) WriteSamples(samples []Sample) (err error) {
bitsPerSample := w.Format.BitsPerSample
numChannels := w.Format.NumChannels
var i, b uint16
var by []byte
for _, sample := range samples {
for i = 0; i < numChannels; i++ {
value := toUint(sample.Values[i], int(bitsPerSample))
for b = 0; b < bitsPerSample; b += 8 {
by = append(by, uint8((value>>b)&math.MaxUint8))
}
}
}
_, err = w.Write(by)
return
}
func toUint(value int, bits int) uint {
var result uint
switch bits {
case 32:
result = uint(uint32(value))
case 16:
result = uint(uint16(value))
case 8:
result = uint(value)
default:
if value < 0 {
result = uint((1 << uint(bits)) + value)
} else {
result = uint(value)
}
}
return result
}