-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathinit.lua
More file actions
690 lines (601 loc) · 17.5 KB
/
Copy pathinit.lua
File metadata and controls
690 lines (601 loc) · 17.5 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
local app = app
local Class = require "Base.Class"
local Base = require "Unit.Section"
local Branch = require "Chain.Branch"
local Persist = require "Persist"
local Keyboard = require "Keyboard"
local Utils = require "Utils"
local Settings = require "Settings"
local UnitFactory = require "Unit.Factory"
local ply = app.SECTION_PLY
-- Unit Class
local Unit = Class {}
Unit:include(Base)
function Unit:init(args)
Base.init(self, args)
self:setClassName("Unit")
local title = args.title or app.logError("%s.init: title is missing.", self)
self:setInstanceName(title)
self:setInstanceKey(args.instanceKey or Persist.generateInstanceKey())
local chain = args.chain or app.logError("%s.init: chain is missing.", self)
local depth = args.depth or app.logError("%s.init: depth is missing.", self)
local loadInfo = args.loadInfo or
app.logError("%s.init: loadInfo is missing.", self)
local mnemonic = args.mnemonic or
app.logError("%s.init: mnemonic is missing.", self)
local channelCount = args.channelCount or chain.channelCount
self.suppressTitleGeneration = args.suppressTitleGeneration or false
self.channelCount = channelCount
self.loadInfo = loadInfo
-- Started putting version numbers on units from v0.3.09
self.version = args.version or 1
self.mnemonic = mnemonic
self.depth = depth
self.title = title
self.chain = chain
self.stopCount = 1
self.started = false
self.branches = {}
self.objects = {}
local Source = require "Source.Internal"
self.leftOutputSource = Source("local", self, 1)
if channelCount > 1 then
self.rightOutputSource = Source("local", self, 2)
end
self.aliases = args.aliases or {}
self.pUnit = args.pUnit or app.Unit(title, channelCount)
-- load and compile the graph
self:onLoadGraph(channelCount)
if not self.pUnit:compile() then
app.logError("%s.init: Failed to compile the graph.", self)
end
-- Get the builtin controls and views from the unit implementation.
local controls, viewDescriptors =
self:onLoadViews(self.objects, self.branches)
controls = controls or {}
viewDescriptors = viewDescriptors or {
expanded = {},
collapsed = {}
}
-- register each control
for id, control in pairs(controls) do
self:addControl(id, control)
end
-- add scope contextual views (if any)
local cviewDescriptors = {}
if self.controls.scope == nil then
local added = false
for _, cname in ipairs(viewDescriptors["expanded"]) do
if viewDescriptors[cname] == nil and cviewDescriptors[cname] == nil then
cviewDescriptors[cname] = {
"scope",
cname
}
added = true
end
end
if added then
local OutputScope = require "Unit.ViewControl.OutputScope"
local scope = OutputScope {
monitor = self,
width = 4 * ply
}
self:addControl("scope", scope)
end
end
-- create each view and add its controls
for viewName, descriptor in pairs(viewDescriptors) do
self:parseViewDescriptor(viewName, descriptor)
end
-- process additional contextual views
for viewName, descriptor in pairs(cviewDescriptors) do
self:parseViewDescriptor(viewName, descriptor)
end
-- Mark default set of controls and branches as builtin.
-- This distinguishes them from the ControlBranches added later.
for _, control in pairs(self.controls) do
control.isBuiltin = true
end
for _, branch in pairs(self.branches) do
branch.isBuiltin = true
end
self:saveView("expanded")
end
function Unit:getRootChain()
return self.chain:getRootChain()
end
function Unit:findUnitByTitle(title)
for _, branch in pairs(self.branches) do
local unit = branch:findUnitByTitle(title)
if unit then
return unit
end
end
end
function Unit:findByInstanceKey(key)
if self:getInstanceKey() == key then
return self
end
for _, branch in pairs(self.branches) do
local o = branch:findByInstanceKey(key)
if o then
return o
end
end
end
function Unit:getOutputSource(i)
if i == 1 or i == nil or self.channelCount == 1 then
return self.leftOutputSource
elseif i == 2 then
return self.rightOutputSource
end
end
function Unit:getOutputDisplayName(channel)
return self.title
end
function Unit:setTitle(title)
self.title = title
self:broadcastDown("unitTitleChanged", title)
for name, branch in pairs(self.branches) do
branch:setTitle(title)
end
if self.leftOutputSource then
self.leftOutputSource:onRename()
end
if self.rightOutputSource then
self.rightOutputSource:onRename()
end
end
--- Set the background graphic for the named view
-- @param name The name of the view created by onLoadViews.
-- @param graphic The background graphic to use.
function Unit:setViewBackground(name, graphic)
local view = self:getView(name)
if view then
-- Place the background graphic position after the unit header.
graphic:setPosition(app.SECTION_PLY, 0)
view:setBackground(graphic)
else
app.logError("Unit.setViewBackground: view '%s' does not exist.", name)
end
end
-- syntax sugar for Unit definition
function Unit:addToMuteGroup(control)
self.chain:addToMuteGroup(control)
end
function Unit:addObject(name, o)
name = name or Utils.findNextUnusedKey(self.objects, "object")
o:setName(name)
self.pUnit:addObject(o)
self.objects[name] = o
return o
end
function Unit:addMonoBranch(name, inObject, inletName, outObject, outletName)
name = name or Utils.findNextUnusedKey(self.branches, "branch")
local branch = Branch {
title = self.title,
subTitle = name,
depth = self.depth + 1,
channelCount = 1,
leftDestination = inObject:getInput(inletName),
-- branch specific arguments
leftOutObject = outObject,
leftOutletName = outletName,
unit = self
}
self.branches[name] = branch
branch.name = name
return branch
end
function Unit:addStereoBranch(name, leftInObject, leftInletName, rightInObject,
rightInletName, leftOutObject, leftOutletName,
rightOutObject, rightOutletName)
name = name or Utils.findNextUnusedKey(self.branches, "branch")
local branch = Branch {
title = self.title,
subTitle = name,
depth = self.depth + 1,
channelCount = 2,
leftDestination = leftInObject:getInput(leftInletName),
rightDestination = rightInObject:getInput(rightInletName),
-- branch specific arguments
leftOutObject = leftOutObject,
leftOutletName = leftOutletName,
rightOutObject = rightOutObject,
rightOutletName = rightOutletName,
unit = self
}
self.branches[name] = branch
branch.name = name
return branch
end
function Unit:addBranch(name, branch)
self.branches[name] = branch
branch.name = name
branch:setTitle(self.title, name)
branch:updateDepth(self.depth + 1)
branch:setUnit(self)
end
function Unit:renameBranch(oldName, newName)
local branch = self:getBranch(oldName)
if branch then
self.branches[branch.name] = nil
self.branches[newName] = branch
branch.name = newName
branch:setTitle(self.title, newName)
end
end
function Unit:removeBranch(name)
local branch = self.branches[name]
if branch then
self.branches[name] = nil
end
end
function Unit:getBranch(name)
return self.branches[name]
end
function Unit:enable(soft)
if not soft then
self.disabled = false
end
if not self.disabled then
self.pUnit:enable()
self:notifyBranches("enable", true)
end
end
function Unit:disable(soft)
if not soft then
self.disabled = true
end
self.pUnit:disable()
self:notifyBranches("disable", true)
end
function Unit:start()
if self.stopCount > 0 then
self.stopCount = self.stopCount - 1
if self.stopCount == 0 then
self.started = true
self:notifyBranches("start")
end
end
end
function Unit:stop()
if self.stopCount == 0 then
self.stopCount = 1
self.started = false
self:notifyBranches("stop")
else
self.stopCount = self.stopCount + 1
end
end
function Unit:enableBypass()
self.pUnit:setBypass(true)
self.chain:rebuildGraph()
if Settings.get("unitDisableOnBypass") == "yes" then
self:disable()
end
self.controls.header:bypassEnabled()
end
function Unit:disableBypass()
self:enable()
self.pUnit:setBypass(false)
self.chain:rebuildGraph()
self.controls.header:bypassDisabled()
end
function Unit:toggleBypass()
local chain = self.chain
local wasMuted = chain:muteIfNeeded()
if self.pUnit:getBypass() then
self:disableBypass()
else
self:enableBypass()
end
chain:unmuteIfNeeded(wasMuted)
end
function Unit:getInwardConnection(ch)
if self.pUnit == nil then
app.logError("Unit.getInwardConnection: pUnit is nil.")
end
return self.pUnit:getInwardConnection(ch - 1)
end
function Unit:getOutput(ch)
if self.pUnit == nil then
app.logError("Unit.getOutput: pUnit is nil.")
end
if self.channelCount == 1 or ch == nil or ch == 1 then
return self.pUnit:getOutput(0)
elseif ch == 2 then
return self.pUnit:getOutput(1)
end
end
function Unit:getMonitoringOutput(ch)
return self:getOutput(ch)
end
function Unit:serialize()
local t = {
loadInfo = UnitFactory.serializeLoadInfo(self.loadInfo),
activeView = self.currentViewName,
bypass = self.pUnit:getBypass(),
lastPresetPath = self.lastPresetPath,
lastPresetFilename = self.lastPresetFilename,
unitVersion = self.version,
firmwareVersion = app.FIRMWARE_VERSION,
instanceKey = self:getInstanceKey()
}
if self.hasUserTitle then
t.hasUserTitle = true
t.title = self.title
end
-- walk each control
local controls = {}
for cname, control in pairs(self.controls) do
if control.isBuiltin and control.serialize then
local tmp = control:serialize()
if next(tmp) ~= nil then
controls[cname] = tmp
end
end
end
t.controls = controls
-- Make sure the menu generation code was run at least once.
if not self.menuLoadedAtLeastOnce then
self:showMenu(true)
end
-- walk each object
t.objects = Persist.serializeObjects(self.objects)
-- walk each Branch
local branches = {}
for bname, branch in pairs(self.branches) do
if branch.isBuiltin and branch:isSerializationNeeded() then
branches[bname] = branch:serialize()
end
end
t.branches = branches
-- walk each ControlBranch
local controlBranches = {}
for bname, branch in pairs(self.controlBranches) do
controlBranches[bname] = branch:serialize()
end
t.controlBranches = controlBranches
-- walk the expanded view and save the order
t.controlOrder = self:getControlOrder("expanded")
return t
end
function Unit:deserialize(t)
self:removeAllControlBranches()
if t.hasUserTitle and t.title then
self:setTitle(t.title)
self.hasUserTitle = true
end
self:setLastPreset(t.lastPresetPath, t.lastPresetFilename)
if t.instanceKey then
self:setInstanceKey(t.instanceKey)
end
if t.objects then
Persist.deserializeObjects(self.objects, self.aliases, t.objects)
else
app.logInfo("%s:deserialize: no 'objects' table.", self)
end
-- walk each control in the expanded view
if t.controls then
-- deserialize each control (if it exists)
local controls = self.controls
for cname, cdata in pairs(t.controls) do
local control = controls[cname]
if control and control.isBuiltin and control.deserialize then
control:deserialize(cdata)
end
end
end
-- load branches
if t.branches then
local branches = self.branches
for bname, bdata in pairs(t.branches) do
local branch = branches[bname]
if branch then
branch:deserialize(bdata)
end
end
else
app.logInfo("%s:deserialize: no 'branches' table.", self)
end
-- load control branches
if t.controlBranches then
for _, bdata in pairs(t.controlBranches) do
if bdata.type and bdata.id then
self:addControlBranch(bdata.type, bdata.id, bdata)
self:placeControl(bdata.id, "expanded")
end
end
end
-- restore control order in each view
if t.controlOrder then
self:applyControlOrder("expanded", t.controlOrder)
end
-- tell all controls to update
self:notifyControls("update")
if t.bypass then
self:enableBypass()
end
-- activate the saved view
if t.activeView then
self:switchView(t.activeView)
end
end
function Unit:doRename()
local keyboard = Keyboard("Rename unit", self.title, true, "NamingStuff")
local task = function(text)
if text then
self:setTitle(text)
self.hasUserTitle = true
end
end
keyboard:subscribe("done", task)
keyboard:show()
end
function Unit:notifyBranches(method, ...)
for name, branch in pairs(self.branches) do
local f = branch[method]
if f ~= nil then
-- app.logInfo("%s:notifyBranches > %s:%s",self,branch,method)
f(branch, ...)
end
end
end
function Unit:notifyAll(method, ...)
self:notifyControls(method, ...)
for name, branch in pairs(self.branches) do
local f = branch[method]
if f ~= nil then
-- app.logInfo("%s:notifyAll > %s:%s",self,branch,method)
f(branch, ...)
end
branch:notifyAll(method, ...)
end
end
function Unit:setLastPreset(path, filename)
self.lastPresetPath = path
self.lastPresetFilename = filename
end
function Unit:getLastPreset()
return self.lastPresetPath, self.lastPresetFilename
end
local defaultMenuItems = {
"infoHeader",
"renameUnit",
"loadPreset",
"savePreset",
"editControls"
}
local isDefaultMenuItem = {}
for i, cname in ipairs(defaultMenuItems) do
isDefaultMenuItem[cname] = true
end
function Unit:addDefaultMenuItems(order, controls)
-- create default controls if needed
local Task = require "Unit.MenuControl.Task"
local MenuHeader = require "Unit.MenuControl.Header"
if controls.infoHeader == nil then
controls.infoHeader = MenuHeader {
description = string.format("%s (Last Preset: %s)", self.title,
self.lastPresetFilename)
}
end
if controls.loadPreset == nil then
controls.loadPreset = Task {
description = "Load Preset",
task = function()
Persist.loadUnitPreset(self)
end
}
end
if controls.savePreset == nil then
controls.savePreset = Task {
description = "Save Preset",
task = function()
Persist.saveUnitPreset(self)
end
}
end
if controls.renameUnit == nil then
controls.renameUnit = Task {
description = "Rename Unit",
task = function()
self:doRename()
end
}
end
if controls.editControls == nil then
controls.editControls = Task {
description = "Edit Controls",
task = function()
self:doEditControls()
end
}
end
-- remove default items from order array
Utils.removeValuesFromArray(order, isDefaultMenuItem)
-- append the default items to the end
for _, cname in ipairs(defaultMenuItems) do
order[#order + 1] = cname
end
end
function Unit:showMenu(justLoad)
local controls, order, sub = self:onShowMenu(self.objects, self.branches)
self.menuLoadedAtLeastOnce = true
if justLoad then
return
end
order = order or {}
controls = controls or {}
self:addDefaultMenuItems(order, controls)
local Menu = require "Unit.Menu"
local menu = Menu(self, sub)
for _, cname in ipairs(order) do
if cname == "" or cname == "newRow" then
menu:startNewRow()
else
local control = controls[cname]
if control then
menu:addControl(control, control.isHeader)
end
end
end
menu:show()
end
function Unit:getPresetVersion(t)
local version = t.unitVersion
if type(version) == "number" then
return version
else
return 0
end
end
-----------------------------------------------
-- custom handlers
function Unit:onLoadGraph(channelCount)
end
function Unit:onLoadViews(objects, branches)
end
function Unit:onShowMenu(objects, branches)
end
function Unit:onLoadFinished()
end
-----------------------------------------------
function Unit:onGenerateTitle()
if self.suppressTitleGeneration then
return
end
local i = 1
local title = self.title or "Unknown"
local candidateName = title .. string.format(" #%d", i)
local root = self:getRootChain()
while root:findUnitByTitle(candidateName) do
i = i + 1
candidateName = title .. string.format(" #%d", i)
end
self:setTitle(candidateName)
end
function Unit:onRemove()
if self.leftOutputSource then
self.leftOutputSource:onDelete()
end
if self.rightOutputSource then
self.rightOutputSource:onDelete()
end
self.pUnit:disable()
-- self.pUnit:disconnectAllObjects()
for _, control in pairs(self.controls) do
self.chain:removeFromMuteGroup(control)
end
-- stop branches
for name, branch in pairs(self.branches) do
branch:stop()
branch:releaseResources()
branch:hide()
-- prevent stop from being called again
self.branches[name] = nil
end
Base.onRemove(self) -- this will notify all controls
end
return Unit