-
Notifications
You must be signed in to change notification settings - Fork 3
/
audio.go
167 lines (138 loc) · 3.69 KB
/
audio.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
package audio
import (
"github.com/oov/audio/converter"
"io"
)
type Reader interface {
ReadFloat32(p []float32) (n int, err error)
ReadFloat64(p []float64) (n int, err error)
}
type Writer interface {
WriteFloat32(p []float32) (n int, err error)
WriteFloat64(p []float64) (n int, err error)
}
type InterleavedReader interface {
ReadFloat32Interleaved(p [][]float32) (n int, err error)
ReadFloat64Interleaved(p [][]float64) (n int, err error)
}
type InterleavedWriter interface {
WriteFloat32Interleaved(p [][]float32) (n int, err error)
WriteFloat64Interleaved(p [][]float64) (n int, err error)
}
type reader struct {
conv converter.Converter
r io.Reader
buf []byte
}
func NewReader(conv converter.Converter, r io.Reader) Reader {
return &reader{
conv: conv,
r: r,
}
}
func (r *reader) ReadFloat32(p []float32) (n int, err error) {
ln := len(p)
if ln > len(r.buf) {
r.buf = make([]byte, ln)
}
n, err = r.r.Read(r.buf[:ln])
r.conv.ToFloat32(r.buf[:n], p)
return
}
func (r *reader) ReadFloat64(p []float64) (n int, err error) {
ln := len(p)
if ln > len(r.buf) {
r.buf = make([]byte, ln)
}
n, err = r.r.Read(r.buf[:ln])
r.conv.ToFloat64(r.buf[:n], p)
return
}
type interleavedReader struct {
conv converter.InterleavedConverter
r io.Reader
buf []byte
}
func NewInterleavedReader(conv converter.InterleavedConverter, r io.Reader) InterleavedReader {
return &interleavedReader{
conv: conv,
r: r,
}
}
func (r *interleavedReader) ReadFloat32Interleaved(p [][]float32) (n int, err error) {
ln := len(p) * len(p[0]) * r.conv.SampleSize()
if ln > len(r.buf) {
r.buf = make([]byte, ln)
}
n, err = r.r.Read(r.buf[:ln])
r.conv.ToFloat32Interleaved(r.buf[:n], p)
n /= len(p) * r.conv.SampleSize()
return
}
func (r *interleavedReader) ReadFloat64Interleaved(p [][]float64) (n int, err error) {
ln := len(p) * len(p[0]) * r.conv.SampleSize()
if ln > len(r.buf) {
r.buf = make([]byte, ln)
}
n, err = r.r.Read(r.buf[:ln])
r.conv.ToFloat64Interleaved(r.buf[:n], p)
n /= len(p) * r.conv.SampleSize()
return
}
type writer struct {
w io.Writer
buf []byte
conv converter.Converter
}
func NewWriter(conv converter.Converter, w io.Writer) Writer {
return Writer(&writer{w: w, conv: conv})
}
func (w *writer) WriteFloat32(p []float32) (n int, err error) {
ln := len(p)
if ln > len(w.buf) {
w.buf = make([]byte, ln)
}
w.conv.FromFloat32(p, w.buf[:ln])
n, err = w.w.Write(w.buf[:ln*w.conv.SampleSize()])
return
}
func (w *writer) WriteFloat64(p []float64) (n int, err error) {
ln := len(p)
if ln > len(w.buf) {
w.buf = make([]byte, ln)
}
w.conv.FromFloat64(p, w.buf[:ln])
n, err = w.w.Write(w.buf[:ln*w.conv.SampleSize()])
return
}
type interleavedWriter struct {
w io.Writer
buf []byte
conv converter.InterleavedConverter
}
func NewInterleavedWriter(conv converter.InterleavedConverter, w io.Writer) InterleavedWriter {
return &interleavedWriter{
w: w,
conv: conv,
}
}
func (w *interleavedWriter) WriteFloat32Interleaved(p [][]float32) (n int, err error) {
ln := len(p) * len(p[0]) * w.conv.SampleSize()
if ln > len(w.buf) {
w.buf = make([]byte, ln)
}
w.conv.FromFloat32Interleaved(p, w.buf[:ln])
n, err = w.w.Write(w.buf[:ln])
n /= len(p) * w.conv.SampleSize()
return
}
func (w *interleavedWriter) WriteFloat64Interleaved(p [][]float64) (n int, err error) {
ln := len(p) * len(p[0]) * w.conv.SampleSize()
if ln > len(w.buf) {
w.buf = make([]byte, ln)
}
w.conv.FromFloat64Interleaved(p, w.buf[:ln])
n, err = w.w.Write(w.buf[:ln])
n /= len(p) * w.conv.SampleSize()
return
}