/
sndfile.go
156 lines (138 loc) · 2.96 KB
/
sndfile.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
package ugen
import "github.com/mkb218/gosndfile/sndfile"
import "sync"
// import "time"
type SndfileOut struct {
UGenBase
name string
fi sndfile.Info
file *sndfile.File
start, stop *sync.Once
appendMode bool
quitchan chan q
}
func NewSndfileOut(name string, fi sndfile.Info, inputugens int, appendMode bool) (s *SndfileOut) {
s = new(SndfileOut)
s.inputs = make([]UGen, inputugens)
s.fi = fi
s.name = name
s.quitchan = make(chan q)
s.start = new(sync.Once)
s.stop = new(sync.Once)
s.stop.Do(func(){})
return
}
func init() {
var _ UGen = new(SndfileOut)
}
func (s *SndfileOut) GetParams() []ParamDesc {
return []ParamDesc{}
}
func (s *SndfileOut) Start(op OutputParams) (err error) {
defer func() {
if err != nil {
if s.file != nil {
s.file.Close()
}
}
}()
s.start.Do(func() {
MakeRecycleChannel(op)
s.fi.Samplerate = int32(op.SampleRate)
m := sndfile.Write
if s.appendMode {
m = sndfile.ReadWrite
}
bufsize := 0
ochans := 0
for _, u := range s.inputs {
ochans += len(u.OutputChannels())
bufsize += (op.BufferSize * len(u.OutputChannels()))
}
logger.Println(s.name,ochans,"output channels")
s.fi.Channels = int32(ochans)
s.file, err = sndfile.Open(s.name, m, &s.fi)
if err != nil {
logger.Print(err)
s.start = new(sync.Once)
return
}
if s.appendMode {
_, err = s.file.Seek(0, sndfile.End)
if err != nil {
s.start = new(sync.Once)
return
}
}
for _, u := range s.inputs {
u.Start(op)
}
s.stop = new(sync.Once)
obuf := make([]float32, bufsize)
go func() {
// create ticker
t := TickerFor(op)
defer func() {
for _, u := range s.inputs {
u.Stop()
}
// stop ticker
t.Stop()
s.stop.Do(func() {
s.start = new(sync.Once)
})
}()
for {
// wait for ticker
select {
case <- t.C:
case <- s.quitchan:
return
}
channum := 0
var wg sync.WaitGroup
for _, u := range s.inputs {
for _, c := range u.OutputChannels() {
// logger.Println(c)
wg.Add(1)
go func(channum int, c chan []float32) {
select {
case ibuf := <- c:
for snum, sval := range ibuf {
// if sval != 0 {
// logger.Println(obuf[snum*ochans+channum], sval)
// }
obuf[snum*ochans+channum] = sval
// if sval != 0 {
// logger.Println(obuf[snum*ochans+channum], sval)
// }
}
go func() { RecycleBuf(ibuf, op) }()
case <- s.quitchan:
return
}
wg.Done()
}(channum, c)
channum++
}
}
wg.Wait()
var n int64
n, err = s.file.WriteItems(obuf)
if n != int64(len(obuf)) || err != nil {
logger.Print(s.name, "couldn't write from buf length", len(obuf), "wrote", n, "err", err)
return
}
}
}()
})
return
}
func (s *SndfileOut) Stop() (err error) {
s.stop.Do(func() {
s.quitchan <- q{}
s.start = new(sync.Once)
err = s.file.Close()
})
return
}