@kairotic/app 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/index.html +1101 -0
- package/kairotic/api.cljs +362 -0
- package/kairotic/assistant.cljs +505 -0
- package/kairotic/backends/gun-backend.js +371 -0
- package/kairotic/canvas.cljs +751 -0
- package/kairotic/code.cljs +108 -0
- package/kairotic/components.cljs +838 -0
- package/kairotic/core.cljs +96 -0
- package/kairotic/corefns.cljs +473 -0
- package/kairotic/docs.mjs +2 -0
- package/kairotic/engine.cljs +482 -0
- package/kairotic/examples.mjs +96 -0
- package/kairotic/faust.cljs +225 -0
- package/kairotic/help.cljs +1036 -0
- package/kairotic/hiccup.cljs +566 -0
- package/kairotic/input.cljs +271 -0
- package/kairotic/kfile.mjs +138 -0
- package/kairotic/library.cljs +214 -0
- package/kairotic/links.cljs +41 -0
- package/kairotic/mt.cljs +1604 -0
- package/kairotic/mtboxes.cljs +276 -0
- package/kairotic/packages/analysis.mjs +64 -0
- package/kairotic/packages/controls.mjs +28 -0
- package/kairotic/packages/data.mjs +22 -0
- package/kairotic/packages/faust-units/cmaes.mjs +82 -0
- package/kairotic/packages/faust-units/graph.mjs +212 -0
- package/kairotic/packages/faust-units/listen.mjs +125 -0
- package/kairotic/packages/faust-units/notation.mjs +139 -0
- package/kairotic/packages/faust-units/parse.mjs +439 -0
- package/kairotic/packages/faust-units/recipe.mjs +195 -0
- package/kairotic/packages/faust-units/render.mjs +69 -0
- package/kairotic/packages/faust-units/units.mjs +130 -0
- package/kairotic/packages/faust-units/view.mjs +353 -0
- package/kairotic/packages/faust-units.mjs +9 -0
- package/kairotic/packages/io.mjs +58 -0
- package/kairotic/packages/net.mjs +28 -0
- package/kairotic/packages/patterns.mjs +34 -0
- package/kairotic/packages/spatial.mjs +22 -0
- package/kairotic/packages/spectral/shift.mjs +169 -0
- package/kairotic/packages/spectral.mjs +16 -0
- package/kairotic/packages/synth.mjs +28 -0
- package/kairotic/packages/theory/counterpoint.mjs +798 -0
- package/kairotic/packages/theory/mirror.mjs +221 -0
- package/kairotic/packages/theory.mjs +46 -0
- package/kairotic/patterns.cljs +538 -0
- package/kairotic/presets.cljs +227 -0
- package/kairotic/quantize.cljs +782 -0
- package/kairotic/record.cljs +169 -0
- package/kairotic/release.cljs +90 -0
- package/kairotic/ring.cljs +129 -0
- package/kairotic/ringaudio.cljs +232 -0
- package/kairotic/ringcore.mjs +270 -0
- package/kairotic/score.cljs +176 -0
- package/kairotic/shapes.cljs +283 -0
- package/kairotic/signal.cljs +327 -0
- package/kairotic/sounds.cljs +565 -0
- package/kairotic/sounds.mjs +108 -0
- package/kairotic/span.cljs +129 -0
- package/kairotic/spell.cljs +281 -0
- package/kairotic/structure.cljs +145 -0
- package/kairotic/talk.cljs +115 -0
- package/kairotic/tempo.cljs +226 -0
- package/kairotic/treecanvas.cljs +299 -0
- package/kairotic/tutorials.mjs +1283 -0
- package/kairotic/vendor/codemirror.mjs +23 -0
- package/package.json +23 -4
- package/page/00-work.cljs +862 -0
- package/page/01-commands.cljs +1219 -0
- package/page/02-patch-view.cljs +1051 -0
- package/page/03-history-view.cljs +481 -0
- package/page/04-panels.cljs +448 -0
- package/page/05-gestures.cljs +1247 -0
- package/page/06-connection.cljs +219 -0
- package/page/07-claude.cljs +430 -0
- package/page/08-sessions.cljs +423 -0
- package/page/09-learn.cljs +602 -0
- package/page/10-embed.cljs +469 -0
- package/page/11-startup.cljs +61 -0
- package/page/12-feedback.cljs +172 -0
- package/page/13-perform.cljs +325 -0
- package/page/14-editor.cljs +656 -0
- package/page/15-presets.cljs +230 -0
- package/page/16-release.cljs +242 -0
- package/page/17-sounds.cljs +147 -0
- package/page/18-lines.cljs +594 -0
- package/page/19-files.cljs +247 -0
- package/page/19-html.cljs +152 -0
- package/page/20-talk.cljs +527 -0
- package/page/21-net.cljs +110 -0
- package/page/22-learn.cljs +303 -0
- package/page/23-ids.cljs +233 -0
- package/page/24-clipboard.cljs +136 -0
- package/page/25-tidy.cljs +74 -0
- package/page/26-probes.cljs +111 -0
- package/sources.mjs +2 -0
|
@@ -0,0 +1,798 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
// @kairotic/theory · counterpoint.mjs: the canon box's counterpoint, two voices note by note (discussion #172).
|
|
3
|
+
//
|
|
4
|
+
// The kernel is Study 43's (studies/43-mirrors/kairotic/kernel.mjs, copied as it is: the hard rules of meter,
|
|
5
|
+
// dissonance and the mode for each voice and between them, ties and suspensions, fourth species, the system as
|
|
6
|
+
// data, the degree of imitation, the comes inverted or augmented, the futures counted exactly over pairs). After it, what the box adds for playing (`live`): the soft
|
|
7
|
+
// rules of Study 41 as costs, the mode as a spec, and a note asked for (a pitch, any key) corrected to the nearest
|
|
8
|
+
// legal note with a future.
|
|
9
|
+
//
|
|
10
|
+
// A pitch is a diatonic step on the white keys, 0 = C4; the box speaks MIDI and Hz.
|
|
11
|
+
//
|
|
12
|
+
// ─────────────────────────────── the kernel (Study 43) ───────────────────────────────
|
|
13
|
+
|
|
14
|
+
const mod = (a, m) => ((a % m) + m) % m;
|
|
15
|
+
const sign = (x) => (x > 0) - (x < 0);
|
|
16
|
+
|
|
17
|
+
// ─── systems: a tuning, the steps of a scale in it, and what its intervals are ───
|
|
18
|
+
//
|
|
19
|
+
// A system is data (the page writes it in EDN):
|
|
20
|
+
// tuning {cents: [the degrees above the reference, the last one the period], ref: Hz of degree 0} — the
|
|
21
|
+
// format of Scala (.scl) as the app's canvas keeps it
|
|
22
|
+
// steps which degrees of the tuning make the scale (all of them if left out); a pitch is a step of it,
|
|
23
|
+
// 0 the reference, and N steps make a period
|
|
24
|
+
// vertical for each class of interval (steps apart, modulo N): "perfect", "imperfect" or "dissonant", or a
|
|
25
|
+
// list of [{cents: [lo, hi], quality}…, {else: quality}] when the class depends on its real size
|
|
26
|
+
// melodic the melodic intervals allowed, by steps: [{steps, cents?: [lo, hi], up?: true, else?: reason,
|
|
27
|
+
// forbid?: reason}…]; wider: the reason for a leap wider than any listed
|
|
28
|
+
// leaps {small, large}: from small steps a leap turns back, from large one a step turns it back
|
|
29
|
+
// suspensions {upper: [classes], lower: [classes], compound: [classes held above only when compound]}
|
|
30
|
+
// cadence {forbid: [[from-class, to-class]…]}
|
|
31
|
+
// names a name for each step (for the pitches), intervals: a name for each span (default: ordinals)
|
|
32
|
+
// canon {at: [the distances in steps at which the comes may imitate], invertible: [n…]: the inversions
|
|
33
|
+
// the counterpoint may be asked to stand (the lower voice taken up n-1 steps)}
|
|
34
|
+
|
|
35
|
+
const NAMES = ["unison", "2nd", "3rd", "4th", "5th", "6th", "7th", "octave"];
|
|
36
|
+
const ordinal = (s) => {
|
|
37
|
+
if (s <= 7) return NAMES[s];
|
|
38
|
+
const n = s + 1, last = n % 10;
|
|
39
|
+
return n + (n >= 11 && n <= 13 ? "th" : last === 1 ? "st" : last === 2 ? "nd" : last === 3 ? "rd" : "th");
|
|
40
|
+
};
|
|
41
|
+
// (to a thousandth of a cent, as the EDN shows them)
|
|
42
|
+
const edo = (n, period = 1200) => Array.from({ length: n }, (_, i) => Math.round((1000 * period * (i + 1)) / n) / 1000);
|
|
43
|
+
|
|
44
|
+
/** The diatonic system of Studies 38–41: the white keys of 12-TET, Fux's consonances and melodic intervals. */
|
|
45
|
+
export const DIATONIC = {
|
|
46
|
+
name: "diatonic, 12-TET",
|
|
47
|
+
tuning: { cents: edo(12), ref: 261.6255653005986 },
|
|
48
|
+
steps: [0, 2, 4, 5, 7, 9, 11],
|
|
49
|
+
names: ["C", "D", "E", "F", "G", "A", "B"],
|
|
50
|
+
vertical: { 0: "perfect", 2: "imperfect", 5: "imperfect", 4: [{ cents: [680, 720], quality: "perfect" }, { else: "dissonant" }] },
|
|
51
|
+
melodic: [{ steps: 1 }, { steps: 2 }, { steps: 3, cents: [490, 510], else: "an augmented 4th (tritone)" },
|
|
52
|
+
{ steps: 4, cents: [690, 710], else: "a diminished 5th (tritone)" },
|
|
53
|
+
{ steps: 5, up: true, cents: [790, 810], else: "a 6th (only the minor 6th up)" }, { steps: 6, forbid: "a 7th" }, { steps: 7 }],
|
|
54
|
+
wider: "a leap wider than the octave",
|
|
55
|
+
leaps: { small: 3, large: 5 },
|
|
56
|
+
suspensions: { upper: [6, 3, 1], lower: [1], compound: [1] },
|
|
57
|
+
cadence: { forbid: [[6, 4]] },
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// the modes of the white keys (Studies 39–41): the final, the tenor (steps above it), the ambitus and the starts
|
|
61
|
+
// (steps from the final); and how far apart the voices may be (spacing, in steps: a 12th)
|
|
62
|
+
DIATONIC.modes = {
|
|
63
|
+
dorian: { final: 1, tenor: 4, ambitus: [-1, 8], starts: [0, 4, 7] }, hypodorian: { final: 1, tenor: 2, ambitus: [-4, 5], starts: [0, 4, 7, -3] },
|
|
64
|
+
ionian: { final: 0, tenor: 4, ambitus: [-1, 8], starts: [0, 4, 7] }, hypoionian: { final: 0, tenor: 2, ambitus: [-4, 5], starts: [0, 4, 7, -3] },
|
|
65
|
+
mixolydian: { final: 4, tenor: 4, ambitus: [-1, 8], starts: [0, 4, 7] }, hypomixolydian: { final: 4, tenor: 3, ambitus: [-4, 5], starts: [0, 4, 7, -3] },
|
|
66
|
+
aeolian: { final: 5, tenor: 4, ambitus: [-1, 8], starts: [0, 4, 7] }, hypoaeolian: { final: 5, tenor: 2, ambitus: [-4, 5], starts: [0, 4, 7, -3] },
|
|
67
|
+
};
|
|
68
|
+
DIATONIC.spacing = 11;
|
|
69
|
+
// the canon: at which distances (steps) the comes may imitate, and at which intervals the counterpoint may be
|
|
70
|
+
// asked to be invertible (n: the lower voice taken up n-1 steps, as at the octave 8, the 10th 10, the 12th 12)
|
|
71
|
+
DIATONIC.canon = { at: [-7, 7, 0, -4, 4, -3, 3], invertible: [8, 10, 12] };
|
|
72
|
+
|
|
73
|
+
/** Quarter-comma meantone (12 notes, E♭ to G♯): the same scale and rules, the thirds pure. */
|
|
74
|
+
export const MEANTONE = {
|
|
75
|
+
...DIATONIC, name: "diatonic, quarter-comma meantone",
|
|
76
|
+
tuning: { cents: [76.049, 193.157, 310.265, 386.314, 503.422, 579.471, 696.578, 772.627, 889.735, 1006.843, 1082.892, 1200], ref: 261.6255653005986 },
|
|
77
|
+
melodic: DIATONIC.melodic.map((r) => (r.steps === 5 ? { ...r, cents: [790, 830] } : r)),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** A septimal scale in 31-TET: the white keys with B♭ a harmonic seventh above C (7:4, 25 steps of 31), which
|
|
81
|
+
* sounds as a consonance; and the 7:6 and 7:5 it makes with G and E. */
|
|
82
|
+
export const SEPTIMAL31 = {
|
|
83
|
+
...DIATONIC, name: "septimal, 31-TET",
|
|
84
|
+
tuning: { cents: edo(31), ref: 261.6255653005986 },
|
|
85
|
+
steps: [0, 5, 10, 13, 18, 23, 25],
|
|
86
|
+
names: ["C", "D", "E", "F", "G", "A", "B♭⁷"],
|
|
87
|
+
vertical: { 0: "perfect", 2: "imperfect", 5: "imperfect",
|
|
88
|
+
4: [{ cents: [680, 720], quality: "perfect" }, { else: "dissonant" }],
|
|
89
|
+
6: [{ cents: [950, 985], quality: "imperfect" }, { else: "dissonant" }] },
|
|
90
|
+
melodic: DIATONIC.melodic.map((r) => (r.steps === 5 ? { ...r, cents: [790, 830] } : r)),
|
|
91
|
+
// (the comes may also imitate at the harmonic seventh, here a consonance)
|
|
92
|
+
canon: { at: [-7, 7, 0, -4, 4, -3, 3, -6, 6], invertible: [8, 10, 12] },
|
|
93
|
+
modes: { septimal: { final: 0, tenor: 4, ambitus: [-1, 8], starts: [0, 4, 7] },
|
|
94
|
+
"hypo-septimal": { final: 0, tenor: 2, ambitus: [-4, 5], starts: [0, 4, 7, -3] } },
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** The anhemitonic pentatonic (C D E G A of 12-TET): five steps, a step being a 2nd or a minor 3rd. */
|
|
98
|
+
export const PENTATONIC = {
|
|
99
|
+
name: "pentatonic, 12-TET",
|
|
100
|
+
tuning: { cents: edo(12), ref: 261.6255653005986 },
|
|
101
|
+
steps: [0, 2, 4, 7, 9],
|
|
102
|
+
names: ["C", "D", "E", "G", "A"],
|
|
103
|
+
intervals: ["unison", "step", "two steps", "three steps", "four steps", "octave"],
|
|
104
|
+
vertical: { 0: "perfect",
|
|
105
|
+
1: [{ cents: [290, 310], quality: "imperfect" }, { else: "dissonant" }],
|
|
106
|
+
2: [{ cents: [390, 410], quality: "imperfect" }, { else: "dissonant" }],
|
|
107
|
+
3: [{ cents: [690, 710], quality: "perfect" }, { cents: [790, 810], quality: "imperfect" }, { else: "dissonant" }],
|
|
108
|
+
4: [{ cents: [890, 910], quality: "imperfect" }, { else: "dissonant" }] },
|
|
109
|
+
melodic: [{ steps: 1 }, { steps: 2 }, { steps: 3, cents: [690, 710], else: "a minor 6th" }, { steps: 5 }],
|
|
110
|
+
wider: "a leap wider than the octave",
|
|
111
|
+
leaps: { small: 2, large: 3 },
|
|
112
|
+
suspensions: { upper: [2, 4], lower: [1], compound: [] },
|
|
113
|
+
cadence: { forbid: [[4, 3]] },
|
|
114
|
+
modes: { "penta-C": { final: 0, tenor: 3, ambitus: [-1, 6], starts: [0, 3, 5] },
|
|
115
|
+
"penta-A": { final: 4, tenor: 3, ambitus: [-1, 6], starts: [0, 3, 5] },
|
|
116
|
+
"penta-D": { final: 1, tenor: 3, ambitus: [-1, 6], starts: [0, 3, 5] } },
|
|
117
|
+
spacing: 8,
|
|
118
|
+
canon: { at: [-5, 5, 0, -3, 3, -2, 2], invertible: [6, 8] },
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const SYSTEMS = { diatonic: DIATONIC, meantone: MEANTONE, septimal31: SEPTIMAL31, pentatonic: PENTATONIC };
|
|
122
|
+
|
|
123
|
+
/** A system, ready to be asked: its pitches in cents and Hz, the quality of a vertical, the error of a move. */
|
|
124
|
+
export function makeSystem(def = DIATONIC) {
|
|
125
|
+
if (def && def.cents && def.quality) return def; // already made
|
|
126
|
+
const tc = (def.tuning && def.tuning.cents) || DIATONIC.tuning.cents;
|
|
127
|
+
const period = tc[tc.length - 1], all = [0, ...tc.slice(0, -1)];
|
|
128
|
+
const idx = def.steps || all.map((_, i) => i);
|
|
129
|
+
const degs = idx.map((i) => all[i]), N = degs.length;
|
|
130
|
+
const ref = (def.tuning && def.tuning.ref) || 261.6255653005986;
|
|
131
|
+
const octaveOf = (s) => Math.floor(s / N);
|
|
132
|
+
const cents = (s) => octaveOf(s) * period + degs[mod(s, N)];
|
|
133
|
+
const names = def.names || (N === 7 ? DIATONIC.names : degs.map((_, i) => String(i + 1)));
|
|
134
|
+
const inames = def.intervals;
|
|
135
|
+
const intervalName = (s) => (inames && inames[s]) || (N === 7 ? ordinal(s) : `${s}-step`);
|
|
136
|
+
const periodName = def.periodName || (Math.abs(period - 1200) < 1 ? "octave" : "period");
|
|
137
|
+
// the vertical classes: a quality, or one by real size
|
|
138
|
+
const vq = Array.from({ length: N }, (_, c) => {
|
|
139
|
+
const v = def.vertical ? def.vertical[c] : undefined;
|
|
140
|
+
return v === undefined ? (c === 0 ? "perfect" : "dissonant") : v;
|
|
141
|
+
});
|
|
142
|
+
function quality(a, b) {
|
|
143
|
+
const v = vq[mod(Math.abs(b - a), N)];
|
|
144
|
+
if (typeof v === "string") return v;
|
|
145
|
+
const ct = mod(Math.abs(cents(b) - cents(a)), period);
|
|
146
|
+
for (const r of v) if (r.cents && ct >= r.cents[0] - 1e-6 && ct <= r.cents[1] + 1e-6) return r.quality;
|
|
147
|
+
const e = v.find((r) => r.else);
|
|
148
|
+
return e ? e.else : "dissonant";
|
|
149
|
+
}
|
|
150
|
+
const mel = new Map((def.melodic || [{ steps: 1 }, { steps: 2 }]).map((r) => [r.steps, r]));
|
|
151
|
+
const widest = Math.max(...mel.keys());
|
|
152
|
+
function melodicError(a, b) {
|
|
153
|
+
const s = b - a, n = Math.abs(s);
|
|
154
|
+
if (n === 0) return null;
|
|
155
|
+
const r = mel.get(n);
|
|
156
|
+
if (!r) return n > widest && def.wider ? def.wider : `a ${intervalName(n)} is not a melodic interval of the style`;
|
|
157
|
+
if (r.forbid) return r.forbid;
|
|
158
|
+
if (r.up && s < 0) return r.else || `a ${intervalName(n)} only up`;
|
|
159
|
+
if (r.cents) { const c = Math.abs(cents(b) - cents(a)); if (c < r.cents[0] - 1e-6 || c > r.cents[1] + 1e-6) return r.else || `this ${intervalName(n)} is not a melodic interval of the style`; }
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
const leaps = def.leaps || { small: 3, large: 5 };
|
|
163
|
+
const sus = def.suspensions || { upper: [], lower: [], compound: [] };
|
|
164
|
+
const forbidCadence = ((def.cadence && def.cadence.forbid) || []).map(([x, y]) => [x, y]);
|
|
165
|
+
return {
|
|
166
|
+
def, name: def.name || "system", N, period, ref, degs, octaveOf, cents, quality, melodicError, intervalName, periodName,
|
|
167
|
+
leaps, suspensions: { upper: sus.upper || [], lower: sus.lower || [], compound: sus.compound || [] }, forbidCadence,
|
|
168
|
+
pitchName: (s) => names[mod(s, N)] + (4 + octaveOf(s)),
|
|
169
|
+
hz: (s) => ref * Math.pow(2, cents(s) / 1200),
|
|
170
|
+
midi: (s) => 69 + 12 * Math.log2((ref * Math.pow(2, cents(s) / 1200)) / 440),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const D7 = makeSystem(DIATONIC);
|
|
175
|
+
// (the diatonic system's functions, as Studies 38–41 exported them)
|
|
176
|
+
export const octaveOf = D7.octaveOf;
|
|
177
|
+
export const semis = (s) => D7.cents(s) / 100;
|
|
178
|
+
export const intervalName = D7.intervalName;
|
|
179
|
+
export const quality = D7.quality;
|
|
180
|
+
export const melodicError = D7.melodicError;
|
|
181
|
+
|
|
182
|
+
/** The band of a degree of imitation: "canon", "near", "motion" or "free". */
|
|
183
|
+
export function band(s) { return s >= 0.999 ? "canon" : s >= 0.5 ? "near" : s >= 0.2 ? "motion" : "free"; }
|
|
184
|
+
|
|
185
|
+
const OFF_BY_DEFAULT = new Set(["strict-direct", "syncopation"]);
|
|
186
|
+
|
|
187
|
+
export function canon(spec) {
|
|
188
|
+
const { tonic, k, d, b = 1, n, lo, hi, spacing = 11 } = spec;
|
|
189
|
+
const phase = spec.phase || 0; // rests before the dux's first note: an upbeat (anacrusis)
|
|
190
|
+
const maxHold = Math.max(1, spec.maxHold ?? 2); // the beats a note may last, held
|
|
191
|
+
// the system: its quality, melodic rules, names (the diatonic one unless the spec brings another)
|
|
192
|
+
const S = makeSystem(spec.system || DIATONIC), N7 = S.N;
|
|
193
|
+
const { quality, melodicError, intervalName } = S;
|
|
194
|
+
// the transformation of the canon (Study 43): the comes is the dux's note, upside down about an axis
|
|
195
|
+
// (inversion), or held twice as long (augmentation: each note of the dux m beats of the comes)
|
|
196
|
+
const tf = spec.transform || {};
|
|
197
|
+
const inv = tf.kind === "inversion", aug = tf.kind === "augmentation" ? Math.max(2, Math.round(tf.m || 2)) : 1;
|
|
198
|
+
const axisT = tf.axis ?? tonic;
|
|
199
|
+
const Tx = (x) => (inv ? 2 * axisT - x : x) + k;
|
|
200
|
+
const [tlo, thi] = [Tx(lo), Tx(hi)].sort((a, z) => a - z);
|
|
201
|
+
const clo = spec.clo ?? tlo, chi = spec.chi ?? thi;
|
|
202
|
+
const imitation = spec.imitation ?? 1, bnd = band(imitation);
|
|
203
|
+
const invert = [...(spec.invert || [])].sort((x, y) => x - y);
|
|
204
|
+
const on = (id) => (spec.on && id in spec.on ? !!spec.on[id] : id === "syncopation" ? spec.species === 4 : !OFF_BY_DEFAULT.has(id));
|
|
205
|
+
const starts = spec.starts ? [...spec.starts] : [tonic, tonic + 4, tonic + 7];
|
|
206
|
+
const cstarts = spec.cstarts ? [...spec.cstarts] : starts.map(Tx);
|
|
207
|
+
const finals = [];
|
|
208
|
+
for (let i = -2; i <= 2; i++) { const f = tonic + N7 * i; if (f >= lo && f <= hi) finals.push(f); }
|
|
209
|
+
const strong = (t) => (t + phase) % b === 0;
|
|
210
|
+
const lowerFirst = (x, c) => (k > 0 ? [x, c] : [c, x]);
|
|
211
|
+
const transforms = [[null, (a, z) => [a, z]], ...invert.map((N) => [N, (a, z) => [z, a + N - 1]])];
|
|
212
|
+
const label = (N) => (N ? `under inversion at the ${intervalName(N - 1)}: ` : "");
|
|
213
|
+
/** The comes's note in a strict canon at time t (the dux's, d beats before, k steps away, transformed), or null. */
|
|
214
|
+
const source = (t) => (t - d >= 0 ? Math.floor((t - d) / aug) : null);
|
|
215
|
+
const model = (D, t) => { const i = source(t); return i !== null && i < D.length ? Tx(D[i]) : null; };
|
|
216
|
+
|
|
217
|
+
function dissonant(x, c) {
|
|
218
|
+
const [l0, h0] = lowerFirst(x, c);
|
|
219
|
+
for (const [, f] of transforms) { const [l, h] = f(l0, h0); if (h >= l && quality(l, h) === "dissonant") return true; }
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The melodic rules of one line: a reason, or null. who: "" for the dux, "the comes: " for the comes. The same
|
|
224
|
+
* pitch again is the note held (a tie), maxHold beats at most; the other rules look at the notes struck. */
|
|
225
|
+
function melodic(line, t, x, who, hold = maxHold) {
|
|
226
|
+
const prev = t >= 1 ? line[t - 1] : null;
|
|
227
|
+
if (prev === null || prev === undefined) return null;
|
|
228
|
+
let j = t - 1; // where the note before began
|
|
229
|
+
while (j >= 1 && line[j - 1] === prev) j--;
|
|
230
|
+
if (x === prev) return t - j >= hold ? `${who}a note is held for ${hold === 2 ? "two" : hold} beats at most` : null;
|
|
231
|
+
if (on("melodic")) { const e = melodicError(prev, x); if (e) return who + e; }
|
|
232
|
+
const a1 = j >= 1 ? line[j - 1] : null; // the note struck before it
|
|
233
|
+
if (on("leap-recovery") && a1 !== null && a1 !== undefined) {
|
|
234
|
+
const m = prev - a1, s = x - prev;
|
|
235
|
+
if (Math.abs(m) >= S.leaps.large && s !== -sign(m)) return `${who}after a leap of a ${intervalName(Math.abs(m))}, a step ${m > 0 ? "down" : "up"}`;
|
|
236
|
+
if (Math.abs(m) >= S.leaps.small && Math.abs(m) < S.leaps.large && sign(s) !== -sign(m))
|
|
237
|
+
return `${who}after the leap of a ${intervalName(Math.abs(m))} the line turns ${m > 0 ? "down" : "up"}`;
|
|
238
|
+
}
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Which voice holds its note at t (from t-1): "dux", "comes", or null (none, or both). */
|
|
243
|
+
const holder = (D, C, t) => {
|
|
244
|
+
if (t < 1 || C[t] === null || C[t - 1] === null) return null;
|
|
245
|
+
const hd = D[t] === D[t - 1], hc = C[t] === C[t - 1];
|
|
246
|
+
return hd && !hc ? "dux" : hc && !hd ? "comes" : null;
|
|
247
|
+
};
|
|
248
|
+
/** Whether the vertical at t (already in D, C) is a suspension: a dissonance on a strong beat, held by one voice. */
|
|
249
|
+
const suspensionAt = (D, C, t) => b > 1 && t >= 1 && strong(t) && C[t] !== null && dissonant(D[t], C[t]) && holder(D, C, t) !== null;
|
|
250
|
+
|
|
251
|
+
/** The rules of the dux alone for its note x at time t = D.length: a reason, or null. */
|
|
252
|
+
function checkDux(D, C, x) {
|
|
253
|
+
const t = D.length;
|
|
254
|
+
if (t >= n) return "the work is finished";
|
|
255
|
+
if (on("range") && (x < lo || x > hi)) return "out of range";
|
|
256
|
+
if (on("start") && t === 0 && !starts.includes(x)) return "the dux starts on the final, its 5th, its octave or the 4th below";
|
|
257
|
+
const e = melodic(D, t, x, ""); if (e) return e;
|
|
258
|
+
if (on("final") && t === n - 1 && !finals.includes(x)) return "the dux ends on the final";
|
|
259
|
+
if (on("approach") && t === n - 2 && !finals.some((f) => Math.abs(x - f) === 1)) return "the final is approached by step";
|
|
260
|
+
// fourth species: the dux is the cantus firmus, a note a bar (struck on the strong beat, held on the weak one)
|
|
261
|
+
if (on("syncopation") && b === 2 && t >= 1) {
|
|
262
|
+
if (strong(t) && x === D[t - 1]) return "fourth species: the cantus firmus moves on every strong beat";
|
|
263
|
+
if (!strong(t) && x !== D[t - 1]) return "fourth species: the cantus firmus holds its note for the whole bar";
|
|
264
|
+
}
|
|
265
|
+
// a suspension just before resolves: the voice that held it goes down a step
|
|
266
|
+
if (t >= 2 && suspensionAt(D, C, t - 1)) {
|
|
267
|
+
if (holder(D, C, t - 1) === "dux" && x !== D[t - 1] - 1) return "the suspension resolves down by step (the dux)";
|
|
268
|
+
} else if (on("weak-dissonance") && t >= 1 && C[t - 1] !== null && dissonant(D[t - 1], C[t - 1]) && Math.abs(x - D[t - 1]) !== 1)
|
|
269
|
+
// a dissonance just before is left by step (the dux's part of it)
|
|
270
|
+
return "the dissonance before is left by step (the dux)";
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Why a dissonance on a strong beat is not a suspension that may stand (null when it may): true when the rules
|
|
275
|
+
* of suspensions are off or there is nothing held; otherwise the reason, in words. */
|
|
276
|
+
function suspensionError(N, l, h, f, pl0, ph0, hasPrev, held, heldLower0, t) {
|
|
277
|
+
if (!on("suspension") || b === 1) return true;
|
|
278
|
+
if (!hasPrev || !held) return "not held from the beat before";
|
|
279
|
+
const [pl, ph] = f(pl0, ph0);
|
|
280
|
+
if (ph < pl || quality(pl, ph) === "dissonant") return "a suspension is prepared by a consonance";
|
|
281
|
+
if (t >= n - 1) return "a suspension needs a beat to resolve";
|
|
282
|
+
if (on("suspension-kinds")) {
|
|
283
|
+
const lower = N ? !heldLower0 : heldLower0, cls = mod(h - l, N7), K = S.suspensions;
|
|
284
|
+
const list = (cs) => cs.map((c) => intervalName(c + (K.compound.includes(c) ? N7 : 0))).join(", ") || "none";
|
|
285
|
+
if (lower && !K.lower.includes(cls)) return N7 === 7 && cls === 3 ? "a 4th in the lower voice would resolve to a 5th (4-5)" : `held in the lower voice, only: ${list(K.lower)}`;
|
|
286
|
+
if (!lower && K.compound.includes(cls) && h - l < N7) return `a ${intervalName(cls)} in the upper voice would resolve to the unison`;
|
|
287
|
+
if (!lower && !K.upper.includes(cls)) return `held in the upper voice, only: ${list(K.upper)}`;
|
|
288
|
+
}
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** The rules of the comes, and between the voices, for the pair (x, c) at t (x already passed checkDux). */
|
|
293
|
+
function checkPair(D, C, x, c) {
|
|
294
|
+
const t = D.length;
|
|
295
|
+
if (t < d) return c === null ? null : "the comes has not entered yet";
|
|
296
|
+
if (c === null) return "the comes sounds from its entry on";
|
|
297
|
+
if (on("range") && (c < clo || c > chi)) return "the comes: out of range";
|
|
298
|
+
const m = model(D, t);
|
|
299
|
+
// (in fourth species the comes strikes its last note, the final: there the canon breaks off into its cadence)
|
|
300
|
+
const cadence = on("syncopation") && b === 2 && t === n - 1;
|
|
301
|
+
if (bnd === "canon" && c !== m && !cadence) return "a canon: the comes is the dux's note";
|
|
302
|
+
if (bnd === "near" && Math.abs(c - m) > 2) return "the comes keeps within a 3rd of the dux's note";
|
|
303
|
+
if (on("start") && t === d && !cstarts.includes(c)) return "the comes enters on the final, its 5th, its octave or the 4th below (shifted)";
|
|
304
|
+
// fourth species: the comes is syncopated, holding its note over the strong beats (and striking the final)
|
|
305
|
+
if (on("syncopation") && b === 2 && t > d) {
|
|
306
|
+
if (strong(t) && t < n - 1 && c !== C[t - 1]) return "fourth species: the comes holds its note over the strong beat";
|
|
307
|
+
if ((!strong(t) || t === n - 1) && c === C[t - 1]) return "fourth species: the comes strikes a new note on the weak beat";
|
|
308
|
+
}
|
|
309
|
+
const e = melodic(C, t, c, "the comes: ", maxHold * aug); if (e) return e;
|
|
310
|
+
const prev = t >= 1 ? D[t - 1] : null, pc = t >= 1 ? C[t - 1] : null;
|
|
311
|
+
const resolving = t >= 2 && suspensionAt(D, C, t - 1);
|
|
312
|
+
if (resolving) {
|
|
313
|
+
if (holder(D, C, t - 1) === "comes" && c !== pc - 1) return "the suspension resolves down by step (the comes)";
|
|
314
|
+
} else if (on("weak-dissonance") && pc !== null && dissonant(prev, pc) && Math.abs(c - pc) !== 1)
|
|
315
|
+
return "the dissonance before is left by step (the comes)";
|
|
316
|
+
// between the voices
|
|
317
|
+
if (on("no-crossing") && k !== 0) { const [l, h] = lowerFirst(x, c); if (l > h) return "the voices cross"; }
|
|
318
|
+
if (on("spacing") && Math.abs(x - c) > spacing) return `more than a ${intervalName(spacing)} apart`;
|
|
319
|
+
const [l0, h0] = lowerFirst(x, c);
|
|
320
|
+
const hasPrev = pc !== null;
|
|
321
|
+
const [pl0, ph0] = hasPrev ? lowerFirst(prev, pc) : [0, 0];
|
|
322
|
+
const sb = b > 1 && strong(t) && t - b >= 0 && C[t - b] !== null ? lowerFirst(D[t - b], C[t - b]) : null;
|
|
323
|
+
// a suspension here: one voice holds its note (from a consonance before), the other moves
|
|
324
|
+
const hd = hasPrev && x === prev, hc = hasPrev && c === pc;
|
|
325
|
+
const held = hd !== hc ? (hd ? "dux" : "comes") : null;
|
|
326
|
+
const heldLower0 = held && (k > 0 ? held === "dux" : held === "comes");
|
|
327
|
+
let diss = false, unsuspended = null;
|
|
328
|
+
for (const [N, f] of transforms) {
|
|
329
|
+
const [l, h] = f(l0, h0);
|
|
330
|
+
if (N && h < l) return `${label(N)}the voices would cross (wider than the ${intervalName(N - 1)})`;
|
|
331
|
+
const q = quality(l, h), cls = mod(h - l, N7);
|
|
332
|
+
if (q === "dissonant") {
|
|
333
|
+
diss = true;
|
|
334
|
+
if (on("consonance") && strong(t)) {
|
|
335
|
+
const why = suspensionError(N, l, h, f, pl0, ph0, hasPrev, held, heldLower0, t);
|
|
336
|
+
if (why) unsuspended = unsuspended || `${label(N)}a ${intervalName(cls)} (dissonant)${b > 1 ? " on a strong beat" : ""}${why === true ? "" : ": " + why}`;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (hasPrev) {
|
|
340
|
+
const [pl, ph] = f(pl0, ph0);
|
|
341
|
+
if (on("parallels") && q === "perfect" && quality(pl, ph) === "perfect" && mod(ph - pl, N7) === cls && (pl !== l || ph !== h))
|
|
342
|
+
return label(N) + "parallel " + (cls === 0 ? S.periodName : intervalName(cls)) + "s";
|
|
343
|
+
const dl = l - pl, dh = h - ph;
|
|
344
|
+
if (on("direct-octaves") && cls === 0 && dl !== 0 && sign(dl) === sign(dh) && (on("strict-direct") || Math.abs(dh) !== 1))
|
|
345
|
+
return `${label(N)}${l === h ? "a unison" : intervalName(h - l)} by direct motion`;
|
|
346
|
+
}
|
|
347
|
+
if (sb && on("strong-parallels") && q === "perfect") {
|
|
348
|
+
const [sl, sh] = f(sb[0], sb[1]);
|
|
349
|
+
if (quality(sl, sh) === "perfect" && mod(sh - sl, N7) === cls && (sl !== l || sh !== h))
|
|
350
|
+
return label(N) + (cls === 0 ? S.periodName : intervalName(cls)) + "s on consecutive strong beats";
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (unsuspended) return unsuspended;
|
|
354
|
+
if (resolving && diss) return "the suspension resolves to a consonance";
|
|
355
|
+
if (diss && b > 1 && strong(t) && on("consonance")) {
|
|
356
|
+
if (t === n - 1) return "the work does not end on a dissonance";
|
|
357
|
+
} else if (diss && b > 1 && on("weak-dissonance")) {
|
|
358
|
+
if (Math.abs(x - prev) !== 1) return "a dissonance on a weak beat: the dux comes to it by step";
|
|
359
|
+
if (!hasPrev) return "the comes cannot enter on a dissonance";
|
|
360
|
+
if (Math.abs(c - pc) !== 1) return "a dissonance on a weak beat: the comes comes to it by step";
|
|
361
|
+
if (dissonant(prev, pc)) return "two dissonances in a row";
|
|
362
|
+
if (t === n - 1) return "the work does not end on a dissonance";
|
|
363
|
+
}
|
|
364
|
+
if (on("entry-unison") && t === d && x === c) return "the comes does not enter on the unison";
|
|
365
|
+
if (on("final") && t === n - 1 && quality(x, c) !== "perfect") return "the last vertical is a perfect consonance";
|
|
366
|
+
if (on("cadence") && t === n - 1 && hasPrev) {
|
|
367
|
+
const to = mod(h0 - l0, N7), from = mod(ph0 - pl0, N7);
|
|
368
|
+
if (S.forbidCadence.some(([x, y]) => x === from && y === to)) return `the cadence does not go from a ${intervalName(from)} to a ${intervalName(to)}`;
|
|
369
|
+
}
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const comesChoices = (t) => {
|
|
374
|
+
if (t < d) return [null];
|
|
375
|
+
const out = [];
|
|
376
|
+
for (let c = clo; c <= chi; c++) out.push(c);
|
|
377
|
+
return out;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
// ─── the count over pairs ───
|
|
381
|
+
// the notes the rules look back at: a note held (maxHold beats at most) and the note struck before it
|
|
382
|
+
const LD = Math.max(maxHold + 1, b, bnd === "canon" || bnd === "near" ? d : 0);
|
|
383
|
+
const LC = Math.max(maxHold * aug + 1, b);
|
|
384
|
+
// (in an augmentation the comes reaches further back into the dux, the further the work goes)
|
|
385
|
+
const fromD = (t) => Math.max(0, aug > 1 && (bnd === "canon" || bnd === "near") ? Math.min(t - LD, Math.floor((t - d) / aug)) : t - LD);
|
|
386
|
+
const memo = new Map();
|
|
387
|
+
const W = Math.max(hi, chi) - Math.min(lo, clo) + 2, base = Math.min(lo, clo) - 1;
|
|
388
|
+
function key(D, C, t) {
|
|
389
|
+
const f = fromD(t), g = Math.max(0, t - LC);
|
|
390
|
+
// a number while it fits in a double exactly; else a string (a long augmentation)
|
|
391
|
+
if ((t - f + t - g) * Math.log2(W) < 46) {
|
|
392
|
+
let code = t;
|
|
393
|
+
for (let j = f; j < t; j++) code = code * W + (D[j] - base);
|
|
394
|
+
for (let j = g; j < t; j++) code = code * W + (C[j] === null ? 0 : C[j] - base);
|
|
395
|
+
return code;
|
|
396
|
+
}
|
|
397
|
+
return t + ":" + D.slice(f, t).join(",") + ":" + C.slice(g, t).join(",");
|
|
398
|
+
}
|
|
399
|
+
function countFrom(D, C) {
|
|
400
|
+
const t = D.length;
|
|
401
|
+
if (t >= n) return 1;
|
|
402
|
+
const kk = key(D, C, t), got = memo.get(kk);
|
|
403
|
+
if (got !== undefined) return got;
|
|
404
|
+
let v = 0;
|
|
405
|
+
for (let x = lo; x <= hi; x++) {
|
|
406
|
+
if (checkDux(D, C, x) !== null) continue;
|
|
407
|
+
for (const c of comesChoices(t)) {
|
|
408
|
+
if (checkPair(D, C, x, c) !== null) continue;
|
|
409
|
+
D.push(x); C.push(c); v += countFrom(D, C); D.pop(); C.pop();
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
memo.set(kk, v);
|
|
413
|
+
return v;
|
|
414
|
+
}
|
|
415
|
+
/** The ways to finish the work from (D, C), exact. */
|
|
416
|
+
const count = (D, C) => (D.length > n ? 0 : countFrom([...D], [...C]));
|
|
417
|
+
|
|
418
|
+
/** The same count a few milliseconds at a time (see Study 39), resolving to the count or to null if cancelled. */
|
|
419
|
+
function countAsync(D0, C0, { budget = 12, onProgress, cancelled } = {}) {
|
|
420
|
+
return new Promise((resolve) => {
|
|
421
|
+
const D = [...D0], C = [...C0];
|
|
422
|
+
if (D.length >= n) return resolve(D.length === n ? 1 : 0);
|
|
423
|
+
const root = key(D, C, D.length), got = memo.get(root);
|
|
424
|
+
if (got !== undefined) return resolve(got);
|
|
425
|
+
// a frame: the pairs to try at its time, the next one, the sum so far
|
|
426
|
+
const pairsAt = () => {
|
|
427
|
+
const t = D.length, out = [];
|
|
428
|
+
for (let x = lo; x <= hi; x++) {
|
|
429
|
+
if (checkDux(D, C, x) !== null) continue;
|
|
430
|
+
for (const c of comesChoices(t)) if (checkPair(D, C, x, c) === null) out.push([x, c]);
|
|
431
|
+
}
|
|
432
|
+
return out;
|
|
433
|
+
};
|
|
434
|
+
const stack = [{ pairs: pairsAt(), i: 0, sum: 0, kk: root }];
|
|
435
|
+
const fraction = () => { let fr = 0, w = 1; for (const f of stack) { const m = Math.max(1, f.pairs.length); w /= m; fr += Math.max(0, f.i - 1) * w; } return Math.min(1, fr); };
|
|
436
|
+
function step() {
|
|
437
|
+
if (cancelled && cancelled()) return resolve(null);
|
|
438
|
+
const end = performance.now() + budget;
|
|
439
|
+
let it = 0;
|
|
440
|
+
while (stack.length) {
|
|
441
|
+
if ((++it & 63) === 0 && performance.now() > end) {
|
|
442
|
+
if (onProgress) onProgress(fraction(), memo.size);
|
|
443
|
+
setTimeout(step, 0);
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
const f = stack[stack.length - 1];
|
|
447
|
+
if (f.i >= f.pairs.length) {
|
|
448
|
+
memo.set(f.kk, f.sum);
|
|
449
|
+
stack.pop();
|
|
450
|
+
if (!stack.length) return resolve(f.sum);
|
|
451
|
+
D.pop(); C.pop();
|
|
452
|
+
stack[stack.length - 1].sum += f.sum;
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
const [x, c] = f.pairs[f.i++];
|
|
456
|
+
D.push(x); C.push(c);
|
|
457
|
+
if (D.length >= n) { f.sum += 1; D.pop(); C.pop(); continue; }
|
|
458
|
+
const kk = key(D, C, D.length), v = memo.get(kk);
|
|
459
|
+
if (v !== undefined) { f.sum += v; D.pop(); C.pop(); continue; }
|
|
460
|
+
stack.push({ pairs: pairsAt(), i: 0, sum: 0, kk });
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
step();
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/** The dux's candidates at the point: {note, reason} or {note, futures} (the futures summed over the comes's
|
|
468
|
+
* notes that fit). A note whose own rules pass but that no comes fits says why, from the first comes tried. */
|
|
469
|
+
function fanDux(D, C) {
|
|
470
|
+
const t = D.length, out = [];
|
|
471
|
+
for (let x = lo; x <= hi; x++) {
|
|
472
|
+
const r = checkDux(D, C, x);
|
|
473
|
+
if (r) { out.push({ note: x, reason: r }); continue; }
|
|
474
|
+
let fut = 0, fits = 0, why = null;
|
|
475
|
+
for (const c of comesChoices(t)) {
|
|
476
|
+
const rc = checkPair(D, C, x, c);
|
|
477
|
+
if (rc) { if (!why || (c !== null && model(D, t) === c)) why = rc; continue; }
|
|
478
|
+
fits++;
|
|
479
|
+
fut += count([...D, x], [...C, c]);
|
|
480
|
+
}
|
|
481
|
+
out.push(fits ? { note: x, reason: null, futures: fut } : { note: x, reason: "no note of the comes fits: " + why });
|
|
482
|
+
}
|
|
483
|
+
return out;
|
|
484
|
+
}
|
|
485
|
+
/** The comes's candidates for the dux's note x at the point: {note, reason} or {note, futures}. */
|
|
486
|
+
function fanComes(D, C, x) {
|
|
487
|
+
return comesChoices(D.length).map((c) => {
|
|
488
|
+
const r = checkPair(D, C, x, c);
|
|
489
|
+
return r ? { note: c, reason: r } : { note: c, reason: null, futures: count([...D, x], [...C, c]) };
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
/** What each dissonance is, by time: null or {dux, comes} as "passing", "neighbour", "dissonant", or, for a
|
|
493
|
+
* suspension, "suspension" (the voice that holds it) and "moving" (the other). */
|
|
494
|
+
function roles(D, C) {
|
|
495
|
+
const out = [];
|
|
496
|
+
for (let t = 0; t < D.length; t++) {
|
|
497
|
+
const x = D[t], c = C[t];
|
|
498
|
+
if (c === null || !dissonant(x, c)) { out.push(null); continue; }
|
|
499
|
+
if (suspensionAt(D, C, t)) { const h = holder(D, C, t); out.push({ dux: h === "dux" ? "suspension" : "moving", comes: h === "comes" ? "suspension" : "moving" }); continue; }
|
|
500
|
+
const role = (a, m, z) => (a === null || a === undefined || z === null || z === undefined ? "dissonant" : sign(m - a) === sign(z - m) ? "passing" : "neighbour");
|
|
501
|
+
out.push({ dux: role(D[t - 1], x, D[t + 1]), comes: role(C[t - 1], c, C[t + 1]) });
|
|
502
|
+
}
|
|
503
|
+
return out;
|
|
504
|
+
}
|
|
505
|
+
return { spec, band: bnd, checkDux, checkPair, count, countAsync, fanDux, fanComes, roles, dissonant, strong, model,
|
|
506
|
+
finals, lowerFirst, memo, window: [LD, LC], suspensionAt, holder, system: S };
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// ─────────────────────────────── live: the canon box ───────────────────────────────
|
|
510
|
+
|
|
511
|
+
/** The modes on the white keys (Study 39): the final, the tenor in steps above it, authentic or plagal. */
|
|
512
|
+
export const MODES = {
|
|
513
|
+
dorian: { final: 1, tenor: 4, plagal: false }, hypodorian: { final: 1, tenor: 2, plagal: true },
|
|
514
|
+
ionian: { final: 0, tenor: 4, plagal: false }, hypoionian: { final: 0, tenor: 2, plagal: true },
|
|
515
|
+
mixolydian: { final: 4, tenor: 4, plagal: false }, hypomixolydian: { final: 4, tenor: 3, plagal: true },
|
|
516
|
+
aeolian: { final: 5, tenor: 4, plagal: false }, hypoaeolian: { final: 5, tenor: 2, plagal: true },
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
/** The soft rules of Study 41 and their weights (kairotic/canon.cljs there). */
|
|
520
|
+
export const WEIGHTS = {
|
|
521
|
+
"leap-size": 1, tie: 1.5, "suspension-life": 1.5, "contrary-after-leap": 1.2, "step-back": 1.2, "leap-after-leap": 0.6, return: 1,
|
|
522
|
+
hover: 0.6, run: 1.5, open: 1, climax: 1.5, sequence: 1.5, motion: 2.5, modal: 1, "direct-perfect": 3,
|
|
523
|
+
"imperfect-run": 2, passing: 1, alternation: 1.5, imitation: 1, memory: 1, novelty: 0.3,
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
const LETTERS = ["C", "D", "E", "F", "G", "A", "B"];
|
|
527
|
+
const CHROMA = ["C", "C♯", "D", "E♭", "E", "F", "F♯", "G", "G♯", "A", "B♭", "B"];
|
|
528
|
+
export const pitchName = (s) => LETTERS[mod(s, 7)] + (4 + octaveOf(s));
|
|
529
|
+
export const midiOf = (s) => 60 + semis(s);
|
|
530
|
+
/** The name of a MIDI note, black keys included. */
|
|
531
|
+
export const midiName = (m) => CHROMA[mod(Math.round(m), 12)] + (Math.floor(Math.round(m) / 12) - 1);
|
|
532
|
+
/** Semitones above C4 for a frequency. */
|
|
533
|
+
export const semisOfHz = (hz) => 12 * Math.log2(hz / 261.6255653005986);
|
|
534
|
+
const LEAP = { 0: 0, 1: 0, 2: 0.2, 3: 0.4, 4: 0.5, 5: 0.8, 7: 0.8 };
|
|
535
|
+
const bitsOf = (c) => (c > 0 ? Math.log2(c) : -1);
|
|
536
|
+
const clean = (parts) => { for (const key of Object.keys(parts)) if (!parts[key]) delete parts[key]; return parts; };
|
|
537
|
+
const total = (parts) => Object.values(parts).reduce((a, v) => a + v, 0);
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* A canon to play live, note by note. o: {mode, k, d, beats, bars, upbeat, imitation, species, maxHold, spacing,
|
|
541
|
+
* invert, on, weights} (the defaults: dorian, the comes an octave below, 3 beats later, 2/2, 8 bars, no upbeat, a
|
|
542
|
+
* strict canon, second species with ties; species 4: the dux a cantus firmus, the comes syncopated; maxHold: the
|
|
543
|
+
* beats a note may be held, 2).
|
|
544
|
+
* D, C as in the kernel: the dux and the comes so far.
|
|
545
|
+
*/
|
|
546
|
+
export function live(o = {}) {
|
|
547
|
+
// the system (Study 42) and a mode of it. A box's words name them in any order ("pentatonic penta-A", "dorian"):
|
|
548
|
+
// a word that is a key of SYSTEMS is the system, the other the mode; a system may also come as data (EDN)
|
|
549
|
+
const given = o.system && typeof o.system === "object" ? o.system : null;
|
|
550
|
+
const words = [o.mode, o.system].filter((w) => typeof w === "string" && w);
|
|
551
|
+
const sysWord = given ? null : words.find((w) => SYSTEMS[w]);
|
|
552
|
+
const modeWord = words.find((w) => !SYSTEMS[w]);
|
|
553
|
+
const sysdef = given || (sysWord ? SYSTEMS[sysWord] : DIATONIC);
|
|
554
|
+
const systemName = given ? given.name || "a system given as data" : sysWord || "diatonic";
|
|
555
|
+
const S = makeSystem(sysdef), N = S.N;
|
|
556
|
+
const { quality, intervalName, pitchName } = S;
|
|
557
|
+
const semis = (s) => S.cents(s) / 100, P = S.period / 100;
|
|
558
|
+
const midiOf = (s) => 60 + semis(s) + 12 * Math.log2(S.ref / 261.6255653005986);
|
|
559
|
+
// its modes: the final, the tenor (steps above it), the ambitus and the starts (steps from the final)
|
|
560
|
+
const MS = sysdef.modes || { [sysdef.name || "system"]: { final: 0, tenor: Math.round((4 * N) / 7), ambitus: [-1, N + 1], starts: [0] } };
|
|
561
|
+
const mode = MS[modeWord] ? modeWord : Object.keys(MS)[0], M = MS[mode];
|
|
562
|
+
// a mode asked for that the system does not have: its first mode, and why
|
|
563
|
+
const modeNote = modeWord && !MS[modeWord] ? `${modeWord} is not a mode of ${systemName} (${Object.keys(MS).join(", ")}): ${mode}` : null;
|
|
564
|
+
const tonic = M.final, tenor = tonic + M.tenor;
|
|
565
|
+
const [a0, z0] = M.ambitus || [-1, N + 1];
|
|
566
|
+
const lo = tonic + a0, hi = tonic + z0;
|
|
567
|
+
const b = Math.max(1, Math.round(o.beats ?? 2));
|
|
568
|
+
const phase = Math.max(0, Math.min(b - 1, Math.round(o.upbeat ?? 0)));
|
|
569
|
+
const bars = Math.min(o.transform === "augmentation" ? 6 : 99, Math.max(2, Math.round(o.bars ?? 8)));
|
|
570
|
+
const n = b * (bars - 1) + 1 - phase;
|
|
571
|
+
// the distance of the imitation: one the system allows (an octave asked of a system of other than 7 steps is its period)
|
|
572
|
+
const at = (sysdef.canon && sysdef.canon.at) || [-N, N, 0];
|
|
573
|
+
let k = Math.round(o.k ?? -N);
|
|
574
|
+
if (!at.includes(k) && N !== 7 && k % 7 === 0) k = (k / 7) * N;
|
|
575
|
+
const d = Math.max(1, Math.round(o.d ?? 3));
|
|
576
|
+
const imitation = o.transform === "augmentation" ? 1 : Math.max(0, Math.min(1, o.imitation ?? 1));
|
|
577
|
+
const starts = (M.starts || [0]).map((x) => tonic + x).filter((x) => x >= lo && x <= hi);
|
|
578
|
+
const species = o.species === 4 && b === 2 ? 4 : 2;
|
|
579
|
+
const maxHold = Math.max(1, Math.round(o.maxHold ?? 2));
|
|
580
|
+
// the transformation: inversion about an axis (a MIDI note, the final by default), or augmentation (twice as
|
|
581
|
+
// slow: a strict canon, and not longer than 6 bars, so that its futures can still be counted)
|
|
582
|
+
const kind = ["inversion", "augmentation"].includes(o.transform) ? o.transform : "canon";
|
|
583
|
+
const axisStep = (() => {
|
|
584
|
+
if (o.axis === undefined || o.axis === null) return tonic;
|
|
585
|
+
let best = tonic;
|
|
586
|
+
for (let s = lo - N; s <= hi + N; s++) if (Math.abs(midiOf(s) - o.axis) < Math.abs(midiOf(best) - o.axis)) best = s;
|
|
587
|
+
return best;
|
|
588
|
+
})();
|
|
589
|
+
// the fifth of the system (for direct fifths): the class nearest 702 cents
|
|
590
|
+
const fifth = S.degs.reduce((bi, c, i) => (Math.abs(c - 702) < Math.abs(S.degs[bi] - 702) ? i : bi), 0);
|
|
591
|
+
const spec = { tonic, k, d, b, phase, n, lo, hi, spacing: o.spacing ?? sysdef.spacing ?? 11, invert: o.invert || [], starts, imitation, species, maxHold,
|
|
592
|
+
on: o.on || {}, system: sysdef, transform: { kind, axis: axisStep, m: 2 } };
|
|
593
|
+
const K = canon(spec);
|
|
594
|
+
const w = { ...WEIGHTS, ...(o.weights || {}) };
|
|
595
|
+
const strong = (t) => (t + phase) % b === 0;
|
|
596
|
+
|
|
597
|
+
/** The soft cost of x after a line (its own shape), by rule; modal: whether the mode's notes count (the dux). */
|
|
598
|
+
function melodicParts(line, x, t, modal) {
|
|
599
|
+
const parts = {}, prev = line.length ? line[line.length - 1] : null;
|
|
600
|
+
if (modal && (mod(x, N) === mod(tonic, N) || mod(x, N) === mod(tenor, N))) parts.modal = -(w.modal * (b > 1 && strong(t) ? 1.5 : 1));
|
|
601
|
+
if (prev === null) return clean(parts);
|
|
602
|
+
if (x === prev) { parts.tie = w.tie; return clean(parts); }
|
|
603
|
+
// the rest on the notes struck: a note held is one note
|
|
604
|
+
line = line.filter((y, i) => i === 0 || y !== line[i - 1]);
|
|
605
|
+
const tl2 = line.length, m2 = tl2 >= 2 ? prev - line[tl2 - 2] : null;
|
|
606
|
+
return struck(line, x, m2, parts);
|
|
607
|
+
}
|
|
608
|
+
function struck(line, x, m, parts) {
|
|
609
|
+
const tl = line.length, prev = line[tl - 1];
|
|
610
|
+
const s = x - prev, nn = Math.abs(s), all = [...line, x];
|
|
611
|
+
const mv = []; for (let i = 1; i < tl; i++) mv.push(line[i] - line[i - 1]); mv.push(s);
|
|
612
|
+
const L = mv.length;
|
|
613
|
+
if (nn > 0) parts["leap-size"] = w["leap-size"] * (LEAP[nn] ?? 1.5);
|
|
614
|
+
if (tl >= 2 && nn > 0 && x === line[tl - 2]) parts.return = w.return;
|
|
615
|
+
const hov = line.slice(0, -1).slice(-3).filter((y) => y === x).length;
|
|
616
|
+
if (hov) parts.hover = hov * w.hover;
|
|
617
|
+
if (m !== null && Math.abs(m) >= 2 && nn > 0 && sign(m) === sign(s)) parts["contrary-after-leap"] = w["contrary-after-leap"] * (nn >= 2 ? 2 : 1);
|
|
618
|
+
if (m !== null && Math.abs(m) >= 3 && nn > 1) parts["step-back"] = w["step-back"];
|
|
619
|
+
if (m !== null && Math.abs(m) >= 2 && nn >= 2) parts["leap-after-leap"] = w["leap-after-leap"];
|
|
620
|
+
if (nn > 0) { let run = 0; for (let i = L - 1; i >= 0 && mv[i] !== 0 && sign(mv[i]) === sign(s); i--) run++; if (run > 4) parts.run = w.run; }
|
|
621
|
+
if (tl >= 5 && Math.max(...all) - Math.min(...all) < 4) parts.open = w.open;
|
|
622
|
+
if (tl > 1 && s > 0 && x === Math.max(...line)) parts.climax = w.climax;
|
|
623
|
+
const same = (i, j, len) => { for (let q = 0; q < len; q++) if (mv[i + q] !== mv[j + q]) return false; return true; };
|
|
624
|
+
if (L >= 4 && mv[L - 1] === mv[L - 3] && mv[L - 2] === mv[L - 4] && mv[L - 1] !== mv[L - 2]) parts.sequence = w.sequence;
|
|
625
|
+
else if (L >= 6 && same(L - 3, L - 6, 3)) parts.sequence = (2 / 3) * w.sequence;
|
|
626
|
+
else if (m !== null && nn > 0) { for (let i = 0; i < L - 3; i++) if (mv[i] === m && mv[i + 1] === s) { parts.memory = -w.memory; break; } }
|
|
627
|
+
if (m !== null && m === s && nn > 0) parts.novelty = w.novelty;
|
|
628
|
+
return clean(parts);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/** The soft cost of the vertical [x c] at the next time, by rule. */
|
|
632
|
+
function verticalParts(D, C, x, c) {
|
|
633
|
+
const parts = {}, t = D.length, prev = t ? D[t - 1] : null, pc = t ? C[t - 1] : null;
|
|
634
|
+
if (c === null || pc === null || prev === null) return parts;
|
|
635
|
+
const dx = x - prev, dc = c - pc, [l, h] = K.lowerFirst(x, c);
|
|
636
|
+
const motion = dx === 0 && dc === 0 ? "none" : dx === 0 || dc === 0 ? "oblique" : sign(dx) === sign(dc) ? "similar" : "contrary";
|
|
637
|
+
if (motion === "oblique") parts.motion = 0.5 * w.motion;
|
|
638
|
+
if (motion === "similar") {
|
|
639
|
+
parts.motion = 0.8 * w.motion;
|
|
640
|
+
const cls = mod(h - l, N);
|
|
641
|
+
if (quality(l, h) === "perfect" && cls === fifth && cls !== 0) parts["direct-perfect"] = w["direct-perfect"];
|
|
642
|
+
let run = 1;
|
|
643
|
+
for (let i = t - 1; i >= 1; i--) {
|
|
644
|
+
const p = D[i], q = C[i], p0 = D[i - 1], q0 = C[i - 1];
|
|
645
|
+
if (q === null || q0 === null) break;
|
|
646
|
+
const [ll, hh] = K.lowerFirst(p, q);
|
|
647
|
+
if (quality(ll, hh) === "imperfect" && mod(hh - ll, N) === cls && sign(p - p0) === sign(q - q0) && p !== p0) run++; else break;
|
|
648
|
+
}
|
|
649
|
+
if (quality(l, h) === "imperfect" && run > 3) parts["imperfect-run"] = w["imperfect-run"];
|
|
650
|
+
}
|
|
651
|
+
if (b > 1 && !strong(t) && K.dissonant(x, c)) parts.passing = -w.passing;
|
|
652
|
+
if (K.suspensionAt([...D, x], [...C, c], t)) parts["suspension-life"] = -w["suspension-life"];
|
|
653
|
+
if (t >= 2) {
|
|
654
|
+
const vc = (i) => (C[i] === null ? null : mod(Math.abs(D[i] - C[i]), N));
|
|
655
|
+
const iv = mod(Math.abs(x - c), N), i1 = vc(t - 1), i2 = vc(t - 2);
|
|
656
|
+
if (i1 !== null && i2 !== null && iv === i2 && iv !== i1) parts.alternation = w.alternation;
|
|
657
|
+
}
|
|
658
|
+
return clean(parts);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/** What the comes's c costs for how it imitates the dux, by the degree. */
|
|
662
|
+
function imitationCost(D, C, c) {
|
|
663
|
+
const t = D.length, m = K.model(D, t), s = imitation, ww = w.imitation;
|
|
664
|
+
if (K.band === "near") return m === null ? 0 : ww * Math.abs(c - m) * (1 + (6 * (s - 0.5)) / 0.4);
|
|
665
|
+
if (K.band === "motion") {
|
|
666
|
+
const i = t - d, md = i >= 1 ? D[i] - D[i - 1] : null, pc = t ? C[t - 1] : null, cm = pc === null ? null : c - pc;
|
|
667
|
+
if (md === null || cm === null) return 0;
|
|
668
|
+
return (sign(md) !== sign(cm) ? ww * (1 + (4 * (s - 0.2)) / 0.3) : 0) + ww * 0.3 * s * Math.abs(cm - md);
|
|
669
|
+
}
|
|
670
|
+
return 0;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/** The soft cost of the comes's c with the dux's x next: the vertical, the comes's own line, the imitation. */
|
|
674
|
+
function pairParts(D, C, x, c) {
|
|
675
|
+
if (c === null) return {};
|
|
676
|
+
const own = melodicParts(C.filter((y) => y !== null), c, D.length, false), parts = verticalParts(D, C, x, c);
|
|
677
|
+
for (const [key, v] of Object.entries(own)) parts["comes/" + key] = v;
|
|
678
|
+
const im = imitationCost(D, C, c);
|
|
679
|
+
if (im > 0) parts.imitation = im;
|
|
680
|
+
return parts;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/** The comes's best note with the dux's x next: {note, futures, cost, parts}, or null when none has a future. */
|
|
684
|
+
/** A choice among candidates by cost: at temperature 0 (or without a random number r in [0, 1)) the cheapest;
|
|
685
|
+
* above it a softmax of -cost/temperature, drawn with r. */
|
|
686
|
+
function softPick(cands, cost, temp, r) {
|
|
687
|
+
const cs = cands.slice().sort((p, q) => cost(p) - cost(q) || (p.note ?? -100) - (q.note ?? -100));
|
|
688
|
+
if (!cs.length || !(temp > 0.001) || r === undefined || r === null) return cs[0] || null;
|
|
689
|
+
const m = cost(cs[0]), ws = cs.map((c) => Math.exp((m - cost(c)) / temp)), x = r * ws.reduce((a, v) => a + v, 0);
|
|
690
|
+
let acc = 0;
|
|
691
|
+
for (let j = 0; j < cs.length; j++) { acc += ws[j]; if (x < acc) return cs[j]; }
|
|
692
|
+
return cs[cs.length - 1];
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** The comes's note with the dux's x next: the cheapest (at temperature 0), or one drawn by temp with r. */
|
|
696
|
+
function bestComes(D, C, x, temp = 0, r = null) {
|
|
697
|
+
const cands = [];
|
|
698
|
+
for (const e of K.fanComes(D, C, x)) {
|
|
699
|
+
if (e.reason || !(e.futures > 0)) continue;
|
|
700
|
+
const parts = pairParts(D, C, x, e.note);
|
|
701
|
+
cands.push({ note: e.note, futures: e.futures, cost: total(parts), parts });
|
|
702
|
+
}
|
|
703
|
+
return softPick(cands, (c) => c.cost, temp, r);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/** The dux's candidates next: {note, reason} or {note, futures, cost, comes, score} (score: its cost and the
|
|
707
|
+
* best comes's), in the order of the notes. */
|
|
708
|
+
function fan(D, C) {
|
|
709
|
+
const t = D.length;
|
|
710
|
+
return K.fanDux(D, C).map((e) => {
|
|
711
|
+
if (e.reason) return { note: e.note, reason: e.reason };
|
|
712
|
+
if (!(e.futures > 0)) return { note: e.note, reason: null, futures: 0 };
|
|
713
|
+
const parts = melodicParts(D, e.note, t, true), own = total(parts), bc = bestComes(D, C, e.note);
|
|
714
|
+
return { note: e.note, reason: null, futures: e.futures, cost: own, parts, comes: bc.note, comesParts: bc.parts, score: own + bc.cost };
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/** The futuribles next, the most plausible first (by score, then by more futures). */
|
|
719
|
+
const futuribles = (D, C) => fan(D, C).filter((e) => e.futures > 0).sort((p, q) => p.score - q.score || q.futures - p.futures || p.note - q.note);
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* The pitch asked for (semitones above C4, any key, a fraction for a bend) as the dux's next note: itself when it
|
|
723
|
+
* is legal and has a future, otherwise the nearest one that is (an asked pitch outside the range is first
|
|
724
|
+
* taken by octaves into it; between two as near, the more plausible). With the comes's best note with it.
|
|
725
|
+
* {note, comes, asked, corrected, reason, futures, fan} or null when nothing can follow (the work is finished).
|
|
726
|
+
*/
|
|
727
|
+
function play(D, C, asked, opt = {}) {
|
|
728
|
+
if (D.length >= n) return null;
|
|
729
|
+
const F = fan(D, C), ok = F.filter((e) => e.futures > 0);
|
|
730
|
+
if (!ok.length) return null;
|
|
731
|
+
const r = Math.round(asked);
|
|
732
|
+
let a = asked;
|
|
733
|
+
const sl = semis(lo), sh = semis(hi);
|
|
734
|
+
while (a < sl - P / 2) a += P;
|
|
735
|
+
while (a > sh + P / 2) a -= P;
|
|
736
|
+
const best = ok.slice().sort((p, q) => Math.abs(semis(p.note) - a) - Math.abs(semis(q.note) - a) || p.score - q.score || p.note - q.note)[0];
|
|
737
|
+
// the step the key asked for: a note of the system within a quarter of a tone or so (a key of 12 for a tuning of another)
|
|
738
|
+
const near = (x) => { let s0 = null; for (let s = lo - 2 * N; s <= hi + 2 * N; s++) if (Math.abs(semis(s) - x) < 0.45 && (s0 === null || Math.abs(semis(s) - x) < Math.abs(semis(s0) - x))) s0 = s; return s0; };
|
|
739
|
+
const step = near(asked);
|
|
740
|
+
let reason = null;
|
|
741
|
+
if (best.note !== step) {
|
|
742
|
+
const e = step === null ? null : F.find((f) => f.note === step);
|
|
743
|
+
reason = step === null ? `${midiName(60 + r)} is not a note of the system (${systemName})`
|
|
744
|
+
: !e ? `${pitchName(step)} is out of the range (${pitchName(lo)}–${pitchName(hi)}): taken by octaves`
|
|
745
|
+
: e.reason ? e.reason
|
|
746
|
+
: "no way to finish the canon from it";
|
|
747
|
+
}
|
|
748
|
+
// the comes: the cheapest (as the futuribles say, :comes), or with a temperature one drawn among those with a future
|
|
749
|
+
const comes = opt.temp > 0.001 && opt.r !== undefined ? bestComes(D, C, best.note, opt.temp, opt.r).note : best.comes;
|
|
750
|
+
return { note: best.note, comes, asked: r, corrected: reason !== null, reason, futures: best.futures, score: best.score, fan: F };
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/** The note before held over the beat (a tie), as play gives it: when it may not be held, the nearest that can. */
|
|
754
|
+
function hold(D, C, opt = {}) {
|
|
755
|
+
if (!D.length) return null;
|
|
756
|
+
const r = play(D, C, semis(D[D.length - 1]), opt);
|
|
757
|
+
if (r && r.corrected) r.reason = `${pitchName(D[D.length - 1])} cannot be held: ${r.reason}`;
|
|
758
|
+
return r;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/** The engine's own next pair, as play gives it, or null: the most plausible, or with opt {temp, r, r2} the
|
|
762
|
+
* dux's note drawn by temp with r among the futuribles (by their score), the comes's with r2. */
|
|
763
|
+
function engine(D, C, opt = {}) {
|
|
764
|
+
const f = softPick(futuribles(D, C), (c) => c.score, opt.temp, opt.r);
|
|
765
|
+
return f ? play(D, C, semis(f.note), { temp: opt.temp, r: opt.r2 }) : null;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/** The debts pending at this point, in words. */
|
|
769
|
+
function obligations(D, C) {
|
|
770
|
+
const t = D.length, out = [], prev = t ? D[t - 1] : null, pc = t ? C[t - 1] : null;
|
|
771
|
+
if (t >= n) return out;
|
|
772
|
+
if (t >= 2 && K.suspensionAt(D, C, t - 1)) out.push(`resolve the suspension: ${K.holder(D, C, t - 1) === "dux" ? "the dux" : "the comes"} a step down`);
|
|
773
|
+
else if (prev !== null && pc !== null && K.dissonant(prev, pc)) out.push("leave the dissonance by step, in both voices");
|
|
774
|
+
const m = t >= 2 ? prev - D[t - 2] : null;
|
|
775
|
+
if (m !== null && Math.abs(m) >= 3) out.push(`after the leap of a ${intervalName(Math.abs(m))}: ${Math.abs(m) >= 5 ? "a step" : "turn"} ${m > 0 ? "down" : "up"}`);
|
|
776
|
+
const mn = K.model(D, t);
|
|
777
|
+
if (mn !== null && K.band === "canon") out.push(`a canon: the comes sounds ${pitchName(mn)}`);
|
|
778
|
+
if (mn !== null && K.band === "near") out.push(`the comes near ${pitchName(mn)} (within a 3rd)`);
|
|
779
|
+
if (mn !== null && K.band === "motion") out.push("the comes moves as the dux moved");
|
|
780
|
+
if (b > 1 && t >= d) out.push(strong(t) ? "a strong beat: a consonance" : "a weak beat: a consonance, or a dissonance passing by step");
|
|
781
|
+
if (t === n - 2) out.push("approach the final by step");
|
|
782
|
+
if (t === n - 1) out.push(`end on the final (${K.finals.map(pitchName).join(" or ")}), on a perfect consonance`);
|
|
783
|
+
if (t < n - 2) out.push(`end on the final in ${n - t} notes`);
|
|
784
|
+
return out;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
return {
|
|
788
|
+
spec, mode, n, bars, beats: b, species, transform: kind, axis: axisStep, kernel: K, lo, hi, tonic, tenor, strong, fan, futuribles, play, hold, engine, obligations,
|
|
789
|
+
pairParts, melodicParts, midi: midiOf, name: pitchName, semis, hz: S.hz, system: S, systemName, modes: Object.keys(MS), modeNote, N, k,
|
|
790
|
+
count: (D, C) => K.count(D, C),
|
|
791
|
+
bits: (D, C) => bitsOf(K.count(D, C)),
|
|
792
|
+
/** The count from the start, a few milliseconds at a time: after it every legal point is counted. */
|
|
793
|
+
warm: (opts) => K.countAsync([], [], opts || {}),
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
// api/import-module hands a component the default export
|
|
798
|
+
export default { live, canon, band, MODES, WEIGHTS, semis, pitchName, midiOf, midiName, semisOfHz, quality, intervalName };
|