-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
400 lines (341 loc) · 7.71 KB
/
run.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package gosynth
import (
"github.com/jcreedcmu/gosynth/service"
"github.com/jcreedcmu/gosynth/ugen"
"encoding/binary"
"flag"
"fmt"
"github.com/gordonklaus/portaudio"
"github.com/youpy/go-coremidi"
"math"
"os"
"sort"
"sync"
"time"
)
type Bus []float64
// Ughhh this just needs to be at least as big as the audio processing
// buffer size, no harm if it's bigger.
const SAFE_BUF_LEN = 1024
var out64 = [][]float64{
make([]float64, SAFE_BUF_LEN),
make([]float64, SAFE_BUF_LEN),
make([]float64, SAFE_BUF_LEN),
make([]float64, SAFE_BUF_LEN),
}
func getBus(n int) *float64 {
return &out64[n][0]
}
const sampleRate = 44100
var globalTime int64 = 0 // in audio samples. A signed 32-bit number
// would last 13 hours, but a signed 64-bit int goes about 6 million
// years. Good enough for even Cage and Jem Finer.
var master_vol = 1.0
var post_amp = 0.05
var resFreq = 2646.2
var Q = 1.0
var bus Bus = make([]float64, 4)
var f *os.File
var mutex = &sync.Mutex{}
var pedal = false
const STOP = 0
const RESTART = 1
func (bleeps Bleeps) noteOn(ugenName string, pitch int, vel int) {
mutex.Lock()
defer mutex.Unlock()
osc, ok := bleeps[pitch]
if ok {
// reuse old note
osc.ui.Msg(RESTART)
osc.pedal_hold = false
} else {
// alloc new note
freq := 440 * math.Pow(2, float64(pitch-69)/12)
amp := 0.0006 * float64(vel)
bleeps[pitch] = &PedalBleep{
pedal_hold: false,
ui: ugens[ugenName].Create(),
param: []*float64{getBus(0), &freq, &},
}
}
}
func (bleeps Bleeps) noteOff(pitch int) {
mutex.Lock()
defer mutex.Unlock()
osc, ok := bleeps[pitch]
if ok {
if pedal {
osc.pedal_hold = true
} else {
osc.ui.Msg(STOP)
}
}
}
func (bleeps Bleeps) pedalOn() {
mutex.Lock()
defer mutex.Unlock()
pedal = true
}
func (bleeps Bleeps) pedalOff() {
mutex.Lock()
defer mutex.Unlock()
pedal = false
for _, osc := range bleeps {
if osc.pedal_hold {
osc.pedal_hold = false
osc.ui.Msg(STOP)
}
}
}
func handleMidiEvent(ev []byte, bleeps Bleeps) {
if len(ev) != 3 {
fmt.Printf("UNEXPECTED MIDI: %+v\n", ev)
return
}
status := int(ev[0])
data1 := int(ev[1])
data2 := int(ev[2])
fmt.Printf("MIDI: %d %d %d\n", status, data1, data2)
switch {
case status >= 0x90 && status < 0xa0:
if data1 < 21 {
fmt.Printf("UNEXPECTED LOW NOTE %+v\n", ev)
return
}
if data2 != 0 {
bleeps.noteOn("midi", data1, data2)
} else {
bleeps.noteOff(data1)
}
case status >= 0x80 && status < 0x90:
bleeps.noteOff(data1)
case status == 0xb0:
if data2 == 0 {
bleeps.pedalOff()
} else {
bleeps.pedalOn()
}
default:
fmt.Printf("UNEXPECTED MIDI: %+v\n", ev)
}
}
// Some profiling stuff
var inner time.Duration
var innerCount int64
type PedalBleep struct {
pedal_hold bool
priority float64
ui *ugen.Uinst
param []*float64
}
type Bleeps map[int]*PedalBleep
var percOdom int
var percs = Bleeps(make(map[int]*PedalBleep))
var bleeps = Bleeps(make(map[int]*PedalBleep))
var ugens = make(map[string]*ugen.Ugen)
type ByPriority struct {
Ix []int
Bleeps map[int]*PedalBleep
}
func (a ByPriority) Len() int { return len(a.Ix) }
func (a ByPriority) Swap(i, j int) { a.Ix[i], a.Ix[j] = a.Ix[j], a.Ix[i] }
func (a ByPriority) Less(i, j int) bool {
return a.Bleeps[a.Ix[i]].priority < a.Bleeps[a.Ix[j]].priority
}
func (bleeps Bleeps) priOrder() []int {
ix := []int{}
for k, _ := range percs {
ix = append(ix, k)
}
sort.Sort(ByPriority{
Ix: ix,
Bleeps: bleeps,
})
return ix
}
func filterOn(ugenName string, priority float64, param []*float64) int {
id := percOdom
percOdom++
ugen := ugens[ugenName]
if ugen == nil {
fmt.Printf("No such ugen %s\n", ugenName)
return 0
}
percs[id] = &PedalBleep{
ui: ugens[ugenName].Create(),
param: param,
priority: priority,
}
return id
}
// assumes mutex already held
func genOn(ugenName string, priority float64, pitch int, vel float64) int {
id := percOdom
percOdom++
freq := 440 * math.Pow(2, float64(pitch-69)/12)
amp := 0.01 * vel
percs[id] = &PedalBleep{
ui: ugens[ugenName].Create(),
param: []*float64{getBus(0), &freq, &},
priority: priority,
}
return id
}
func genOff(id int) {
percs[id].ui.Msg(STOP)
}
func genAllOff() {
for id := range percs {
percs[id].ui.Msg(STOP)
}
}
func processAudio(out [][]float32) {
mutex.Lock()
defer mutex.Unlock()
start := time.Now()
buflen := len(out[0])
for i := range out[0] {
out64[0][i] = 0
out64[1][i] = 0
out64[2][i] = 0
out64[3][i] = 0
}
for i, osc := range bleeps {
kill := osc.ui.Run(osc.param, buflen)
if kill {
bleeps[i].ui.Destroy()
delete(bleeps, i)
continue
}
}
for _, i := range percs.priOrder() {
osc := percs[i]
kill := osc.ui.Run(osc.param, buflen)
if kill {
percs[i].ui.Destroy()
delete(percs, i)
continue
}
}
for i := range out[0] {
out[0][i] = float32(out64[0][i])
out[1][i] = float32(out64[1][i])
}
if f != nil {
chk(binary.Write(f, binary.BigEndian, out[0]))
}
globalTime += int64(buflen)
FlushCmdQueueLocked(globalTime)
inner = inner + time.Now().Sub(start)
innerCount++
}
func openStream(cbk interface{}) (*portaudio.Stream, error) {
outDev, err := portaudio.DefaultOutputDevice()
if err != nil {
return nil, err
}
p := portaudio.LowLatencyParameters(nil, outDev)
p.Input.Channels = 0
p.Output.Channels = 2
p.SampleRate = sampleRate
p.FramesPerBuffer = 0
return portaudio.OpenStream(p, cbk)
}
func LoadUgen(filename string, name string) error {
old, ok := ugens[name]
if ok {
old.Close()
delete(ugens, name)
}
ug, err := ugen.Load(filename)
if err != nil {
return err
}
ugens[name] = ug
return nil
}
func UnloadUgen(name string) {
ugens[name].Close()
delete(ugens, name)
}
func Run() {
chk(LoadUgen("./inst/spread.so", "spread"))
chk(LoadUgen("./inst/reverb.so", "reverb"))
chk(LoadUgen("./inst/lead2.so", "midi"))
chk(LoadUgen("./inst/lead.so", "lead"))
chk(LoadUgen("./inst/bass.so", "bass"))
chk(LoadUgen("./inst/snare.so", "snare"))
chk(LoadUgen("./inst/lopass.so", "lopass"))
shouldRecord := flag.Bool("record", false, "whether to record")
addr := flag.String("addr", "localhost:8080", "http service address")
flag.Parse()
portaudio.Initialize()
defer portaudio.Terminate()
if *shouldRecord {
var err error
f, err = os.Create("/tmp/recording.f32")
// # to play:
// $ play -x -r 44100 -c 1 /tmp/recording.f32
// # to convert to wav:
// $ sox -x -r 44100 -c 1 /tmp/recording.f32 recording.wav
chk(err)
}
s, err := openStream(processAudio)
fmt.Println("%+v\n", s.Info())
chk(err)
defer s.Close()
if true {
service.Initialize(*addr, cmdHandle)
}
if true {
client, err := coremidi.NewClient("midi client")
chk(err)
port, err := coremidi.NewInputPort(client, "test",
func(source coremidi.Source, event []byte) {
handleMidiEvent(event, bleeps)
})
chk(err)
sources, err := coremidi.AllSources()
chk(err)
for _, source := range sources {
// func(source coremidi.Source) {
fmt.Printf("Listening to midi source %s [%s]\n", source.Name(), source.Manufacturer())
port.Connect(source)
// }(source)
}
}
filterOn("reverb", 80.0, []*float64{getBus(0)})
filterOn("lopass", 90.0, []*float64{getBus(0), &resFreq, &Q})
filterOn("spread", 100.0, []*float64{getBus(0), getBus(1)})
go func() {
for {
fmt.Printf("inner loop taking avg ~%f samples\n", inner.Seconds()*sampleRate/float64(innerCount))
inner = 0
innerCount = 0
time.Sleep(1 * time.Second)
}
}()
chk(s.Start())
select {}
defer chk(s.Stop())
}
// some filters
func overdrive(n int, LIMIT float64) func(bus Bus) {
return func(bus Bus) {
w := bus[n]
if math.Abs(w) > LIMIT {
if w > 0 {
w = LIMIT
} else {
w = -LIMIT
}
}
bus[n] = w
}
}
// Utils
func chk(err error) {
if err != nil {
panic(err)
}
}