This repository has been archived by the owner on Aug 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
271 lines (224 loc) · 5.74 KB
/
index.js
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
var MidiStream = require('midi-stream')
var ObservStruct = require('observ-struct')
var ObservMidi = require('observ-midi')
var Observ = require('observ')
var computed = require('observ/computed')
var ArrayStack = require('./lib/array-stack')
var FlashArray = require('./lib/flash-array')
var AnyTrigger = require('./lib/on-trigger')
var LightStack = require('observ-midi/light-stack')
var MidiParam = require('./lib/midi-to-param')
var watchThrottle = require('throttle-observ/watch')
var throttle = require('throttle-observ')
module.exports = LaunchControl
function LaunchControl (context, state) {
var port = MidiStream('Launch Control', {
normalizeNotes: true
})
var onTrigger = AnyTrigger(state.items)
var obs = ObservStruct({})
// FIRST ROW OF KNOBS:
var knobs = ObservMidi(port, {
tempo: '184/21',
swing: '184/22',
param3: '184/23',
param4: '184/24',
param5: '184/25',
param6: '184/26',
param7: '184/27',
param8: '184/28'
})
watchThrottle(knobs.tempo, 20, function(value) {
if (value != null) {
state.tempo.set(value+60)
}
})
watchThrottle(knobs.swing, 20, function(value) {
if (value != null) {
state.swing.set(value / 128)
}
})
var params = [
MidiParam(context, 'Launch Control > 3', throttle(knobs.param3)),
MidiParam(context, 'Launch Control > 4', throttle(knobs.param4)),
MidiParam(context, 'Launch Control > 5', throttle(knobs.param5)),
MidiParam(context, 'Launch Control > 6', throttle(knobs.param6)),
MidiParam(context, 'Launch Control > 7', throttle(knobs.param7)),
MidiParam(context, 'Launch Control > 8', throttle(knobs.param8)),
]
params.forEach(function(param) {
context.paramLookup.put(param.id(), param)
})
//////
// SECOND ROW OF KNOBS:
var volumes = ObservMidi(port, [
'184/41',
'184/42',
'184/43',
'184/44',
'184/45',
'184/46',
'184/47',
])
volumes(function (values) {
values.forEach(function (val, i) {
var item = state.items.get(i)
if (item && item.node.output) {
if (val == null) {
val = 64
}
item.node.output.gain.value = (val / 64)
}
})
})
////////
// CONTROL BUTTONS:
var offset = Observ(0)
var controlButtons = LightStack(port, {
clearOthers: '184/115',
suppressOthers: '184/114',
tap: '152/28',
nudgeLeft: '184/116',
nudgeRight: '184/117'
})
controlButtons.tap.light(light(0, 1))
controlButtons.clearOthers.light(0)
controlButtons.tap(function (value) {
if (value) {
controlButtons.tap.flash(light(0,2))
context.actions.tapTempo()
}
})
controlButtons.nudgeLeft(function (value) {
if (value) {
controlButtons.nudgeLeft.turnOff = controlButtons.nudgeLeft.light(127)
context.speed.set(0.95)
} else {
controlButtons.nudgeLeft.turnOff()
context.speed.set(1)
}
})
controlButtons.nudgeRight(function (value) {
if (value) {
controlButtons.nudgeRight.turnOff = controlButtons.nudgeRight.light(127)
context.speed.set(1.05)
} else {
controlButtons.nudgeRight.turnOff()
context.speed.set(1)
}
})
controlButtons.clearOthers(function (value) {
if (value) {
suppressOthers(true)
}
})
var lastSuppress = null
var turnOffSuppressLight = null
controlButtons.suppressOthers(function (value) {
if (value) {
lastSuppress = suppressOthers()
turnOffSuppressLight = this.light(127)
} else if (lastSuppress) {
lastSuppress()
turnOffSuppressLight()
lastSuppress = null
}
})
///
// SELECT BUTTONS:
var selectedButton = null
var buttonBase = computed([offset, state.items, state.selected], function(offset, items, selected) {
var result = []
for (var i=0;i<8;i++) {
var item = state.items.get(i)
if (item) {
if (item.path === selected) {
result.push(light(2, 3))
selectedButton = i
} else {
result.push(light(1, 0))
}
} else {
result.push(0)
}
}
return result
})
var buttonFlash = FlashArray()
onTrigger(function(i) {
if (i === selectedButton) {
buttonFlash.flash(i, light(3, 3), 40)
} else {
buttonFlash.flash(i, light(3, 0), 40)
controlButtons.clearOthers.flash(127, 100)
}
})
var buttons = ObservMidi(port, [
'152/9',
'152/10',
'152/11',
'152/12',
'152/25',
'152/26',
'152/27',
], ArrayStack([
buttonBase,
buttonFlash
]))
buttons(function (values) {
var result = null
values.forEach(function(val, i) {
if (val) {
result = i
}
})
if (result != null) {
var item = state.items.get(result)
if (item) {
state.selected.set(item.path)
}
}
})
return obs
// scoped
function suppressOthers (flatten) {
var releases = []
state.items.forEach(function (item) {
if (item.path !== state.selected()) {
if (item.node && item.node.controllers) {
item.node.controllers.forEach(function (controller) {
if (controller.looper) {
releases.push(controller.looper.transform(function (grid) {
grid.data = []
return grid
}))
if (flatten) {
controller.looper.flatten()
}
}
})
}
}
})
return function () {
releases.forEach(invoke)
}
}
}
function invoke (fn) {
fn()
}
function light(r, g, flag){
if (!r || r < 0) r = 0
if (r > 3) r = 3
if (!g || g < 0) g = 0
if (g > 3) g = 3
if (flag == 'flash') {
flag = 8
} else if (flag == 'buffer') {
flag = 0
} else {
flag = 12
}
return ((16 * g) + r) + flag
}