forked from faiface/beep
/
encode.go
97 lines (89 loc) · 2.32 KB
/
encode.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
package wav
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"github.com/faiface/beep"
"github.com/pkg/errors"
)
// Encode writes all audio streamed from s to w in WAVE format.
//
// Format precision must be 1 or 2 bytes.
func Encode(w io.WriteSeeker, s beep.Streamer, format beep.Format) (err error) {
defer func() {
if err != nil {
err = errors.Wrap(err, "wav")
}
}()
if format.NumChannels <= 0 {
return errors.New("wav: invalid number of channels (less than 1)")
}
if format.Precision != 1 && format.Precision != 2 {
return errors.New("wav: unsupported precision, 1 or 2 is supported")
}
h := header{
RiffMark: [4]byte{'R', 'I', 'F', 'F'},
FileSize: -1, // finalization
WaveMark: [4]byte{'W', 'A', 'V', 'E'},
FmtMark: [4]byte{'f', 'm', 't', ' '},
FormatSize: 16,
FormatType: 1,
NumChans: int16(format.NumChannels),
SampleRate: int32(format.SampleRate),
ByteRate: int32(int(format.SampleRate) * format.NumChannels * format.Precision),
BytesPerFrame: int16(format.NumChannels * format.Precision),
BitsPerSample: int16(format.Precision) * 8,
DataMark: [4]byte{'d', 'a', 't', 'a'},
DataSize: -1, // finalization
}
if err := binary.Write(w, binary.LittleEndian, &h); err != nil {
return err
}
var (
bw = bufio.NewWriter(w)
samples = make([][2]float64, 512)
buffer = make([]byte, len(samples)*format.Width())
written int
)
for {
n, ok := s.Stream(samples)
if !ok {
break
}
buf := buffer
switch {
case format.Precision == 1:
for _, sample := range samples[:n] {
buf = buf[format.EncodeUnsigned(buf, sample):]
}
case format.Precision == 2:
for _, sample := range samples[:n] {
buf = buf[format.EncodeSigned(buf, sample):]
}
default:
panic(fmt.Errorf("wav: encode: invalid precision: %d", format.Precision))
}
nn, err := bw.Write(buffer[:n*format.Width()])
if err != nil {
return err
}
written += nn
}
if err := bw.Flush(); err != nil {
return err
}
// finalize header
h.FileSize = int32(44 + written) // 44 is the size of the header
h.DataSize = int32(written)
if _, err := w.Seek(0, io.SeekStart); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, &h); err != nil {
return err
}
if _, err := w.Seek(0, io.SeekEnd); err != nil {
return err
}
return nil
}