-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsoundac.mjs
1321 lines (1288 loc) · 56.4 KB
/
csoundac.mjs
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
/*
* CSOUNDAC MODULE FOR STRUDEL
*
* Author: Michael Gogins
*
* [csound-ac](https://github.com/gogins/csound-ac), or CsoundAC, is a C++
* algorithmic composition library designed for use with Csound.
*
* [csound-wasm](https://github.com/gogins/csound-wasm) is a WebAssembly
* library containing both Csound and CsoundAC, with a JavaScript API,
* designed for use in Web browsers and npm applications.
*
* This module brings chords and scales, and operations upon them,
* from the CsoundAC library for algorithmic composition into the
* Strudel (Tidal Cycles-based) JavaScript pattern language. This is
* done by deriving from the StatefulPatterns class new classes whose
* member functions become Patterns.
*
* Another use of StatefulPatterns is to define algorithmic note generators,
* often driven by a `pure` pattern that acts as a clock.
*
* Please note, however, that this module, although it defines a number of
* Patterns, is not built into Strudel and is designed to be dynamically
* imported in patches created by users in the Strudel REPL. Therefore, code
* in this module, as with all other modules directly imported in code
* run by the Strudel REPL, must not use template strings.
*/
/**
* Global instance of Csound, shared with Strudel.
*/
let csound = globalThis.__csound__;
/**
* Global instance of CsoundAC.
*/
let csoundac = globalThis.__csoundac__;
/**
* Global reference to the cloud-5 parameters addon.
*/
let parameters = globalThis.__parameters__;
let audioContext = new AudioContext();
import {diagnostic, diagnostic_level, ALWAYS, DEBUG, INFORMATION, WARNING, ERROR, NEVER, StatefulPatterns} from '../statefulpatterns.mjs';
export {diagnostic, diagnostic_level, ALWAYS, DEBUG, INFORMATION, WARNING, ERROR, NEVER, StatefulPatterns};
/**
* Similar to `arrange,` but permits a section to be silenced by setting its
* number of cycles to 0; useful for assembling Patterns into longer-form
* compositions.
*
* @param {...any} sections An array of arrays, in the format
* `[[cycles, Pattern],...]`.
* @returns {Pattern} A Pattern.
*/
export function track(...sections) {
sections = sections.filter(function(element) {
return element[0] >= 1;
});
const total = sections.reduce((sum, [cycles]) => sum + cycles, 0);
sections = sections.map(([cycles, section]) => [cycles, section.fast(cycles)]);
return timeCat(...sections).slow(total);
};
/**
* Returns the frequency corresponding to any of various ways that pitch
* is represented in Strudel events.
*
* @param {Hap} hap A Hap that has some sort of pitch.
* @returns {number} Its frequency in cycles per second.
*/
const getFrequency = (hap) => {
let {
value,
context
} = hap;
// if value is number => interpret as midi number as long as its not marked as frequency
if (typeof value === 'object') {
if (value.freq) {
return value.freq;
}
return getFreq(value.note || value.n || value.value);
}
if (typeof value === 'number' && context.type !== 'frequency') {
value = midiToFreq(hap.value);
} else if (typeof value === 'string' && isNote(value)) {
value = midiToFreq(noteToMidi(hap.value));
} else if (typeof value !== 'number') {
throw new Error('not a note or frequency: ' + value);
}
return value;
};
/**
* A utility that assigns a pitch represented as a MIDI key number to the Hap,
* using the existing pitch property if it exists.
*
* @param {Hap} hap The Hap.
* @param {number} midi_key A MIDI key number.
* @returns {Hap} A new Hap.
*/
export function setPitch(hap, midi_key) {
if (typeof hap.value === 'undefined') {
hap.value = midi_key;
} else if (typeof hap.value === 'object') {
if (typeof hap.value.freq !== 'undefined') {
hap.value.freq = midiToFreq(midi_key);
} else if (typeof hap.value.note !== 'undefined') {
hap.value.note = midi_key;
} else if (typeof hap.value.n !== 'undefined') {
hap.value.n = midi_key;
}
} else {
// Number or string all get the MIDI key.
hap.value = midi_key;
}
return hap;
}
/**
* A utility that returns the MIDI key number for a frequency in Hz,
* as a real number allowing fractions for microtones.
*
* @param {number} frequency The frequency in cycles per second.
* @returns {number} A (possibly fractional) MIDI key number.
*/
export function frequencyToMidiReal(frequency) {
const middle_c = 261.62558;
let octave_ = Math.log(frequency / middle_c) / Math.log(2.) + 8.;
let midi_key = octave_ * 12. - 36.;
return midi_key;
}
/**
* A utility that returns the MIDI key number for a frequency in Hz,
* as the nearest integer.
*
* @param {number} frequency The frequency in cycles per second.
* @returns {number} The MIDI key number as an integer.
*/
export function frequencyToMidiInteger(frequency) {
let midi_key = frequencyToMidiReal(frequency);
return Math.round(midi_key);
}
/**
* A utility for making a _value_ copy of a Chord (or a Scale, which
* is derived from Chord). Object b is resized to the size of a, and a's
* pitches are copied to b. Currently, only pitches are copied.
*
* @param {Chord} a The source Chord (or Scale).
* @param {Chord} b The target Chord (or Scale).
*/
export function Clone(a, b) {
b.resize(a.voices())
for (let voice = 0; voice < a.voices(); ++voice) {
let a_pitch = a.getPitch(voice);
let b_pitch = b.getPitch(voice);
b.setPitch(voice, a_pitch);
if (diagnostic_level() >= DEBUG) registerPatterns(['[voice ', voice, 'a:', a_pitch, 'old b:', b_pitch, 'new b:', b.getPitch(voice), '\n'].join(' '));
}
}
export function print_counter(pattern, counter, value) {
if (value.constructor.name === 'Hap') {
diagnostic('[' + pattern + '] sync: counter: ' + counter + ' value: ' + value.show() + '\n', ALWAYS);
} else if (value.constructor.name === 'Chord') {
diagnostic('[' + pattern + '] sync: counter: ' + counter + ' value: ' + value.toString() + '\n', ALWAYS);
} else {
diagnostic('[' + pattern + '] sync: counter: ' + counter + ' value: ' + value + '\n', ALWAYS);
}
}
let instrument_count = 10;
export function set_instrument_count(new_count) {
let old_count = instrument_count;
instrument_count = new_count;
return old_count;
}
/**
* Returns the RGB color for an HSV color.
*
* @param {number} h The hue.
* @param {number} s The saturation.
* @param {number} v The value.
* @returns {Array} The RGB color.
*/
export function hsvToRgb(h,s,v) {
var rgb, i, data = [];
if (s === 0) {
rgb = [v,v,v];
} else {
h = h / 60;
i = Math.floor(h);
data = [v*(1-s), v*(1-s*(h-i)), v*(1-s*(1-(h-i)))];
switch(i) {
case 0:
rgb = [v, data[2], data[0]];
break;
case 1:
rgb = [data[1], v, data[0]];
break;
case 2:
rgb = [data[0], v, data[2]];
break;
case 3:
rgb = [data[0], data[1], v];
break;
case 4:
rgb = [data[2], data[0], v];
break;
default:
rgb = [v, data[0], data[1]];
break;
}
}
return '#' + rgb.map(function(x){
return ('0a' + Math.round(x*255).toString(16)).slice(-2);
}).join('');
};
let csoundn_counter = 0;
/**
* @function csoundn
*
* @description A Pattern that sends notes to Csound for rendering with MIDI
* semantics. Hap values are translated to Csound pfields as follows:
* <pre>
* p1 -- Csound instrument either as a number (1-based, can be a fraction),
* or as a string name.
* p2 -- time in beats (usually seconds) from start of performance.
* p3 -- duration in beats (usually seconds).
* p4 -- MIDI key number from Strudel's Hap value (as a real number, not an
* integer, in [0, 127].
* p5 -- MIDI velocity from Strudel's `gain` control (as a real number, not
* an integer, in [0, 127].
* p6 -- Spatial depth dimension, from a `depth` control, defaulting to 0.
* p7 -- Spatial pan dimension, from Strudel's `pan` control, in [0, 1],
* defaulting to 0.5.
* p8 -- Spatial height dimension, from a `height` control, defaulting to 0.
* </pre>
* @param {number} instrument The Csound instrument number (p1); may be patternified.
* @param {Pattern} pat The target of this Pattern.
*/
export const csoundn = register('csoundn', (instrument, pat) => {
let p1;
if (typeof instrument === 'string') {
p1 = '\"' + instrument + '\"';
} else {
p1 = instrument;
}
return pat.onTrigger((tidal_time, hap) => {
try {
if (!csound) {
diagnostic('[csoundn]: Csound is not yet loaded.\n', WARNING);
return;
}
// Time in seconds counting from now.
let p2 = tidal_time - getAudioContext().currentTime;
if (p2 < 0) {
p2 = 0;
}
const p3 = hap.duration.valueOf() + 0;
const frequency = getFrequency(hap);
// Translate frequency to MIDI key number _without_ rounding.
const C4 = 261.62558;
let octave = Math.log(frequency / C4) / Math.log(2.0) + 8.0;
const p4 = octave * 12.0 - 36.0;
// We prefer floating point precision, but over the MIDI range [0, 127].
///const p5 = 127 * (hap.context?.velocity ?? 0.9);
let gain;
if (typeof hap.value.gain === 'undefined') {
gain = .9;
} else {
gain = hap.value.gain;
}
let p5 = 127 * gain;
let p6;
if (typeof hap.value.depth === 'undefined') {
p6 = 0;
} else {
p6 = hap.value.depth;
}
let p7;
if (typeof hap.value.pan === 'undefined') {
p7 = 0;
} else {
p7 = hap.value.pan;
}
let p8;
if (typeof hap.value.height === 'undefined') {
p8 = 0;
} else {
p8 = hap.value.depth;
}
const i_statement = ['i', p1, p2, p3, p4, p5, p6, p7, p8, '\n'].join(' ');
console.log('[csoundn] ' + i_statement);
csound.readScore(i_statement);
// Any controls in the Hap that start with 'gi' or 'gk' will be
// treated as Csound control channels, and their values will be
// sent to Csound. Normally, these channels have been defined in
// the Csound orchestra code.
for (let control in hap.value) {
if (control.startsWith('gi') || control.startsWith('gk')) {
csound.SetControlChannel(control, parseFloat(hap.value[control]));
}
}
csoundn_counter = csoundn_counter + 1;
if ((diagnostic_level() >= INFORMATION) === true) {
print_counter('csoundn', csoundn_counter, hap);
}
// Color the event by both insno and gain.
// insno is hue, and gain is value, in HSV.
if (globalThis.haps_from_outputs) {
if (typeof hap.value !== 'object') {
hap.value = {note: p4, gain: gain};
} else {
hap.value.note = p4;
hap.value.gain = gain;
}
hap.value.color = hsvToRgb((p1 / instrument_count) * 360, 1, gain);
globalThis.haps_from_outputs.push(hap);
}
} catch (except) {
diagnostic('[csoundn] error: ' + except + '\n', ERROR);
}
});
});
let chordn_counter = 0;
/**
* Creates and initializes a CsoundAC Chord object. This function should be
* called from module scope in Strudel code before creating any Patterns. The
* Chord class is based on Dmitri Tymoczko's model of chord space, and
* represents an equally tempered chord of the specified number of voices as
* a single point in chord space, where each dimension of the space
* corresponds to one voice of the Chord. Chords are equipped with numerous
* operations from pragmatic music theory, atonal music theory, and
* neo-Riemannian music theory.
*
* @param {string} name The common musical name of the Chord, e.g. "Cb9."
* @returns {Chord} A new CsoundAC Chord object.
*/
export function Chord(name) {
if (diagnostic_level() >= DEBUG) diagnostic('[csacChord] Creating Chord...\n');
let chord_ = csoundac.chordForName(name);
if (diagnostic_level() >= DEBUG) diagnostic('[csacChord]:' + chord_.toString() + '\n');
return chord_;
}
/**
* Creates and initializes a CsoundAC Scale object. This function can be
* called from module scope in Strudel code before creating any Patterns. The
* Scale class is derived from the CsoundAC Chord class, but has been
* equipped with additional methods based on Dimitri Tymoczko's model of
* functional harmony. This enables algorithmically generating Chords from
* scale degrees, transposing Chords by scale degrees, generating all
* possible modulations given a pivot chord, and implementing secondary
* dominants and tonicizations based on scale degree.
*
* @param {Scale} name The common musical name of the Scale, e.g. "C major."
* @returns {Scale} A new Scale object.
*/
export function Scale(name) {
name = name.replace('_', ' ');
if (diagnostic_level() >= DEBUG) diagnostic('[Scale] Creating Scale...\n');
let scale_ = csoundac.scaleForName(name);
if (diagnostic_level() >= DEBUG) diagnostic('[Scale] ' + scale_.name() + '\n');
return scale_;
}
/**
* Creates and initializes a CsoundAC PITV object. This function should be
* called from module scope in Strudel code before creating any Patterns. The
* PITV object is a 4 dimensional cyclic group whose dimensions are TI set
* class (P), chord inversion (I), pitch-class transposition (T), and index
* of octavewise revoicing within the specified range (V). The elements of
* the group are chords in 12 tone equal temperament with the specified
* number of voices. There is a one-to-one mapping between PITV indices and
* chords, such that each voiced chord corresponds to a PITV index, and each
* PITV index corresponds to a voiced chord. This enables algorithmically
* generating harmonies and voicings by independently varying P, I, T, and V.
*
* @param {number} voices The number of voices in the chord space.
* @param {number} bass The lowest pitch (as a MIDI key number) in the chord
* space.
* @param {number} range The range (in MIDI key numbers) of the chord space.
* @returns {PITV} A new PITV object.
*/
export function Pitv(voices, bass, range) {
if (diagnostic_level() >= DEBUG) diagnostic('[Pitv] Creating PITV group...\n');
let pitv = new csoundac.PITV();
pitv.bass = bass;
pitv.initialize(voices, range, 1., false);
pitv.P = 0;
pitv.I = 0;
pitv.T = 0;
pitv.V = 0;
pitv.list(true, false, false);
return pitv;
}
/**
* Creates a class to hold state, and defines Patterns for creating and using
* that state to work with CsoundAC Chords. An instance of this class must be
* created at module scope and passed to the relevant Patterns.
*
* Some hacks are used to co-ordinate state with triggers:
* - Assume that chord changes happen only once at any given time.
* - In the trigger, apply the input to the Pattern if and only if the input
* is different from the old input.
*/
export class ChordPatterns extends StatefulPatterns {
constructor(chord, modality) {
super();
this.registerPatterns();
if (typeof chord === 'string') {
this.ac_chord = csoundac.chordForName(chord);
if (diagnostic_level() >= DEBUG) diagnostic('[ChordPatterns] created new chord.\n');
} else {
this.ac_chord = chord; if (diagnostic_level() >= DEBUG) diagnostic('[ChordPatterns] using existing chord.\n');
}
if (typeof modality == 'undefined') {
this.ac_modality = this.ac_chord;
} else {
this.ac_modality = modality;
}
this.prior_chord = this.ac_chord;
this.value = 0;
this.acC_counter = 0;
this.acC_chord_string = null;
this.acCT_counter = 0;
this.acCT_semitones = null
this.acCI_counter = 0;
this.acCI_center = null;
this.acCK_counter = 0;
this.acCK_state = null;
this.acCQ_counter = 0;
this.acCQ_semitones = null;
this.acCOP_counter = 0;
this.acCRP_counter = 0;
this.acCO_counter = 0;
this.acCV_counter = 0;
this.acCVV_counter = 0;
this.acCVVL_counter = 0;
}
/**
* Applies a Chord or chord name to this.
*
* @param {boolean} is_onset Whether this Hap is the onset of its cycle.
* @param {string} chord_id Identifies the chord.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acC(is_onset, chord_id, hap) {
if (is_onset === true) {
if (typeof chord_id === 'string') {
this.ac_chord = csoundac.chordForName(chord_id);
if (diagnostic_level() >= DEBUG) diagnostic('[acC onset] created new Chord.\n');
} else {
this.ac_scale = scale;
if (diagnostic_level() >= DEBUG) diagnostic('[acC onset] using existing Chord.\n');
}
if (this.acS_chord_string != this.ac_chord.toString()) {
this.acS_chord_string = this.ac_chord.toString();
this.ac_chord = this.ac_scale.chord(1, this.voices, 3);
if (diagnostic_level() >= WARNING) {
diagnostic(['[acS onset] new Chord:', this.ac_chord.toString(), this.ac_chord.name(), '\n'].join(' '));
}
this.acC_counter = this.acC_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acC', this.acC_counter, hap);
}
}
}
return hap;
}
/**
* Applies a transposition to the Chord of this.
*
* @param {boolean} is_onset Indicates whether or not this is Hap onset of its cycle.
* @param {number} semitones Number of semitones to transpose; may be negative.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acCT(is_onset, semitones, hap) {
if (is_onset === true) {
if (this.acCT_semitones != semitones) {
this.acCT_semitones = semitones;
if (diagnostic_level() >= DEBUG) diagnostic(['[acCT onset] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.ac_chord = this.ac_chord.T(semitones);
if (diagnostic_level() >= WARNING) diagnostic(['[acCT onset] transformed chord:', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.acCT_counter = this.acCT_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acCT', this.acCT_counter, hap);
}
}
}
return hap;
}
/**
* Applies an inversion to the Chord of this. The transformation can be
* patternified with a Pattern of flips (changes in the value of the flip
* input).
*
* @param {boolean} is_onset Indicates whether or not this is Hap onset of its cycle.
* @param {number} center The center of reflection.
* @param {Hap} hap The current Hap.
* @returns {Hap} The new Hap.
*/
acCI(is_onset, center, flip, hap) {
if (is_onset === true) {
if (this.acCI_flip != flip) {
this.acCI_flip = flip;
if (diagnostic_level() >= DEBUG) diagnostic(['[acCI] onset: current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.ac_chord = this.ac_chord.I(center);
if (diagnostic_level() >= WARNING) diagnostic(['[acCI] onset: transformed chord:', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.acCI_counter = this.acCI_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acCI', this.acCI_counter, hap);
}
}
}
return hap;
}
/**
* Applies the interchange by inversion operation of the Generalized
* Contextual Group of Fiore and Satyendra to the Chord of this. The
* transformation can be patternified with a Pattern of flips (changes in
* the value of the flip input).
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {number} flip If this value changes, the transformation is applied.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acCK(is_onset, flip, hap) {
if (is_onset === true) {
if (this.flip != flip) {
this.flip = flip;
if (diagnostic_level() >= DEBUG) diagnostic(['[acCK onset] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.ac_chord = this.ac_chord.K();
if (diagnostic_level() >= WARNING) diagnostic(['[acCK onset] transformed chord:', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.acCK_counter = this.acCK_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acCK', this.acCK_counter, hap);
}
}
}
return hap;
}
/**
* Applies the contexual transposition operation of the Generalized
* Contextual Group of Fiore and Satyendra to the Chord of this. The
* modality is set in the constructor of this class.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {number} semitones The number of semitones by which this Chord
* is to be tranposed; may be negative.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acCQ(is_onset, semitones, hap) {
if (is_onset === true) {
if (diagnostic_level() >= DEBUG) diagnostic(['[acCQ onset] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.ac_chord = this.ac_chord.Q(semitones, this.ac_modality, 1);
if (diagnostic_level() >= WARNING) diagnostic(['[acCQ onset] transformed chord:', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.acCQ_counter = this.acCQ_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acCQ', this.acCQ_counter, hap);
}
}
return hap;
}
/**
* Transforms the Chord of this to its 'OP' form; 'chord' is an extremely
* flexible and therefore ambiguous term, but the 'OP' form is what most
* musicians usually mean by 'chord': A chord where the octaves of the
* pitches do not matter and the order of the voices does not matter. This
* transformation can be useful for returning chords that have been
* transformed such that their voices are out of range back to a more
* normal form.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acCOP(is_onset, hap) {
if (is_onset === true) {
if (diagnostic_level() >= DEBUG) diagnostic(['[acCOP onset] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.ac_chord = this.ac_chord.eOP();
if (diagnostic_level() >= WARNING) diagnostic(['[acCOP onset] transformed chord:', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.acCOP_counter = this.acCOP_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acCOP', this.acCOP_counter, hap);
}
}
return hap;
}
/**
* Transforms the Chord of this to its 'RP' form; 'chord' is an extremely
* flexible and therefore ambiguous term, but the 'RP' form is a chord
* where the octaves are folded within the indicated range, and like 'OP'
* the order of the voices does not matter. This
* transformation can be useful for returning chords that have been
* transformed such that their voices are out of range back to a user-
* defined range.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {number} range The range of this chord space.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acCRP(is_onset, range, hap) {
if (is_onset === true) {
if (diagnostic_level() >= DEBUG) diagnostic(['[acCRP onset] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.ac_chord = this.ac_chord.eRP(range);
if (diagnostic_level() >= WARNING) diagnostic(['[acCRP onset] transformed chord:', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.acCRP_counter = this.acCRP_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acCRP', this.acCRP_counter, hap);
}
}
return hap;
}
/**
* Applies the Chord of this to the _pitch-class_ of the Hap, i.e., moves
* the _pitch-class_ of the Hap to the nearest _pitch-class_ of the Chord.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acCV(is_onset, hap) {
if (is_onset === true) {
let frequency;
try {
frequency = getFrequency(hap);
} catch (error) {
diagnostic('[acCV value]: not a note!\n');
return;
}
let current_midi_key = frequencyToMidiInteger(frequency);
let epcs = this.ac_chord.epcs();
if (diagnostic_level() >= DEBUG) diagnostic(['[acCV value] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), '\n'].join(' '));
if (diagnostic_level() >= DEBUG) diagnostic(['[acCV value] current hap: ', hap.show(), '\n'].join(' '));
let note = csoundac.conformToPitchClassSet(current_midi_key, epcs);
hap = setPitch(hap, note);
ChordPatterns.acCV_counter = ChordPatterns.acCV_counter + 1;
if (diagnostic_level() >= WARNING) diagnostic(['[acCV value] new hap: ', hap.show(), '\n'].join(' '));
if (diagnostic_level() >= INFORMATION) {
print_counter('acCV onset', ChordPatterns.acCV_counter, hap);
}
} else {
let frequency;
try {
frequency = getFrequency(hap);
} catch (error) {
diagnostic('[acCV value]: not a note!\n');
return;
}
let current_midi_key = frequencyToMidiInteger(frequency);
let epcs = this.ac_chord.epcs();
if (diagnostic_level() >= DEBUG) diagnostic(['[acCV value] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), '\n'].join(' '));
if (diagnostic_level() >= DEBUG) diagnostic(['[acCV value] current hap: ', hap.show(), '\n'].join(' '));
let note = csoundac.conformToPitchClassSet(current_midi_key, epcs);
hap = setPitch(hap, note);
//~ if (diagnostic_level() >= DEBUG) diagnostic(['[acCV value] new hap: ', hap.show(), '\n'].join(' '));
//~ if (diagnostic_level() >= INFORMATION) {
//~ print_counter('acCV value', ChordPatterns.acCV_counter, hap);
//~ }
}
return hap;
}
/**
* acCO: Transforms the Chord of this by the indicated number of
* octavewise revoicings: negative means subtract an octave
* from the highest voice, positive means add an octave to the
* lowest voice. This corresponds to the musician's notion of
* "inversion."
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {number} revoicings The number of octavewise revoicings to apply.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acCO(is_onset, revoicings, hap) {
if (is_onset) {
if (diagnostic_level() >= DEBUG) diagnostic(['[acCO] onset: current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.ac_chord = this.ac_chord.v(revoicings);
if (diagnostic_level() >= WARNING) diagnostic(['[acCO] onset: transformed chord:', this.ac_chord.toString(), this.ac_chord.eOP().name(), hap.show(), '\n'].join(' '));
this.acCO_counter = this.acCO_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acCO', this.acCO_counter, hap);
}
this.prior_chord = this.ac_chord;
}
return hap;
}
/**
* acCVV: Generate a note that represents a particular voice of the
* Chord.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {number} bass The MIDI key number of the lowest pitch.
* @param {number} voice The number of the voice of the Chord to use.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acCVV(is_onset, bass, voice, hap) {
let new_midi_key = bass + this.ac_chord.getPitch(voice);
hap = setPitch(hap, new_midi_key);
if (diagnostic_level() >= DEBUG) diagnostic(['[acCVV value]:', 'new_midi_key:', new_midi_key, 'new note:', hap.show(), '\n'].join(' '));
this.prior_chord = this.ac_chord;
return hap;
}
/**
* acCVVL: Generate a note that represents a particular voice of the
* Chord, as the closest voice-leading from the prior Chord.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {number} bass The MIDI key of the lowest pitch to use.
* @param {number} range The range in MIDI keys. Pitches are wrapped back up or down
* if the revoicing takes them out of this range.
* @param {number} voice The number of the voice in the Chord to use.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acCVVL(is_onset, bass, range, voice, hap) {
if (this.prior_chord != this.ac_chord) {
let new_chord = csoundac.voiceleadingClosestRange(this.prior_chord, this.ac_chord, range, true);
const message = ['[acCVVL]:', '\n prior_chord: ', this.prior_chord.toString(), '\n ac_chord: ', this.ac_chord.toString(), '\n new ac_chord:',new_chord.toString() + '\n'].join(' ');
if (diagnostic_level() >= DEBUG) diagnostic(message);
console.log(message);
this.ac_chord = new_chord;
}
let new_midi_key = bass + this.ac_chord.getPitch(voice);
hap = setPitch(hap, new_midi_key);
if (diagnostic_level() >= DEBUG) diagnostic(['[acCVVL value]:', 'new_midi_key:', new_midi_key, 'new note:', hap.show(), '\n'].join(' '));
this.prior_chord = this.ac_chord;
return hap;
}
}
/**
* Creates a class to hold state, and defines Patterns for creating and using
* that state to work with CsoundAC Scales. An instance of this class must be
* created at module scope and passed to the relevant Patterns. The
* constructor sets the number of voices in Chords associated with the Scale,
* by default 4.
*
* State is co-ordinated with the triggers of the Patterns by only updating
* the state when the input of the Pattern changes.
*/
export class ScalePatterns extends StatefulPatterns {
constructor(scale, voices = 3) {
super();
this.registerPatterns();
this.voices = voices;
if (typeof scale === 'string') {
// Have to use underscores instead of spaces in the Strudel REPL.
scale = scale.replace('_', ' ');
this.ac_scale = csoundac.scaleForName(scale);
if (diagnostic_level() >= WARNING) diagnostic('[acS onset] created new scale.\n');
} else {
this.ac_scale = scale;
if (diagnostic_level() >= DEBUG) diagnostic('[acS onset] using existing scale.\n');
}
this.ac_chord = this.ac_scale.chord(1, this.voices, 3);
this.prior_chord = this.ac_chord;
this.acS_counter = 0;
this.acS_scale_string = null;
this.acSS_counter = 0;
this.acSS_scale_step = null;
this.acST_counter = 0;
this.acST_scale_steps = null;
this.acSM_counter = 0;
this.acSM_index = null;
this.acSO_counter = 0;
this.acSV_counter = 0;
this.acSCV_counter = 0;
}
/**
* acS: Insert a CsoundAC Scale into the Pattern's state.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {Scale} scale The Scale object to be used.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acS(is_onset, scale, hap) {
if (is_onset === true) {
if (typeof scale === 'string') {
// Have to use underscores instead of spaces in the Strudel REPL.
scale = scale.replace('_', ' ');
this.ac_scale = csoundac.scaleForName(scale);
if (diagnostic_level() >= DEBUG) diagnostic('[acS onset] created new scale.\n');
} else {
this.ac_scale = scale;
if (diagnostic_level() >= DEBUG) diagnostic('[acS onset] using existing scale.\n');
}
if (this.acS_scale_string != this.ac_scale.toString()) {
this.acS_scale_string = this.ac_scale.toString();
this.ac_chord = this.ac_scale.chord(1, this.voices, 3);
if (diagnostic_level() >= WARNING) {
diagnostic(['[acS onset] new scale:', this.ac_scale.toString(), this.ac_scale.name(), '\n'].join(' '));
diagnostic(['[acS onset] new chord:', this.ac_chord.toString(), this.ac_chord.name(), '\n'].join(' '));
}
this.acS_counter = this.acS_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acS', this.acS_counter, hap);
}
}
}
return hap;
}
/**
* acSS: Insert the Chord at the specified scale step of the Scale in
* the Pattern's state, into the state.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {number} scale_step The specific scale step of the Chord in the Scale.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acSS(is_onset, scale_step, hap) {
if (is_onset === true) {
if (this.acSS_scale_step != scale_step) {
this.acSS_scale_step = scale_step;
if (diagnostic_level() >= DEBUG) diagnostic(['[acSS onset] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), '\n'].join(' '));
this.ac_chord = this.ac_scale.chord(scale_step, this.voices, 3);
if (diagnostic_level() >= WARNING) diagnostic(['[acSS onset] transformed chord:', this.ac_chord.toString(), this.ac_chord.eOP().name(), '\n'].join(' '));
this.acSS_counter = this.acSS_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acSS', this.acSS_counter, hap);
}
}
}
return hap;
}
/**
* acST: Transpose the Chord in the Pattern's state by the specified
* number of scale steps in the Scale in the state.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {number} scale_steps The number of steps in this Scale by which to
* transpose the Chord in this.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acST(is_onset, scale_steps, hap) {
if (is_onset === true) {
if (this.acST_scale_steps != scale_steps) {
this.acST_scale_steps = scale_steps;
if (diagnostic_level() >= WARNING) diagnostic(['[acST onset] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), '\n'].join(' '));
this.ac_chord = this.ac_scale.transpose_degrees(this.ac_chord, scale_steps, 3);
if (diagnostic_level() >= WARNING) diagnostic(['[acST onset] transformed chord:', this.ac_chord.toString(), this.ac_chord.eOP().name(), '\n'].join(' '));
this.acST_counter = this.acST_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acST', this.acST_counter, hap);
}
}
}
return hap;
}
/**
* acSM: Modulate from the Scale in the Pattern's state, using the
* Chord in the state as a pivot, choosing one of the possible
* modulations by index.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {number} index The index of the specific modulation to be used.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acSM(is_onset, index, hap) {
if (is_onset === true) {
if (this.acSM_index != index) {
this.acSM_index = index;
let pivot_chord_eop = this.ac_chord.eOP();
let possible_modulations = this.ac_scale.modulations(pivot_chord_eop);
let new_scale = this.ac_scale;
let modulation_count = possible_modulations.size();
let wrapped_index = -1;
if (modulation_count > 0) {
wrapped_index = index % modulation_count;
new_scale = possible_modulations.get(wrapped_index);
if (diagnostic_level() >= WARNING) {
diagnostic('[acSM onset] modulating in: ' + this.ac_scale.toString() + ' ' + this.ac_scale.name() + '\n');
diagnostic('[acSM onset] from pivot: ' + pivot_chord_eop.toString(), + ' ' + pivot_chord_eop.name() + '\n');
diagnostic('[acSM onset] modulations: ' + modulation_count + ' => ' + wrapped_index + '\n');
diagnostic('[acSM onset] modulated to: ' + new_scale.toString() + ' ' + new_scale.name() + '\n');
diagnostic('[acSM onset] hap: ' + hap.show() + '\n');
}
this.ac_scale = new_scale;
}
this.acSM_counter = this.acSM_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acSM', this.acSM_counter, hap);
}
}
}
return hap;
}
/**
* acSV: Move notes in the Pattern to fit the Scale in the Pattern's
* state.
*
* @param {boolean} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {Hap} The current Hap.
* @returns {Hap} A new Hap.
*/
acSV(is_onset, hap) {
if (is_onset === true) {
let frequency;
try {
frequency = getFrequency(hap);
} catch (error) {
diagnostic('[acSV value] not a note!\n');
return;
}
let current_midi_key = frequencyToMidiInteger(frequency);
let epcs = this.ac_scale.epcs();
if (diagnostic_level() >= DEBUG) diagnostic(['[acSV value] current scale: ', this.ac_scale.toString(), this.ac_scale.eOP().name(), '\n'].join(' '));
if (diagnostic_level() >= DEBUG) diagnostic(['[acSV value] current hap: ', hap.show(), '\n'].join(' '));
let note = csoundac.conformToPitchClassSet(current_midi_key, epcs);
hap = setPitch(hap, note);
if (diagnostic_level() >= WARNING) diagnostic(['[acSV value] new hap: ', hap.show(), '\n'].join(' '));
this.acSV_counter = this.acSV_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acSV', this.acSV_counter, hap);
}
} else {
let frequency;
try {
frequency = getFrequency(hap);
} catch (error) {
diagnostic('[acSV value] not a note!\n');
return;
}
let current_midi_key = frequencyToMidiInteger(frequency);
let epcs = this.ac_scale.epcs();
if (diagnostic_level() >= DEBUG) diagnostic(['[acSV value] current scale: ', this.ac_scale.toString(), this.ac_scale.eOP().name(), '\n'].join(' '));
if (diagnostic_level() >= DEBUG) diagnostic(['[acSV value] current hap: ', hap.show(), '\n'].join(' '));
let note = csoundac.conformToPitchClassSet(current_midi_key, epcs);
hap = setPitch(hap, note);
if (diagnostic_level() >= DEBUG) diagnostic(['[acSV value] new hap: ', hap.show(), '\n'].join(' '));
}
return hap;
}
/**
* acSCV: Move notes in the Pattern to fit the Chord in the Pattern's
* state.
*
* @param {number} is_onset Indicates whether this Hap is the onset of its cycle.
* @param {Hap} hap The current Hap.
* @returns {Hap} A new Hap.
*/
acSCV(is_onset, hap) {
if (is_onset === true) {
let frequency;
try {
frequency = getFrequency(hap);
} catch (error) {
diagnostic('[acSCV value] not a note!\n');
return;
}
let current_midi_key = frequencyToMidiInteger(frequency);
let epcs = this.ac_chord.epcs();
if (diagnostic_level() >= DEBUG) diagnostic(['[acSCV onset] current scale: ', this.ac_scale.toString(), this.ac_scale.eOP().name(), '\n'].join(' '));
if (diagnostic_level() >= DEBUG) diagnostic(['[acSCV onset] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), '\n'].join(' '));
if (diagnostic_level() >= DEBUG) diagnostic(['[acSCV onset] current hap: ', hap.show(), '\n'].join(' '));
let note = csoundac.conformToPitchClassSet(current_midi_key, epcs);
hap = setPitch(hap, note);
if (diagnostic_level() >= WARNING) diagnostic(['[acSCV onset] new hap: ', hap.show(), '\n'].join(' '));
this.acSCV_counter = this.acSCV_counter + 1;
if (diagnostic_level() >= INFORMATION) {
print_counter('acSCV', this.acSCV_counter, hap);
}
} else {
let frequency;
try {
frequency = getFrequency(hap);
} catch (error) {
diagnostic('[acSCV value] not a note!\n');
return;
}
let current_midi_key = frequencyToMidiInteger(frequency);
let epcs = this.ac_chord.epcs();
if (diagnostic_level() >= DEBUG) diagnostic(['[acSCV value] current scale: ', this.ac_scale.toString(), this.ac_scale.eOP().name(), '\n'].join(' '));
if (diagnostic_level() >= DEBUG) diagnostic(['[acSCV value] current chord: ', this.ac_chord.toString(), this.ac_chord.eOP().name(), '\n'].join(' '));
if (diagnostic_level() >= DEBUG) diagnostic(['[acSCV value] current hap: ', hap.show(), '\n'].join(' '));
let note = csoundac.conformToPitchClassSet(current_midi_key, epcs);
hap = setPitch(hap, note);