@libraz/libcantus 0.9.0 → 0.9.2
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 +25 -2
- package/dist/analyze/index.cjs +1697 -0
- package/dist/analyze/index.cjs.map +1 -0
- package/dist/analyze/index.d.cts +429 -0
- package/dist/analyze/index.d.ts +429 -0
- package/dist/analyze/index.js +51 -0
- package/dist/analyze/index.js.map +1 -0
- package/dist/chunk-5HTNMQW4.js +653 -0
- package/dist/chunk-5HTNMQW4.js.map +1 -0
- package/dist/chunk-7Y7WF64D.js +410 -0
- package/dist/chunk-7Y7WF64D.js.map +1 -0
- package/dist/chunk-A4532ZFR.js +259 -0
- package/dist/chunk-A4532ZFR.js.map +1 -0
- package/dist/chunk-BHUT2S5G.js +22 -0
- package/dist/chunk-BHUT2S5G.js.map +1 -0
- package/dist/chunk-CCBLVAPK.js +42 -0
- package/dist/chunk-CCBLVAPK.js.map +1 -0
- package/dist/chunk-COKTX2G5.js +907 -0
- package/dist/chunk-COKTX2G5.js.map +1 -0
- package/dist/chunk-DYWFNC4A.js +152 -0
- package/dist/chunk-DYWFNC4A.js.map +1 -0
- package/dist/chunk-GQKN5KDT.js +159 -0
- package/dist/chunk-GQKN5KDT.js.map +1 -0
- package/dist/chunk-LN7ZZMD2.js +530 -0
- package/dist/chunk-LN7ZZMD2.js.map +1 -0
- package/dist/chunk-MSWFZF7J.js +118 -0
- package/dist/chunk-MSWFZF7J.js.map +1 -0
- package/dist/chunk-R3S2LUBH.js +2722 -0
- package/dist/chunk-R3S2LUBH.js.map +1 -0
- package/dist/chunk-SDM3VJJ5.js +41 -0
- package/dist/chunk-SDM3VJJ5.js.map +1 -0
- package/dist/chunk-XTC3X6MS.js +816 -0
- package/dist/chunk-XTC3X6MS.js.map +1 -0
- package/dist/chunk-ZXW5YIWK.js +55 -0
- package/dist/chunk-ZXW5YIWK.js.map +1 -0
- package/dist/core/index.cjs +432 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +210 -0
- package/dist/core/index.d.ts +210 -0
- package/dist/core/index.js +73 -0
- package/dist/core/index.js.map +1 -0
- package/dist/function-2bJW1Zp_.d.ts +187 -0
- package/dist/function-r2sh-jOO.d.cts +187 -0
- package/dist/generate/index.cjs +3787 -0
- package/dist/generate/index.cjs.map +1 -0
- package/dist/generate/index.d.cts +980 -0
- package/dist/generate/index.d.ts +980 -0
- package/dist/generate/index.js +52 -0
- package/dist/generate/index.js.map +1 -0
- package/dist/index-BMPxwjwV.d.ts +151 -0
- package/dist/index-BOBXBNwP.d.cts +157 -0
- package/dist/index-BdeQ1r_p.d.cts +56 -0
- package/dist/index-BreoJRw0.d.ts +127 -0
- package/dist/index-CGMZNCx6.d.cts +140 -0
- package/dist/index-CGMZNCx6.d.ts +140 -0
- package/dist/index-CM5W4rcm.d.ts +157 -0
- package/dist/index-CP3wAfEH.d.ts +56 -0
- package/dist/index-Cl7mhOFM.d.cts +164 -0
- package/dist/index-Cl7mhOFM.d.ts +164 -0
- package/dist/index-DZ1M21do.d.cts +127 -0
- package/dist/index-D_ZTUNKp.d.cts +151 -0
- package/dist/index.cjs +5277 -4893
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -2621
- package/dist/index.d.ts +14 -2621
- package/dist/index.js +170 -6152
- package/dist/index.js.map +1 -1
- package/dist/model/index.cjs +2435 -0
- package/dist/model/index.cjs.map +1 -0
- package/dist/model/index.d.cts +630 -0
- package/dist/model/index.d.ts +630 -0
- package/dist/model/index.js +21 -0
- package/dist/model/index.js.map +1 -0
- package/dist/styled-BjXtOFNk.d.cts +282 -0
- package/dist/styled-D7BAx3S3.d.ts +282 -0
- package/dist/theory/index.cjs +1677 -0
- package/dist/theory/index.cjs.map +1 -0
- package/dist/theory/index.d.cts +535 -0
- package/dist/theory/index.d.ts +535 -0
- package/dist/theory/index.js +149 -0
- package/dist/theory/index.js.map +1 -0
- package/dist/types-MGP29r3t.d.cts +30 -0
- package/dist/types-MGP29r3t.d.ts +30 -0
- package/package.json +31 -1
- package/src/analyze/arrange/index.ts +21 -0
- package/src/analyze/arrange/internal.ts +125 -0
- package/src/analyze/arrange/tension.ts +187 -0
- package/src/analyze/arrange/tracks.ts +279 -0
- package/src/{detect → analyze/detect}/index.ts +82 -23
- package/src/analyze/functional/borrowed.ts +84 -0
- package/src/analyze/functional/cadence.ts +66 -0
- package/src/analyze/functional/function.ts +184 -0
- package/src/analyze/functional/index.ts +23 -0
- package/src/analyze/functional/internal.ts +29 -0
- package/src/analyze/functional/roman.ts +393 -0
- package/src/analyze/index.ts +49 -0
- package/src/{timeline → analyze/timeline}/index.ts +87 -24
- package/src/{analysis → analyze/voice}/index.ts +81 -12
- package/src/core/index.ts +47 -0
- package/src/{interval → core/interval}/index.ts +15 -1
- package/src/{meter → core/meter}/index.ts +108 -5
- package/src/{pitch → core/pitch}/index.ts +58 -3
- package/src/{random → core/random}/index.ts +13 -1
- package/src/{tuning → core/tuning}/index.ts +28 -1
- package/src/{bass → generate/bass}/index.ts +66 -17
- package/src/{countermelody → generate/countermelody}/index.ts +54 -24
- package/src/{drums → generate/drums}/fills.ts +31 -20
- package/src/{drums → generate/drums}/index.ts +127 -9
- package/src/{drums → generate/drums}/internal.ts +7 -1
- package/src/{drums → generate/drums}/rng.ts +2 -2
- package/src/{drums → generate/drums}/swing.ts +7 -2
- package/src/{groove → generate/groove}/index.ts +118 -22
- package/src/{harmonize → generate/harmonize}/index.ts +96 -23
- package/src/generate/index.ts +59 -0
- package/src/{motif → generate/motif}/index.ts +94 -17
- package/src/{progression → generate/progression}/index.ts +61 -27
- package/src/{reharmony → generate/reharmony}/index.ts +46 -15
- package/src/{rhythm → generate/rhythm}/index.ts +47 -13
- package/src/index.ts +130 -116
- package/src/model/chord.ts +423 -0
- package/src/model/index.ts +5 -947
- package/src/model/interval.ts +82 -0
- package/src/model/key.ts +266 -0
- package/src/model/note.ts +176 -0
- package/src/model/progression.ts +161 -0
- package/src/model/shared.ts +12 -0
- package/src/{chord → theory/chord}/index.ts +85 -4
- package/src/{chordscale → theory/chordscale}/index.ts +67 -9
- package/src/{counterpoint → theory/counterpoint}/index.ts +23 -2
- package/src/{harmony → theory/harmony}/index.ts +19 -2
- package/src/theory/index.ts +107 -0
- package/src/{safety → theory/safety}/index.ts +59 -12
- package/src/theory/scale/degrees.ts +123 -0
- package/src/theory/scale/index.ts +28 -0
- package/src/theory/scale/key.ts +56 -0
- package/src/theory/scale/masks.ts +153 -0
- package/src/{spelling → theory/spelling}/index.ts +21 -3
- package/src/{symbol → theory/symbol}/index.ts +91 -36
- package/src/theory/voicing/index.ts +5 -0
- package/src/theory/voicing/internal.ts +168 -0
- package/src/theory/voicing/leading.ts +112 -0
- package/src/theory/voicing/satb.ts +195 -0
- package/src/theory/voicing/styled.ts +176 -0
- package/src/arrange/index.ts +0 -409
- package/src/functional/index.ts +0 -602
- package/src/scale/index.ts +0 -215
- package/src/voicing/index.ts +0 -523
- /package/src/{types.ts → core/types.ts} +0 -0
- /package/src/{drums → generate/drums}/beat.ts +0 -0
- /package/src/{drums → generate/drums}/euclid.ts +0 -0
- /package/src/{drums → generate/drums}/ghost.ts +0 -0
- /package/src/{drums → generate/drums}/hihat.ts +0 -0
- /package/src/{drums → generate/drums}/hit.ts +0 -0
- /package/src/{drums → generate/drums}/kick.ts +0 -0
- /package/src/{drums → generate/drums}/percussion.ts +0 -0
package/src/voicing/index.ts
DELETED
|
@@ -1,523 +0,0 @@
|
|
|
1
|
-
import type { Chord } from '../chord/index.js';
|
|
2
|
-
import { chordPitchClasses, chordToneRole } from '../chord/index.js';
|
|
3
|
-
import {
|
|
4
|
-
createsParallelOctave,
|
|
5
|
-
createsParallelPerfect,
|
|
6
|
-
createsVoiceCrossing,
|
|
7
|
-
createsVoiceOverlap,
|
|
8
|
-
exceedsSpacing,
|
|
9
|
-
} from '../counterpoint/index.js';
|
|
10
|
-
|
|
11
|
-
/** An inclusive MIDI pitch range for a single voice. */
|
|
12
|
-
export type VoiceRange = {
|
|
13
|
-
min: number;
|
|
14
|
-
max: number;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Default four-voice SATB ranges, ascending (index 0 = lowest):
|
|
19
|
-
* bass E2–C4 (40–60), tenor C3–G4 (48–67), alto G3–D5 (55–74),
|
|
20
|
-
* soprano C4–G5 (60–79).
|
|
21
|
-
*/
|
|
22
|
-
export const SATB_RANGES: readonly VoiceRange[] = [
|
|
23
|
-
{ min: 40, max: 60 },
|
|
24
|
-
{ min: 48, max: 67 },
|
|
25
|
-
{ min: 55, max: 74 },
|
|
26
|
-
{ min: 60, max: 79 },
|
|
27
|
-
];
|
|
28
|
-
|
|
29
|
-
/** Options controlling {@link voiceChord} and {@link voiceProgression}. */
|
|
30
|
-
export type VoicingOptions = {
|
|
31
|
-
/** Number of voices to realize (default 4). Ignored when `ranges` is given. */
|
|
32
|
-
voices?: number;
|
|
33
|
-
/** Explicit per-voice ranges, ascending (index 0 = lowest). Takes precedence over `voices`. */
|
|
34
|
-
ranges?: VoiceRange[];
|
|
35
|
-
/** Maximum spacing in semitones between adjacent upper voices (default 12). */
|
|
36
|
-
maxSpacing?: number;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/** Default maximum spacing between adjacent upper voices (one octave). */
|
|
40
|
-
const DEFAULT_MAX_SPACING = 12;
|
|
41
|
-
|
|
42
|
-
/** Overall pitch floor/ceiling used when deriving ranges for arbitrary voice counts. */
|
|
43
|
-
const DERIVED_LOW = 40;
|
|
44
|
-
const DERIVED_HIGH = 79;
|
|
45
|
-
/** Span of each derived per-voice range in semitones. */
|
|
46
|
-
const DERIVED_SPAN = 19;
|
|
47
|
-
|
|
48
|
-
/** Score penalty per counterpoint violation between consecutive voicings. */
|
|
49
|
-
const VIOLATION_PENALTY = 1000;
|
|
50
|
-
/** Score penalty per chord tone absent from a voicing. */
|
|
51
|
-
const MISSING_TONE_PENALTY = 500;
|
|
52
|
-
/** Score penalty per doubled tone that is neither the root nor the fifth. */
|
|
53
|
-
const POOR_DOUBLING_PENALTY = 4;
|
|
54
|
-
/** Hard cap on candidate voicings evaluated per chord, keeping the search bounded. */
|
|
55
|
-
const MAX_CANDIDATES = 4000;
|
|
56
|
-
|
|
57
|
-
/** Reduce a value to a pitch class in [0, 11]. */
|
|
58
|
-
function pitchClass(value: number): number {
|
|
59
|
-
return ((Math.trunc(value) % 12) + 12) % 12;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Resolve the per-voice ranges implied by the options: explicit `ranges` win,
|
|
64
|
-
* four voices use {@link SATB_RANGES}, and other counts get evenly spaced
|
|
65
|
-
* ranges spanning roughly the bass-to-soprano compass.
|
|
66
|
-
*/
|
|
67
|
-
function resolveRanges(opts?: VoicingOptions): VoiceRange[] {
|
|
68
|
-
if (opts?.ranges !== undefined) {
|
|
69
|
-
if (opts.ranges.length === 0) {
|
|
70
|
-
throw new Error('ranges must contain at least one voice range');
|
|
71
|
-
}
|
|
72
|
-
return opts.ranges.map((range) => ({ ...range }));
|
|
73
|
-
}
|
|
74
|
-
const voices = opts?.voices ?? 4;
|
|
75
|
-
if (voices < 1) {
|
|
76
|
-
throw new Error('voices must be at least 1');
|
|
77
|
-
}
|
|
78
|
-
if (voices === 4) {
|
|
79
|
-
return SATB_RANGES.map((range) => ({ ...range }));
|
|
80
|
-
}
|
|
81
|
-
if (voices === 1) {
|
|
82
|
-
return [{ min: DERIVED_LOW, max: DERIVED_HIGH }];
|
|
83
|
-
}
|
|
84
|
-
const ranges: VoiceRange[] = [];
|
|
85
|
-
for (let i = 0; i < voices; i += 1) {
|
|
86
|
-
const min = Math.round(
|
|
87
|
-
DERIVED_LOW + (i * (DERIVED_HIGH - DERIVED_SPAN - DERIVED_LOW)) / (voices - 1),
|
|
88
|
-
);
|
|
89
|
-
ranges.push({ min, max: min + DERIVED_SPAN });
|
|
90
|
-
}
|
|
91
|
-
return ranges;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** All MIDI pitches of a pitch class inside an inclusive range, ascending. */
|
|
95
|
-
function pitchesForPc(pc: number, range: VoiceRange): number[] {
|
|
96
|
-
const result: number[] = [];
|
|
97
|
-
for (let midi = Math.ceil(range.min); midi <= range.max; midi += 1) {
|
|
98
|
-
if (pitchClass(midi) === pc) {
|
|
99
|
-
result.push(midi);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return result;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Enumerate candidate voicings for a chord: the bass takes the chord's bass
|
|
107
|
-
* (or root) pitch class at each available octave, and each upper voice takes
|
|
108
|
-
* any chord pitch class within its range. Candidates are built in ascending
|
|
109
|
-
* voice order and pruned to exclude voice crossings and over-wide adjacent
|
|
110
|
-
* spacing (the bass–tenor pair is allowed an extra octave, per convention).
|
|
111
|
-
* Enumeration is deterministic and capped at {@link MAX_CANDIDATES}.
|
|
112
|
-
*/
|
|
113
|
-
function enumerateVoicings(chord: Chord, ranges: VoiceRange[], maxSpacing: number): number[][] {
|
|
114
|
-
const chordPcs = chordPitchClasses(chord);
|
|
115
|
-
const bassPc = pitchClass(chord.bassPc ?? chord.rootPc);
|
|
116
|
-
const results: number[][] = [];
|
|
117
|
-
const current: number[] = [];
|
|
118
|
-
const build = (voice: number): void => {
|
|
119
|
-
if (results.length >= MAX_CANDIDATES) {
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
if (voice === ranges.length) {
|
|
123
|
-
results.push([...current]);
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
const range = ranges[voice];
|
|
127
|
-
if (range === undefined) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
const pcs = voice === 0 ? [bassPc] : chordPcs;
|
|
131
|
-
const prev = current[voice - 1];
|
|
132
|
-
for (const pc of pcs) {
|
|
133
|
-
for (const pitch of pitchesForPc(pc, range)) {
|
|
134
|
-
if (prev !== undefined) {
|
|
135
|
-
if (pitch < prev) {
|
|
136
|
-
continue; // would cross below the next lower voice
|
|
137
|
-
}
|
|
138
|
-
const spacingLimit = voice === 1 ? maxSpacing + 12 : maxSpacing;
|
|
139
|
-
if (pitch - prev > spacingLimit) {
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
current.push(pitch);
|
|
144
|
-
build(voice + 1);
|
|
145
|
-
current.pop();
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
build(0);
|
|
150
|
-
return results;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Structural quality penalty of a single voicing: heavily penalize missing
|
|
155
|
-
* chord tones, mildly penalize doubling anything other than the root or fifth.
|
|
156
|
-
*/
|
|
157
|
-
function structuralPenalty(pitches: number[], chord: Chord): number {
|
|
158
|
-
const counts = new Map<number, number>();
|
|
159
|
-
for (const pitch of pitches) {
|
|
160
|
-
const pc = pitchClass(pitch);
|
|
161
|
-
counts.set(pc, (counts.get(pc) ?? 0) + 1);
|
|
162
|
-
}
|
|
163
|
-
let penalty = 0;
|
|
164
|
-
for (const pc of chordPitchClasses(chord)) {
|
|
165
|
-
if (!counts.has(pc)) {
|
|
166
|
-
penalty += MISSING_TONE_PENALTY;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
const rootPc = pitchClass(chord.rootPc);
|
|
170
|
-
const fifthPc = pitchClass(chord.rootPc + 7);
|
|
171
|
-
for (const [pc, count] of counts) {
|
|
172
|
-
if (count > 1 && pc !== rootPc && pc !== fifthPc) {
|
|
173
|
-
penalty += (count - 1) * POOR_DOUBLING_PENALTY;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return penalty;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Count counterpoint violations between two consecutive voicings of equal
|
|
181
|
-
* length: parallel perfects and parallel octaves on every voice pair, voice
|
|
182
|
-
* crossings in the new voicing, and — on adjacent pairs — voice overlaps and
|
|
183
|
-
* over-wide spacing between upper voices.
|
|
184
|
-
*/
|
|
185
|
-
function violationCount(prev: number[], cur: number[], maxSpacing: number): number {
|
|
186
|
-
let count = 0;
|
|
187
|
-
for (let lower = 0; lower < cur.length; lower += 1) {
|
|
188
|
-
for (let upper = lower + 1; upper < cur.length; upper += 1) {
|
|
189
|
-
const prevLower = prev[lower];
|
|
190
|
-
const prevUpper = prev[upper];
|
|
191
|
-
const curLower = cur[lower];
|
|
192
|
-
const curUpper = cur[upper];
|
|
193
|
-
if (
|
|
194
|
-
prevLower === undefined ||
|
|
195
|
-
prevUpper === undefined ||
|
|
196
|
-
curLower === undefined ||
|
|
197
|
-
curUpper === undefined
|
|
198
|
-
) {
|
|
199
|
-
continue;
|
|
200
|
-
}
|
|
201
|
-
if (createsParallelPerfect(prevUpper, curUpper, prevLower, curLower)) {
|
|
202
|
-
count += 1;
|
|
203
|
-
}
|
|
204
|
-
if (createsParallelOctave(prevUpper, curUpper, prevLower, curLower)) {
|
|
205
|
-
count += 1;
|
|
206
|
-
}
|
|
207
|
-
if (createsVoiceCrossing(curUpper, curLower)) {
|
|
208
|
-
count += 1;
|
|
209
|
-
}
|
|
210
|
-
if (upper === lower + 1) {
|
|
211
|
-
if (createsVoiceOverlap(prevUpper, curUpper, prevLower, curLower)) {
|
|
212
|
-
count += 1;
|
|
213
|
-
}
|
|
214
|
-
if (lower > 0 && exceedsSpacing(curUpper, curLower, maxSpacing)) {
|
|
215
|
-
count += 1;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return count;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Total voice-leading cost between two voicings: the sum of absolute semitone
|
|
225
|
-
* motion across voices. The arrays must be the same length; when they differ
|
|
226
|
-
* the voicings are not comparable and the cost is `Infinity`.
|
|
227
|
-
*
|
|
228
|
-
* @param from The previous voicing, one MIDI pitch per voice.
|
|
229
|
-
* @param to The next voicing, one MIDI pitch per voice.
|
|
230
|
-
* @returns The summed absolute motion, or `Infinity` when lengths differ.
|
|
231
|
-
*/
|
|
232
|
-
export function voiceLeadingCost(from: number[], to: number[]): number {
|
|
233
|
-
if (from.length !== to.length) {
|
|
234
|
-
return Number.POSITIVE_INFINITY;
|
|
235
|
-
}
|
|
236
|
-
let total = 0;
|
|
237
|
-
for (let i = 0; i < from.length; i += 1) {
|
|
238
|
-
const a = from[i];
|
|
239
|
-
const b = to[i];
|
|
240
|
-
if (a === undefined || b === undefined) {
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
total += Math.abs(b - a);
|
|
244
|
-
}
|
|
245
|
-
return total;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Realize a single chord as one MIDI pitch per voice, ascending (index 0 =
|
|
250
|
-
* lowest). The bass voice takes the chord's `bassPc` when set, otherwise the
|
|
251
|
-
* root; upper voices take chord pitch classes, doubling the root or fifth as
|
|
252
|
-
* needed to fill all voices. The result stays inside each voice's range, keeps
|
|
253
|
-
* adjacent upper voices within `maxSpacing`, avoids voice crossing, and is
|
|
254
|
-
* deterministic: a compact close-position voicing centered in the ranges.
|
|
255
|
-
*
|
|
256
|
-
* @param chord The chord to voice.
|
|
257
|
-
* @param opts Voicing options; defaults to four voices in {@link SATB_RANGES}.
|
|
258
|
-
* @returns MIDI pitches, ascending, one per voice.
|
|
259
|
-
* @throws If no voicing fits the given ranges.
|
|
260
|
-
*/
|
|
261
|
-
export function voiceChord(chord: Chord, opts?: VoicingOptions): number[] {
|
|
262
|
-
const ranges = resolveRanges(opts);
|
|
263
|
-
const maxSpacing = opts?.maxSpacing ?? DEFAULT_MAX_SPACING;
|
|
264
|
-
const candidates = enumerateVoicings(chord, ranges, maxSpacing);
|
|
265
|
-
let best: number[] | undefined;
|
|
266
|
-
let bestScore = Number.POSITIVE_INFINITY;
|
|
267
|
-
for (const candidate of candidates) {
|
|
268
|
-
let score = structuralPenalty(candidate, chord);
|
|
269
|
-
for (let i = 0; i < candidate.length; i += 1) {
|
|
270
|
-
const pitch = candidate[i];
|
|
271
|
-
const range = ranges[i];
|
|
272
|
-
if (pitch === undefined || range === undefined) {
|
|
273
|
-
continue;
|
|
274
|
-
}
|
|
275
|
-
// Prefer pitches near the middle of each voice's range for a centered,
|
|
276
|
-
// compact default voicing.
|
|
277
|
-
score += Math.abs(pitch - (range.min + range.max) / 2);
|
|
278
|
-
}
|
|
279
|
-
if (score < bestScore) {
|
|
280
|
-
bestScore = score;
|
|
281
|
-
best = candidate;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
if (best === undefined) {
|
|
285
|
-
throw new Error('no voicing satisfies the given ranges');
|
|
286
|
-
}
|
|
287
|
-
return best;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Voice a chord progression with smooth voice leading. The first chord is
|
|
292
|
-
* voiced with {@link voiceChord}; each subsequent chord picks, from a bounded
|
|
293
|
-
* deterministic candidate set, the voicing minimizing the voice-leading cost
|
|
294
|
-
* from the previous voicing plus a large penalty per counterpoint violation
|
|
295
|
-
* (parallel perfects/octaves, voice crossing, voice overlap, and over-wide
|
|
296
|
-
* upper-voice spacing).
|
|
297
|
-
*
|
|
298
|
-
* @param chords The chords to voice in order.
|
|
299
|
-
* @param opts Voicing options; defaults to four voices in {@link SATB_RANGES}.
|
|
300
|
-
* @returns One voicing per chord, each ascending with one MIDI pitch per voice.
|
|
301
|
-
* @throws If any chord admits no voicing within the given ranges.
|
|
302
|
-
*/
|
|
303
|
-
export function voiceProgression(chords: Chord[], opts?: VoicingOptions): number[][] {
|
|
304
|
-
const ranges = resolveRanges(opts);
|
|
305
|
-
const maxSpacing = opts?.maxSpacing ?? DEFAULT_MAX_SPACING;
|
|
306
|
-
const result: number[][] = [];
|
|
307
|
-
let prev: number[] | undefined;
|
|
308
|
-
for (const chord of chords) {
|
|
309
|
-
if (prev === undefined) {
|
|
310
|
-
prev = voiceChord(chord, opts);
|
|
311
|
-
result.push(prev);
|
|
312
|
-
continue;
|
|
313
|
-
}
|
|
314
|
-
const candidates = enumerateVoicings(chord, ranges, maxSpacing);
|
|
315
|
-
let best: number[] | undefined;
|
|
316
|
-
let bestScore = Number.POSITIVE_INFINITY;
|
|
317
|
-
for (const candidate of candidates) {
|
|
318
|
-
const score =
|
|
319
|
-
structuralPenalty(candidate, chord) +
|
|
320
|
-
voiceLeadingCost(prev, candidate) +
|
|
321
|
-
VIOLATION_PENALTY * violationCount(prev, candidate, maxSpacing);
|
|
322
|
-
if (score < bestScore) {
|
|
323
|
-
bestScore = score;
|
|
324
|
-
best = candidate;
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
if (best === undefined) {
|
|
328
|
-
throw new Error('no voicing satisfies the given ranges');
|
|
329
|
-
}
|
|
330
|
-
result.push(best);
|
|
331
|
-
prev = best;
|
|
332
|
-
}
|
|
333
|
-
return result;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* A tertian voicing style for {@link voiceChordStyled}.
|
|
338
|
-
*
|
|
339
|
-
* - `close`: the plain close-position tertian stack.
|
|
340
|
-
* - `drop2`: the second voice from the top dropped an octave (drop-2 voicing).
|
|
341
|
-
* - `drop3`: the third voice from the top dropped an octave (drop-3 voicing).
|
|
342
|
-
* - `shell`: root plus guide tones (third and seventh) for seventh chords, or
|
|
343
|
-
* root/third/fifth for triads; the fifth and tensions are omitted.
|
|
344
|
-
* - `rootless`: the root omitted, keeping third/fifth/seventh and tensions
|
|
345
|
-
* (a typical left-hand jazz voicing).
|
|
346
|
-
*/
|
|
347
|
-
export type VoicingStyle = 'close' | 'drop2' | 'drop3' | 'shell' | 'rootless';
|
|
348
|
-
|
|
349
|
-
/** Options controlling {@link voiceChordStyled}. */
|
|
350
|
-
export type StyledVoicingOptions = {
|
|
351
|
-
/** Voicing style to build (default `close`). */
|
|
352
|
-
style?: VoicingStyle;
|
|
353
|
-
/** Constrain the highest voice to this pitch class (0..11) when given. */
|
|
354
|
-
topNote?: number;
|
|
355
|
-
/** Base octave for the close stack; the stack begins near `12 * octave` (default 4). */
|
|
356
|
-
octave?: number;
|
|
357
|
-
/** Omit the root regardless of style (relevant for jazz voicings). */
|
|
358
|
-
rootless?: boolean;
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
/** Default base octave for a styled close-position stack. */
|
|
362
|
-
const DEFAULT_STYLE_OCTAVE = 4;
|
|
363
|
-
|
|
364
|
-
/** Return the pitch classes of a list in order, without duplicates. */
|
|
365
|
-
function dedupePcs(pcs: number[]): number[] {
|
|
366
|
-
const seen = new Set<number>();
|
|
367
|
-
const result: number[] = [];
|
|
368
|
-
for (const pc of pcs) {
|
|
369
|
-
if (!seen.has(pc)) {
|
|
370
|
-
seen.add(pc);
|
|
371
|
-
result.push(pc);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
return result;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
/** The lowest MIDI pitch of a pitch class at or above a floor. */
|
|
378
|
-
function lowestPitchAtOrAbove(pc: number, floor: number): number {
|
|
379
|
-
return floor + ((((pc - pitchClass(floor)) % 12) + 12) % 12);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
/** The pitch class in `pcs` whose circular distance to `target` is smallest. */
|
|
383
|
-
function nearestPc(target: number, pcs: number[]): number {
|
|
384
|
-
let best = pcs[0] ?? target;
|
|
385
|
-
let bestDist = 12;
|
|
386
|
-
for (const pc of pcs) {
|
|
387
|
-
const dist = Math.min((((pc - target) % 12) + 12) % 12, (((target - pc) % 12) + 12) % 12);
|
|
388
|
-
if (dist < bestDist) {
|
|
389
|
-
bestDist = dist;
|
|
390
|
-
best = pc;
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
return best;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* Realize a single chord as an explicit tertian voicing in a chosen style,
|
|
398
|
-
* independent of the SATB range search used by {@link voiceChord}.
|
|
399
|
-
*
|
|
400
|
-
* The chord tones are stacked in close position from the bass (`bassPc`, else
|
|
401
|
-
* `rootPc`) in the given scientific-pitch `octave` upward (octave 4 starts the
|
|
402
|
-
* bass near C4 = MIDI 60, matching {@link midiToNote} and the bass module).
|
|
403
|
-
* When `topNote` is given the stack is rotated so the highest voice's pitch
|
|
404
|
-
* class equals that note (or the nearest chord tone). The chosen
|
|
405
|
-
* {@link VoicingStyle} then transforms the stack, and the result is returned as
|
|
406
|
-
* ascending MIDI pitches (index 0 = lowest).
|
|
407
|
-
*
|
|
408
|
-
* @param chord The chord to voice.
|
|
409
|
-
* @param opts Styled voicing options; defaults to a close voicing at octave 4.
|
|
410
|
-
* @returns MIDI pitches, ascending, one per retained voice.
|
|
411
|
-
*/
|
|
412
|
-
export function voiceChordStyled(chord: Chord, opts?: StyledVoicingOptions): number[] {
|
|
413
|
-
const style = opts?.style ?? 'close';
|
|
414
|
-
const base = 12 * ((opts?.octave ?? DEFAULT_STYLE_OCTAVE) + 1);
|
|
415
|
-
const rootPc = pitchClass(chord.rootPc);
|
|
416
|
-
const bassPc = pitchClass(chord.bassPc ?? chord.rootPc);
|
|
417
|
-
const omitRoot = style === 'rootless' || opts?.rootless === true;
|
|
418
|
-
|
|
419
|
-
// Tertian chord tones in order, with the bass tone rotated to the bottom.
|
|
420
|
-
let order = dedupePcs(chord.intervals.map((interval) => pitchClass(chord.rootPc + interval)));
|
|
421
|
-
const bassIndex = order.indexOf(bassPc);
|
|
422
|
-
if (bassIndex > 0) {
|
|
423
|
-
order = [...order.slice(bassIndex), ...order.slice(0, bassIndex)];
|
|
424
|
-
} else if (bassIndex < 0) {
|
|
425
|
-
order = [bassPc, ...order];
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
if (style === 'shell') {
|
|
429
|
-
const roles = order.map((pc) => chordToneRole(pc, chord));
|
|
430
|
-
// The guide tone is the seventh when present; on a sixth chord the sixth
|
|
431
|
-
// stands in for it, and otherwise the voicing falls back to the fifth.
|
|
432
|
-
const guide = roles.includes('seventh')
|
|
433
|
-
? 'seventh'
|
|
434
|
-
: roles.includes('sixth')
|
|
435
|
-
? 'sixth'
|
|
436
|
-
: 'fifth';
|
|
437
|
-
order = order.filter((_pc, i) => {
|
|
438
|
-
const role = roles[i];
|
|
439
|
-
return role === 'root' || role === 'third' || role === guide;
|
|
440
|
-
});
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
if (omitRoot) {
|
|
444
|
-
order = order.filter((pc) => pc !== rootPc);
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
if (order.length === 0) {
|
|
448
|
-
return [];
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
if (opts?.topNote !== undefined) {
|
|
452
|
-
const target = order.includes(pitchClass(opts.topNote))
|
|
453
|
-
? pitchClass(opts.topNote)
|
|
454
|
-
: nearestPc(pitchClass(opts.topNote), order);
|
|
455
|
-
const targetIndex = order.indexOf(target);
|
|
456
|
-
order = [...order.slice(targetIndex + 1), ...order.slice(0, targetIndex + 1)];
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
// Stack the ordered pitch classes upward in close position.
|
|
460
|
-
const stack: number[] = [];
|
|
461
|
-
let prev: number | undefined;
|
|
462
|
-
for (const pc of order) {
|
|
463
|
-
if (prev === undefined) {
|
|
464
|
-
prev = lowestPitchAtOrAbove(pc, base);
|
|
465
|
-
} else {
|
|
466
|
-
let delta = (((pc - pitchClass(prev)) % 12) + 12) % 12;
|
|
467
|
-
if (delta === 0) {
|
|
468
|
-
delta = 12;
|
|
469
|
-
}
|
|
470
|
-
prev += delta;
|
|
471
|
-
}
|
|
472
|
-
stack.push(prev);
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
if (style === 'drop2' && stack.length >= 2) {
|
|
476
|
-
stack[stack.length - 2] = (stack[stack.length - 2] ?? 0) - 12;
|
|
477
|
-
} else if (style === 'drop3' && stack.length >= 3) {
|
|
478
|
-
stack[stack.length - 3] = (stack[stack.length - 3] ?? 0) - 12;
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
return stack.sort((a, b) => a - b);
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* Voice a single chord to follow smoothly from an arbitrary current voicing.
|
|
486
|
-
*
|
|
487
|
-
* Candidate voicings of `chord` are enumerated within ranges taken from `opts`,
|
|
488
|
-
* or — when neither `voices` nor `ranges` is given — from a one-octave window
|
|
489
|
-
* around each pitch of `current` (so the result matches `current`'s voice
|
|
490
|
-
* count). Each candidate is scored by structural quality, voice-leading motion
|
|
491
|
-
* from `current`, and a large penalty per counterpoint violation; the lowest
|
|
492
|
-
* scoring candidate is returned, ascending.
|
|
493
|
-
*
|
|
494
|
-
* @param current The current voicing to lead from, ascending (index 0 = lowest).
|
|
495
|
-
* @param chord The next chord to voice.
|
|
496
|
-
* @param opts Voicing options; when omitted, ranges follow `current`'s span.
|
|
497
|
-
* @returns The chosen voicing, ascending with one MIDI pitch per voice.
|
|
498
|
-
* @throws If no voicing fits the derived ranges.
|
|
499
|
-
*/
|
|
500
|
-
export function nextVoicing(current: number[], chord: Chord, opts?: VoicingOptions): number[] {
|
|
501
|
-
const ranges =
|
|
502
|
-
opts?.ranges !== undefined || opts?.voices !== undefined
|
|
503
|
-
? resolveRanges(opts)
|
|
504
|
-
: current.map((pitch) => ({ min: pitch - 12, max: pitch + 12 }));
|
|
505
|
-
const maxSpacing = opts?.maxSpacing ?? DEFAULT_MAX_SPACING;
|
|
506
|
-
const candidates = enumerateVoicings(chord, ranges, maxSpacing);
|
|
507
|
-
let best: number[] | undefined;
|
|
508
|
-
let bestScore = Number.POSITIVE_INFINITY;
|
|
509
|
-
for (const candidate of candidates) {
|
|
510
|
-
const score =
|
|
511
|
-
structuralPenalty(candidate, chord) +
|
|
512
|
-
voiceLeadingCost(current, candidate) +
|
|
513
|
-
VIOLATION_PENALTY * violationCount(current, candidate, maxSpacing);
|
|
514
|
-
if (score < bestScore) {
|
|
515
|
-
bestScore = score;
|
|
516
|
-
best = candidate;
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
if (best === undefined) {
|
|
520
|
-
throw new Error('no voicing satisfies the given ranges');
|
|
521
|
-
}
|
|
522
|
-
return best;
|
|
523
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|