-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.js
More file actions
704 lines (581 loc) · 17 KB
/
test2.js
File metadata and controls
704 lines (581 loc) · 17 KB
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
sketch.default2d();
outlets = 5; // 0: MIDI-style, 1: UI, 2: event count, 3: modified groove, 4: loop count
var events = [];
var hasDrawnStatic = false;
var currentPos = 0;
var numGridlines = 0;
var draggingIndex = -1;
var dragOffset = [0, 0];
var quantizeAmount = 0;
var velocityExp = 1.0;
var velocityBias = 0.0;
var margin = 0.05;
var loopCounter = 0;
var lastPlaybackPos = 0;
var lastCheckedGridIndex = -1;
var memorySpanInLoops = 1; // 1 bar = 1 loop worth of memory
var queuedEvents = [];
var responsiveness = 0.5; // default
var shouldDeferOutput = false;
function setResponsiveness(val) {
val = parseFloat(val);
if (isNaN(val) || val < 0.05 || val > 1.0) {
return;
}
responsiveness = val;
}
function calculateGridIndex(pos) {
return Math.round(pos * 32); // 32 divisions in a 2-bar loop
}
function screenToNormalized(x, y) {
var w = box.rect[2] - box.rect[0];
var h = box.rect[3] - box.rect[1];
var nx = x / w;
var ny = 1.0 - y / h;
return [
(nx - margin) / (1 - 2 * margin),
(ny - margin) / (1 - 2 * margin)
];
}
function prepare2DSpace() {
with (sketch) {
glmatrixmode("projection");
glloadidentity();
glortho(0, 1, 0, 1, -1, 1);
glmatrixmode("modelview");
glloadidentity();
}
}
function mapToDisplay(x, y) {
return [
margin + x * (1 - 2 * margin),
margin + y * (1 - 2 * margin)
];
}
function findClosestPoint(norm, thresholdSq) {
var minDistSq = thresholdSq;
var closestIndex = -1;
for (var i = 0; i < events.length; i++) {
var dx = norm[0] - events[i].position;
var dy = norm[1] - events[i].velocity;
var distSq = dx * dx + dy * dy;
if (distSq < minDistSq) {
minDistSq = distSq;
closestIndex = i;
}
}
return closestIndex;
}
function isTooClose(pos, vel, thresholdSq) {
for (var i = 0; i < events.length; i++) {
var dx = events[i].position - pos;
var dy = events[i].velocity - vel;
if ((dx * dx + dy * dy) < thresholdSq) return true;
}
return false;
}
function drawStatic() {
with (sketch) {
glclearcolor(0.1, 0.1, 0.1, 1.0);
glclear();
prepare2DSpace();
var w = box.rect[2] - box.rect[0];
var h = box.rect[3] - box.rect[1];
var aspect = w / h;
// Draw outer border
glcolor(0.8, 0.8, 0.8, 0.3);
moveto(margin, margin);
lineto(1 - margin, margin);
lineto(1 - margin, 1 - margin);
lineto(margin, 1 - margin);
lineto(margin, margin);
// Draw gridlines if any
if (numGridlines > 0) {
glcolor(0.4, 0.4, 0.4, 0.5);
for (var i = 1; i <= numGridlines; i++) {
var gx = i / (numGridlines + 1);
var [x, _] = mapToDisplay(gx, 0);
moveto(x, margin);
lineto(x, 1 - margin);
}
}
// Draw note events
var modified = getModifiedGroove(true);
for (var i = 0, eIndex = 0; i < modified.length; i += 3, eIndex++) {
var x = modified[i];
var y = modified[i + 1];
var isHollow = modified[i + 2];
var [dx, dy] = mapToDisplay(x, y);
var r = 0.05 * (1 - 2 * margin);
var event = events[eIndex];
var isMustard = event && event.loopIndex === -1;
if (isHollow) {
if (isMustard) glcolor(0.8, 0.6, 0.2, 0.4); // mustard hollow
else glcolor(0.4, 0.6, 1.0, 0.3); // blue hollow
moveto(dx, dy);
ellipse(r / aspect, r);
} else {
if (isMustard) {
glcolor(0.6, 0.4, 0.1, 0.3); // mustard base
moveto(dx, dy);
ellipse(r / aspect, r);
glcolor(1.0, 0.9, 0.4, 0.9); // mustard highlight
moveto(dx, dy);
ellipse(r / aspect, r);
} else {
glcolor(0.4, 0.6, 1.0, 0.3); // blue base
moveto(dx, dy);
ellipse(r / aspect, r);
glcolor(0.8, 0.9, 1.0, 0.9); // blue highlight
moveto(dx, dy);
ellipse(r / aspect, r);
}
}
}
}
hasDrawnStatic = true;
}
function drawDynamic() {
with (sketch) {
prepare2DSpace();
glcolor(1, 1, 1, 1);
var [cx, _] = mapToDisplay(currentPos, 0);
moveto(cx, margin);
lineto(cx, 1 - margin);
}
}
function getModifiedGroove(forDrawing) {
if (events.length === 0) return forDrawing ? [] : ["clear"];
var output = [];
for (var i = 0; i < events.length; i++) {
var e = events[i];
var pos = e.position;
if (numGridlines > 0) {
var step = 1 / (numGridlines + 1);
var grid = Math.round(pos / step) * step;
pos = pos + (grid - pos) * quantizeAmount;
}
var vel = e.velocity + (e.velocity - 0.5) * velocityExp / (e.velocity * 3) + velocityBias;
vel = Math.min(Math.max(vel, 0), 1);
if (forDrawing) {
var isHollow = vel <= 0.001;
output.push(pos);
output.push(vel);
output.push(isHollow);
} else {
if (vel > 0.001) {
output.push(pos);
output.push(Math.min(Math.round(vel * 127), 127));
}
}
}
return output;
}
function quantize(f) {
quantizeAmount = f;
hasDrawnStatic = false;
bang();
outputPoints(true);
}
function scale(f) {
velocityExp = f;
hasDrawnStatic = false;
bang();
outputPoints(true);
}
function bias(f) {
velocityBias = f;
hasDrawnStatic = false;
bang();
outputPoints(true);
}
function memory(i) {
memorySpanInLoops = i;
}
function onclick(x, y, button, shift) {
var norm = screenToNormalized(x, y);
var thresholdSq = 0.02;
if (shift) {
var index = findClosestPoint(norm, thresholdSq);
if (index !== -1) {
events.splice(index, 1);
hasDrawnStatic = false;
bang();
outputPoints(true);
return;
}
}
var index = findClosestPoint(norm, thresholdSq);
if (index !== -1) {
draggingIndex = index;
dragOffset = [
norm[0] - events[index].position,
norm[1] - events[index].velocity
];
if (shift && numGridlines > 0) {
var step = 1 / (numGridlines + 1);
var snapped = Math.round(events[index].position / step) * step;
events[index].position = Math.min(Math.max(snapped, 0), 1);
hasDrawnStatic = false;
bang();
}
return;
}
draggingIndex = -1;
}
function ondrag(x, y) {
if (draggingIndex === -1) return;
var norm = screenToNormalized(x, y);
var e = events[draggingIndex];
var newX = norm[0] - dragOffset[0];
var newY = norm[1] - dragOffset[1];
// Lock movement to ±1/64 around this point's gridIndex
var gridCenter = e.gridIndex / 32;
var range = 1 / 64;
e.position = Math.min(Math.max(newX, gridCenter - range), gridCenter + range*.98);
e.velocity = Math.min(Math.max(newY, 0), 1);
hasDrawnStatic = false;
bang();
}
function onidle() {
if (draggingIndex !== -1) outputPoints();
draggingIndex = -1;
}
function ondblclick(x, y) {
var norm = screenToNormalized(x, y);
addOnset(norm[0], norm[1]);
ondrag(x, y);
}
function addOnset(time, velocity, queue_for_later) {
queue_for_later = queue_for_later === true; // default = false
velocity = Math.min(Math.max(velocity, 0), 1);
var gridIndex = calculateGridIndex(time);
var newEvent = {
position: time,
velocity: velocity,
loopIndex: loopCounter,
gridIndex: gridIndex
};
if (queue_for_later) {
queuedEvents.push(newEvent);
return;
}
insertEventIfValid(newEvent);
}
function insertEventIfValid(event) {
var thresholdSq = 0.0001;
var shouldAdd = true;
for (var i = events.length - 1; i >= 0; i--) {
var e = events[i];
if (e.gridIndex === event.gridIndex) {
if (e.loopIndex < event.loopIndex) {
events.splice(i, 1); // remove older loop
} else if (e.loopIndex === event.loopIndex) {
if (event.velocity > e.velocity) {
events.splice(i, 1); // replace lower-velocity event
} else {
shouldAdd = false;
}
} else {
shouldAdd = false;
}
}
}
for (var j = 0; j < events.length && shouldAdd; j++) {
var e = events[j];
var dx = e.position - event.position;
var dy = e.velocity - event.velocity;
if ((dx * dx + dy * dy) < thresholdSq) {
shouldAdd = false;
break;
}
}
if (shouldAdd) {
events.push(event);
hasDrawnStatic = false;
drawStatic();
drawDynamic();
// DEFER OUTPUT
shouldDeferOutput = true;
}
}
function setPattern() {
var args = arrayfromargs(arguments);
var data = args.slice();
var newEvents = [];
// Step 1: Remove all existing loop -1 events
events = events.filter(function(e) {
return e.loopIndex !== -1;
});
// Step 2: Build new candidate events
for (var i = 0; i < data.length; i += 2) {
var pos = data[i];
var vel = data[i + 1];
if (typeof pos === "number" && typeof vel === "number") {
newEvents.push({
position: pos,
velocity: Math.min(Math.max(vel, 0), 1),
loopIndex: -1,
gridIndex: calculateGridIndex(pos)
});
}
}
// Step 3: Filter by gridIndex to keep only loudest event per grid
var eventMap = {};
for (var i = 0; i < newEvents.length; i++) {
var e = newEvents[i];
var gi = e.gridIndex;
if (!(gi in eventMap) || e.velocity > eventMap[gi].velocity) {
eventMap[gi] = e;
}
}
// Step 4: Add unique (non-duplicate) events to main list
var threshold = 0.0001;
for (var key in eventMap) {
if (eventMap.hasOwnProperty(key)) {
var newEvent = eventMap[key];
var exists = false;
for (var j = 0; j < events.length; j++) {
var e = events[j];
var dx = e.position - newEvent.position;
var dy = e.velocity - newEvent.velocity;
if ((dx * dx + dy * dy) < threshold) {
exists = true;
break;
}
}
if (!exists) {
events.push(newEvent);
}
}
}
// Step 5: Redraw and output
hasDrawnStatic = false;
drawStatic();
drawDynamic();
outputPoints(true);
refresh();
}
function postEvents() {
post("---- Current Events ----\n");
for (var i = 0; i < events.length; i++) {
var e = events[i];
if (e.position < 0 || e.position >= 1) continue; // 🔒 Skip events outside visible range
post("[" + i + "] pos: " + e.position.toFixed(4) +
", vel: " + e.velocity.toFixed(4) +
", loopIndex: " + e.loopIndex +
", gridIndex: " + e.gridIndex + "\n");
}
post("------------------------\n");
}
function clearEvents() {
var beforeCount = events.length;
// Keep only mustard events (loopIndex === -1)
events = events.filter(function(e) {
return e.loopIndex === -1;
});
var removedCount = beforeCount - events.length;
if (removedCount > 0) {
hasDrawnStatic = false;
drawStatic();
drawDynamic();
outputPoints(true);
refresh();
} else {
}
}
function clear() {
clearEvents();
}
function reset() {
clearEvents();
}
function bang() {
sketch.glclear();
drawStatic();
drawDynamic();
refresh();
}
function setPlaybackPosition(pos) {
// Detect wraparound (loop completed)
if (pos < lastPlaybackPos) {
loopCounter++;
outlet(4, loopCounter); // output updated loop count
}
// Trigger memory check when we enter a new 16th-note grid
var currentGridIndex = Math.floor(pos * 32);
if (currentGridIndex !== lastCheckedGridIndex) {
checkMemory();
lastCheckedGridIndex = currentGridIndex;
}
// Flush deferred output at multiples of 0.25
if (shouldDeferOutput && (Math.abs(pos % responsiveness) < 0.001)) {
outputPoints(true);
shouldDeferOutput = false;
}
lastPlaybackPos = pos;
currentPos = pos;
bang();
}
function checkMemory() {
var maxAge = loopCounter - memorySpanInLoops;
var currentGridIndex = Math.floor(currentPos * 32);
// Step 1: Deep copy events before deletion
var before = events.map(function(e) {
return {
position: e.position,
velocity: e.velocity,
loopIndex: e.loopIndex,
gridIndex: e.gridIndex
};
});
// Step 2: Remove only events that are old AND match current timestep (gridIndex)
for (var i = events.length - 1; i >= 0; i--) {
var e = events[i];
if (
e.loopIndex >= 0 &&
e.loopIndex <= maxAge &&
e.gridIndex === currentGridIndex
) {
events.splice(i, 1);
}
}
// Step 3: Compare before and after to see if something changed
var changed = false;
if (before.length !== events.length) {
changed = true;
} else {
for (var i = 0; i < before.length; i++) {
var a = before[i];
var b = events[i];
if (
a.position !== b.position ||
a.velocity !== b.velocity ||
a.loopIndex !== b.loopIndex ||
a.gridIndex !== b.gridIndex
) {
changed = true;
break;
}
}
}
// Step 4: Redraw and output if changed
if (changed) {
hasDrawnStatic = false;
drawStatic();
drawDynamic();
outputPoints(true);
// postEvents();
}
// After memory check, insert any queued events
if (queuedEvents.length > 0) {
for (var i = 0; i < queuedEvents.length; i++) {
insertEventIfValid(queuedEvents[i]);
}
queuedEvents = [];
}
}
function playhead_stopped() {
loopCounter = 0;
outlet(4, loopCounter);
}
function msg_int(n) {
setNumGridlines(n);
}
function msg_float(n) {
setNumGridlines(Math.round(n));
}
function setNumGridlines(n) {
numGridlines = Math.max(0, n);
hasDrawnStatic = false;
bang();
}
function onresize(w, h) {
hasDrawnStatic = false;
drawStatic();
drawDynamic();
refresh();
}
onresize.local = 1;
function outputPoints(force) {
if (!force && JSON.stringify(events) === JSON.stringify(outputPoints.lastState)) return;
outputPoints.lastState = JSON.parse(JSON.stringify(events));
var modified = getModifiedGroove(false);
if (modified.length === 0) {
outlet(0, "clear");
outlet(1, "clear");
outlet(2, 0);
outlet(3, "clear");
outlet(4, loopCounter);
return;
}
outlet(0, modified);
prepare_for_preset();
outlet(2, events.length);
outlet(3, modified);
outlet(4, loopCounter);
}
outputPoints.lastState = [];
function prepare_for_preset() {
var rawOut = [];
for (var i = 0; i < events.length; i++) {
var e = events[i];
rawOut.push(e.position);
rawOut.push(e.velocity);
}
outlet(1, rawOut);
}
function removePresetEvents(bang) {
if (events.length === 0) return;
var beforeCount = events.length;
// Remove all events with loopIndex -1
events = events.filter(function(e) {
return e.loopIndex !== -1;
});
var removedCount = beforeCount - events.length;
if (removedCount > 0) {
hasDrawnStatic = false;
drawStatic();
drawDynamic();
outputPoints(true);
refresh();
} else {
}
}
function mult(i) {
i = parseFloat(i);
if (isNaN(i)) return;
for (var j = 0; j < events.length; j++) {
var e = events[j];
e.position *= i;
e.gridIndex = calculateGridIndex(e.position);
}
hasDrawnStatic = false;
drawStatic();
drawDynamic();
outputPoints(true);
}
function div(i) {
i = parseInt(i);
if (isNaN(i) || i < 1) return;
var newEvents = [];
for (var j = 0; j < events.length; j++) {
var e = events[j];
for (var k = 0; k < i; k++) {
newEvents.push({
position: (e.position / i) + (k / i),
velocity: e.velocity,
loopIndex: e.loopIndex,
gridIndex: calculateGridIndex((e.position / i) + (k / i))
});
}
}
events = newEvents;
hasDrawnStatic = false;
drawStatic();
drawDynamic();
outputPoints(true);
}
drawStatic();
drawDynamic();
refresh();