-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstatevector_circles.js
1615 lines (1412 loc) · 54.7 KB
/
statevector_circles.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 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Component that renders a statevector as circles with phases and amplitudes
*
*/
// Number of values in addition to the circuit grid
// stored as metadata in the clip
var NUM_ADDITIONAL_METADATA_VALUES = 8;
// Threshold for regarding a state as having any probability
var PROBABILITY_THRESHOLD = 0.24;
// Value that adjusts velocity when probability affects velocity
var PROBABILITY_VELOCITY_SCALE = 0.6;
// Note velocity when not affected by probability
var CONSTANT_VELOCITY = 120;
var SV_GRID_POS_Y = 5.0;
var SV_GRID_HEIGHT = 160;
var SV_GRID_STEP_WIDTH = 3.125;
var SV_GRID_HORIZ_PADDING = 2; // pixels between each of the four grids
var SV_GRID_FULL_SIZE_MAX_BASIS_STATES = 64;
var SCALES_SHORT_NAME = 'Scales';
var RAGAS_SHORT_NAME = 'Ragas';
var maxDisplayedSteps = 256;
var numSvGrids = 4;
// Inlet 0 receives "viz" messages with a statevector to display
// Inlet 1 receives global phase shift integer from 0 - NUM_PITCHES
// Inlet 2 receives instrument type selection:
// 0: kit (midi is chromatic, from 36 - 51
// 1: diatonic octave -1,
// 2: diatonic octave 0
// 3: diatonic octave 1
// 4: diatonic octave 2
// 5: diatonic octave 3
// 6: diatonic octave 4
// 7: diatonic octave 5
// 8: diatonic octave 6
// Inlet 3 receives name of current clip
// Inlet 4 receives 0 to shift global phase in such a way
// that makes the first basis state (with probability above a threshold)
// have a 0 pitch index (if possible).
// Receiving 1 indicates that global phase should be locked.
// Inlet 5 receives number of semitones to transpose
// Inlet 6 receives messages that indicate whether notes are to be legato
// Inlet 7 receives messages that indicate whether scale is to be reversed
// Inlet 8 receives messages that indicate whether scale is to be halved
// Inlet 9 receives messages that indicate current scale type
// Inlet 10 receives messages that indicate current beats in cycle A
// Inlet 11 receives messages that indicate whether pitch num 15 is a rest
// Inlet 12 receives messages that indicate current beats in cycle B
// Inlet 13 receives 0 for scales, and 1 for ragas, to populate
// the Scales/Ragas slider in the UI.
// Inlet 14 receives messages that indicate whether notes are to be stochastic
// Inlet 15 receives messages that indicate whether notes are to be stochastic
// Inlet 16 receives messages that indicate whether clip is to play only once
// Inlet 17 receives messages that indicate that probability amplitudes affect note velocity
this.inlets = 18;
setinletassist(0, "receives 'viz' messages with a statevector to display");
setinletassist(1, "receives global phase shift integer from 0 - NUM_PITCHES");
setinletassist(2, "receives instrument type selection (0 to 8)");
setinletassist(3, "receives name of current clip");
setinletassist(4, "0 to shift global phase, 1 lock global phase");
setinletassist(5, "receives number of semitones to transpose");
setinletassist(6, "receives messages that indicate whether notes are to be legato");
setinletassist(7, "receives messages that indicate whether scale is to be reversed");
setinletassist(8, "receives messages that indicate whether scale is to be halved");
setinletassist(9, "receives messages that indicate current scale type");
setinletassist(10, "receives messages that indicate current beats in cycle A");
setinletassist(11, "receives messages that indicate whether pitch num 15 is a rest");
setinletassist(12, "receives messages that indicate current beats in cycle B");
setinletassist(13, "receives 0 for scales, and 1 for ragas");
setinletassist(14, "receives messages that indicate whether notes are to be stochastic");
setinletassist(15, "receives messages that indicate whether notes are to be stochastic");
setinletassist(16, "receives messages that indicate whether clip is to play only once");
setinletassist(17, "receives messages that indicate that probability amplitudes affect note velocity");
// Outlet 0 sends global phase shift
// Outlet 1 sends pitch transform index
// Outlet 2 sends number of semitones transposition
// Outlet 3 sends indication of whether notes are to be legato
// Outlet 4 sends indication of whether scale is to be reversed
// Outlet 5 sends indication of whether scale is to be halved
// Outlet 6 sends the current scale type value
// Outlet 7 sends the current beats in cycle A
// Outlet 8 sends indication of whether pitch num 15 is a rest
// Outlet 9 sends the current beats in a cycle B
// Outlet 10 sends 0 if pitch is to be locked, or 1 if phase is to be locked
// Outlet 11 sends width messages to this.device
// Outlet 12 sends 0 if scales, or 1 if ragas, are to be displayed in the UI
// Outlet 13 sends indication of whether notes are to be stochastic
// Outlet 14 sends indication of whether notes are to be quantized
// Outlet 15 sends messages to the grid selection dial
// Outlet 16 sends messages to the gate rotator dial
// Outlet 17 sends indication of whether clip is to be played only once
// Outlet 18 sends indication that probability amplitudes affect note velocity
this.outlets = 19;
setoutletassist(0, "sends global phase shift");
setoutletassist(1, "sends pitch transform index");
setoutletassist(2, "sends number of semitones transposition");
setoutletassist(3, "sends indication of whether notes are to be legato");
setoutletassist(4, "sends indication of whether scale is to be reversed");
setoutletassist(5, "sends indication of whether scale is to be halved");
setoutletassist(6, "sends the current scale type value");
setoutletassist(7, "sends the current beats in cycle A");
setoutletassist(8, "sends indication of whether pitch num 15 is a rest");
setoutletassist(9, "sends the current beats in a cycle B");
setoutletassist(10, "sends 0 if pitch is to be locked, or 1 if phase is to be locked");
setoutletassist(11, "sends width messages to this.device");
setoutletassist(12, "sends 0 if scales, or 1 if ragas, are to be displayed in the UI");
setoutletassist(13, "sends indication of whether notes are to be stochastic");
setoutletassist(14, "sends indication of whether notes are to be quantized");
setoutletassist(15, "sends messages to the grid selection dial");
setoutletassist(16, "sends messages to the gate rotator dial");
setoutletassist(17, "sends indication of whether clip is to be played only once");
setoutletassist(18, "sends indication that probability amplitudes affect note velocity");
sketch.default2d();
var vbrgb = [1., 1., 1., 1.];
// Current statevector
var svArray = [1.0, 0.0, 0.0, 0.0];
// Number of radians to add to the phase of each state to
// apply the desired global phase
var globalPhaseShift = 0.0;
// MIDI (0-127) representation of global phase shift value
var globalPhaseShiftMidi = 0;
// Flag that indicates not to zero the globalPhaseShift
var preserveGlobalPhaseShift = false;
// Flag that indicates whether note duration should be
// until the the next note begins playing.
var legato = false;
// Instrument type selection
// TODO: Find better name
var pitchTransformIndex = 0;
// Number of semitones to transpose
var numTransposeSemitones = 0;
// Use inverted scale
var reverseScale = false;
// Use half the number of pitches in the scale
var halfScale = false;
// Make pitch number 15 a rest
var restPitchNum15 = true;
// Compute note pitches with stochastic approach
var stochasticPitches = false;
var tmpStochasticPitches = false;
// Type of scale to use
var curScaleType = 0; //Major
// Flag that indicates whether to use ragas instead of scales
var useRagasInsteadOfScales = false;
// Length of cycle A
var curCycleLengthA = 2;
// Length of cycle B
var curCycleLengthB = 2;
var prevPiOver8Phase = 0;
var curClipPath = "";
var beatsPerMeasure = 4.0;
var curNumBasisStates = 4;
var quantizeNotes = false;
// When true, the clip won't loop
var oneShot = false;
// When true, probability amplitudes affect velocity
var probsAffectVelocity = true;
// Dictionary for sending notes to Live
var notesDict = {
notes: []
};
// Array booleans indicating whether probability is above threshold
var basisStatesSignificantProbs = [];
var basisStatesProbs = [];
// Array of cumulative probabilities for all basis states
var accumBasisStatesProbs = [];
// The phase for each basis state
var basisStatePiOver8Phases = [];
draw();
refresh();
function msg_int(val) {
if (inlet != 3 && inlet != 14) {
// Turn off stochastic behavior
outlet(13, 'int', 0);
}
if (inlet == 1) {
//preserveGlobalPhaseShift = (val > 0);
globalPhaseShiftMidi = val;
setGlobalPhaseShift(val);
}
else if (inlet == 2) {
pitchTransformIndex = val;
// Conditionally disable Folded and Inverted toggle buttons
var foldedToggle = this.patcher.getnamed("folded_toggle");
var invertedToggle = this.patcher.getnamed("inverted_toggle");
foldedToggle.setattr('active', pitchTransformIndex == 0 ? 0 : 1);
foldedToggle.setattr('ignoreclick', pitchTransformIndex == 0 ? 1 : 0);
invertedToggle.setattr('active', pitchTransformIndex == 0 ? 0 : 1);
invertedToggle.setattr('ignoreclick', pitchTransformIndex == 0 ? 1 : 0);
var qpo = this.patcher.getnamed("qasmpad");
qpo.js.padNoteNamesDirty = true;
computeProbsPhases();
}
else if (inlet == 3) {
var tempPreserveGlobalPhaseShift = preserveGlobalPhaseShift;
preserveGlobalPhaseShift = true;
var qpo = this.patcher.getnamed("qasmpad");
curClipPath = qpo.js.getPathByClipNameIdx(val);
qpo.js.padNoteNamesDirty = true;
populateCircGridFromClip();
// Because different clip is selected, select first circuit grid
qpo.js.setSelCircGridNum(0);
outlet(15, 'int', qpo.js.selCircGridNum);
// Zero and disable the rotator dial
outlet(16, 'int', 0);
qpo.js.enableRotateGateDial(false);
// Set setCurCircNodeType to HAND
qpo.js.setCurCircNodeType(qpo.js.CircuitNodeTypes.HAND);
preserveGlobalPhaseShift = tempPreserveGlobalPhaseShift;
}
else if (inlet == 4) {
// Preserve either global phase, or first pitch with above threshold probability
preserveGlobalPhaseShift = (val > 0);
// Conditionally disable phase shift
var phaseShiftDial = this.patcher.getnamed("phase_shift_dial");
phaseShiftDial.setattr('ignoreclick', val == 0);
var tc = phaseShiftDial.getattr('textcolor', 1, 1, 1, 1);
var alpha = val > 0 ? 1 : 0.2;
phaseShiftDial.setattr('textcolor', tc[0], tc[1], tc[2], alpha);
phaseShiftDial.setattr('slidercolor', tc[0], tc[1], tc[2], alpha);
phaseShiftDial.setattr('tribordercolor', tc[0], tc[1], tc[2], alpha);
}
else if (inlet == 5) {
//preserveGlobalPhaseShift = true;
numTransposeSemitones = val;
var qpo = this.patcher.getnamed("qasmpad");
qpo.js.padNoteNamesDirty = true;
computeProbsPhases();
}
else if (inlet == 6) {
// Make notes legato
legato = (val > 0);
computeProbsPhases();
}
else if (inlet == 7) {
// Make scale reversed
reverseScale = (val > 0);
var qpo = this.patcher.getnamed("qasmpad");
qpo.js.padNoteNamesDirty = true;
computeProbsPhases();
}
else if (inlet == 8) {
// Make scale half its range
halfScale = (val > 0);
var qpo = this.patcher.getnamed("qasmpad");
qpo.js.padNoteNamesDirty = true;
computeProbsPhases();
}
else if (inlet == 9) {
// Set the scale type
curScaleType = val;
var qpo = this.patcher.getnamed("qasmpad");
qpo.js.padNoteNamesDirty = true;
computeProbsPhases();
}
else if (inlet == 10) {
// Set cycle A length
curCycleLengthA = val;
computeProbsPhases();
}
else if (inlet == 11) {
// Make pitch number 15 a rest
restPitchNum15 = (val > 0);
var qpo = this.patcher.getnamed("qasmpad");
qpo.js.padNoteNamesDirty = true;
computeProbsPhases();
}
else if (inlet == 12) {
// Set cycle B length
curCycleLengthB = val;
computeProbsPhases();
}
else if (inlet == 13) {
// Value 0 for scales, 1 for ragas
useRagasInsteadOfScales = (val > 0);
//curScaleType = 0;
// TODO: Decide how to handle
//outlet(6, 'int', 0); // Set to scale/raga index 0
var qpo = this.patcher.getnamed("qasmpad");
var scaleTypeDial = this.patcher.getnamed("scale_type");
scaleTypeDial.setattr('_parameter_shortname',
val == 0 ? SCALES_SHORT_NAME : RAGAS_SHORT_NAME);
// TODO: Load slider/dial with scales or raga names
scaleTypeDial.setattr('_parameter_range',
qpo.js.getMusicalScaleNames(val > 0));
qpo.js.padNoteNamesDirty = true;
computeProbsPhases();
}
else if (inlet == 14) {
// Use stochastic approach for note pitches
stochasticPitches = (val > 0);
var tempPreserveGlobalPhaseShift = preserveGlobalPhaseShift;
preserveGlobalPhaseShift = true;
computeProbsPhases();
preserveGlobalPhaseShift = tempPreserveGlobalPhaseShift;
}
else if (inlet == 15) {
// Quantize notes into harmonic intervals or chords
quantizeNotes = (val > 0);
computeProbsPhases();
}
else if (inlet == 16) {
// Clip plays just once, or loops
oneShot = (val > 0);
computeProbsPhases();
}
else if (inlet == 17) {
// Probabilities affect note velocities
probsAffectVelocity = (val > 0);
computeProbsPhases();
}
}
/**
* Accept a viz message, which visualizes a statevector
*
* @param svlist Statevector as a list of floats, with each pair of floats
* expressing one complex number (without a character such as i
* that symbolizes an imaginary component.
*/
function viz(svlist) {
// Turn off stochastic behavior
outlet(13, 'int', 0);
svArray = svlist.toString().split(' ');
curNumBasisStates = svArray.length / 2;
dimSvGrid();
computeProbsPhases();
}
function dimSvGrid() {
var qpo = this.patcher.getnamed("qasmpad");
for (var gIdx = 0; gIdx < numSvGrids; gIdx++) {
var svGrid = this.patcher.getnamed('svgrid[' + gIdx + ']');
var gridWidth = Math.min(curNumBasisStates, SV_GRID_FULL_SIZE_MAX_BASIS_STATES) * SV_GRID_STEP_WIDTH;
var posX = qpo.js.svGridPosX + gIdx * (gridWidth + SV_GRID_HORIZ_PADDING);
svGrid.setattr('presentation_position', posX, SV_GRID_POS_Y);
svGrid.setattr('presentation_size', gridWidth, SV_GRID_HEIGHT);
svGrid.setattr('columns', Math.floor(curNumBasisStates / numSvGrids));
}
outlet(11, 'setwidth', qpo.js.svGridPosX + numSvGrids * (gridWidth + SV_GRID_HORIZ_PADDING));
}
function clearSvGrid() {
for (var gIdx = 0; gIdx < numSvGrids; gIdx++) {
var svGrid = this.patcher.getnamed('svgrid[' + gIdx + ']');
svGrid.message('clear');
}
}
function setSvGridCell(colIdx, rowIdx, measured) {
var svGridIdx = Math.floor(colIdx / curNumBasisStates * numSvGrids);
var svGrid = this.patcher.getnamed('svgrid[' + svGridIdx + ']');
var svGridColIdx = colIdx % (curNumBasisStates / numSvGrids);
if (measured) {
svGrid.setattr('stepcolor', 0.0, 1.0, 0.0, 1.0);
}
else {
svGrid.setattr('stepcolor', 0.0, 0.0, 1.0, 1.0);
}
svGrid.message('setcell', svGridColIdx + 1, rowIdx + 1, 127);
}
/**
* Sample from the probability distribution of all basis states,
* simulating a measurement
*/
function sampleBasisStatesProbDist() {
var retBasisState = 0;
var qpo = this.patcher.getnamed("qasmpad");
if (accumBasisStatesProbs != null && accumBasisStatesProbs.length == curNumBasisStates) {
var rand = Math.random();
for (var idx = 0; idx < accumBasisStatesProbs.length; idx++) {
retBasisState = idx;
if (rand <= accumBasisStatesProbs[idx]) {
break;
}
}
}
return retBasisState;
}
/**
* Compute probabilities and phases
*/
function computeProbsPhases() {
var qpo = this.patcher.getnamed("qasmpad");
clearSvGrid();
var pitchNums = [];
var numBasisStates = svArray.length / 2;
var numBasisStatesWithNonZeroProbability = 0;
var gamakaType = qpo.js.GamakaTypes.NONE;
var globalPhaseShifted = false;
var cumulativeProbs = 0;
var highestProbability = 0.0;
basisStatesProbs = [];
accumBasisStatesProbs = [];
basisStatePiOver8Phases = [];
basisStatesSignificantProbs = [];
for (var i = 0; i < curNumBasisStates; i++) {
basisStatePiOver8Phases.push(0);
basisStatesSignificantProbs.push(false);
}
for (var svIdx = 0; svIdx < svArray.length; svIdx += 2) {
var real = svArray[svIdx];
var imag = svArray[svIdx + 1];
var amplitude = Math.sqrt(Math.pow(Math.abs(real), 2) + Math.pow(Math.abs(imag), 2));
var probability = Math.pow(Math.abs(amplitude), 2);
if (probability > 0) {
numBasisStatesWithNonZeroProbability++;
}
// Store the probabilities of all the states
basisStatesProbs.push(probability);
// For stochastic option, store the accumulated probabilities of all the states
if (stochasticPitches) {
cumulativeProbs += probability;
accumBasisStatesProbs.push(cumulativeProbs);
}
}
for (var svIdx = 0; svIdx < svArray.length; svIdx += 2) {
var real = svArray[svIdx];
var imag = svArray[svIdx + 1];
var amplitude = Math.sqrt(Math.pow(Math.abs(real), 2) + Math.pow(Math.abs(imag), 2));
var probability = Math.pow(Math.abs(amplitude), 2);
highestProbability = Math.max(highestProbability, probability);
var pitchNum = -1;
if (probability > PROBABILITY_THRESHOLD / numBasisStatesWithNonZeroProbability) {
var polar = cartesianToPolar(real, imag);
// If first basis state with significant probability has non-zero phase,
// shift global phase by its phase
if (!preserveGlobalPhaseShift && !globalPhaseShifted) {
globalPhaseShifted = true;
if (polar.theta < 0) {
polar.theta += 2 * Math.PI;
}
var piOver8Phase = Math.round(polar.theta / (Math.PI / (qpo.js.NUM_PITCHES / 2)));
piOver8Phase += qpo.js.NUM_PITCHES - prevPiOver8Phase;
piOver8Phase = piOver8Phase % qpo.js.NUM_PITCHES;
globalPhaseShiftMidi = (qpo.js.NUM_PITCHES - piOver8Phase) % qpo.js.NUM_PITCHES;
outlet(0, 'int', globalPhaseShiftMidi);
}
var shiftedPhase = polar.theta + globalPhaseShift;
if (shiftedPhase < 0.0) {
shiftedPhase += (2 * Math.PI);
}
pitchNum = Math.round(shiftedPhase / (2 * Math.PI) * qpo.js.NUM_PITCHES + qpo.js.NUM_PITCHES, 0) % qpo.js.NUM_PITCHES;
if (basisStatePiOver8Phases.length > svIdx / 2 &&
basisStatesSignificantProbs.length > svIdx / 2) {
basisStatePiOver8Phases[svIdx / 2] = pitchNum;
basisStatesSignificantProbs[svIdx / 2] = true;
}
else {
post('\nUnexpected basisStatePiOver8Phases length: ' + basisStatePiOver8Phases.length +
' or basisStatesSignificantProbs length: ' + basisStatesSignificantProbs.length);
}
if (!stochasticPitches) {
if (svIdx / 2 < maxDisplayedSteps) {
setSvGridCell((svIdx / 2), pitchNum, false);
}
}
}
if (svIdx / 2 < maxDisplayedSteps) {
if (!basisStateIncluded(svIdx / 2, numBasisStates, curCycleLengthA, curCycleLengthB)) {
for (var pIdx = 0; pIdx < qpo.js.NUM_PITCHES; pIdx++) {
setSvGridCell((svIdx / 2), pIdx, false);
}
}
}
if (!stochasticPitches) {
pitchNums.push(pitchNum);
}
}
// In the case that stochastic pitches are desired,
// iterate once again over the basis states and push
// stochastic pitches into pitchNums
if (stochasticPitches) {
for (var basisStateIdx = 0; basisStateIdx < basisStatePiOver8Phases.length; basisStateIdx++) {
if (basisStatesSignificantProbs[basisStateIdx]) {
var measBasisState = sampleBasisStatesProbDist();
var po8Phase = basisStatePiOver8Phases[measBasisState];
pitchNums.push(po8Phase);
if (basisStateIdx < maxDisplayedSteps) {
setSvGridCell(basisStateIdx, po8Phase, true);
}
}
else {
pitchNums.push(-1);
}
}
}
// Set the notes into the clip
notesDict.notes = [];
var clip = new LiveAPI(curClipPath);
clip.call('remove_notes_extended', 0, 128, 0, 256);
var foundFirstPitch = false;
var formerPitchNum = 0;
var successorPitchNum = 0;
var currentMidiNum = 0;
// Tracks the beats in the loop
var beatIdx = 0;
var velocity = CONSTANT_VELOCITY;
for (var pnIdx = 0; pnIdx < pitchNums.length; pnIdx++) {
if (basisStateIncluded(pnIdx, numBasisStates, curCycleLengthA, curCycleLengthB)) {
currentMidiNum = qpo.js.pitchIdxToMidi(pitchNums[pnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum);
if (pitchNums[pnIdx] > -1 && !(restPitchNum15 && pitchNums[pnIdx] == 15) && currentMidiNum < 127) {
if (!foundFirstPitch) {
prevPiOver8Phase = pitchNums[pnIdx];
foundFirstPitch = true;
}
if (probsAffectVelocity) {
if (basisStatesProbs.length > pnIdx && highestProbability > 0.0) {
velocity = Math.min(Math.floor((basisStatesProbs[pnIdx] / highestProbability) * 127), 127);
// Scale velocity so that volumes drop out more gradually
velocity = Math.min(Math.floor(velocity + ((127 - velocity) * PROBABILITY_VELOCITY_SCALE)));
}
}
else {
velocity = CONSTANT_VELOCITY;
}
//post('\nvelocity for pnIdx ' + pnIdx + ': ' + velocity);
var duration = 0.25;
var successorNoteFound = false;
for (var remPnIdx = pnIdx + 1; remPnIdx < pitchNums.length; remPnIdx++) {
var remMidiNum = qpo.js.pitchIdxToMidi(pitchNums[remPnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[remPnIdx] <= formerPitchNum);
if (pitchNums[remPnIdx] > -1 && !(restPitchNum15 && pitchNums[remPnIdx] == 15) && remMidiNum < 127) {
successorNoteFound = true;
successorPitchNum = pitchNums[remPnIdx];
if (legato) {
if (quantizeNotes) {
duration = Math.max((remPnIdx - pnIdx) / beatsPerMeasure, 1.0);
}
else {
duration = (remPnIdx - pnIdx) / beatsPerMeasure;
}
}
break;
}
}
if (!successorNoteFound) {
if (legato) {
// No successor note was found so duration of final note extends
// to the end of the loop
duration = (pitchNums.length - pnIdx) / beatsPerMeasure;
}
}
if (pitchTransformIndex == 0) {
notesDict.notes.push(
{
pitch: pitchNums[pnIdx] + 36,
start_time: beatIdx / beatsPerMeasure,
duration: duration,
velocity: velocity
}
);
}
else {
gamakaType = pitchIdxToGamaka(pitchNums[pnIdx], curScaleType,
useRagasInsteadOfScales, formerPitchNum);
var gamakaPlayed = false;
if (gamakaType == qpo.js.GamakaTypes.SLIDE_UP_2_PITCHES) {
// Ensure that there is a pitch from which to slide
if (pitchNums[pnIdx] >= 2) {
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(pitchNums[pnIdx] - 2,
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure,
duration: duration * 0.30,
velocity: velocity
}
);
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(pitchNums[pnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.25,
duration: duration * 0.75,
velocity: velocity
}
);
gamakaPlayed = true;
}
}
else if (gamakaType == qpo.js.GamakaTypes.SLIDE_DOWN) {
// Ensure that the previous note is higher in pitch than the
// gamaka note.
if (formerPitchNum > 0 && formerPitchNum > pitchNums[pnIdx]) {
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(formerPitchNum,
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure,
duration: duration * 0.30,
velocity: velocity
}
);
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(pitchNums[pnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.25,
duration: duration * 0.75,
velocity: velocity
}
);
gamakaPlayed = true;
}
}
else if (gamakaType == qpo.js.GamakaTypes.ASCENDING_SLIDE_OSCILLATE) {
// Ensure that the previous note is lower in pitch than the
// gamaka note, and that the following pitch is higher
if (formerPitchNum >= 0 && formerPitchNum < pitchNums[pnIdx] &&
successorPitchNum > pitchNums[pnIdx]) {
// Begin slide from the previous note pitch
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(formerPitchNum,
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure,
duration: duration * 0.50,
velocity: velocity
}
);
// Slide up to pitch in the note following the gamaka
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(successorPitchNum,
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.33, // 1/3
//duration: duration * 0.25,
duration: duration * 0.50,
velocity: velocity
}
);
// Slide down to pitch in the gamaka note
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(pitchNums[pnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
//start_time: beatIdx / beatsPerMeasure + duration * 0.375, // 6/16
start_time: beatIdx / beatsPerMeasure + duration * 0.66, // 2/3
//duration: duration * 0.25,
duration: duration * 0.34,
velocity: velocity
}
);
/*
// Slide up to pitch in the note following the gamaka
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(successorPitchNum,
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.5625, // 9/16
duration: duration * 0.25,
velocity: velocity
}
);
// Slide down to pitch in the gamaka note
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(pitchNums[pnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.75, // 12/16
duration: duration * 0.25,
velocity: velocity
}
);
*/
gamakaPlayed = true;
}
}
else if (gamakaType == qpo.js.GamakaTypes.ASCENDING_OSCILLATE) {
// Ensure that the previous note is lower in pitch than the
// gamaka note.
if (formerPitchNum > 0 && formerPitchNum < pitchNums[pnIdx]) {
// Begin slide from the previous note pitch
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(formerPitchNum,
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure,
duration: duration * 0.30,
velocity: velocity
}
);
// Slide up to pitch of the gamaka note
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(pitchNums[pnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.25,
duration: duration * 0.30,
velocity: velocity
}
);
// Slide down to the previous note pitch
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(formerPitchNum,
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.5,
duration: duration * 0.30,
velocity: velocity
}
);
// Slide back up to pitch of the gamaka note
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(pitchNums[pnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.75,
duration: duration * 0.25,
velocity: velocity
}
);
gamakaPlayed = true;
}
}
else if (gamakaType == qpo.js.GamakaTypes.DESCENDING_OSCILLATE) {
// Ensure that the previous note is higher in pitch than the
// gamaka note.
if (formerPitchNum > 0 && formerPitchNum > pitchNums[pnIdx]) {
// Begin slide from the gamaka pitch
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(pitchNums[pnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure,
duration: duration * 0.30,
velocity: velocity
}
);
// Slide up to pitch of the previous note
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(formerPitchNum,
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.25,
duration: duration * 0.30,
velocity: velocity
}
);
// Slide down to the gamaka note pitch
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(pitchNums[pnIdx],
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,
curScaleType,
useRagasInsteadOfScales,
pitchNums[pnIdx] <= formerPitchNum),
start_time: beatIdx / beatsPerMeasure + duration * 0.5,
duration: duration * 0.30,
velocity: velocity
}
);
// Slide back up to pitch of the previous note
notesDict.notes.push(
{
pitch: qpo.js.pitchIdxToMidi(formerPitchNum,
pitchTransformIndex,
numTransposeSemitones,
reverseScale,
halfScale,