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 1
/
Copy pathindex.js
360 lines (294 loc) · 9.28 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
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
var LoopGrid = require('loop-grid')
var Looper = require('loop-grid/looper')
var computeTargets = require('loop-grid/compute-targets')
var computeFlags = require('loop-grid/compute-flags')
var Selector = require('loop-grid-selector')
var Holder = require('loop-grid-holder')
var Mover = require('loop-grid-mover')
var Repeater = require('loop-grid-repeater')
var Suppressor = require('loop-grid-suppressor')
var ObservVarhash = require('observ-varhash')
var ObservStruct = require('observ-struct')
var ObservMidi = require('observ-midi')
var ObservGridStack = require('observ-grid-stack')
var ObservGridGrabber = require('observ-grid/grabber')
var ObservMidiPort = require('midi-port-holder')
var MidiButtons = require('observ-midi/light-stack')
var watchButtons = require('./lib/watch-buttons.js')
var Observ = require('observ')
var ArrayGrid = require('array-grid')
var DittyGridStream = require('ditty-grid-stream')
var computedPortNames = require('midi-port-holder/computed-port-names')
var watch = require('observ/watch')
var mapWatchDiff = require('./lib/map-watch-diff-stack.js')
var mapGridValue = require('observ-grid/map-values')
var computeIndexesWhereContains = require('observ-grid/indexes-where-contains')
var stateLights = require('./state-lights.js')
var repeatStates = [2, 1, 2/3, 1/2, 1/3, 1/4, 1/6, 1/8]
module.exports = function(context){
var loopGrid = LoopGrid(context)
var looper = Looper(loopGrid)
var scheduler = context.scheduler
var gridMapping = getLaunchpadGridMapping()
loopGrid.shape.set(gridMapping.shape)
var shiftHeld = false
// controller midi port
var portHolder = ObservMidiPort()
var duplexPort = portHolder.stream
duplexPort.on('switch', turnOffAllLights)
duplexPort.on('switching', turnOffAllLights)
// extend loop-grid instance
var obs = ObservStruct({
port: portHolder,
loopLength: loopGrid.loopLength,
chunkPositions: ObservVarhash({})
})
obs.gridState = ObservStruct({
active: loopGrid.active,
playing: loopGrid.playing,
recording: looper.recording,
triggers: loopGrid.grid
})
watch(looper, loopGrid.loops.set)
obs.context = context
obs.playback = loopGrid
obs.looper = looper
obs.portChoices = computedPortNames()
obs.repeatLength = Observ(2)
var flags = computeFlags(context.chunkLookup, obs.chunkPositions, loopGrid.shape)
watch( // compute targets from chunks
computeTargets(context.chunkLookup, obs.chunkPositions, loopGrid.shape),
loopGrid.targets.set
)
// grab the midi for the current port
obs.grabInput = function(){
portHolder.grab()
}
// loop transforms
var transforms = {
selector: Selector(gridMapping.shape, gridMapping.stride),
holder: Holder(looper.transform),
mover: Mover(looper.transform),
repeater: Repeater(looper.transform),
suppressor: Suppressor(looper.transform, gridMapping.shape, gridMapping.stride)
}
var outputLayers = ObservGridStack([
// recording
mapGridValue(looper.recording, stateLights.redLow),
// active
mapGridValue(loopGrid.active, stateLights.greenLow),
// selected
mapGridValue(transforms.selector, stateLights.green),
// suppressing
mapGridValue(transforms.suppressor, stateLights.red),
// playing
mapGridValue(loopGrid.playing, stateLights.amber)
])
var controllerGrid = ObservMidi(duplexPort, gridMapping, outputLayers)
var inputGrabber = ObservGridGrabber(controllerGrid)
var noRepeat = computeIndexesWhereContains(flags, 'noRepeat')
var grabInputExcludeNoRepeat = inputGrabber.bind(this, {exclude: noRepeat})
// trigger notes at bottom of input stack
var output = DittyGridStream(inputGrabber, loopGrid.grid, context.scheduler)
output.on('data', loopGrid.triggerEvent)
// midi button mapping
var buttons = MidiButtons(duplexPort, {
store: '176/104',
flatten: '176/105',
undo: '176/106',
redo: '176/107',
hold: '176/108',
suppress: '176/109',
snap2: '176/110',
select: '176/111'
})
var releaseLoopLengthLights = []
watchButtons(buttons, {
store: function(value){
if (value){
this.flash(stateLights.green)
looper.store()
}
},
flatten: function(value){
if (value){
if (looper.isTransforming()){
looper.flatten()
transforms.selector.stop()
this.flash(stateLights.green, 100)
} else {
this.flash(stateLights.red, 100)
transforms.suppressor.start(transforms.selector.selectedIndexes())
looper.flatten()
transforms.suppressor.stop()
transforms.selector.stop()
}
}
},
undo: function(value){
if (value){
if (shiftHeld){ // halve loopLength
var current = obs.loopLength() || 1
obs.loopLength.set(current/2)
this.flash(stateLights.green, 100)
} else {
looper.undo()
this.flash(stateLights.red, 100)
buttons.store.flash(stateLights.red)
}
}
},
redo: function(value){
if (value){
if (shiftHeld){ // double loopLength
var current = obs.loopLength() || 1
obs.loopLength.set(current*2)
this.flash(stateLights.green, 100)
} else {
looper.redo()
this.flash(stateLights.red, 100)
buttons.store.flash(stateLights.red)
}
}
},
hold: function(value){
if (value){
var turnOffLight = this.light(stateLights.yellow)
transforms.holder.start(
scheduler.getCurrentPosition(),
transforms.selector.selectedIndexes(),
turnOffLight
)
} else {
transforms.holder.stop()
}
},
suppress: function(value){
if (value){
var turnOffLight = this.light(stateLights.red)
transforms.suppressor.start(transforms.selector.selectedIndexes(), turnOffLight)
} else {
transforms.suppressor.stop()
}
},
select: function(value){
if (value){
var turnOffLight = this.light(stateLights.green)
transforms.selector.start(inputGrabber, function done(){
transforms.mover.stop()
transforms.selector.clear()
turnOffLight()
})
} else {
if (transforms.selector.selectedIndexes().length){
transforms.mover.start(inputGrabber, transforms.selector.selectedIndexes())
} else {
transforms.selector.stop()
}
}
}
})
// shift button (share select button)
watch(buttons.select, function(value){
if (value){
shiftHeld = true
// turn on loop length lights
releaseLoopLengthLights.push(
buttons.undo.light(stateLights.greenLow),
buttons.redo.light(stateLights.greenLow)
)
} else {
shiftHeld = false
// turn off loop length lights
while (releaseLoopLengthLights.length){
releaseLoopLengthLights.pop()()
}
}
})
// light up undo buttons by default
buttons.undo.light(stateLights.redLow)
buttons.redo.light(stateLights.redLow)
buttons.store.light(stateLights.amberLow)
// light up store button when transforming (flatten mode)
var releaseFlattenLight = null
watch(looper.transforms, function(values){
if (values.length && !releaseFlattenLight){
releaseFlattenLight = buttons.flatten.light(stateLights.greenLow)
} else if (releaseFlattenLight){
releaseFlattenLight()
releaseFlattenLight = null
}
})
var repeatButtons = MidiButtons(duplexPort, {
0: '144/8',
1: '144/24',
2: '144/40',
3: '144/56',
4: '144/72',
5: '144/88',
6: '144/104',
7: '144/120'
})
// repeater
var releaseRepeatLight = null
mapWatchDiff(repeatStates, repeatButtons, obs.repeatLength.set)
watch(obs.repeatLength, function(value){
var button = repeatButtons[repeatStates.indexOf(value)]
if (button){
if (releaseRepeatLight) releaseRepeatLight()
releaseRepeatLight = button.light(stateLights.amberLow)
}
transforms.holder.setLength(value)
if (value < 2){
transforms.repeater.start(grabInputExcludeNoRepeat, value)
} else {
transforms.repeater.stop()
}
})
// visual metronome / loop position
var releaseBeatLight = null
var currentBeatLight = null
var currentBeat = null
watch(loopGrid.loopPosition, function(value){
var beat = Math.floor(value[0])
var index = Math.floor(value[0] / value[1] * 8)
var button = repeatButtons[index]
if (index != currentBeatLight){
if (button){
releaseBeatLight&&releaseBeatLight()
releaseBeatLight = button.light(stateLights.greenLow, 0)
}
currentBeatLight = index
}
if (beat != currentBeat){
button.flash(stateLights.green)
currentBeat = beat
}
})
// cleanup / disconnect from keyboard on destroy
obs.destroy = function(){
turnOffAllLights()
portHolder.destroy()
output.destroy()
loopGrid.destroy()
}
return obs
// scoped
function turnOffAllLights(){
duplexPort.write([176, 0, 0])
}
}
function round(value, dp){
var pow = Math.pow(10, dp || 0)
return Math.round(value * pow) / pow
}
function getLaunchpadGridMapping(){
var result = []
for (var r=0;r<8;r++){
for (var c=0;c<8;c++){
var noteId = (r*16) + (c % 8)
result.push('144/' + noteId)
}
}
return ArrayGrid(result, [8, 8])
}