-
Notifications
You must be signed in to change notification settings - Fork 3
/
MotionSensor.lua
149 lines (130 loc) · 4.34 KB
/
MotionSensor.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
-- GLOBALS: app, os, verboseLevel, connect, tie
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 Utils = require "Utils"
local Encoder = require "Encoder"
local ply = app.SECTION_PLY
local MotionSensor = Class{}
MotionSensor:include(Unit)
function MotionSensor:init(args)
args.title = "Motion Sensor"
args.mnemonic = "MS"
Unit.init(self,args)
end
function MotionSensor:onLoadGraph()
-- create objects
local compare = self:createObject("Comparator","compare")
local invert = self:createObject("Multiply","invert")
local negOne = self:createObject("Constant","negOne")
local sum = self:createObject("Sum","sum")
local rectify = self:createObject("Rectify","rectify")
local delay = self:createObject("Delay","delay",1)
local env = self:createObject("EnvelopeFollower","env")
local release = self:createObject("ParameterAdapter","release")
local attack = self:createObject("ParameterAdapter","attack")
local pregain = self:createObject("Multiply","pregain")
local preGainAmt = self:createObject("Constant","preGainAmt")
local gain = self:createObject("Multiply","gain")
local level = self:createObject("GainBias","level")
local levelRange = self:createObject("MinMax","levelRange")
-- set parameters
compare:setGateMode()
compare:hardSet("Threshold",0.010)
compare:hardSet("Hysteresis",0.00)
negOne:hardSet("Value",-1.0)
rectify:optionSet("Type",3) --full rectification
self:setMaxDelayTime(0.1)
delay:hardSet("Left Delay",0.001)
preGainAmt:hardSet("Value",4.0)
-- connect inputs/outputs
connect(self,"In1",sum,"Left")
connect(self,"In1",invert,"Left")
connect(negOne,"Out",invert,"Right")
connect(invert,"Out",delay,"Left In")
connect(delay,"Left Out",sum,"Right")
connect(sum,"Out",rectify,"In")
connect(rectify,"Out",env,"In")
connect(level,"Out",levelRange,"In")
connect(level,"Out",gain,"Left")
connect(env,"Out", pregain,"Left")
connect(preGainAmt,"Out",pregain,"Right")
connect(pregain,"Out",gain,"Right")
connect(gain,"Out",compare,"In")
connect(compare,"Out",self,"Out1")
-- tie parameters
tie(env,"Release Time",release,"Out")
tie(env,"Attack Time",attack,"Out")
-- register exported ports
self:createMonoBranch("release",release,"In",release,"Out")
self:createMonoBranch("attack",attack,"In",attack,"Out")
self:createMonoBranch("level",level,"In",level,"Out")
end
local views = {
expanded = {"input","level","attack","release","mode"},
collapsed = {},
input = {"scope","input"}
}
local function linMap(min,max,superCoarse,coarse,fine,superFine)
local map = app.LinearDialMap(min,max)
map:setSteps(superCoarse,coarse,fine,superFine)
return map
end
local sensMap = linMap(0,100,1,0.1,0.01,0.001)
function MotionSensor:onLoadViews(objects,branches)
local controls = {}
controls.input = InputGate {
button = "input",
description = "Unit Input",
unit = self,
comparator = objects.compare,
}
controls.level = GainBias {
button = "sens",
branch = branches.level,
description = "Level",
gainbias = objects.level,
range = objects.levelRange,
biasMap = sensMap,
biasUnits = app.unitNone,
initialBias = 50.0,
gainMap = Encoder.getMap("gain"),
}
controls.attack = GainBias {
button = "attack",
description = "Attack Time",
branch = branches.attack,
gainbias = objects.attack,
range = objects.attack,
biasMap = Encoder.getMap("unit"),
biasUnits = app.unitSecs,
initialBias = 0.5
}
controls.release = GainBias {
button = "release",
description = "Release Time",
branch = branches.release,
gainbias = objects.release,
range = objects.release,
biasMap = Encoder.getMap("unit"),
biasUnits = app.unitSecs,
initialBias = 0.5
}
controls.mode = ModeSelect {
button = "o",
description = "Type",
option = objects.compare:getOption("Mode"),
choices = {"toggle","gate","trigger"},
muteOnChange = true
}
return controls, views
end
function MotionSensor:setMaxDelayTime(secs)
local requested = Utils.round(secs,1)
local allocated = self.objects.delay:allocateTimeUpTo(requested)
end
return MotionSensor