-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
1647 lines (1285 loc) · 43.3 KB
/
sketch.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
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2019 Alberto Fiore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** DOM stuff */
let progrWin, activeProgrEl = null, lastActiveProgrEl = null, lastActiveProgrIdx = -1;
// responsive stuff
let isXSm = false, isSm = false, isMd = false, isLg = false, isXLg = false, isXXLg = false;
if (window.innerWidth >= 1450)
isXXLg = true; // 5 octave keyboard
else if (window.innerWidth >= 1200)
isXLg = true; // 4 octave keyboard
else if (window.innerWidth >= 992)
isLg = true; // 3 octave keyboard
else if (window.innerWidth >= 768)
isMd = true; // 2 octave keyboard
else if (window.innerWidth >= 576)
isSm = true; // 1 octave keyboard
else
isXSm = true; // 1 octave keyboard
let playedKeyEl = null, playedKeyEl2 = null, playedKeyEl3 = null, playedKeyEl4 = null;
let noteTxt = document.getElementById("note");
let keyTxt = document.getElementById("key");
/** CONFIGS */
let attackLevel = 0.25;
let releaseLevel = 0;
let attackTime = 0.001;
let decayTime = 0.5;
var susPercent = 0.;
let releaseTime = 0.;
let env, env2, env3, env4, envF;
let osc, osc2, osc3, osc4;
let freq = 220, freq2 = 220, freq3 = 220, freq4 = 220;
let filter, filterFreq = parseInt(document.getElementById("filterFreq").value, 10), filterRes = 2;
let synthOn = document.getElementById("synthOn").checked, drumsOn = document.getElementById("drumsOn").checked, seventhOn = document.getElementById("seventhOn").checked, beatOn = document.getElementById("beatOn").checked, inversionOn = document.getElementById("inversionOn").checked;
/* DRUM STUFF **/
let beatSound, bdSound, hhSound, hh2Sound, snrSound;
let bdPat, hhPat, hh2Pat, snrPat;
let bdIdx, hhIdx, hh2Idx, snrIdx;
let patSize;
let mult3, mult7, mult6, mult5, mult4;
/*
CIRCLE OF FIFTHS
In the maj/min CoF, you can use the dominant V chord as pivot, because it is the I chord of the next key
To modulate from maj key to its relative minor, this program uses ii - iv correspondence or iii - i correspondence
To modulate form min key to its relative major, this program uses I - vi correspondence or VI - I correspondence
*/
let majCircle = [60, 67, 62, 69, 64, 71, 66, 61, 68, 63, 70, 65]; // start from C4
let minCircle = [69, 64, 71, 66, 61, 68, 63, 70, 65, 60, 67, 62]; // start from A4
let minInterval = [2, 1, 2, 2, 1, 2, 2]; // intervals to build a scale, don't touch!
let majInterval = [2, 2, 1, 2, 2, 2, 1]; // intervals to build a scale, don't touch!
// shifts corresponds to kind of chords
let shiftMaj = [0, 4, 7, 11];
let shiftMin = [0, 3, 7, 10];
let shiftDim = [0, 3, 6, 9];
let shiftDom = [0, 4, 7, 10];
let shiftSharpDim = [1, 4, 7, 10];
// altered chords
let shiftMinFlat5 = [0, 3, 6, 10];
// shift array is used to build chords
let shift = [shiftMaj, shiftMin, shiftDim, shiftDom, shiftSharpDim, shiftMinFlat5];
// activeKey/minKey/majKey contain indexes of shift array (to build chords)
let minKey = [1, 2, 0, 1, 0, 0, 2]; // 0 -> maj, 1 -> min, 2 -> dim, 3 -> dom chord -> i ii° III iv V VI vii°
let majKey = [0, 1, 1, 0, 0, 1, 2]; // 0 -> maj, 1 -> min, 2 -> dim, 3 -> dom chord -> I ii iii IV V vi vii°
let activeKey, activeProgr, activeShift;
// Some common chord progression:
// - progr property: references indexes of activeKey (which refers to either minKey or majKey)
// - varMaj/varMin are overrides of the progr property: they refer to indexes of shift array:
// - varMaj is used only in major keys
// - varMin is used only in minor keys
let progr = [
// JAZZ
{ progr: [1, 4, 0, 0], varMaj: [1, 3, 0, 0], varMin: [5, 3, 1, 1], enabled: true },
{ progr: [0, 0, 1, 1, 2, 5], varMaj: [0, 4, 1, 4, 1, 3], enabled: true },
{ progr: [0, 5, 1, 4, 2, 5, 1, 4], varMaj: [0, 1, 1, 3, 1, 3, 1, 3], enabled: true },
{ progr: [0, 1, 4, 3], varMaj: [0, 1, 3, 0], enabled: true },
{ progr: [0, 0, 3, 3, 2, 5, 1, 4, 0, 0], varMaj: [0, 3, 0, 1, 1, 3, 1, 3, 0, 0], enabled: true },
{ progr: [0, 0, 1, 4], varMaj: [0, 0, 1, 3], enabled: true },
{ progr: [0, 0, 1, 1, 1, 4, 0, 0], varMaj: [0, 0, 3, 3, 1, 3, 0, 0], enabled: true },
{ progr: [2, 5, 1, 4], varMaj: [3, 3, 3, 3], enabled: true },
// OTHER (no varMaj/varMin means to use shifts of the active key)
{ progr: [0, 3, 5], enabled: true },
{ progr: [0, 4, 5, 3], enabled: true },
{ progr: [0, 3, 0, 4, 0], enabled: true },
{ progr: [2, 5, 1, 4], enabled: true },
{ progr: [0, 3, 6, 2, 5, 1, 4, 0], enabled: true },
{ progr: [0, 3, 4, 0], enabled: true }
];
let progrIdx = 0;
let enabledProgr = [];
let progr0 = []; // Indexes of progr that start with 0 (I/i chord)
let progr3 = []; // Indexes of progr that have 3 (IV/iv chord) as second chord
let progr5 = []; // Indexes of progr that have 5 (vi/VI chord) as second chord
/**
TIME SIGNATURE:
- bottom number: beat -> what note is a beat (1 ... 1/16)
- top number: measure/bar (= + beat) -> how many beats are in a bar
**/
let tsT = parseInt(document.getElementById("ts-top").value, 10), tsB = parseInt(document.getElementById("ts-bottom").value, 10);
let bpm = parseInt(document.getElementById("bpm").value, 10);
let minuteMs = 60000;
let beatMs, barMs, sixteenthMs;
/** SEQUENCER STUFF */
let tds; // elements of sequencer
let seqIdx; // index of active sequencer element
let sixteenthWidth; // width of a square representing a 1/16 note in the sequencer
let tonic = stringToMidi("A3");
// midi notes of the current scale
let midiSequence = [];
// how many notes are in a scale
let numNotes = 7;
let noteIdx = 0, noteIdx2 = 0, progrNoteIdx = 0;
let midiNote; // current playing note
let durL2 = []; // duration of notes generated in a bar
// this random number generator creates numbers according to the probabilities given,
// it is used to choose a note index that corresponds to a chord inversion
let drngInv;
const NO_INV = 1, INV = 0;
let probNoInv = 1, probInv = 0;
let drngInv2;
const NO_INV2 = 1, INV2 = 0;
let probNoInv2 = 1, probInv2 = 0;
/** MODULATION MODES */
let MOD_MODE_MIN_NO_MOD = 0, MOD_MODE_MAJ_NO_MOD = 1, MOD_MODE_ONLY_MIN = 2, MOD_MODE_ONLY_MAJ = 3, MOD_MODE_MAJ_MIN = 4, MOD_MODE_MAJ_MIN_HARD = 5;
let modMode = MOD_MODE_MAJ_MIN;
// this random number gen creates number according to the probabilities associated to 3 events:
// - no modulation,
// - modulation to the relative min/maj key
// - modulation to the next key in the same circle (min/maj),
let drngMod;
const NO_MOD = 0, MOD_REL = 1, MOD_NEXT = 2;
// predefined set of probs:
// index correspond to one of the modulation modes in the order they're defined
let modProbs = [{
probNoMod: 1, probModRel: 0, probModNext: 1
},
{
probNoMod: 1, probModRel: 0, probModNext: 1
},
{
probNoMod: 1 / 2., probModRel: 0, probModNext: 1 / 2.
},
{
probNoMod: 1 / 2., probModRel: 0, probModNext: 1 / 2.
},
{
probNoMod: 1 / 2., probModRel: 1 / 6., probModNext: 2 / 6.
},
{
probNoMod: 1 / 3., probModRel: 1 / 3., probModNext: 1 / 3.
}
];
// this random number generator creates numbers according to the probabilities associated to 3 events:
// - no progression change,
// - change progression
let drngChgProg;
const NO_CHG_PROG = 1, CHG_PROG = 0;
let probNoChgProg = 1, probChgProg = 0;
let cancel = false, cancel2 = false, cancel3 = false, cancel4 = false, cancel5 = false, cancel6 = false, cancel7 = false, cancel8 = false; // vars to stop timers
let isPlaying = false;
/*
REGARDING TIMERS IN JS:
*usually*, in this application consecutive timer calls are delayed more than 4ms, so there should be no problem here
ref. https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Reasons_for_delays_longer_than_specified
*/
let timerDelayCompensationMs = 20, timerDelayCompensationMs2 = 2;
// minToMaj/majToMin => arrays to associate:
// - scale index of pivot chord in the starting key
// - array from where to get a new progression
// - index of next chord in the new progression
// - progr5 is where are stored progression with chord nr. 5 (which is vi/VI) as the second chord (index 1) of the progression
// - progr3 is where are stored progression with chord nr. 3 (which is IV/iv) as the second chord (index 1) of the progression
let minToMaj = [
{ "startKeyIdx": 0, "progrArr": progr5, "nuProgrNoteIdx": 1 },
{ "startKeyIdx": 5, "progrArr": progr0, "nuProgrNoteIdx": 0 }];
let majToMin = [
{ "startKeyIdx": 1, "progrArr": progr3, "nuProgrNoteIdx": 1 },
{ "startKeyIdx": 2, "progrArr": progr0, "nuProgrNoteIdx": 0 }];
/* PERFORMANCE MONITORING (for debugging) */
let monitoring = false;
function openProgWindow() {
progrWin = window.open("progressions.html", "_blank", 'toolbar=0,location=0,menubar=0');
progrWin.onload = function () {
updateProgressionList();
};
}
function preload() {
soundFormats('wav');
beatSound = loadSound('assets/beat.wav');
bdSound = loadSound('assets/bd.wav');
hhSound = loadSound('assets/hh.wav');
hh2Sound = loadSound('assets/hh2.wav');
snrSound = loadSound('assets/snr.wav');
}
function setup() {
document.getElementById("cursor-holder").style.display = "none";
cnv = createCanvas(100, 20);
cnv.parent('cursor-holder');
createKeyboard();
document.addEventListener('keypress', function (e) {
if (e.code === "Space") {
e.preventDefault();
if (isPlaying) {
cancel = true, cancel2 = true, cancel3 = true, cancel4 = true, cancel5 = true, cancel6 = true, cancel7 = true, cancel8 = true;
isPlaying = false;
} else {
play();
}
}
});
$('#synthOn').on('change', function (e) {
synthOn = e.target.checked;
});
$('#drumsOn').on('change', function (e) {
drumsOn = e.target.checked;
});
$('#beatOn').on('change', function (e) {
beatOn = e.target.checked;
});
$('#inversionOn').on('change', function (e) {
inversionOn = e.target.checked;
});
$('#seventhOn').on('change', function (e) {
seventhOn = e.target.checked;
});
$('#filterFreq').on('change', function (e) {
filterFreq = parseInt(document.getElementById("filterFreq").value, 10);
envF.setRange(filterFreq, 10);
});
env = new p5.Envelope();
env.setADSR(attackTime, decayTime, susPercent, releaseTime);
env.setRange(attackLevel, releaseLevel);
env2 = new p5.Envelope();
env2.setADSR(attackTime, decayTime, susPercent, releaseTime);
env2.setRange(attackLevel, releaseLevel);
env3 = new p5.Envelope();
env3.setADSR(attackTime, decayTime, susPercent, releaseTime);
env3.setRange(attackLevel, releaseLevel);
env4 = new p5.Envelope();
env4.setADSR(attackTime, decayTime, susPercent, releaseTime);
env4.setRange(attackLevel, releaseLevel);
osc = new p5.SawOsc();
osc.amp(env);
osc.start();
osc.freq(freq);
osc2 = new p5.SawOsc();
osc2.amp(env2);
osc2.start();
osc2.freq(freq2);
osc3 = new p5.SawOsc();
osc3.amp(env3);
osc3.start();
osc3.freq(freq3);
osc4 = new p5.SawOsc();
osc4.amp(env4);
osc4.start();
osc4.freq(freq4);
envF = new p5.Envelope();
envF.setADSR(0.05, 0.75, 0.2, 0);
envF.setRange(filterFreq, 10);
filter = new p5.LowPass();
filter.freq(envF);
filter.res(filterRes);
// Disconnect env from master output.
// Then, connect it to the filter, so that we only hear the filtered sound
osc.disconnect();
osc.connect(filter);
osc2.disconnect();
osc2.connect(filter);
osc3.disconnect();
osc3.connect(filter);
osc4.disconnect();
osc4.connect(filter);
reverb = new p5.Reverb();
// connect soundFile to reverb, process w/
// 4 second reverbTime, decayRate of 18%
reverb.process(filter, 4, 18);
// setup chord progr
setupChordProgr();
}
function setupChordProgr() {
enabledProgr = [], progr0 = [], progr3 = [], progr5 = [];
for (let i = 0; i < progr.length; i++) {
if (!progr[i].enabled)
continue;
enabledProgr.push(i);
if (progr[i]["progr"][0] == 0) {
progr0.push(i);
}
if (progr[i]["progr"][1] == 3) {
progr3.push(i);
}
if (progr[i]["progr"][1] == 5) {
progr5.push(i);
}
}
}
function switchProgr(index) {
let wasEnabled = progr[index].enabled;
progr[index].enabled = !progr[index].enabled;
// setup chord progr
setupChordProgr();
let el = progrWin.document.getElementById("progList");
let nuClass = wasEnabled ? "btn-danger" : "btn-success";
let b = el.children[index].getElementsByTagName("button")[0];
if (wasEnabled)
b.classList.remove("btn-success");
else
b.classList.remove("btn-danger");
b.classList.add(nuClass);
}
function updateProgressionList() {
lastActiveProgrEl = null;
activeProgrEl = null;
lastActiveProgrIdx = -1;
let el = progrWin.document.getElementById("progList");
while (el.firstChild) el.removeChild(el.firstChild);
let progrCnt = 0;
for (let p of progr) {
let table = '<table class="table table-sm table-borderless">' +
'<tr class="table-success">';
for (let noteIdx of p.progr) {
table += '<td class="text-center">' + toRomanIndex(noteIdx) + "</td>";
}
table += '<td class="text-center"><button class="btn btn-' + (p.enabled ? "success" : "danger") + '" onclick="opener.switchProgr(' + progrCnt + ')">On/Off</button></td>';
table += '</tr>' +
'</table>';
el.insertAdjacentHTML("beforeend", '<tr><td>' + table + "</td></tr>");
progrCnt++;
}
}
function updateProgressionViz() {
lastActiveProgrEl = activeProgrEl;
let el = progrWin.document.getElementById("progList");
activeProgrEl = el.getElementsByTagName("table")[progrIdx];
//console.log(activeProgrEl);
if (lastActiveProgrEl === null)
lastActiveProgrEl = activeProgrEl;
}
function showActiveProgrIdx(idx) {
if (activeProgrEl !== null) {
let el = activeProgrEl.getElementsByTagName("tr")[0];
//console.log(el.children[idx])
if (lastActiveProgrIdx >= 0)
if (lastActiveProgrEl.getElementsByClassName("activeProgr").length > 0)
lastActiveProgrEl.getElementsByTagName("tr")[0].children[lastActiveProgrIdx].classList.remove("activeProgr");
else
el.children[lastActiveProgrIdx].classList.remove("activeProgr");
el.children[idx].classList.add("activeProgr");
lastActiveProgrIdx = idx;
}
}
function play() {
// console.log(cancel);
// console.log(cancel2);
// console.log(cancel3);
// console.log(isPlaying);
if (isPlaying)
cancel = true, cancel2 = true, cancel3 = true, cancel4 = true, cancel5 = true, cancel6 = true, cancel7 = true, cancel8 = true;
else
isPlaying = true;
beatMs = minuteMs / bpm;
progrIdx = enabledProgr[parseInt(random() * enabledProgr.length, 10)];
// console.log(progrIdx);
activeProgr = progr[progrIdx]['progr'];
modMode = parseInt(document.getElementById("modMode").value, 10);
// console.log(modMode);
let minOn = (modMode == MOD_MODE_ONLY_MIN || modMode == MOD_MODE_MIN_NO_MOD);
activeKey = minOn ? minKey : majKey;
if (typeof progrWin != "undefined") {
updateProgressionList();
updateProgressionViz();
}
if (typeof this.id !== "undefined") tonic = parseInt(this.id.substring(1), 10);
keyTxt.textContent = "Key: " + midiToString(tonic, false) + " " + (activeKey == minKey ? "min" : "maj");
midiSequence = [];
//console.log(beatMs);
//console.log(tonic);
let note = tonic;
for (let i = 0; i < numNotes; i++) {
midiSequence.push(note);
note += minOn ? minInterval[i] : majInterval[i];
}
/**
Create a list with possible note durations
Ex. time signature 3/2 -> 1 half note = 1 beat, 3 beat/bar, 1 full note = 2 beat
Algorithm:
- start with duration indicated by time signature denominator -> ex. 3/2 -> start duration = ½, which corresponds to 1 beat
- if beat/bar > 1 (ex. 3/2) -> multiply duration and number of beats by 2 while they're < time signature beat/bar and duration < 1 (ex. ½ * 2 = 1 -> 2 beat)
- divide start duration and number of beats by 2 while they're > 1/16
**/
tsT = parseInt(document.getElementById("ts-top").value, 10);
tsB = parseInt(document.getElementById("ts-bottom").value, 10);
bpm = parseInt(document.getElementById("bpm").value, 10);
let dur = 1. / tsB;
let beatN = 1;
let durL = [];
while (beatN < tsT && dur < 1) {
durL.push(Math.round(beatMs));
dur *= 2;
beatMs *= 2;
beatN *= 2;
}
beatMs = minuteMs / bpm;
dur = 1. / tsB;
beatN = 1;
// 0.0625 = 1/16
while (dur > 0.0625) {
dur /= 2;
beatMs /= 2;
beatN /= 2;
durL.unshift(Math.round(beatMs));
}
//console.log(durL);
beatMs = Math.round(minuteMs / bpm);
if (monitoring)
console.log("beatMs " + beatMs);
barMs = Math.round(beatMs * tsT);
// randomly choose between durations to fill a bar
let r = 0;
let totDur = 0;
durL2 = [];
while (totDur < barMs) {
r = parseInt(random(durL.length), 10);
totDur += durL[r];
durL2.push(durL[r]);
}
if (totDur > barMs) {
totDur -= durL[r];
durL2.splice(durL2.length - 1, 1);
}
if (monitoring)
console.log("durL2 " + durL2);
/** SEQUENCER */
patSize = (16 / tsB) * tsT;
if (monitoring)
console.log("patSize " + patSize);
sixteenthMs = Math.round(barMs / patSize);
if (monitoring) {
console.log("barMs " + barMs);
console.log("sixteenthMs " + sixteenthMs);
}
let seq = document.getElementById("sequencer");
while (seq.firstChild) {
seq.removeChild(seq.firstChild);
}
for (let i = 0; i < patSize; i++) {
seq.insertAdjacentHTML("beforeend", "<td></td>");
}
tds = seq.getElementsByTagName('td');
let k = 0;
for (let i = 0; i < durL2.length; i++) {
let numOfSixteenths = Math.round(durL2[i] / sixteenthMs);
// console.log(durL2[i] + " " + numOfSixteenths)
let r = Math.round(random(255));
let g = Math.round(random(255));
let b = Math.round(random(255));
for (let j = 0; j < numOfSixteenths; j++) {
tds[k++].setAttribute("style", "background-color:rgb(" + r + "," + g + "," + b + ")");
}
}
resizeCanvas(seq.getBoundingClientRect().width, 20);
document.getElementById("cursor-holder").style.display = "block";
sixteenthWidth = Math.round(tds[0].getBoundingClientRect().width);
drngInv = new DistributedRandomNumberGenerator();
// the probability is directly proportional to the length of the note
// (it is used to decide if a chord should be inverted or not)
/*
for (let i = 0; i < durL2.length; i++) {
let prob = durL2[i] / totDur;
//console.log(prob);
drngInv.addNumber(i, prob);
}
*/
drngInv.addNumber(NO_INV, probNoInv);
drngInv.addNumber(INV, probInv);
drngInv2 = new DistributedRandomNumberGenerator();
drngInv2.addNumber(NO_INV2, probNoInv2);
drngInv2.addNumber(INV2, probInv2);
drngMod = new DistributedRandomNumberGenerator();
drngMod.addNumber(NO_MOD, modProbs[modMode].probNoMod);
drngMod.addNumber(MOD_REL, modProbs[modMode].probModRel);
drngMod.addNumber(MOD_NEXT, modProbs[modMode].probModNext);
drngChgProg = new DistributedRandomNumberGenerator();
drngChgProg.addNumber(CHG_PROG, probChgProg);
drngChgProg.addNumber(NO_CHG_PROG, probNoChgProg);
progrNoteIdx = 0;
/** DRUM BEAT programming */
let groups = [];
let cnt = patSize / 2;
while (true) {
//console.log(cnt)
if (cnt == 2 || cnt == 1) {
groups.push(2);
cnt -= 2;
break;
} else if (cnt == 0) {
break;
}
let increment = random() > 0.5 ? 2 : 3;
groups.push(increment);
cnt -= increment;
}
let groupLength = groups.length;
if (cnt == -1) {
// palindrome pattern
for (let i = groupLength - 2; i >= 0; i--) {
groups.push(groups[i]);
}
} else {
// repeat pattern
for (let i = 0; i < groupLength; i++) {
groups.push(groups[i]);
}
}
let groupStart = [0];
let start = 0;
for (let i = 1; i < groups.length; i++) {
start += groups[i - 1];
groupStart.push(start);
}
// console.log(groups);
// console.log(groupStart);
let hhCombo2 = ["00",
//"01",
//"10",
"11"];
let hhCombo3 = [
"000",
//"001",
"010",
//"011",
//"100",
"101",
//"110",
"111"];
let hh2Combo2 = [
//"01",
//"10",
"11"];
let hh2Combo3 = [
//"001",
"010",
//"011",
//"100",
"101",
//"110",
"111"];
bdPat = [];
snrPat = [];
hhPat = [];
hh2Pat = [];
for (let i = 0; i < patSize; i++) {
let startIdx = groupStart.indexOf(i);
if (i == 0 || (startIdx > -1 && groups[startIdx] == 3 && startIdx % 2 == 0)) {
bdPat.push(1);
} else {
bdPat.push(0);
}
if (startIdx > -1 && groups[startIdx] == 2 && startIdx % 2 != 0) {
snrPat.push(1);
} else {
snrPat.push(0);
}
if (startIdx > -1 && groups[startIdx] == 2) {
let hhCombo = hhCombo2[parseInt(random(hhCombo2.length), 10)];
hhPat.push(parseInt(hhCombo.charAt(0), 10));
hhPat.push(parseInt(hhCombo.charAt(1), 10));
if (hhCombo === "00") {
let hh2Combo = hh2Combo2[parseInt(random(hh2Combo2.length), 10)];
hh2Pat.push(parseInt(hh2Combo.charAt(0), 10));
hh2Pat.push(parseInt(hh2Combo.charAt(1), 10));
} else {
hh2Pat.push(0);
hh2Pat.push(0);
}
} else if (startIdx > -1 && groups[startIdx] == 3) {
let hhCombo = hhCombo3[parseInt(random(hhCombo3.length), 10)];
hhPat.push(parseInt(hhCombo.charAt(0), 10));
hhPat.push(parseInt(hhCombo.charAt(1), 10));
hhPat.push(parseInt(hhCombo.charAt(2), 10));
if (hhCombo === "000") {
let hh2Combo = hh2Combo3[parseInt(random(hh2Combo3.length), 10)];
hh2Pat.push(parseInt(hh2Combo.charAt(0), 10));
hh2Pat.push(parseInt(hh2Combo.charAt(1), 10));
hh2Pat.push(parseInt(hh2Combo.charAt(2), 10));
} else {
hh2Pat.push(0);
hh2Pat.push(0);
hh2Pat.push(0);
}
}
}
// console.log(bdPat);
// console.log(snrPat);
// console.log(hhPat);
// console.log(hh2Pat);
bdIdx = 0;
snrIdx = 0;
hhIdx = 0;
hh2Idx = 0;
seqIdx = 0;
/** performance monitoring */
perf = []
perf8 = []
perf2 = []
perf3 = []
perf4 = []
timer();
// beat (metronome)
timer2();
// drum timers
timer4();
timer5();
timer6();
timer7();
}
function draw() {
background(0, 100, 200);
rect(seqIdx * sixteenthWidth, 0, sixteenthWidth, 20);
}
function timer() {
let t0 = Date.now();
if (monitoring)
perf.push(t0);
if (cancel) {
cancel = false;
if (monitoring) {
if (perf.length % 2 != 0)
perf.pop();
let sum = 0
for (let i = perf.length - 1; i > 0; i--) {
let delta = perf[i] - perf[i - 1];
sum += delta;
}
//console.log(perf);
console.log("perf " + (sum / (perf.length - 1)));
}
return;
}
if (synthOn) {
activeProgr = progr[progrIdx]['progr'];
let restart = false;
if (progrNoteIdx >= activeProgr.length) {
progrNoteIdx = 0;
restart = true;
// console.log("restart " + restart);
// change probabilities
probChgProg = probChgProg > 0 ? probChgProg * 2 : 0.2;
if (probChgProg > 1)
probChgProg = 1;
probNoChgProg = 1 - probChgProg;
drngChgProg.addNumber(CHG_PROG, probChgProg);
drngChgProg.addNumber(NO_CHG_PROG, probNoChgProg);
}
let randMod = drngMod.getDistributedRandomNumber();
// console.log("PROB ---> don't change progr = " + probNoChgProg + " change progr = " + probChgProg);
let randChgProg = drngChgProg.getDistributedRandomNumber();
if (randMod == MOD_NEXT &&
(restart || (randChgProg == CHG_PROG && activeProgr[progrNoteIdx] == 4))) {
// is this a new iteration ...
// ... or are we playing the V chord in a progression? Modulate to a new key!
// console.log("activeKey is maj: " + (activeKey == majKey) + "... modulation to the next key in the circle....");
// recalculate the scale
let activeCircle;
let shiftNext = 0; // how much transposition to apply to get to the next key in the circle
if (activeKey == majKey)
activeCircle = majCircle;
else
activeCircle = minCircle;
for (let i = 0; i < activeCircle.length; i++) {
let diff = activeCircle[i] - tonic;
if (diff < 0) diff *= -1;
if (diff % 12 == 0) {
// found position in the circle
// .. change the key!
shiftNext = activeCircle[(i + 1) % activeCircle.length] - activeCircle[i];
break;
}
}
tonic += shiftNext; // shift the scale
// console.log("new tonic " + midiToString(tonic));
keyTxt.textContent = "Key: " + midiToString(tonic, false) + " " + (activeKey == minKey ? "min" : "maj");
let activeInterval = activeKey == minKey ? minInterval : majInterval;
let note = tonic;
for (let i = 0; i < numNotes; i++) {
midiSequence[i] = note;
note += activeInterval[i];
}
if (randChgProg == CHG_PROG) { // change progr type
// reset probabilities
probChgProg = 0;
probNoChgProg = 1 - probChgProg;
drngChgProg.addNumber(CHG_PROG, probChgProg);
drngChgProg.addNumber(NO_CHG_PROG, probNoChgProg);
if (restart)
progrIdx = enabledProgr[parseInt(random() * enabledProgr.length, 10)];
else
progrIdx = progr0[parseInt(random() * progr0.length, 10)];
progrNoteIdx = 0; // progr0 -> reset to I chord
if (typeof progr[progrIdx] === 'undefined')
progrIdx = enabledProgr[parseInt(random() * enabledProgr.length, 10)];
activeProgr = progr[progrIdx]['progr'];
if (typeof progrWin != "undefined")
updateProgressionViz();
// console.log("new progr " + progrIdx);
}
} else if (randMod == MOD_REL &&
(restart ||
(randChgProg == CHG_PROG && activeKey == majKey && (activeProgr[progrNoteIdx] == majToMin[0]["startKeyIdx"] || activeProgr[progrNoteIdx] == majToMin[1]["startKeyIdx"])) ||
(randChgProg == CHG_PROG && activeKey == minKey && (activeProgr[progrNoteIdx] == minToMaj[0]["startKeyIdx"] || activeProgr[progrNoteIdx] == minToMaj[1]["startKeyIdx"])))
) {
// is this a new iteration ...
// ... or are we in a major/minor key and are we playing the ii/I chord in a progression?
// Modulate to the relative minor/major key!
// let's move to the relative minor/major key
// isRelShifted: boolean that indicates if we have to move to the shifted relative min/maj
// (example, move from Cmaj to Emin or from Emin to Cmaj)
let isMajToMin, circle, nuCircle, interval, modulation, isRelShifted;
if (activeKey == majKey)
isMajToMin = true;
else
isMajToMin = false;
//console.log("modulation to relative minor/major key ... isMajToMin: " + isMajToMin);
activeKey = isMajToMin ? minKey : majKey;
if (typeof progrWin !== "undefined")
updateProgressionList();
circle = isMajToMin ? majCircle : minCircle;
nuCircle = isMajToMin ? minCircle : majCircle;
interval = isMajToMin ? minInterval : majInterval;
if (isMajToMin)
if (activeProgr[progrNoteIdx] == majToMin[0]["startKeyIdx"]) {
modulation = majToMin[0];
isRelShifted = false;
} else {
modulation = majToMin[1];
isRelShifted = true;
}
else {
if (activeProgr[progrNoteIdx] == minToMaj[0]["startKeyIdx"]) {
modulation = minToMaj[0];
isRelShifted = false;
} else {
modulation = minToMaj[1];
isRelShifted = true;
}
}
for (let i = 0; i < circle.length; i++) {
let diff = circle[i] - tonic;
if (diff < 0) diff *= -1;
if (diff % 12 == 0) {
// found position in the circle
// .. change the key!
tonic = !isRelShifted ? nuCircle[i] - diff : isMajToMin ? nuCircle[(i + 1) % nuCircle.length] - diff : nuCircle[(i - 1 < 0 ? nuCircle.length - 1 : i - 1)] - diff;
break;
}
}
// console.log("new tonic " + midiToString(tonic));
keyTxt.textContent = "Key: " + midiToString(tonic, false) + " " + (isMajToMin ? "min" : "maj");
// recalculate the scale
let note = tonic;
for (let i = 0; i < numNotes; i++) {
midiSequence[i] = note;
note += interval[i];
}
if (randChgProg == CHG_PROG) { // change progr type
// reset probabilities
probChgProg = 0;
probNoChgProg = 1 - probChgProg;
drngChgProg.addNumber(CHG_PROG, probChgProg);
drngChgProg.addNumber(NO_CHG_PROG, probNoChgProg);
let progrArr = modulation["progrArr"];
if (progrArr.length == 0 || restart)
progrIdx = enabledProgr[parseInt(random() * enabledProgr.length, 10)];