-
Notifications
You must be signed in to change notification settings - Fork 3
/
Logics.lua
381 lines (335 loc) · 11.7 KB
/
Logics.lua
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
local app = app
local Class = require "Base.Class"
local Unit = require "Unit"
local ModeSelect = require "Unit.ViewControl.OptionControl"
local InputGate = require "Unit.ViewControl.InputGate"
local OutputScope = require "Unit.ViewControl.OutputScope"
local GainBias = require "Unit.ViewControl.GainBias"
local BranchMeter = require "Unit.ViewControl.BranchMeter"
local Task = require "Unit.MenuControl.Task"
local MenuHeader = require "Unit.MenuControl.Header"
local GainBias = require "Unit.ViewControl.GainBias"
local Encoder = require "Encoder"
local ply = app.SECTION_PLY
local Logics = Class{}
Logics:include(Unit)
function Logics:init(args)
args.title = "Logics"
args.mnemonic = "Lg"
Unit.init(self,args)
end
function Logics:onLoadGraph(channelCount)
-- create input circuit objects
local a = self:addObject("a",app.ConstantGain())
local b = self:addObject("b",app.ConstantGain())
local sum = self:addObject("sum",app.Sum())
a:hardSet("Gain",1.0)
b:hardSet("Gain",1.0)
a:setClampInDecibels(-59.9)
b:setClampInDecibels(-59.9)
self:addMonoBranch("inA", a, "In", a,"Out")
self:addMonoBranch("inB", b, "In", b,"Out")
local compA = self:addObject("compA",app.Comparator())
local compB = self:addObject("compB",app.Comparator())
compA:hardSet("Hysteresis",0.0)
compB:hardSet("Hysteresis",0.0)
compA:setGateMode()
compB:setGateMode()
local modA = self:addObject("modA",app.Multiply())
local modB = self:addObject("modB",app.Multiply())
-- create control objects
local threshold = self:addObject("threshold",app.ParameterAdapter())
local thresholdOutlet = self:addObject("thresholdOutlet",app.Constant())
tie(compA,"Threshold",threshold,"Out")
tie(compB,"Threshold",threshold,"Out")
tie(thresholdOutlet,"Value",threshold,"Out")
self:addMonoBranch("threshold",threshold,"In",threshold,"Out")
local truth = self:addObject("truth",app.GainBias())
local falsth = self:addObject("falsth",app.GainBias())
local truthRange = self:addObject("truthRange",app.MinMax())
local falsthRange = self:addObject("falsthRange",app.MinMax())
self:addMonoBranch("true",truth,"In",truth,"Out")
self:addMonoBranch("false",falsth,"In",falsth,"Out")
-- create AND logic objects
local ANDSum1 = self:addObject("ANDSum1",app.Sum())
local ANDSum2 = self:addObject("ANDSum2",app.Sum())
local compAND = self:addObject("compAND",app.Comparator())
compAND:setGateMode()
compAND:hardSet("Hysteresis",0.0)
local ANDThreholdAdapter = self:addObject("ANDThreholdAdapter",app.ParameterAdapter())
ANDThreholdAdapter:hardSet("Gain",1.0)
-- create OR logic objects
local ORSum = self:addObject("ORSum",app.Sum())
local compOR = self:addObject("compOR",app.Comparator())
compOR:setGateMode()
compOR:hardSet("Hysteresis",0.0)
-- create XOR logic objects
local compXORA = self:addObject("compXORA",app.Comparator())
local compXORB = self:addObject("compXORB",app.Comparator())
local XORSum1 = self:addObject("XORSum1",app.Sum())
local XORSum2 = self:addObject("XORSum2",app.Sum())
local XORSum3 = self:addObject("XORSum3",app.Sum())
local XORMult1 = self:addObject("XORMult1",app.Multiply())
local XORMult2 = self:addObject("XORMult2",app.Multiply())
local XORConst = self:addObject("XORConst",app.Constant())
XORConst:hardSet("Value",-1.0)
local compXOR = self:addObject("compXOR",app.Comparator())
compXOR:setGateMode()
compXOR:hardSet("Hysteresis",0.0)
compXORA:setGateMode()
compXORA:hardSet("Hysteresis",0.0)
compXORB:setGateMode()
compXORB:hardSet("Hysteresis",0.0)
-- create selection circuit objects
local selectMult1 = self:addObject("selectMult1",app.Multiply())
local selectMult2 = self:addObject("selectMult2",app.Multiply())
local selectMult3 = self:addObject("selectMult3",app.Multiply())
local selectMult4 = self:addObject("selectMult4",app.Multiply())
local ANDSelectorConst = self:addObject("ANDSelectorConst",app.Constant())
local ORSelectorConst = self:addObject("ORSelectorConst",app.Constant())
local XORSelectorConst = self:addObject("XORSelectorConst",app.Constant())
local NOTInverterConst = self:addObject("NOTInverterConst",app.Constant())
ANDSelectorConst:hardSet("Value",1.0)
ORSelectorConst:hardSet("Value",0.0)
XORSelectorConst:hardSet("Value",0.0)
NOTInverterConst:hardSet("Value",-1.0)
local selectMix1 = self:addObject("selectMix1",app.Sum())
local selectMix2 = self:addObject("selectMix2",app.Sum())
local selectMix3 = self:addObject("selectMix3",app.Sum())
local NOTOffsetConst = self:addObject("NOTOffsetConst",app.Constant())
NOTOffsetConst:hardSet("Value",0.0)
-- create output objects
local outMult1 = self:addObject("outMult1",app.Multiply())
local outMult2 = self:addObject("outMult2",app.Multiply())
local outMult3 = self:addObject("outMult3",app.Multiply())
local outSum1 = self:addObject("outSum1",app.Sum())
local outSum2 = self:addObject("outSum2",app.Sum())
local negOne = self:addObject("negOne",app.Constant())
local one = self:addObject("one",app.Constant())
negOne:hardSet("Value",-1.0)
one:hardSet("Value",1.0)
-- wire input circuit
connect(a,"Out",compA,"In")
connect(b,"Out",compB,"In")
connect(compA,"Out",modA,"Left")
connect(compB,"Out",modB,"Left")
connect(thresholdOutlet,"Out",modA,"Right")
connect(thresholdOutlet,"Out",modB,"Right")
-- wire AND logic
connect(modA,"Out",ANDSum1,"Left")
connect(modB,"Out",ANDSum1,"Right")
connect(thresholdOutlet,"Out",ANDSum2,"Left")
connect(thresholdOutlet,"Out",ANDSum2,"Right")
connect(ANDSum1,"Out",compAND,"In")
tie(compAND,"Threshold",ANDThreholdAdapter,"Out")
connect(ANDSum2,"Out",ANDThreholdAdapter,"In")
-- compAND to selection circuit input
--wire OR logic
connect(modA,"Out",ORSum,"Left")
connect(modB,"Out",ORSum,"Right")
connect(ORSum,"Out",compOR,"In")
tie(compOR,"Threshold",threshold,"Out")
-- compOR to selection circuit input
--wire XOR logic
connect(compOR,"Out",XORSum1,"Left")
connect(compAND,"Out",XORMult1,"Left")
connect(XORConst,"Out",XORMult1,"Right")
connect(XORMult1,"Out",XORSum1,"Right")
-- XORSum1 to selection circuit
--wire selection circuit
connect(compAND,"Out",selectMult1,"Left")
connect(ANDSelectorConst,"Out",selectMult1,"Right")
connect(compOR,"Out",selectMult2,"Left")
connect(ORSelectorConst,"Out",selectMult2,"Right")
connect(XORSum1,"Out",selectMult3,"Left")
connect(XORSelectorConst,"Out",selectMult3,"Right")
connect(selectMult1,"Out",selectMix1,"Left")
connect(selectMult2,"Out",selectMix1,"Right")
connect(selectMix1,"Out",selectMix2,"Left")
connect(selectMult3,"Out",selectMix2,"Right")
connect(selectMix2,"Out",selectMult4,"Left")
connect(NOTInverterConst,"Out",selectMult4,"Right")
connect(selectMult4,"Out",selectMix3,"Left")
connect(NOTOffsetConst,"Out",selectMix3,"Right")
--selectMix3 to output circuit
--connect output circuit
connect(truth,"Out",outMult1,"Left")
connect(falsth,"Out",outMult2,"Left")
connect(truth,"Out",truthRange,"In")
connect(falsth,"Out",falsthRange,"In")
connect(selectMix3,"Out",outMult3,"Left")
connect(selectMix3,"Out",outMult1,"Right")
connect(negOne,"Out",outMult3,"Right")
connect(outMult3,"Out",outSum1,"Left")
connect(one,"Out",outSum1,"Right")
connect(outSum1,"Out",outMult2,"Right")
connect(outMult1,"Out",outSum2,"Left")
connect(outMult2,"Out",outSum2,"Right")
connect(outSum2,"Out",self,"Out1")
-- connect(XORSum1,"Out",self,"Out1")
if channelCount > 1 then
connect(outSum2,"Out",self,"Out2")
end
end
local views = {
expanded = {"a","b","threhold","truth","falseth"},
collapsed = {},
input = {}
}
function Logics:onLoadViews(objects,branches)
local controls = {}
controls.a = BranchMeter {
button = "a",
branch = branches.inA,
faderParam = objects.a:getParameter("Gain")
}
controls.b = BranchMeter {
button = "b",
branch = branches.inB,
faderParam = objects.b:getParameter("Gain")
}
controls.threhold = GainBias {
button = "threshold",
description = "Truth Threhold",
branch = branches.threshold,
gainbias = objects.threshold,
range = objects.threshold,
biasMap = Encoder.getMap("default"),
initialBias = 0.1,
}
controls.truth = GainBias {
button = "true",
description = "True Output",
branch = branches["true"],
gainbias = objects.truth,
range = objects.truthRange,
biasMap = Encoder.getMap("default"),
initialBias = 1.0,
}
controls.falseth = GainBias {
button = "false",
description = "False Output",
branch = branches["false"],
gainbias = objects.falsth,
range = objects.falsthRange,
biasMap = Encoder.getMap("default"),
initialBias = 0.0,
}
self:addToMuteGroup(controls.a)
self:addToMuteGroup(controls.b)
return controls, views
end
local menu = {
"setHeader",
"opAND",
"opOR",
"opXOR",
"opNAND",
"opNOR",
"opXNOR",
"infoHeader",
"rename",
"load",
"save",
"edit"
}
local op = "AND"
function Logics:setOp(op)
local objects = self.objects
self.op = op
if op=="AND" then
objects.ANDSelectorConst:hardSet("Value",1.0)
objects.ORSelectorConst:hardSet("Value",0.0)
objects.XORSelectorConst:hardSet("Value",0.0)
objects.NOTInverterConst:hardSet("Value",1.0)
objects.NOTOffsetConst:hardSet("Value",0.0)
elseif op=="OR" then
objects.ANDSelectorConst:hardSet("Value",0.0)
objects.ORSelectorConst:hardSet("Value",1.0)
objects.XORSelectorConst:hardSet("Value",0.0)
objects.NOTInverterConst:hardSet("Value",1.0)
objects.NOTOffsetConst:hardSet("Value",0.0)
elseif op=="XOR" then
objects.ANDSelectorConst:hardSet("Value",0.0)
objects.ORSelectorConst:hardSet("Value",0.0)
objects.XORSelectorConst:hardSet("Value",1.0)
objects.NOTInverterConst:hardSet("Value",1.0)
objects.NOTOffsetConst:hardSet("Value",0.0)
elseif op=="NAND" then
objects.ANDSelectorConst:hardSet("Value",1.0)
objects.ORSelectorConst:hardSet("Value",0.0)
objects.XORSelectorConst:hardSet("Value",0.0)
objects.NOTInverterConst:hardSet("Value",-1.0)
objects.NOTOffsetConst:hardSet("Value",1.0)
elseif op=="NOR" then
objects.ANDSelectorConst:hardSet("Value",0.0)
objects.ORSelectorConst:hardSet("Value",1.0)
objects.XORSelectorConst:hardSet("Value",0.0)
objects.NOTInverterConst:hardSet("Value",-1.0)
objects.NOTOffsetConst:hardSet("Value",1.0)
elseif op=="XNOR" then
objects.ANDSelectorConst:hardSet("Value",0.0)
objects.ORSelectorConst:hardSet("Value",0.0)
objects.XORSelectorConst:hardSet("Value",1.0)
objects.NOTInverterConst:hardSet("Value",-1.0)
objects.NOTOffsetConst:hardSet("Value",1.0)
end
end
function Logics:onShowMenu(objects,branches)
local controls = {}
controls.setHeader = MenuHeader {
description = string.format("Current op is: %s.",self.op)
}
controls.opAND = Task {
description = "AND",
task = function()
self:setOp("AND")
end
}
controls.opOR = Task {
description = "OR",
task = function()
self:setOp("OR")
end
}
controls.opXOR = Task {
description = "XOR",
task = function()
self:setOp("XOR")
end
}
controls.opNAND = Task {
description = "NAND",
task = function()
self:setOp("NAND")
end
}
controls.opNOR = Task {
description = "NOR",
task = function()
self:setOp("NOR")
end
}
controls.opXNOR = Task {
description = "XNOR",
task = function()
self:setOp("XNOR")
end
}
return controls, menu
end
function Logics:onLoadFinished()
self:setOp("AND")
end
function Logics:serialize()
local t = Unit.serialize(self)
t.logicOp = self.op
return t
end
function Logics:deserialize(t)
Unit.deserialize(self,t)
if t.logicOp then
self:setOp(t.logicOp)
end
end
return Logics