-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
77 lines (70 loc) · 1.39 KB
/
packet.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
// 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 plug
import (
"zikichombo.org/sound"
)
type packet struct {
err error
n int
samples []float64
cmap *cmap
nC int
src sound.Source
snk sound.Sink
}
func (p *packet) init(v sound.Form, cs ...int) {
p.cmap = newCmap(v, cs...)
p.err = nil
p.n = 0
p.samples = p.samples[:0]
if len(cs) == 0 {
p.nC = v.Channels()
} else {
p.nC = len(cs)
}
}
func (p *packet) put(dst *Block) int {
sl := p.samples
nC := dst.Channels
frms := p.n
cmap := p.cmap
for c := 0; c < nC; c++ {
cc := cmap.mapC(c)
if cc == -1 {
continue
}
sStart := cc * frms
sEnd := sStart + frms
dStart := c * frms
dEnd := dStart + frms
copy(dst.Samples[dStart:dEnd], sl[sStart:sEnd])
}
return frms
}
func (p *packet) get(src *Block) {
cmap := p.cmap
nC := len(cmap.i)
frms := src.Frames
sl := p.samples
sl = buffer(sl, nC, frms)
for cc := 0; cc < nC; cc++ /*not really*/ {
c := cmap.imapC(cc)
sStart := c * frms
sEnd := sStart + frms
dStart := cc * frms
dEnd := dStart + frms
copy(sl[dStart:dEnd], src.Samples[sStart:sEnd])
}
p.samples = sl
p.n = frms
}
func buffer(d []float64, c, f int) []float64 {
N := c * f
if cap(d) < N {
tmp := make([]float64, (5*N)/3)
copy(tmp, d)
d = tmp
}
return d[:N]
}