-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
76 lines (69 loc) · 1.72 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
package wave
import (
"io"
"github.com/bake/wave/riff"
"github.com/pkg/errors"
)
// Writer writes samples to an io.WriteSeeker.
type Writer struct {
rw *riff.Writer
cw *riff.Writer
fmt Format
}
// NewWriter creates a new WAVE Writer.
func NewWriter(ws io.WriteSeeker, format Format) (*Writer, error) {
rw, err := riff.NewWriter(ws, "WAVE")
if err != nil {
return nil, errors.Wrap(err, "could not create new riff reader")
}
cw, err := rw.Chunk("fmt ")
if err != nil {
return nil, errors.Wrap(err, "could not create format chunk")
}
if err := format.encode(cw); err != nil {
return nil, errors.Wrap(err, "could not encode format chunk")
}
if err := cw.Close(); err != nil {
return nil, errors.Wrap(err, "could not close format chunk")
}
cw, err = rw.Chunk("data")
if err != nil {
return nil, errors.Wrap(err, "could not create data chunk")
}
return &Writer{rw, cw, format}, nil
}
// Sample writes a sample.
func (wavw *Writer) Sample(s int) error {
var p []byte
switch wavw.fmt.BitsPerSample {
case 8:
p = []byte{byte(s)}
case 16:
p = []byte{byte(s), byte(s >> 8)}
case 24:
p = []byte{byte(s), byte(s >> 8), byte(s >> 16)}
case 32:
p = []byte{byte(s), byte(s >> 8), byte(s >> 16), byte(s >> 24)}
}
if _, err := wavw.cw.Write(p); err != nil {
return errors.Wrap(err, "could not write sample")
}
return nil
}
// Samples writes a slice of samples.
func (wavw *Writer) Samples(samples []int) error {
for _, s := range samples {
if err := wavw.Sample(s); err != nil {
return err
}
}
return nil
}
// Close the underlying RIFF writers. The file writer needs to be closed
// separately.
func (wavw *Writer) Close() error {
if err := wavw.cw.Close(); err != nil {
return err
}
return wavw.rw.Close()
}