-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChordSpace.js
3214 lines (2998 loc) · 119 KB
/
ChordSpace.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
/**
C H O R D S P A C E
Copyright (C) 2014 by Michael Gogins
This software is licensed under the terms of the
GNU Lesser General Public License
Part of Silencio, an algorithmic music composition library for Csound.
PLEASE NOTE
This module is deprecated. Please use the code in the WebAssembly build of
CsoundAC instead of this code.
A number of errors in the equivalence classes and symmetries of chord space
exist in this module, but have been fixed in CsoundAC.
There are some differences in the APIs between this module and CsoundAC, but
all functionality is available in CsoundAC with the exception of
ParametricLindenmayer.js.
REGARDING BLUE
Steven Yi's Java program blue, for composing with Csound, uses the Nashorn
JavaScript runtime and does not support the DOM or other objects found in a
Web browser's JavaScript context. To use Silencio in blue:
-- Load sprintf.js and tinycolor.js first.
-- The following polyfill is enough to run some things.
*/
if (typeof console === 'undefined') {
let global = this;
let window = this;
let process = {env: {}};
let console = {};
console.debug = print;
console.warn = print;
console.info = print;
}
(function() {
// All JavaScript dependencies of ChordSpace.js:
// let numeric = require("numeric.js");
// let svd = require("svd.js");
// let Silencio = require("Silencio");
let ChordSpace = {};
ChordSpace.EPSILON = 1;
ChordSpace.epsilonFactor = 1000;
ChordSpace.debug = true;
while (true) {
ChordSpace.EPSILON = ChordSpace.EPSILON / 2;
let nextEpsilon = ChordSpace.EPSILON / 2;
let onePlusNextEpsilon = 1 + nextEpsilon;
if (onePlusNextEpsilon === 1) {
console.info('ChordSpace EPSILON: ' + ChordSpace.EPSILON);
break;
}
}
/**
* C H O R D S P A C E
*
* Copyright 2010, 2011, 2015, 2016, 2020 by Michael Gogins.
*
* This software is licensed under the terms of the GNU Lesser General
* Public License.
*
* This package, part of Silencio, implements a geometric approach to some common
* operations on chords in neo-Riemannian music theory for use in score
* generating procedures:
*
* -- Identifying whether a chord belongs to some equivalence class of music
* theory, or sending a chord to its equivalent within a representative
* fundamental domain of some equivalence class. The equivalence classes are
* octave (O), permutational (P), transpositional, (T), inversional (I), and
* their compounds OP, OPT (set-class or chord type), and OPTI (prime form).
*
* -- Causing chord progressions to move strictly within an orbifold that
* defined by some equivalence class.
*
* -- Implementing chord progressions based on the L, P, R, D, K, Q, and J
* operations of neo-Riemannian theory (thus implementing some aspects of
* "harmony").
*
* -- Implementing chord progressions performed within a more abstract
* equivalence class by means of the best-formed voice-leading within a less
* abstract equivalence class (thus implementing some fundamentals of
* "counterpoint").
*
* -- Implementing the direct product of the four additive groups
* OPTI x T x I x V (V is octavewise revoicings within a given range). This
* provides a set of 4 independent "knobs" for the composer to control prime
* form or set class, transposition, inversion, and voicing (octavewise
* permutation of voices within a specified range).
*
* DEFINITIONS
*
* Pitch is the perception of a distinct sound frequency. It is a logarithmic
* perception; octaves, which sound 'equivalent' in a basic sense, represent
* doublings or halvings of frequency.
*
* Pitches and intervals are represented as real numbers. Middle C is 60 and the
* octave is 12. Our usual system of 12-tone equal temperament, as well as MIDI
* key numbers, are completely represented by the whole numbers; any and all
* other pitches can be represented simply by using fractions.
*
* A voice is a distinct sound that is heard as having a pitch.
*
* A chord is simply a set of voices heard at the same time, represented here
* as a point in a chord space having one dimension of pitch for each voice
* in the chord.
*
* For the purposes of algorithmic composition in Silencio, a score is considered
* to be a sequence of more or less fleeting chords.
*
* EQUIVALENCE CLASSES
*
* An equivalence class identifies elements of a set. Operations that send one
* equivalent point to another induce quotient spaces or orbifolds, where the
* equivalence operation identifies points on one face of the orbifold with
* points on an opposing face. The the space "within" the orbifold is a
* fundamental domain of the equivalence class.
*
* Plain chord space has no equivalence classes. Ordered chords are represented
* as vectors in parentheses (p1, ..., pN). Unordered chords are represented as
* sorted vectors in braces {p1, ..., pN}. Unordering is itself an equivalence
* class (P).
*
* The following equivalence classes apply to pitches and chords, and exist in
* different orbifolds. Equivalence classes can be combined (Callendar, Quinn,
* and Tymoczko, "Generalized Voice-Leading Spaces," _Science_ 320, 2008), and
* the more equivalence classes are combined, the more abstract is the resulting
* orbifold compared to the parent space.
*
* In most cases, a chord space can be divided into a number, possibly
* infinite, of geometrically equivalent fundamental domains for the same
* equivalence class. Therefore, here we use the notion of 'representative'
* fundamental domain. For example, the representative fundamental domain of
* unordered sequences, out of all possible orderings, consists of all sequences
* in their ordinary sorted order. It is important, in the following, to identify
* representative fundamental domains that combine properly, e.g. such that the
* representative fundamental domain of OP / the representative fundamental
* domain of PI equals the representative fundamental domain of OPI. And this in
* turn may require accounting for duplicate elements of the representative
* fundamental domain caused by reflections or singularities in the orbifold.
*
* C Cardinality equivalence, e.g. {1, 1, 2} == {1, 2}. _Not_ assuming
* cardinality equivalence ensures that there is a proto-metric in plain
* chord space that is inherited by all child chord spaces. Cardinality
* equivalence is never assumed here, because we are working in chord
* spaces of fixed dimensionality; e.g. we represent the note middle C
* not as {60}, but as {60, 60, ..., 60}.
*
* O Octave equivalence. The fundamental domain is defined by the pitches
* in a chord spanning the range of an octave or less, and summing to
* an octave or less.
*
* P Permutational equivalence. The fundamental domain is defined by a
* "wedge" of plain chord space in which the voices of a chord are always
* sorted by pitch.
*
* T Transpositional equivalence, e.g. {1, 2} == {7, 8}. The fundamental
* domain is defined as a plane in chord space at right angles to the
* diagonal of unison chords. Represented by the chord always having a
* sum of pitches equal to 0.
*
* TT` A varient of T that preserves equal temperament in chords. The sum
* of the pitches in a chord is the least possible for an equally
* tempered chord.
*
* I Inversional equivalence. Care is needed to distinguish the
* mathematician's sense of 'invert', which means 'pitch-space inversion'
* or 'reflect in a point', from the musician's sense of 'invert', which
* varies according to context but in practice often means 'registral
* inversion' or 'revoice by adding an octave to the lowest tone of a
* chord.' Here, we use 'invert' and 'inversion' in the mathematician's
* sense, and we use the terms 'revoice' and 'voicing' for the musician's
* 'invert' and 'inversion'. The inversion point for any inversion lies
* on the unison diagonal. A fundamental domain is defined as any half of
* chord space that is bounded by a plane at right angles to the unison
* diagonal. Represented as the chord having the first interval
* between voices be smaller than or equal to the final interval
* (recursing for chords of more than 3 voices).
*
* PI Inversional equivalence with permutational equivalence. The
* 'inversion flat' of unordered chord space is a hyperplane consisting
* of all those unordered chords that are invariant under inversion. A
* fundamental domain is defined by any half space bounded by a
* hyperplane containing the inversion flat. It is represented as that
* half of the space on or lower than the hyperplane defined by the
* inversion flat and the unison diagonal.
*
* OP Octave equivalence with permutational equivalence. Tymoczko's orbifold
* for chords; i.e. chords with a fixed number of voices in a harmonic
* context. The fundamental domain is defined as a hyperprism one octave
* long with as many sides as voices and the ends identified by octave
* equivalence and one cyclical permutation of voices, modulo the
* unordering. In OP for trichords in 12TET, the augmented triads run up
* the middle of the prism, the major and minor triads are in 6
* alternating columns around the augmented triads, the two-pitch chords
* form the 3 sides, and the one-pitch chords form the 3 edges that join
* the sides.
*
* OPT The sum of the OP prism as close as possible to the origin, modulo
* the number of voices. Chord type. Note that CM and Cm are different
* OPT. Because the OP prism is canted down from the origin, at least one
* pitch in each OPT chord (excepting the origin itself) is negative.
*
* OPI The OP prism modulo inversion, i.e. 1/2 of the OP prism. The
* representative fundamental consists of those chords less than or equal
* to their inversions modulo OP.
*
* OPTI The OPT sum modulo inversion, i.e. 1/2 of the OPT sum.
* Set-class. Note that CM and Cm are the same OPTI.
*
* TRANSFORMATIONS
*
* Each of the above equivalence classes is, of course, a transformation that
* sends chords outside the fundamental domain to chords inside the fundamental
* domain. And we define the following additional transformations:
*
* T(p, x) Translate p by x.
*
* I(p [, x]) Reflect p in x, by default the origin.
*
* P Send a major triad to the minor triad with the same root,
* or vice versa (Riemann's parallel transformation).
*
* L Send a major triad to the minor triad one major third higher,
* or vice versa (Riemann's Leittonwechsel or leading-tone
* exchange transformation).
*
* R Send a major triad to the minor triad one minor third lower,
* or vice versa (Riemann's relative transformation).
*
* D Send a triad to the next triad a perfect fifth lower
* (dominant transformation).
*
* P, L, and R have been extended as follows, see Fiore and Satyendra,
* "Generalized Contextual Groups", _Music Theory Online_ 11, August 2008:
*
* K(c) Interchange by inversion;
* K(c) := I(c, c[1] + c[2]).
* This is a generalized form of P; for major and minor triads,
* it is exactly the same as P, but it also works with other
* chord types.
*
* Q(c, n, m) Contexual transposition;
* Q(c, n, m) := T(c, n) if c is a T-form of m,
* or T(c, -n) if c is an I-form of M. Not a generalized form
* of L or R; but, like them, K and Q generate the T-I group.
*
* J is from Jessica Rudman, _Common-Tone Preserving Contextual Inversions
* in the Music of Ellen Taaffe Zwilich_, City University of New York, 2015.
* J(c, n [, g [, i]]) Contextual inversion;
* J(c, n [, g [, i]]) returns all (or, optionally, the ith)
* inversion(s) of chord c that preserve the pitch-classes of n
* voices of c. The remaining voices of c invert "around" the
* invariant pitch-classes. The inversions need not preserve any
* equivalence classes with respect to the inverted pitches. If
* there is no such inversion, an empty list is returned. If
* there is more than one such inversion, an ordered list of them
* is returned. Algorithm:
* (1) Create an empty list of inverted chords.
* (2) For each pitch-class from 0 to 12 step g (g is the
* generator of transposition, e.g. 1 in TET):
* (a) Invert c in the pitch-class.
* (b) Test if n pitch-classes of pc are preserved.
* (c) If so, add the inverted chord to the list of
* inversions.
* (d) Sort the list of inversions.
* (3) Return the list of inversions (or, optionally, the ith
* inversion in the list).
*/
ChordSpace.factorial = function(n) {
if (n === 0) {
return 1;
} else {
return n * ChordSpace.factorial(n - 1);
}
};
/**
* Returns whether a is equal to b within acceptable bounds of numerical
* error in the floating-point computations. Used to construct all other
* numerical comparisons.
*/
ChordSpace.eq_epsilon = function(a, b, factor) {
factor = typeof factor !== 'undefined' ? factor : ChordSpace.epsilonFactor;
if (Math.abs(a - b) < (ChordSpace.EPSILON * factor)) {
return true;
}
return false;
};
ChordSpace.gt_epsilon = function(a, b, factor) {
factor = typeof factor !== 'undefined' ? factor : ChordSpace.epsilonFactor;
let eq = ChordSpace.eq_epsilon(a, b, factor);
if (eq) {
return false;
}
if (a > b) {
return true;
}
return false;
};
ChordSpace.lt_epsilon = function(a, b, factor) {
factor = typeof factor !== 'undefined' ? factor : ChordSpace.epsilonFactor;
let eq = ChordSpace.eq_epsilon(a, b, factor);
if (eq) {
return false;
}
if (a < b) {
return true;
}
return false;
};
ChordSpace.ge_epsilon = function(a, b, factor) {
factor = typeof factor !== 'undefined' ? factor : ChordSpace.epsilonFactor;
let eq = ChordSpace.eq_epsilon(a, b, factor);
if (eq) {
return true;
}
if (a > b) {
return true;
}
return false;
};
ChordSpace.le_epsilon = function(a, b, factor) {
factor = typeof factor !== 'undefined' ? factor : ChordSpace.epsilonFactor;
let eq = ChordSpace.eq_epsilon(a, b, factor);
if (eq) {
return true;
}
if (a < b) {
return true;
}
return false;
};
ChordSpace.compare_epsilon = function(a, b) {
if (ChordSpace.lt_epsilon(a, b)) {
return -1;
}
if (ChordSpace.gt_epsilon(a, b)) {
return 1;
}
return 0;
};
ChordSpace.floor = function(x, g) {
g = typeof g !== 'undefined' ? g : 1;
// TODO: Make this work for g <> 1.
let floor_ = Math.floor(x);
return floor_;
}
ChordSpace.ceiling = function(x, g) {
g = typeof g !== 'undefined' ? g : 1;
// TODO: Make this work for g <> 1.
let ceiling_ = Math.ceil(x);
return ceiling_;
}
/**
* The size of the octave, defined to be consistent with 12 tone equal
* temperament and MIDI.
*/
ChordSpace.OCTAVE = 12;
/**
* Middle C, defined to be consistent with MIDI.
*/
ChordSpace.MIDDLE_C = 60;
/**
* Returns the pitch transposed by semitones, which may be any scalar.
* NOTE: Does NOT return the result under any equivalence class.
*/
ChordSpace.T = function(pitch, semitones) {
return pitch + semitones;
};
/**
* Returns the pitch reflected in the center, which may be any pitch.
* NOTE: Does NOT return the result under any equivalence class.
*/
ChordSpace.I = function(pitch, center) {
center = typeof center !== 'undefined' ? center : 0;
return center - pitch;
};
/**
* Returns the Euclidean distance between chords a and b,
* which must have the same number of voices.
*/
ChordSpace.euclidean = function(a, b) {
let sum_of_squared_differences = 0;
for (let voice = 0; voice < a.voices.length; voice++) {
sum_of_squared_differences = sum_of_squared_differences + Math.pow((a.voices[voice] - b.voices[voice]), 2);
}
return Math.sqrt(sum_of_squared_differences);
};
/**
* A chord is one point in a space with one dimension of linear pitch per
* voice. Pitches are represented as semitones with 0 at the origin
* and middle C as 60.
*/
let Chord = function(array_) {
this.voices = [];
this.duration = [];
this.channel = [];
this.velocity = [];
this.pan = [];
if (typeof array_ != 'undefined') {
this.set(array_);
}
};
ChordSpace.Chord = Chord;
/**
* Global of chords defining inversion flat hyperplanes for chords from 3
* through 7 voices, from the _Science_ material.
*/
ChordSpace.inversion_flats = new Map();
/**
* Global of unit normal vectors for reflecting chords in the inversion
* flat that folds OPT to OPTI, for chords from 3 through 7 voices.
*/
ChordSpace.inversion_flat_normals = new Map();
/**
* Returns the number of voices in the chord.
*/
Chord.prototype.size = function() {
return this.voices.length;
};
/**
* Resizes a chord to the specified number of voices.
* Existing voices are not changed. Extra voices are removed.
* New voices are initialized to 0.
*/
Chord.prototype.resize = function(voiceN) {
let original_length = this.voices.length;
this.voices.length = voiceN;
this.duration.length = voiceN;
this.channel.length = voiceN;
this.velocity.length = voiceN;
this.pan.length = voiceN;
for (let voice = original_length; voice < voiceN; voice++) {
this.voices[voice] = 0;
this.duration[voice] = 0;
this.channel[voice] = 0;
this.velocity[voice] = 0;
this.pan[voice] = 0;
}
};
/**
* Resizes the chord to the length of the array, and sets
* the pitches from the values of the array.
*/
Chord.prototype.set = function(array) {
this.resize(array.length);
for (let i = 0; i < this.size(); i++) {
this.voices[i] = array[i];
}
};
Chord.prototype.addVoice = function(pitch) {
this.resize(this.size() + 1);
this.voices[this.size() - 1] = pitch;
return this;
};
Chord.prototype.setPitch = function(voice, value) {
this.voices[voice] = value;
};
Chord.prototype.getPitch = function(voice) {
return this.voices[voice];
};
Chord.prototype.setDuration = function(value) {
for (let voice = 0; voice < this.voices.length; voice++) {
this.duration[voice] = value;
}
};
Chord.prototype.getDuration = function(voice) {
voice = typeof voice !== 'undefined' ? voice : 0;
return this.duration[voice];
};
Chord.prototype.setChannel = function(value) {
for (let voice = 0; voice < this.voices.length; voice++) {
this.channel[voice] = value;
}
};
Chord.prototype.setChannelsToVoices = function(base_voice) {
if (typeof base_voice === 'undefined') {
base_voice = 1;
}
for (let voice = 0; voice < this.voices.length; voice++) {
this.channel[voice] = base_voice + voice;
}
};
Chord.prototype.setPansToVoices = function() {
for (let voice = 0; voice < this.voices.length; voice++) {
this.pan[voice] = (voice + 1) / (this.voices.length + 1);
}
};
Chord.prototype.getChannel = function(voice) {
voice = typeof voice !== 'undefined' ? voice : 0;
return this.channel[voice];
};
Chord.prototype.setVelocity = function(value) {
for (let voice = 0; voice < this.voices.length; voice++) {
this.velocity[voice] = value;
}
};
Chord.prototype.getVelocity = function(voice) {
voice = typeof voice !== 'undefined' ? voice : 0;
return this.velocity[voice];
};
Chord.prototype.setPan = function(value) {
for (let voice = 0; voice < this.voices.length; voice++) {
this.pan[voice] = value;
}
};
Chord.prototype.getPan = function(voice) {
voice = typeof voice !== 'undefined' ? voice : 0;
return this.pan[voice];
};
Chord.prototype.count = function(pitch) {
let n = 0;
for (let voice = 0; voice < this.voices.length; voice++) {
if (ChordSpace.eq_epsilon(this.voices[voice], pitch)) {
n++;
}
}
return n;
};
/**
* Returns a string representation of the chord.
* Quadratic complexity, but short enough not to matter.
*/
Chord.prototype.toString = function() {
let buffer = '[';
for (let voice = 0; voice < this.voices.length; voice++) {
buffer = buffer + sprintf('%12.7f ', this.voices[voice]);
}
buffer = buffer + ']';
return buffer;
};
/**
* Returns a musician-friendly string representation of the chord.
*/
Chord.prototype.toPitches = function() {
let text = ''
for (let voice = 0; voice < this.voices.length; voice++) {
if (voice > 0) {
text = text + " ";
}
text = text + ChordSpace.noteName(this.voices[voice]);
}
return text;
};
/**
* Implements value semantics for ==, for the pitches in this only.
*/
Chord.prototype.eq_epsilon = function(other) {
if (this.voices.length !== other.voices.length) {
return false;
}
for (let voice = 0; voice < this.voices.length; voice++) {
if (ChordSpace.eq_epsilon(this.voices[voice], other.voices[voice]) === false) {
return false;
}
}
return true;
};
Chord.prototype.lt_epsilon = function(other) {
let voiceN = Math.min(this.voices.length, other.voices.length);
for (let voice = 0; voice < voiceN; voice++) {
if (ChordSpace.lt_epsilon(this.voices[voice], other.voices[voice])) {
return true;
}
if (ChordSpace.gt_epsilon(this.voices[voice], other.voices[voice])) {
return false;
}
}
if (this.voices.length < other.voices.length) {
return true;
}
return true;
};
Chord.prototype.gt_epsilon = function(other) {
let voiceN = Math.min(this.voices.length, other.voices.length);
for (let voice = 0; voice < voiceN; voice++) {
if (ChordSpace.gt_epsilon(this.voices[voice], other.voices[voice])) {
return true;
}
if (ChordSpace.lt_epsilon(this.voices[voice], other.voices[voice])) {
return false;
}
}
if (this.voices.length < other.voices.length) {
return false;
}
return true;
};
Chord.prototype.le_epsilon = function(other) {
if (this.eq_epsilon(other)) {
return true;
}
return this.lt_epsilon(other);
};
/**
* Returns whether or not the chord contains the pitch.
*/
Chord.prototype.contains = function(pitch) {
for (let voice = 0; voice < this.voices.length; voice++) {
if (this.voices[voice] === pitch) {
return true;
}
}
return false;
};
ChordSpace.chord_compare_epsilon = function(a, b) {
if (a.lt_epsilon(b)) {
return -1;
}
if (a.gt_epsilon(b)) {
return 1;
}
return 0;
};
/**
* This hash function is used e.g. to give chords value semantics for sets.
*/
Chord.prototype.hash = function() {
var text = '';
for (var voice = 0; voice < this.voices.length; voice++) {
var value = this.voices[voice].toFixed(6).trim();
if (voice === 0) {
text = text.concat(value);
} else {
text = text.concat(',', value);
}
}
return text;
};
/**
* Returns the lowest pitch in the chord,
* and also its voice index.
*/
Chord.prototype.min = function() {
let lowest_voice = 0;
let lowest_pitch = this.voices[lowest_voice];
for (let voice = 1; voice < this.voices.length; voice++) {
if (ChordSpace.lt_epsilon(this.voices[voice], lowest_pitch) === true) {
lowest_pitch = this.voices[voice];
lowest_voice = voice;
}
}
return [lowest_pitch, lowest_voice];
};
/**
* Returns the minimum interval in the chord.
*/
Chord.prototype.minimumInterval = function() {
let minimum_interval = Math.abs(this.voices[1] - this.voices[2]);
for (let v1 = 1; v1 < this.voices.length; v1++) {
for (let v2 = 1; v2 < this.voices.length; v2++) {
if (v1 === v2) {
let interval = Math.abs(this.voices[v1] - this.voices[v2]);
if (interval < minimum_interval) {
minimum_interval = interval;
}
}
}
}
return minimum_interval;
};
/**
* Returns the highest pitch in the chord,
* and also its voice index.
*/
Chord.prototype.max = function() {
let highest_voice = 0;
let highest_pitch = this.voices[highest_voice];
for (let voice = 1; voice < this.voices.length; voice++) {
if (ChordSpace.gt_epsilon(this.voices[voice], highest_pitch) === true) {
highest_pitch = this.voices[voice];
highest_voice = voice;
}
}
return [highest_pitch, highest_voice];
};
/**
* Returns the maximum interval in the chord.
*/
Chord.prototype.maximumInterval = function() {
let maximum_interval = Math.abs(this.voices[1] - this.voices[2]);
for (let v1 = 0; v1 < this.voices.length; v1++) {
for (let v2 = 0; v2 < this.voices.length; v2++) {
if (v1 != v2) {
let interval = Math.abs(this.voices[v1] - this.voices[v2]);
if (interval > maximum_interval) {
maximum_interval = interval;
}
}
}
}
return maximum_interval;
};
Chord.prototype.span = function() {
let top = this.max()[0];
let bottom = this.min()[0];
return top - bottom;
}
/**
* Returns a value copy of the chord.
*/
Chord.prototype.clone = function() {
let clone_ = new Chord();
clone_.resize(this.size());
for (let voice = 0; voice < this.size(); voice++) {
clone_.voices[voice] = this.voices[voice];
clone_.duration[voice] = this.duration[voice];
clone_.channel[voice] = this.channel[voice];
clone_.velocity[voice] = this.velocity[voice];
clone_.pan[voice] = this.pan[voice];
}
return clone_;
};
/**
* Returns the origin of the chord's space.
*/
Chord.prototype.origin = function() {
let clone_ = this.clone();
for (let voice = 0; voice < this.size(); voice++) {
clone_.voices[voice] = 0;
}
return clone_;
};
/**
* Returns the center of OPT, that is, the lowest chord with sum 0 on the
* axis of equally spaced chords, e.g. (-4, 0, 4) for trichords.
*/
Chord.prototype.center = function() {
let n = this.size();
let interval = ChordSpace.OCTAVE / n;
let even = this.clone();
for (let i = 0; i < n; i++) {
even.voices[i] = i * interval;
}
let center_ = even.eT();
return center_;
};
/**
* Returns a new chord whose pitches are the ceilings with respect to g of
* the pitches in this, where g is the generator of transposition.
*/
Chord.prototype.ceiling = function(g) {
g = typeof g !== 'undefined' ? g : 1;
ceiling_ = this.clone();
for (let i = 0; i < this.size(); i++) {
ceiling_.voices[i] = ChordSpace.ceiling(this.voices[i], g);
}
ceiling_.clamp(g);
return ceiling_;
}
/**
* Returns a new chord whose pitches are the floors with respect to g of
* the pitches in this, where g is the generator of transposition.
*/
Chord.prototype.floor = function(g) {
g = typeof g !== 'undefined' ? g : 1;
floor_ = this.clone();
for (let i = 0; i < this.size(); i++) {
floor_.voices[i] = ChordSpace.floor(this.voices[i], g);
}
floor_.clamp(g);
return floor_;
};
Chord.prototype.distanceToOrigin = function() {
let origin_ = this.origin();
return ChordSpace.euclidean(this, origin_);
};
/**
* Returns the sum of the pitches in the chord.
*/
Chord.prototype.sum = function() {
let sum_ = 0;
for (let voice = 0; voice < this.size(); voice++) {
sum_ = sum_ + this.voices[voice];
}
return sum_;
};
Chord.prototype.unisonAtSum = function() {
let unison_ = this.origin();
let pitch = this.sum() / this.size();
for (let voice = 0; voice < this.size(); voice++) {
unison_.voices[voice] = pitch;
}
return unison_;
};
/**
* Returns the Euclidean distance from this chord
* to the unison diagonal of its chord space.
*/
Chord.prototype.distanceToUnisonDiagonal = function() {
let unison_ = this.unisonAtSum();
return ChordSpace.euclidean(this, unison_);
};
/**
* Transposes the chord by the indicated interval (may be a fraction).
* NOTE: Does NOT return the result under any equivalence class.
*/
Chord.prototype.T = function(interval) {
let clone_ = this.clone();
for (let voice = 0; voice < this.size(); voice++) {
clone_.voices[voice] = ChordSpace.T(this.voices[voice], interval);
}
return clone_;
};
/**
* Inverts the chord by another chord that is on the unison diagonal, by
* default the origin.
* NOTE: Does NOT return the result under any equivalence class.
*/
Chord.prototype.I = function(center) {
center = typeof center !== 'undefined' ? center : 0;
let inverse = this.clone();
for (let voice = 0; voice < this.size(); voice++) {
inverse.voices[voice] = ChordSpace.I(this.voices[voice], center);
}
return inverse;
};
/**
* Returns the reflection of this chord specifically in the inversion flat
* within RPT. Preserves the fundamental domain of RP and RPT.
* TODO: Fix for all cardinalities.
*/
Chord.prototype.reflect = function() {
let unit_normal_vector;
if (this.size() === 3) {
// Find a unit vector that is normal to the inversion flat...
let origin_ = this.origin();
let low_normal_endpoint = origin_.clone();
low_normal_endpoint.voices[low_normal_endpoint.size() - 1] = ChordSpace.OCTAVE;
let high_normal_endpoint = origin_.clone();
for (let i = 1; i < high_normal_endpoint.size(); i++ ) {
high_normal_endpoint.voices[i] = ChordSpace.OCTAVE;
}
let normal_vector = high_normal_endpoint.subtract(low_normal_endpoint);
let magnitude = numeric.norm2(normal_vector.voices);
// This is a _unit_ vector normal to the inversion flat.
unit_normal_vector = numeric.div(normal_vector.voices, magnitude);
} else {
unit_normal_vector = ChordSpace.inversion_flat_normals.get(this.size());
}
// H = I_n - 2 * ( u x u), x is outer product.
// For an affine hyperplane, the reflection is:
// Ref(v) = v - 2 {[(v . u) - c] / (u . u)} . u, where c is the distance of the
// hyperplane from the origin.
///let translate_to_origin = numeric.sub(origin, unit_normal_vector);
let translate_to_origin = numeric.sub(this.origin(), unit_normal_vector);
let tensor_ = numeric.tensor(unit_normal_vector, unit_normal_vector);
let product_ = numeric.mul(tensor_, 2);
let identity_ = numeric.identity(this.size());
let householder = numeric.sub(identity_, product_);
let translated_voices = numeric.add(this.voices, translate_to_origin);
let reflected_translated_voices = numeric.dot(householder, translated_voices);
let reflected_voices = numeric.sub(reflected_translated_voices, translate_to_origin);
reflection = new ChordSpace.Chord(reflected_voices);
return reflection;
};
/**
* Moves this chord by adding to it the pitches of another.
* NOTE: Does NOT necessarily return the result under any equivalence
* class.
*/
Chord.prototype.add = function(other) {
let translation = this.clone();
for (let i = 0; i < this.size(); i++) {
translation.voices[i] = this.voices[i] + other.voices[i];
}
return translation;
};
/**
* Moves this chord by subtracting from it the pitches of another.
* NOTE: Does NOT necessarily return the result under any equivalence
* class.
*/
Chord.prototype.subtract = function(other) {
let translation = this.clone();
for (let i = 0; i < this.size(); i++) {
translation.voices[i] = this.voices[i] - other.voices[i];
}
return translation;
};
/**
* Returns the remainder of the dividend divided by the divisor,
* according to the Euclidean definition.
*/
ChordSpace.modulo = function(dividend, divisor) {
let quotient = 0.0;
if (divisor < 0.0) {
quotient = Math.ceil(dividend / divisor);
}
if (divisor > 0.0) {
quotient = Math.floor(dividend / divisor);
}
let remainder = dividend - (quotient * divisor);
return remainder;
};
/**
* Returns the equivalent of the pitch under pitch-class equivalence, i.e.
* the pitch is in the interval [0, OCTAVE).
*/
ChordSpace.epc = function(pitch) {
let pc = ChordSpace.modulo(pitch, ChordSpace.OCTAVE);
return pc;
};
/**
* Returns whether the chord is within the fundamental domain of
* pitch-class equivalence, i.e. is a pitch-class set.
*/
Chord.prototype.isepcs = function() {
for (let voice = 0; voice < this.size(); voice++) {
if (ChordSpace.eq_epsilon(this.voices[voice], ChordSpace.epc(chord.voices[voice])) === false) {
return false;
}
}
return true;
};
Chord.prototype.er = function(range) {
let chord = this.clone();
for (let voice = 0; voice < this.size(); voice++) {
chord.voices[voice] = ChordSpace.modulo(chord.voices[voice], range);
}
return chord;
};