-
Notifications
You must be signed in to change notification settings - Fork 2
/
streams.go
287 lines (274 loc) · 8.62 KB
/
streams.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package signals
import (
"encoding/base64"
"encoding/gob"
"errors"
"io"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strconv"
"strings"
)
func init() {
gob.Register(&Wave{})
}
const bufferSize = 16
// an offset PCM Signal, (so single channel) that streams values from a URL source as required.
// Supported URL schemes, "file:", "data:", "http(s):".
// Encodings for Http(s); MIME: "audio/l?;rate=?","sound/wav"(mono),"audio/x-wav" (mono)
// Encoding for File: ".wav"(mono),".pcm",".gob"
// Encodings for Data: MIME: "base64" or none. (and MIME as for Http.)
// Buffers at least 32 samples, but if queried for a property value that needs a sample prior to that, might return zero. ( really only used for filtering/smoothing)
type Wave struct {
Offset
URL string
reader io.Reader
}
func (s *Wave) property(p x) y {
if s.reader == nil {
wav, err := NewWave(s.URL)
failOn(err)
s.Offset = wav.Offset
s.reader = wav.reader
}
for p > s.MaxX() {
// append available data onto the PCM slice.
// also possibly shift off some data, shortening the PCM slice, retaining at least two buffer lengths.
// partial samples are read but not accessed by property.
switch st := s.Offset.LimitedSignal.(type) {
case PCM8bit:
sd := PCM8bit{st.PCM}
sd.Data = append(sd.Data, make([]byte, bufferSize)...)
n, err := s.reader.Read(sd.Data[len(sd.Data)-bufferSize:])
failOn(err)
sd.Data = sd.Data[:len(sd.Data)-bufferSize+n]
if len(sd.Data) > bufferSize*3 {
sd.Data = sd.Data[bufferSize:]
s.Offset = Offset{sd, s.Offset.Offset + bufferSize*st.samplePeriod}
} else {
s.Offset = Offset{sd, s.Offset.Offset}
}
case PCM16bit:
sd := PCM16bit{st.PCM}
sd.Data = append(sd.Data, make([]byte, bufferSize*2)...)
n, err := s.reader.Read(sd.Data[len(sd.Data)-bufferSize*2:])
failOn(err)
sd.Data = sd.Data[:len(sd.Data)-bufferSize*2+n]
if len(sd.Data) > bufferSize*2*3 {
sd.Data = sd.Data[bufferSize*2:]
s.Offset = Offset{sd, s.Offset.Offset + bufferSize*st.samplePeriod}
} else {
s.Offset = Offset{sd, s.Offset.Offset}
}
case PCM24bit:
sd := PCM24bit{st.PCM}
sd.Data = append(sd.Data, make([]byte, bufferSize*3)...)
n, err := s.reader.Read(sd.Data[len(sd.Data)-bufferSize*3:])
failOn(err)
sd.Data = sd.Data[:len(sd.Data)-bufferSize*3+n]
if len(sd.Data) > bufferSize*3*3 {
sd.Data = sd.Data[bufferSize*3:]
s.Offset = Offset{sd, s.Offset.Offset + bufferSize*st.samplePeriod}
} else {
s.Offset = Offset{sd, s.Offset.Offset}
}
case PCM32bit:
sd := PCM32bit{st.PCM}
sd.Data = append(sd.Data, make([]byte, bufferSize*4)...)
n, err := s.reader.Read(sd.Data[len(sd.Data)-bufferSize*4:])
failOn(err)
sd.Data = sd.Data[:len(sd.Data)-bufferSize*4+n]
if len(sd.Data) > bufferSize*4*3 {
sd.Data = sd.Data[bufferSize*4:]
s.Offset = Offset{sd, s.Offset.Offset + bufferSize*st.samplePeriod}
} else {
s.Offset = Offset{sd, s.Offset.Offset}
}
case PCM48bit:
sd := PCM48bit{st.PCM}
sd.Data = append(sd.Data, make([]byte, bufferSize*6)...)
n, err := s.reader.Read(sd.Data[len(sd.Data)-bufferSize*6:])
failOn(err)
sd.Data = sd.Data[:len(sd.Data)-bufferSize*6+n]
if len(sd.Data) > bufferSize*6*3 {
sd.Data = sd.Data[bufferSize*6:]
s.Offset = Offset{sd, s.Offset.Offset + bufferSize*st.samplePeriod}
} else {
s.Offset = Offset{sd, s.Offset.Offset}
}
case PCM64bit:
sd := PCM64bit{st.PCM}
sd.Data = append(sd.Data, make([]byte, bufferSize*8)...)
n, err := s.reader.Read(sd.Data[len(sd.Data)-bufferSize*8:])
failOn(err)
sd.Data = sd.Data[:len(sd.Data)-bufferSize*8+n]
if len(sd.Data) > bufferSize*8*3 {
sd.Data = sd.Data[bufferSize*8:]
s.Offset = Offset{sd, s.Offset.Offset + bufferSize*st.samplePeriod}
} else {
s.Offset = Offset{sd, s.Offset.Offset}
}
}
}
return s.Offset.property(p)
}
//func updateShifted(s Shifted, r io.Reader, b *[]byte, blockSize int) (err error){
// b=append(b,make([]byte,bufferSize*blockSize)...)
// n, err := r.Read(b[len(b)-bufferSize*blockSize:])
// failOn(err)
// b=b[:len(b)-bufferSize*blockSize+n]
// if len(b)>bufferSize*blockSize*3{
// b=b[bufferSize*blockSize:]
// s.Offset+=bufferSize*s.samplePeriod
// }
//}
func NewWave(URL string) (*Wave, error) {
r, channels, bytes, rate, err := pcmReader(URL)
if err != nil {
return nil, err
}
if channels != 1 {
return nil, errors.New(URL + ":Needs to be mono.")
}
b := make([]byte, bufferSize*bytes)
n, err := r.Read(b)
failOn(err)
b = b[:n]
switch bytes {
case 1:
return &Wave{Offset{NewPCM8bit(rate, b), 0}, URL, r}, nil
case 2:
return &Wave{Offset{NewPCM16bit(rate, b), 0}, URL, r}, nil
case 3:
return &Wave{Offset{NewPCM24bit(rate, b), 0}, URL, r}, nil
case 4:
return &Wave{Offset{NewPCM32bit(rate, b), 0}, URL, r}, nil
case 6:
return &Wave{Offset{NewPCM48bit(rate, b), 0}, URL, r}, nil
case 8:
return &Wave{Offset{NewPCM64bit(rate, b), 0}, URL, r}, nil
}
return nil, errors.New("Sample Bytes not supported:"+URL)
}
var contentTypeParse = regexp.MustCompile(`^audio/l(\d+);rate=(\d+)$`)
// returns a reader to a resource, along with its Channel count, Precision (bytes) and Sample rate.
func pcmReader(resourceLocation string) (io.Reader, uint16, uint16, uint32, error) {
// resp, err := http.Get(resourceLocation)
url, err := url.Parse(resourceLocation)
if err != nil {
return nil, 0, 0, 0, err
}
switch url.Scheme {
case "file":
switch path.Ext(url.Path) {
case ".wav", ".wave", ".WAV", ".WAVE":
file, err := os.Open(url.Path)
if err != nil {
return nil, 0, 0, 0, err
}
_, format, err := readWaveHeader(file)
if err != nil {
return nil, 0, 0, 0, err
}
return file, format.Channels, format.SampleBytes, format.SampleRate, nil
case ".gob", ".GOB":
s, err := LoadGOB(url.Path[:len(url.Path)-4])
if err != nil {
return nil, 0, 0, 0, err
}
var sampleRate uint32 = 22010
samplePeriod := X(1 / float32(sampleRate))
r, w := io.Pipe()
go func() {
defer func() {
e := recover()
if e != nil {
w.CloseWithError(e.(error))
} else {
w.Close()
}
}()
for i, sample := uint32(0), make([]byte, 2); err == nil; i++ {
sample[0], sample[1] = encodePCM16bit(s.property(x(i) * samplePeriod))
_, err = w.Write(sample)
}
}()
return r, 1, 2, sampleRate, nil
case ".pcm":
rate, err := strconv.ParseUint(path.Base(path.Dir(url.Path)), 10, 32)
if err != nil {
return nil, 0, 0, 0, err
}
bits, err := strconv.ParseUint(path.Base(path.Dir(path.Dir(url.Path))[:len(path.Dir(path.Dir(url.Path)))-3]), 10, 20)
if err != nil {
return nil, 0, 0, 0, err
}
file, err := os.Open(url.Path)
if err != nil {
return nil, 0, 0, 0, err
}
return file, 1, uint16(bits / 8), uint32(rate), nil
}
case "data":
mimeAndRest := strings.SplitN(url.Opaque, ";", 2)
encodingAndData := strings.SplitN(mimeAndRest[1], ",", 2)
var r io.Reader
if encodingAndData[0] == "base64" {
r = base64.NewDecoder(base64.StdEncoding, strings.NewReader(encodingAndData[1]))
} else {
r = strings.NewReader(encodingAndData[1])
}
if mimeAndRest[0] == "sound/wav" || mimeAndRest[0] == "audio/x-wav" {
_, format, err := readWaveHeader(r)
if err != nil {
return nil, 0, 0, 0, err
}
return r, format.Channels, format.SampleBytes, format.SampleRate, nil
}
pcmFormat := contentTypeParse.FindStringSubmatch(mimeAndRest[0])
if pcmFormat != nil {
bits, err := strconv.ParseUint(pcmFormat[1], 10, 19)
if err != nil {
return nil, 0, 0, 0, err
}
rate, err := strconv.ParseUint(pcmFormat[2], 10, 32)
if err != nil {
return nil, 0, 0, 0, err
}
return r, 1, uint16(bits / 8), uint32(rate), nil
}
default: // whatever supported and placed in Body, currently basically "http" or "https"
resp, err := http.DefaultClient.Do(&http.Request{Method: "GET", URL: url})
if err != nil {
return nil, 0, 0, 0, err
}
if resp.Header["Content-Type"][0] == "sound/wav" || resp.Header["Content-Type"][0] == "audio/x-wav" {
_, format, err := readWaveHeader(resp.Body)
if err != nil {
return nil, 0, 0, 0, err
}
return resp.Body, format.Channels, format.SampleBytes, format.SampleRate, nil
}
pcmFormat := contentTypeParse.FindStringSubmatch(resp.Header["Content-Type"][0])
if pcmFormat != nil {
bits, err := strconv.ParseUint(pcmFormat[1], 10, 19)
if err != nil {
return nil, 0, 0, 0, err
}
rate, err := strconv.ParseUint(pcmFormat[2], 10, 32)
if err != nil {
return nil, 0, 0, 0, err
}
return resp.Body, 1, uint16(bits / 8), uint32(rate), nil
}
}
return nil, 0, 0, 0, errors.New("Source:" + resourceLocation + " unsupported.")
}
func failOn(e error) {
if e != nil {
panic(e)
}
}