-
Notifications
You must be signed in to change notification settings - Fork 4
/
StepView.js
628 lines (554 loc) · 14.9 KB
/
StepView.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
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
/**
* @param {Clip} clip
* @param {NoteView2} noteview
*/
function StepGrid(stepIndex) {
var states = [];
(function () {
for (var i = 0; i < 8; i++) {
states.push(0);
}
})();
this.states = states;
this.index = function () {
return stepIndex;
};
}
function StepNote(rowindex) {
this.index = function () {
return rowindex;
};
this.isbaseNote = false;
this.notevalue = 0;
}
/**
* @param {Clip} clip
* @param {NoteView2} noteview
*
*/
function StepView(clip, noteview, exitHandler) {
var active = false;
var baseColor = 0;
var loopLen = 0;
var playingPos = -1;
/** @type {StepPositionView} */
var stepLenView = null;
var gateLength = 0.99;
var exitHandler = null;
var baseVelocity = 120;
var monoMode = false;
var straightMap = new StepMap(4, [4.0, 2.0, 1.0, 0.5, 0.25, 0.125], ["Full", "1/2", "1/4", "1/8", "1/16", "1/32"]);
var tripletMap = new StepMap(2, [4 / 3, 2 / 3, 1 / 3, 1 / 6], ["1/2T", "1/4T", "1/8T", "1/16T"], [1.0, 0.5, 0.25, 0.125], 6);
var str2trip = [0, 0, 1, 2, 3, 3];
var trip2str = [1, 2, 3, 4];
var gridMap = straightMap;
// Represents State of Steps in current Grid
var steps = [];
// Represents Mapping of Notes to the 8 rows
var notegrid = [];
// Current Complete Scale Grid
var scalegrid = [];
// Current Base Note Grid
var basegrid = [];
// Maps Note value to Grid Index
var noteToIndex = {};
// Remembers collumn note held for row in index
var lastHoldDown = [];
var fullNoteMapping = [];
(function () {
for (var i = 0; i < 8; i++) {
steps.push(new StepGrid(i));
notegrid.push(new StepNote(i));
lastHoldDown.push(-1);
var notesArray = [];
for (var j = 0; j < 128; j++) {
notesArray.push(0);
}
fullNoteMapping.push(notesArray);
}
})();
var notePos = 60;
var scale = noteview.getScale();
var baseNote = noteview.getBaseNote() % 12;
var scalegridindex = -1;
var offset = 0;
var headInView = false;
var loopSteps = 0;
noteview.setScaleChangedCallback(function (newscale) {
scale = newscale;
baseNote = noteview.getBaseNote() % 12;
showCurrentScale();
setScale(scale);
refreshScaleIndex();
resendColors();
});
function notePosToBaseNote(currentNotePos, pBaseNote) {
var newPos = currentNotePos;
while (newPos % 12 !== pBaseNote) {
newPos++;
}
return newPos;
}
noteview.setBaseVelocityChangedCallback(function (newvelocity) {
baseVelocity = newvelocity;
});
noteview.setBaseNoteChangedCallback(function (/*baseNoteIndex*/) {
scale = noteview.getScale();
baseNote = noteview.getBaseNote() % 12;
notePos = scalegrid[scalegridindex];
scalegridindex = scalegridindex - (scalegridindex % scale.notes.length);
notePos = notePosToBaseNote(notePos, baseNote);
setScale(scale);
refreshScaleIndex();
resendColors();
showCurrentScale();
});
this.setExitHandler = function(callback) {
exitHandler = callback;
};
clip.addStepDataObserver(function (x, y, state) {
fullNoteMapping[x][y] = state;
if (y in noteToIndex) {
var noteindex = noteToIndex[y];
steps[x].states[noteindex] = state;
updateGrid(x, noteindex);
}
});
clip.addPlayingStepObserver(function (playpos) {
if (active) {
setPlayPos(playpos);
stepLenView.setPlayPos(playpos);
}
});
clip.getLoopLength().addRawValueObserver(function (time) {
loopLength = time;
setLoopLen(time);
stepLenView.setLoopLen(time);
correctStepOffsetPosition();
});
var track = clip.getTrack();
track.exists().addValueObserver(function (value) {
if (!active) { return; }
if (!value) {
if(exitHandler)
exitHandler();
}
});
track.addTrackTypeObserver(16, "EMPTY", function (type) {
if (!active) { return; }
if (type !== "Instrument") {
if(exitHandler)
exitHandler();
}
});
this.inMonoMode = function () {
return monoMode;
};
function resetHold() {
for (var i = 0; i < 8; i++) {
lastHoldDown[i] = -1;
}
}
this.notifyModifier = function (modifierState) {
if (modifierState === 3) {
// SHIFT + SELECT
this.scrollNoteIntoView();
}
if(modifierState === 4) {
clip.duplicate(); // Duplicate
} else if (modifierState === 5) {
clip.duplicateContent(); // Duplicate + Shift
}
};
this.handleLockButton = function (value) {
if (value === 0) {
return;
}
if (monoMode) {
monoMode = false;
modifiers.setLockButtonState(false);
host.showPopupNotification("Step Sequencer in Poly Mode");
} else {
monoMode = true;
modifiers.setLockButtonState(true);
host.showPopupNotification("Step Sequencer in Mono Mode");
}
};
this.receiveNote = function (on, note, velocity) {
};
this.modifyGrid = function (incValue, touchModifier) {
if (incValue === 0 && touchModifier) {
host.showPopupNotification("Piano Roll Step Sequencer Grid set to " + gridMap.info());
} else {
gridMap.change(incValue);
host.showPopupNotification("Piano Roll Step Sequencer Grid set to " + gridMap.info());
clip.setStepSize(gridMap.stepValue());
loopSteps = (loopLength / gridMap.stepValue());
stepLenView.setStepSize(gridMap.stepValue(), gridMap.nrOfSteps());
correctStepOffsetPosition();
resendColors();
}
};
this.pushAction = function (value) {
if (value === 0) {
return;
}
if (gridMap === tripletMap) {
gridMap = straightMap;
gridMap.setIndex(trip2str[tripletMap.index()]);
} else {
gridMap = tripletMap;
gridMap.setIndex(str2trip[straightMap.index()]);
}
host.showPopupNotification("Piano Roll Step Sequencer Grid set to " + gridMap.info());
clip.setStepSize(gridMap.stepValue());
loopSteps = (loopLength / gridMap.stepValue());
stepLenView.setStepSize(gridMap.stepValue(), gridMap.nrOfSteps());
resendColors();
correctStepOffsetPosition();
};
function setBaseColor(color) {
baseColor = color;
resendColors();
}
clip.setStepSize(gridMap.stepValue());
clip.addColorObserver(function (red, green, blue) {
var color = convertColor(red, green, blue);
setBaseColor(color);
stepLenView.setBaseColor(color);
globalClipView.clipColorChanged(color);
});
this.setStepLenView = function (view) {
stepLenView = view;
};
/**
* @returns {StepMap}
*/
this.gridMap = function () {
return gridMap;
};
function correctStepOffsetPosition() {
if (offset === 0) {
return;
}
var stepPosition = offset * gridMap.stepValue();
if (stepPosition >= loopLength) {
stepLenView.select(stepLenView.nrOfblocks() - 1, true);
}
}
var updateGrid = function (pos, noteindex) {
if (!active) {
return;
}
var state = steps[pos].states[noteindex];
color = 0;
if (pos + offset >= loopSteps || pos >= gridMap.nrOfSteps()) {
color = 0;
} else if (pos === (playingPos - offset)) {
color = 70;
} else if (state === 0) {
color = notegrid[noteindex].isbaseNote ? 69 : 0;
} else if (state === 1) {
color = baseColor;
} else {
color = baseColor + 2;
}
controls.buttonMatrix.sendValue((7 - noteindex) * 8 + pos, color, true, true);
};
function refreshScaleIndex() {
noteToIndex = {};
var nind = 0;
for (nind = 0; nind < 8; nind++) {
notegrid[nind].isbaseNote = basegrid[scalegridindex + nind];
notegrid[nind].notevalue = scalegrid[scalegridindex + nind];
noteToIndex[scalegrid[scalegridindex + nind]] = nind;
}
for (step = 0; step < 8; step++) {
for (nind = 0; nind < 8; nind++) {
steps[step].states[nind] = fullNoteMapping[step][notegrid[nind].notevalue];
}
}
resetHold();
}
function setScale(scale) {
scalegrid = [];
basegrid = [];
scalegridindex = -1;
var octave = -1;
var notevalue = 0;
var i = 0;
while (notevalue < 127) {
notevalue = scale.notes[i] + 12 * octave + baseNote;
if (notevalue >= 0 && notevalue < 128) {
scalegrid.push(notevalue);
basegrid.push(i === 0 ? true : false);
if (notevalue === notePos) {
scalegridindex = scalegrid.length - 1;
} else if (scalegridindex === -1 && notePos < notevalue) {
scalegridindex = scalegrid.length - 1;
notePos = scalegrid[Math.max(0, scalegrid.length - 2)];
}
}
if (notevalue > 127) {
break;
}
i++;
if (i >= scale.notes.length) {
i = 0;
octave++;
}
}
}
this.setOffset = function (offsetPos, macroOffset) {
offset = offsetPos * gridMap.nrOfSteps() + macroOffset * gridMap.nrOfSteps() * 8;
clip.scrollToStep(offset);
resendColors();
};
function updatePlayPos(previous, newpos) {
var pos = 0;
var row = 0;
if (previous >= 0) {
pos = previous % gridMap.nrOfSteps();
for (row = 0; row < 8; row++) {
updateGrid(pos, row);
}
}
if (newpos >= 0) {
pos = newpos % gridMap.nrOfSteps();
for (row = 0; row < 8; row++) {
updateGrid(pos, row);
}
}
}
function setPlayPos(pos) {
if (!active) {
return;
}
var prev = playingPos;
playingPos = pos;
if (pos >= offset && pos < (offset + gridMap.nrOfSteps())) {
updatePlayPos(prev, playingPos);
headInView = true;
} else if (headInView) {
updatePlayPos(prev, -1);
headInView = false;
}
}
function setLoopLen(time) {
loopLen = time;
var maxOffset = Math.max(0, (loopLen / gridMap.baseStepValue()) - gridMap.nrOfSteps());
loopSteps = (loopLength / gridMap.stepValue());
prev = -1;
if (offset > maxOffset) {
offset = maxOffset;
clip.scrollToStep(offset);
}
resendColors();
}
this.update = function () {
resendColors();
};
var resendColors = function () {
if (!active) {
return;
}
for (var row = 0; row < 8; row++) {
for (var col = 0; col < 8; col++) {
updateGrid(row, col);
}
}
};
function showCurrentScale() {
if (active) {
host.showPopupNotification("Step Editor " + NoteStr[notePos % 12] + " " + (Math.floor(notePos / 12) - 2) + " Scale: " + NoteStr[baseNote % 12] + " " + scale.name);
}
}
this.enter = function () {
active = true;
scale = noteview.getScale();
baseNote = noteview.getBaseNote() % 12;
stepLenView.setStepSize(gridMap.stepValue(), gridMap.nrOfSteps());
showCurrentScale();
setScale(scale);
refreshScaleIndex();
correctStepOffsetPosition();
resendColors();
};
this.exit = function () {
active = false;
};
this.navigate = function (direction) {
switch (direction) {
case DirectionPad.TOP:
if (modifiers.isShiftDown()) {
this.scrollNoteUp();
} else {
this.scrollOctaveUp();
}
break;
case DirectionPad.DOWN:
if (modifiers.isShiftDown()) {
this.scrollNoteDown();
} else {
this.scrollOctaveDown();
}
break;
case DirectionPad.LEFT:
if (modifiers.isSelectDown()) {
this.scrollBaseScaleDown();
} else if (modifiers.isShiftDown()) {
this.scrollScaleDown();
} else {
stepLenView.stepLeft();
}
break;
case DirectionPad.RIGHT:
if (modifiers.isSelectDown()) {
this.scrollBaseScaleUp();
} else if (modifiers.isShiftDown()) {
this.scrollScaleUp();
} else {
stepLenView.stepRight();
}
break;
}
};
this.scrollOctaveUp = function () {
if (scalegridindex + scale.notes.length < scalegrid.length - 8) {
scalegridindex += scale.notes.length;
notePos = scalegrid[scalegridindex];
showCurrentScale();
refreshScaleIndex();
resendColors();
}
};
this.scrollOctaveDown = function () {
if (scalegridindex - scale.notes.length > 0) {
scalegridindex -= scale.notes.length;
notePos = scalegrid[scalegridindex];
showCurrentScale();
refreshScaleIndex();
resendColors();
}
};
this.scrollNoteUp = function () {
if (scalegridindex < scalegrid.length + 7) {
scalegridindex += 1;
notePos = scalegrid[scalegridindex];
showCurrentScale();
refreshScaleIndex();
resendColors();
}
};
this.scrollNoteDown = function () {
if (scalegridindex > 0) {
scalegridindex -= 1;
notePos = scalegrid[scalegridindex];
showCurrentScale();
refreshScaleIndex();
resendColors();
}
};
/**
* Determines the grid index of a given Note. Returns the index of
* the first base note of current scale
*
* @param {int} notevalue note value
**/
function getIndexOfNote(notevalue) {
var lastBaseNote = -1;
for (var i = 0; i < scalegrid.length; i++) {
if (scalegrid[i] % 12 === baseNote) {
lastBaseNote = i;
}
if (scalegrid[i] >= notevalue) {
return lastBaseNote;
}
}
return -1;
}
this.scrollNoteIntoView = function () {
var lowestNote = -1;
var highestNote = -1;
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 127; j++) {
var state = fullNoteMapping[i][j];
if (state) {
if (lowestNote === -1) {
lowestNote = j;
highestNote = j;
} else {
lowestNote = Math.min(lowestNote, j);
highestNote = Math.max(highestNote, j);
}
}
}
}
if (lowestNote !== -1) {
var lowindex = getIndexOfNote(lowestNote);
if (lowindex >= 0) {
scalegridindex = lowindex;
notePos = scalegrid[scalegridindex];
refreshScaleIndex();
resendColors();
}
}
};
this.scrollScaleUp = function () {
noteview.changeScale(1);
};
this.scrollScaleDown = function () {
noteview.changeScale(-1);
};
this.scrollBaseScaleDown = function () {
noteview.changeBaseNote(-1);
};
this.scrollBaseScaleUp = function () {
noteview.changeBaseNote(1);
};
function handleMono(col) {
if (monoMode) {
for (var i = 0; i < 127; i++) {
if (fullNoteMapping[col][i] == 2 || fullNoteMapping[col][i] == 1) {
clip.clearStep(col, i);
}
}
}
}
this.handleEvent = function (row, col, value) {
if (modifiers.isShiftDown()) {
if (value > 0) {
applicationControl.exec(row * 8 + col);
}
return;
}
var noteindex = 7 - row;
if (col >= gridMap.nrOfSteps()) {
return;
}
var state = steps[col].states[noteindex];
if (value === 0) { // TODO Maybe Not
if (lastHoldDown[row] === col) {
lastHoldDown[row] = -1;
}
return;
}
if (lastHoldDown[row] >= 0) {
if (col > lastHoldDown[row]) {
clip.setStep(lastHoldDown[row], notegrid[noteindex].notevalue, baseVelocity, gridMap.stepValue() * (col - lastHoldDown[row] + 1));
for (var xcol = lastHoldDown[row] + 1; xcol <= col; xcol++) {
handleMono(xcol);
}
} else if (lastHoldDown[row] > col) {
clip.setStep(col, notegrid[noteindex].notevalue, baseVelocity, gridMap.stepValue() * (lastHoldDown[row] - col + 1));
handleMono(col);
}
} else if (state === 0) {
clip.setStep(col, notegrid[noteindex].notevalue, baseVelocity, gridMap.stepValue() * gateLength);
handleMono(col);
lastHoldDown[row] = col;
} else {
clip.clearStep(col, notegrid[noteindex].notevalue);
}
};
}