-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuf.go
199 lines (178 loc) · 4.21 KB
/
buf.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
// Copyright 2018 The ZikiChombo Authors. All rights reserved. Use of this source
// code is governed by a license that can be found in the License file.
package sndbuf
import (
"fmt"
"io"
"github.com/zikichombo/sound"
"github.com/zikichombo/sound/cil"
"github.com/zikichombo/sound/freq"
)
// Type Buf implements an in memory sound Seeker/Source/Sink
type T struct {
dat []float64
freq freq.T
nchan int
pos int
il *cil.T
}
// FromSource reads in the source into a buffer (*T).
// If an error occurs while reading other than io.EOF,
// that error is returned.
func FromSource(src sound.Source) (*T, error) {
b := New(src.SampleRate(), src.Channels())
buf := make([]float64, 1024)
for {
n, e := src.Receive(buf)
if e == io.EOF {
break
}
if e != nil {
return nil, e
}
b.Send(buf[:n*src.Channels()])
}
b.Seek(0)
return b, nil
}
// New creates a new sound buffer at sampling frequency f with c channels.
func New(f freq.T, c int) *T {
return &T{
dat: make([]float64, 0, 1024),
freq: f,
nchan: c,
pos: 0}
}
// FromSlice creates a single channel buffer backed by ds.
//
// FromSlice assumes ds is mono-channel.
func FromSlice(ds []float64, f freq.T) *T {
return FromSliceChans(ds, 1, f)
}
// FromSliceChans creates a buffer from slice ds in channel interleaved
// format with samplerate f.
//
// FromSliceChans panics if len(ds) is not a multiple of nc
func FromSliceChans(ds []float64, nc int, f freq.T) *T {
if len(ds)%nc != 0 {
panic("channel alignment")
}
return &T{
dat: ds,
freq: f,
nchan: nc,
pos: 0}
}
// Slice returns the slice of in-memory samples storing the sound data.
//
// The backing slice is channel-interleaved. This differs from
// the Source/Sink expected interface; however it is a natural fit
// for appendable data. To deinterleave the result, see the
// cil package.
func (b *T) Slice() []float64 {
return b.dat
}
// Split returns a slice of buffers, one per channel.
func (b *T) Split() []*T {
return b.SplitTo(nil)
}
// SplitTo places one buffer for each channel in
// dst and returns it. If dst doesn't have sufficient
// capacity, a new slice is returned in its place.
func (b *T) SplitTo(dst []*T) []*T {
if cap(dst) < b.nchan {
dst = make([]*T, b.nchan)
}
dst = dst[:b.nchan]
if b.il == nil {
b.il = cil.New(b.nchan, len(b.dat)/b.nchan)
}
b.il.Deinter(b.dat)
for i := 0; i < b.nchan; i++ {
csl := make([]float64, len(b.dat)/b.nchan)
copy(csl, b.il.Chan(i, b.dat))
dst[i] = FromSlice(csl, b.SampleRate())
}
b.il.Inter(b.dat)
return dst
}
// SampleRate returns the sampling frequency of the sound buffer.
func (b *T) SampleRate() freq.T {
return b.freq
}
// Channels returns the number of channels in the sound buffer.
func (b *T) Channels() int {
return b.nchan
}
// Receive implements snd.Source, placing channel-deinterleaved
// data in dst.
func (b *T) Receive(dst []float64) (int, error) {
if len(dst)%b.nchan != 0 {
return 0, sound.ErrChannelAlignment
}
n := len(dst)
m := len(b.dat) - b.pos
if m == 0 {
return 0, io.EOF
}
if m > n {
m = n
}
frms := m / b.nchan
c := 0
f := 0
// nb b.pos must be at a frame boundary, enforced
// by pared-down interface
for i := b.pos; i < b.pos+m; i++ {
dst[c*frms+f] = b.dat[i]
c++
if c == b.nchan {
c = 0
f++
}
}
b.pos += m
return f, nil
}
// Send implements sound.Sink, taking channel-deinterleaved data in d
// and placing it in the memory buffer.
func (b *T) Send(d []float64) error {
if len(d)%b.nchan != 0 {
return sound.ErrChannelAlignment
}
frms := len(d) / b.nchan
f := 0
c := 0
for f < frms {
si := c*frms + f
b.dat = append(b.dat, d[si])
c++
if c == b.nchan {
c = 0
f++
}
}
return nil
}
func (b *T) Close() error {
return nil
}
// Len returns the length of the buffer in terms of frames.
func (b *T) Len() int64 {
return int64(len(b.dat) / b.nchan)
}
// Pos returns the current position in the buffer in terms of
// frames.
func (b *T) Pos() int64 {
return int64(b.pos / b.nchan)
}
// Seek seeks to the frame with index f. Seek returns a non-nil out
// of range error iff f < 0 or f > b.Len().
//
func (b *T) Seek(f int64) error {
if f < 0 || f > b.Len() {
return fmt.Errorf("io error: out of range")
}
b.pos = int(f) * b.nchan
return nil
}