-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadpro.go
459 lines (393 loc) · 11.4 KB
/
quadpro.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package kit
import (
"encoding/json"
"fmt"
"math/rand"
"sort"
"strings"
"sync"
"time"
)
var TheQuadPro *QuadPro
type QuadPro struct {
// Per-patch things
patch map[string]*Patch
patchLogic map[string]*PatchLogic
started bool
rand *rand.Rand
randMutex sync.Mutex
}
func NewQuadPro() *QuadPro {
quadpro := &QuadPro{
patch: map[string]*Patch{},
patchLogic: map[string]*PatchLogic{},
rand: rand.New(rand.NewSource(1)),
}
return quadpro
}
func (quadpro *QuadPro) Stop() {
quadpro.started = false
}
func (quadpro *QuadPro) Api(api string, apiargs map[string]string) (result string, err error) {
switch api {
case "get":
return quadpro.onGet(apiargs)
case "event":
return "", fmt.Errorf("event no longer handled in Quadpro.Api")
case "ANO":
for _, patch := range quadpro.patch {
synth := patch.Synth()
if synth == nil {
return "", fmt.Errorf("patch Synth is nil?")
} else {
synth.SendANO()
}
}
return "", nil
case "echo":
value, ok := apiargs["value"]
if !ok {
value = "ECHO!"
}
return value, nil
case "load":
category, oksaved := apiargs["category"]
if !oksaved {
return "", fmt.Errorf("missing category parameter")
}
filename, oksaved := apiargs["filename"]
if !oksaved {
return "", fmt.Errorf("missing filename parameter")
}
TheAttractManager.SetAttractMode(false)
data, err := TheHost.GetSavedData(category, filename)
if err != nil {
return "", err
}
return "", quadpro.Load(data, category, filename)
case "save":
category, oksaved := apiargs["category"]
if !oksaved {
return "", fmt.Errorf("missing category parameter")
}
filename, oksaved := apiargs["filename"]
if !oksaved {
return "", fmt.Errorf("missing filename parameter")
}
return "", quadpro.save(category, filename)
case "test":
ntimes, err := GetParamInt("engine.testgesturentimes")
if err != nil {
LogIfError(err)
return "", err
}
intervalf, err := GetParamFloat("engine.testgestureinterval")
if err != nil {
LogIfError(err)
return "", err
}
interval := time.Duration(intervalf * 1000000000)
LogInfo("QuadPro.ExecuteApi test start", "ntimes", ntimes, "interval", interval)
quadpro.doTest(ntimes, interval)
LogInfo("QuadPro.ExecuteApi test end", "ntimes", ntimes, "interval", interval)
return "", nil
default:
LogWarn("QuadPro.ExecuteApi api is not recognized\n", "api", api)
return "", fmt.Errorf("QuadPro.Api unrecognized api=%s", api)
}
}
var CursorSourceToQuadPreset ParamsMap
func (quadpro *QuadPro) Start() {
if quadpro.started {
LogInfo("QuadPro.Start: already started")
return
}
quadpro.started = true
bytes, err := TheHost.GetConfigFileData("buttons.json")
if err == nil {
var f any
err = json.Unmarshal(bytes, &f)
if err != nil {
LogIfError(fmt.Errorf("unable to Unmarshal buttons.json"))
} else {
buttonMap, ok := f.(map[string]any)
if !ok {
LogIfError(err)
} else {
CursorSourceToQuadPreset = buttonMap
}
}
}
TheCursorManager.AddCursorHandler("QuadPro", TheQuadPro, "A", "B", "C", "D")
_ = quadpro.addPatch("A")
_ = quadpro.addPatch("B")
_ = quadpro.addPatch("C")
_ = quadpro.addPatch("D")
bytes, err = TheHost.GetSavedData("quad", "_Current.json")
if err != nil {
LogError(err)
} else {
err = quadpro.Load(bytes, "quad", "_Current.json")
LogIfError(err)
}
}
func (quadpro *QuadPro) Status(source string) string {
return "status" + source
}
func (quadpro *QuadPro) PatchForCursorEvent(ce CursorEvent) (patch *Patch, button string) {
source := ce.Source()
// If the source has some patchLogic...
patchLogic, ok := quadpro.patchLogic[source]
if !ok {
patch = nil
} else {
patch = patchLogic.patch
}
// If the source is a Button...
_, ok = CursorSourceToQuadPreset[source]
if ok {
button = source
}
return patch, button
}
func (quadpro *QuadPro) onCursorEvent(state ActiveCursor) error {
// See if this Cursor was generated by Attract mode
isAttractGenerated := state.Current.IsAttractGenerated()
// Any non-attract-generated cursor or Button will turn attract mode off.
if !isAttractGenerated && TheAttractManager.AttractModeIsOn() {
TheAttractManager.SetAttractMode(false)
}
if !isAttractGenerated {
// If it's not generated by the attract mode,
// reset the attract timer for detecting idle time
TheAttractManager.attractMutex.Lock()
TheAttractManager.lastAttractModeChange = time.Now() // reset the timer
TheAttractManager.attractMutex.Unlock()
}
if state.Button != "" {
mmtt_buttondepth := float32(0.1)
// To avoid false triggering, we only want to be triggered when the
// Z depth is small, i.e. when something has just entered the button area.
if state.Current.Ddu == "down" && state.Current.Pos.Z < mmtt_buttondepth {
val, ok := CursorSourceToQuadPreset[state.Button]
if !ok {
LogInfo("No Preset is attached to button", "button", state.Button)
} else {
preset := val.(string)
if TheQuadPro != nil {
LogOfType("cursor", "Button down", "z", state.Current.Pos.Z)
filename := preset
bytes, err := TheHost.GetSavedData("quad", filename)
if err != nil {
return err
}
err = TheQuadPro.Load(bytes, "quad", preset)
if err != nil {
return err
}
TheHost.ShowText(preset)
}
}
}
// Buttons only do things on cursor down
return nil
}
// For the moment, the cursor to patchLogic mapping is 1-to-1.
// I.e. ce.Source of "A" maps to patchLogic "A"
source := state.Current.Source()
patchLogic, ok := quadpro.patchLogic[source]
if !ok || patchLogic == nil {
LogWarn("Source doesn't exist in patchLogic", "source", source)
return nil
}
cursorStyle := patchLogic.patch.Get("misc.cursorstyle")
gensound := IsTrueValue(patchLogic.patch.Get("misc.generatesound"))
genvisual := IsTrueValue(patchLogic.patch.Get("misc.generatevisual"))
isAttractCursor := state.Current.IsAttractGenerated()
// Don't generate sound from attractMode cursors
if !isAttractCursor && gensound && !TheAttractManager.AttractModeIsOn() {
patchLogic.generateSoundFromCursor(state.Current, cursorStyle)
}
if genvisual {
TheHost.GenerateVisualsFromCursor(state.Current, patchLogic.patch.name)
}
return nil
}
func (quadpro *QuadPro) OnClientRestart(portnum int) {
LogOfType("resolume", "quadpro got clientrestart", "portnum", portnum)
// Refresh the patch that has that portnum
for _, patch := range quadpro.patch {
patch.RefreshAllIfPortnumMatches(portnum)
}
}
func (quadpro *QuadPro) onGet(apiargs map[string]string) (result string, err error) {
paramName, ok := apiargs["name"]
if !ok {
return "", fmt.Errorf("QuadPro.onPatchGet: Missing name argument")
}
if strings.HasPrefix(paramName, "engine") {
return GetParam(paramName)
} else {
return "", fmt.Errorf("QuadPro.onGet: can't handle parameter %s", paramName)
}
}
func (quadpro *QuadPro) onMidiEvent(me MidiEvent) error {
LogOfType("midi", "QuadPro.onMidiEvent", "me", me)
return nil
}
func (quadpro *QuadPro) doTest(ntimes int, interval time.Duration) {
numsteps, err := GetParamInt("engine.testgesturenumsteps")
if err != nil {
LogIfError(err)
return
}
durfloat, err := GetParamFloat("engine.testgestureduration")
if err != nil {
LogIfError(err)
return
}
dur := time.Duration(durfloat * float64(time.Second))
for n := 0; n < ntimes; n++ {
quadpro.randMutex.Lock()
randomPatchName := string("ABCD"[quadpro.rand.Intn(len(Patchs))])
quadpro.randMutex.Unlock()
tag := randomPatchName + ",test"
go TheCursorManager.GenerateRandomGesture(tag, numsteps, dur)
time.Sleep(interval)
}
}
func (quadpro *QuadPro) loadQuadRand() error {
arr, err := TheHost.SavedFileList("quad")
if err != nil {
return err
}
quadpro.randMutex.Lock()
rn := quadpro.rand.Uint64() % uint64(len(arr))
quadpro.randMutex.Unlock()
bytes, err := TheHost.GetSavedData("quad", arr[rn])
if err != nil {
LogIfError(err)
return err
}
err = quadpro.Load(bytes, "quad", arr[rn])
if err != nil {
LogIfError(err)
return err
}
for _, patch := range quadpro.patch {
patch.RefreshAllPatchValues()
}
return nil
}
func (quadpro *QuadPro) Load(bytes []byte, category string, filename string) error {
LogInfo("QuadPro.Load", "category", category, "filename", filename)
paramsMap, err := LoadParamsMap(bytes)
if err != nil {
LogIfError(err)
return err
}
var lasterr error
switch category {
case "quad":
overridemap := OverrideMap()
for _, patch := range quadpro.patch {
err := patch.ApplyPatchValuesFromQuadMap(paramsMap)
if err != nil {
LogIfError(err)
lasterr = err
}
if overridemap != nil {
patch.params.ApplyValuesFromMap(category, overridemap, patch.params.Set)
}
patch.RefreshAllPatchValues()
}
case "engine":
LogWarn("HACK! DISABLED! quadpro.Load shouldn't load engine parameters")
default:
LogWarn("QuadPro.Load: unhandled")
}
// Decide what _Current things we should save
// when we load something. E.g. if we're
// loading a patch with something, we want to
// then save the entire quad
err = nil
isCurrent := strings.HasPrefix(filename, "_Current")
switch category {
case "engine":
// No need to save _Current if we're loading it.
if !isCurrent {
err = EngineParams.Save("engine", "_Current")
}
case "quad":
if !isCurrent {
err = quadpro.saveQuad("_Current")
}
case "patch", "sound", "visual", "effect", "misc":
// If we're loading a patch (or something inside a patch, like sound, visual, etc),
// we save the entire quad, since that's our real persistent state
if !isCurrent {
err = quadpro.saveQuad("_Current")
}
}
if err != nil {
LogIfError(err)
lasterr = err
}
return lasterr
}
func (quadpro *QuadPro) save(category string, filename string) (err error) {
LogOfType("saved", "QuadPro.save", "category", category, "filename", filename)
if category == "engine" {
// err = quadpro.engineClicks.Save("engine", filename)
LogWarn("QuadPro.save: shouldn't be saving global?")
err = fmt.Errorf("QuadPro.save: global shouldn't be handled here")
} else if category == "quad" {
err = quadpro.saveQuad(filename)
} else {
err = fmt.Errorf("QuadPro.Api: unhandled save category %s", category)
}
if err != nil {
LogIfError(err)
}
return err
}
func (quadpro *QuadPro) saveQuad(filename string) error {
LogOfType("saved", "QuadPro.saveQuad", "quad", filename)
sortedPatchNames := []string{}
for _, patch := range quadpro.patch {
sortedPatchNames = append(sortedPatchNames, patch.Name())
}
sort.Strings(sortedPatchNames)
s := "{\n \"params\": {\n"
sep := ""
for _, patchName := range sortedPatchNames {
patch := quadpro.patch[patchName]
sortedNames := patch.ParamNames()
for _, fullName := range sortedNames {
valstring := patch.Get(fullName)
s += fmt.Sprintf("%s \"%s-%s\":\"%s\"", sep, patch.Name(), fullName, valstring)
sep = ",\n"
}
}
s += "\n }\n}"
data := []byte(s)
if !strings.HasSuffix(filename, ".json") {
filename += ".json"
}
return TheHost.SaveDataInFile(data, "quad", filename)
}
func (quadpro *QuadPro) addPatch(name string) *Patch {
patch := NewPatch(name)
quadpro.patch[name] = patch
quadpro.patchLogic[name] = NewPatchLogic(patch)
return patch
}
/*
func (quadpro *QuadPro) scheduleNoteNow(dest string, pitch, velocity uint8, duration Clicks) {
LogInfo("QuadPro.scheculeNoteNow", "dest", dest, "pitch", pitch)
pe := &PhraseElement{Value: NewNoteFull(0, pitch, velocity, duration)}
phr := NewPhrase().InsertElement(pe)
phr.Destination = dest
SchedulePhrase(phr, CurrentClick(), dest)
}
*/