vexflow 5.0.0-alpha.1 → 5.0.0-alpha.3
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/build/cjs/vexflow-bravura.js +2 -2
- package/build/cjs/vexflow-bravura.js.map +1 -1
- package/build/cjs/vexflow-core.js +2 -2
- package/build/cjs/vexflow-core.js.map +1 -1
- package/build/cjs/vexflow-debug-with-tests.js +4 -4
- package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
- package/build/cjs/vexflow-debug.js +4 -4
- package/build/cjs/vexflow-debug.js.map +1 -1
- package/build/cjs/vexflow.js +2 -2
- package/build/cjs/vexflow.js.map +1 -1
- package/build/esm/src/version.js +3 -3
- package/package.json +1 -3
- package/entry/vexflow-bravura.ts +0 -25
- package/entry/vexflow-core.ts +0 -16
- package/entry/vexflow-debug-with-tests.ts +0 -64
- package/entry/vexflow-debug.ts +0 -54
- package/entry/vexflow.ts +0 -47
- package/src/accidental.ts +0 -570
- package/src/annotation.ts +0 -294
- package/src/articulation.ts +0 -383
- package/src/barnote.ts +0 -95
- package/src/beam.ts +0 -999
- package/src/bend.ts +0 -268
- package/src/boundingbox.ts +0 -108
- package/src/canvascontext.ts +0 -348
- package/src/chordsymbol.ts +0 -473
- package/src/clef.ts +0 -164
- package/src/clefnote.ts +0 -50
- package/src/crescendo.ts +0 -132
- package/src/curve.ts +0 -195
- package/src/dot.ts +0 -171
- package/src/easyscore.ts +0 -538
- package/src/element.ts +0 -692
- package/src/factory.ts +0 -694
- package/src/font.ts +0 -343
- package/src/fonts/academico.ts +0 -2
- package/src/fonts/academicobold.ts +0 -2
- package/src/fonts/bravura.ts +0 -2
- package/src/fonts/gonville.ts +0 -2
- package/src/fonts/petaluma.ts +0 -2
- package/src/fonts/petalumascript.ts +0 -2
- package/src/formatter.ts +0 -1107
- package/src/fraction.ts +0 -245
- package/src/frethandfinger.ts +0 -180
- package/src/ghostnote.ts +0 -75
- package/src/glyphnote.ts +0 -68
- package/src/glyphs.ts +0 -5880
- package/src/gracenote.ts +0 -146
- package/src/gracenotegroup.ts +0 -194
- package/src/gracetabnote.ts +0 -30
- package/src/index.ts +0 -90
- package/src/keymanager.ts +0 -153
- package/src/keysignature.ts +0 -272
- package/src/keysignote.ts +0 -46
- package/src/metrics.ts +0 -222
- package/src/modifier.ts +0 -216
- package/src/modifiercontext.ts +0 -177
- package/src/multimeasurerest.ts +0 -229
- package/src/music.ts +0 -443
- package/src/note.ts +0 -690
- package/src/notehead.ts +0 -158
- package/src/notesubgroup.ts +0 -88
- package/src/ornament.ts +0 -301
- package/src/parenthesis.ts +0 -89
- package/src/parser.ts +0 -263
- package/src/pedalmarking.ts +0 -273
- package/src/registry.ts +0 -146
- package/src/rendercontext.ts +0 -98
- package/src/renderer.ts +0 -166
- package/src/repeatnote.ts +0 -25
- package/src/stave.ts +0 -885
- package/src/stavebarline.ts +0 -231
- package/src/staveconnector.ts +0 -281
- package/src/stavehairpin.ts +0 -209
- package/src/staveline.ts +0 -343
- package/src/stavemodifier.ts +0 -93
- package/src/stavenote.ts +0 -1230
- package/src/staverepetition.ts +0 -148
- package/src/stavesection.ts +0 -54
- package/src/stavetempo.ts +0 -162
- package/src/stavetext.ts +0 -69
- package/src/stavetie.ts +0 -235
- package/src/stavevolta.ts +0 -66
- package/src/stem.ts +0 -225
- package/src/stemmablenote.ts +0 -248
- package/src/stringnumber.ts +0 -286
- package/src/strokes.ts +0 -214
- package/src/svgcontext.ts +0 -639
- package/src/system.ts +0 -327
- package/src/tables.ts +0 -796
- package/src/tabnote.ts +0 -449
- package/src/tabslide.ts +0 -115
- package/src/tabstave.ts +0 -31
- package/src/tabtie.ts +0 -44
- package/src/textbracket.ts +0 -231
- package/src/textdynamics.ts +0 -101
- package/src/textnote.ts +0 -175
- package/src/tickable.ts +0 -374
- package/src/tickcontext.ts +0 -280
- package/src/timesignature.ts +0 -229
- package/src/timesignote.ts +0 -45
- package/src/tremolo.ts +0 -51
- package/src/tuning.ts +0 -90
- package/src/tuplet.ts +0 -349
- package/src/typeguard.ts +0 -136
- package/src/util.ts +0 -169
- package/src/version.ts +0 -4
- package/src/vexflow.ts +0 -378
- package/src/vibrato.ts +0 -99
- package/src/vibratobracket.ts +0 -81
- package/src/voice.ts +0 -302
- package/src/web.ts +0 -35
package/src/music.ts
DELETED
|
@@ -1,443 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
|
|
2
|
-
// MIT License
|
|
3
|
-
|
|
4
|
-
import { RuntimeError } from './util';
|
|
5
|
-
|
|
6
|
-
export interface NoteAccidental {
|
|
7
|
-
note: number;
|
|
8
|
-
accidental: AccidentalValue;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface NoteParts {
|
|
12
|
-
root: string;
|
|
13
|
-
accidental: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface KeyParts {
|
|
17
|
-
root: string;
|
|
18
|
-
accidental: string;
|
|
19
|
-
type: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export type KeyValue = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
|
|
23
|
-
|
|
24
|
-
export type RootValue = 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
25
|
-
|
|
26
|
-
export type AccidentalValue = -2 | -1 | 0 | 1 | 2;
|
|
27
|
-
|
|
28
|
-
export interface Key {
|
|
29
|
-
rootIndex: RootValue;
|
|
30
|
-
intVal: KeyValue;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Music implements some standard music theory routines. */
|
|
34
|
-
export class Music {
|
|
35
|
-
/** Number of an canonical notes (12). */
|
|
36
|
-
static get NUM_TONES(): number {
|
|
37
|
-
return this.canonicalNotes.length;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Names of root notes ('c', 'd',...) */
|
|
41
|
-
static get roots(): string[] {
|
|
42
|
-
return ['c', 'd', 'e', 'f', 'g', 'a', 'b'];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Values of the root notes.*/
|
|
46
|
-
static get rootValues(): KeyValue[] {
|
|
47
|
-
return [0, 2, 4, 5, 7, 9, 11];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** Indexes of the root notes.*/
|
|
51
|
-
static get rootIndexes(): Record<string, RootValue> {
|
|
52
|
-
return {
|
|
53
|
-
c: 0,
|
|
54
|
-
d: 1,
|
|
55
|
-
e: 2,
|
|
56
|
-
f: 3,
|
|
57
|
-
g: 4,
|
|
58
|
-
a: 5,
|
|
59
|
-
b: 6,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** Names of canonical notes ('c', 'c#', 'd',...). */
|
|
64
|
-
static get canonicalNotes(): string[] {
|
|
65
|
-
return ['c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#', 'a', 'a#', 'b'];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** Names of diatonic intervals ('unison', 'm2', 'M2',...). */
|
|
69
|
-
static get diatonicIntervals(): string[] {
|
|
70
|
-
return ['unison', 'm2', 'M2', 'm3', 'M3', 'p4', 'dim5', 'p5', 'm6', 'M6', 'b7', 'M7', 'octave'];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** NoteAccidental associated to diatonic intervals. */
|
|
74
|
-
static get diatonicAccidentals(): Record<string, NoteAccidental> {
|
|
75
|
-
return {
|
|
76
|
-
unison: { note: 0, accidental: 0 },
|
|
77
|
-
m2: { note: 1, accidental: -1 },
|
|
78
|
-
M2: { note: 1, accidental: 0 },
|
|
79
|
-
m3: { note: 2, accidental: -1 },
|
|
80
|
-
M3: { note: 2, accidental: 0 },
|
|
81
|
-
p4: { note: 3, accidental: 0 },
|
|
82
|
-
dim5: { note: 4, accidental: -1 },
|
|
83
|
-
p5: { note: 4, accidental: 0 },
|
|
84
|
-
m6: { note: 5, accidental: -1 },
|
|
85
|
-
M6: { note: 5, accidental: 0 },
|
|
86
|
-
b7: { note: 6, accidental: -1 },
|
|
87
|
-
M7: { note: 6, accidental: 0 },
|
|
88
|
-
octave: { note: 7, accidental: 0 },
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** Semitones shift associated to intervals .*/
|
|
93
|
-
static get intervals(): Record<string, number> {
|
|
94
|
-
return {
|
|
95
|
-
u: 0,
|
|
96
|
-
unison: 0,
|
|
97
|
-
m2: 1,
|
|
98
|
-
b2: 1,
|
|
99
|
-
min2: 1,
|
|
100
|
-
S: 1,
|
|
101
|
-
H: 1,
|
|
102
|
-
2: 2,
|
|
103
|
-
M2: 2,
|
|
104
|
-
maj2: 2,
|
|
105
|
-
T: 2,
|
|
106
|
-
W: 2,
|
|
107
|
-
m3: 3,
|
|
108
|
-
b3: 3,
|
|
109
|
-
min3: 3,
|
|
110
|
-
M3: 4,
|
|
111
|
-
3: 4,
|
|
112
|
-
maj3: 4,
|
|
113
|
-
4: 5,
|
|
114
|
-
p4: 5,
|
|
115
|
-
'#4': 6,
|
|
116
|
-
b5: 6,
|
|
117
|
-
aug4: 6,
|
|
118
|
-
dim5: 6,
|
|
119
|
-
5: 7,
|
|
120
|
-
p5: 7,
|
|
121
|
-
'#5': 8,
|
|
122
|
-
b6: 8,
|
|
123
|
-
aug5: 8,
|
|
124
|
-
6: 9,
|
|
125
|
-
M6: 9,
|
|
126
|
-
maj6: 9,
|
|
127
|
-
b7: 10,
|
|
128
|
-
m7: 10,
|
|
129
|
-
min7: 10,
|
|
130
|
-
dom7: 10,
|
|
131
|
-
M7: 11,
|
|
132
|
-
maj7: 11,
|
|
133
|
-
8: 12,
|
|
134
|
-
octave: 12,
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/** Semitones shifts associated with scales. */
|
|
139
|
-
static get scales(): Record<string, number[]> {
|
|
140
|
-
return {
|
|
141
|
-
major: [2, 2, 1, 2, 2, 2, 1],
|
|
142
|
-
minor: [2, 1, 2, 2, 1, 2, 2],
|
|
143
|
-
ionian: [2, 2, 1, 2, 2, 2, 1],
|
|
144
|
-
dorian: [2, 1, 2, 2, 2, 1, 2],
|
|
145
|
-
phyrgian: [1, 2, 2, 2, 1, 2, 2],
|
|
146
|
-
lydian: [2, 2, 2, 1, 2, 2, 1],
|
|
147
|
-
mixolydian: [2, 2, 1, 2, 2, 1, 2],
|
|
148
|
-
aeolian: [2, 1, 2, 2, 1, 2, 2],
|
|
149
|
-
locrian: [1, 2, 2, 1, 2, 2, 2],
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** Scales associated with m (minor) and M (major). */
|
|
154
|
-
static get scaleTypes(): Record<string, number[]> {
|
|
155
|
-
return {
|
|
156
|
-
M: Music.scales.major,
|
|
157
|
-
m: Music.scales.minor,
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** Accidentals abbreviations. */
|
|
162
|
-
static get accidentals(): string[] {
|
|
163
|
-
return ['bb', 'b', 'n', '#', '##'];
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/** Note values. */
|
|
167
|
-
static get noteValues(): Record<string, Key> {
|
|
168
|
-
return {
|
|
169
|
-
c: { rootIndex: 0, intVal: 0 },
|
|
170
|
-
cn: { rootIndex: 0, intVal: 0 },
|
|
171
|
-
'c#': { rootIndex: 0, intVal: 1 },
|
|
172
|
-
'c##': { rootIndex: 0, intVal: 2 },
|
|
173
|
-
cb: { rootIndex: 0, intVal: 11 },
|
|
174
|
-
cbb: { rootIndex: 0, intVal: 10 },
|
|
175
|
-
d: { rootIndex: 1, intVal: 2 },
|
|
176
|
-
dn: { rootIndex: 1, intVal: 2 },
|
|
177
|
-
'd#': { rootIndex: 1, intVal: 3 },
|
|
178
|
-
'd##': { rootIndex: 1, intVal: 4 },
|
|
179
|
-
db: { rootIndex: 1, intVal: 1 },
|
|
180
|
-
dbb: { rootIndex: 1, intVal: 0 },
|
|
181
|
-
e: { rootIndex: 2, intVal: 4 },
|
|
182
|
-
en: { rootIndex: 2, intVal: 4 },
|
|
183
|
-
'e#': { rootIndex: 2, intVal: 5 },
|
|
184
|
-
'e##': { rootIndex: 2, intVal: 6 },
|
|
185
|
-
eb: { rootIndex: 2, intVal: 3 },
|
|
186
|
-
ebb: { rootIndex: 2, intVal: 2 },
|
|
187
|
-
f: { rootIndex: 3, intVal: 5 },
|
|
188
|
-
fn: { rootIndex: 3, intVal: 5 },
|
|
189
|
-
'f#': { rootIndex: 3, intVal: 6 },
|
|
190
|
-
'f##': { rootIndex: 3, intVal: 7 },
|
|
191
|
-
fb: { rootIndex: 3, intVal: 4 },
|
|
192
|
-
fbb: { rootIndex: 3, intVal: 3 },
|
|
193
|
-
g: { rootIndex: 4, intVal: 7 },
|
|
194
|
-
gn: { rootIndex: 4, intVal: 7 },
|
|
195
|
-
'g#': { rootIndex: 4, intVal: 8 },
|
|
196
|
-
'g##': { rootIndex: 4, intVal: 9 },
|
|
197
|
-
gb: { rootIndex: 4, intVal: 6 },
|
|
198
|
-
gbb: { rootIndex: 4, intVal: 5 },
|
|
199
|
-
a: { rootIndex: 5, intVal: 9 },
|
|
200
|
-
an: { rootIndex: 5, intVal: 9 },
|
|
201
|
-
'a#': { rootIndex: 5, intVal: 10 },
|
|
202
|
-
'a##': { rootIndex: 5, intVal: 11 },
|
|
203
|
-
ab: { rootIndex: 5, intVal: 8 },
|
|
204
|
-
abb: { rootIndex: 5, intVal: 7 },
|
|
205
|
-
b: { rootIndex: 6, intVal: 11 },
|
|
206
|
-
bn: { rootIndex: 6, intVal: 11 },
|
|
207
|
-
'b#': { rootIndex: 6, intVal: 0 },
|
|
208
|
-
'b##': { rootIndex: 6, intVal: 1 },
|
|
209
|
-
bb: { rootIndex: 6, intVal: 10 },
|
|
210
|
-
bbb: { rootIndex: 6, intVal: 9 },
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
protected isValidNoteValue(note: number): boolean {
|
|
215
|
-
return note >= 0 && note < Music.canonicalNotes.length;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
protected isValidIntervalValue(interval: number): boolean {
|
|
219
|
-
return interval >= 0 && interval < Music.diatonicIntervals.length;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/** Return root and accidental associated to a note. */
|
|
223
|
-
getNoteParts(noteString: string): NoteParts {
|
|
224
|
-
if (!noteString || noteString.length < 1) {
|
|
225
|
-
throw new RuntimeError('BadArguments', 'Invalid note name: ' + noteString);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (noteString.length > 3) {
|
|
229
|
-
throw new RuntimeError('BadArguments', 'Invalid note name: ' + noteString);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const note = noteString.toLowerCase();
|
|
233
|
-
|
|
234
|
-
const regex = /^([cdefgab])(b|bb|n|#|##)?$/;
|
|
235
|
-
const match = regex.exec(note);
|
|
236
|
-
|
|
237
|
-
if (match !== null) {
|
|
238
|
-
const root = match[1];
|
|
239
|
-
const accidental = match[2];
|
|
240
|
-
|
|
241
|
-
return {
|
|
242
|
-
root,
|
|
243
|
-
accidental,
|
|
244
|
-
};
|
|
245
|
-
} else {
|
|
246
|
-
throw new RuntimeError('BadArguments', 'Invalid note name: ' + noteString);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/** Return root, accidental and type associated to a key. */
|
|
251
|
-
getKeyParts(keyString: string): KeyParts {
|
|
252
|
-
if (!keyString || keyString.length < 1) {
|
|
253
|
-
throw new RuntimeError('BadArguments', 'Invalid key: ' + keyString);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
const key = keyString.toLowerCase();
|
|
257
|
-
|
|
258
|
-
// Support Major, Minor, Melodic Minor, and Harmonic Minor key types.
|
|
259
|
-
const regex = /^([cdefgab])(b|#)?(mel|harm|m|M)?$/;
|
|
260
|
-
const match = regex.exec(key);
|
|
261
|
-
|
|
262
|
-
if (match !== null) {
|
|
263
|
-
const root = match[1];
|
|
264
|
-
const accidental = match[2];
|
|
265
|
-
let type = match[3];
|
|
266
|
-
|
|
267
|
-
// Unspecified type implies major
|
|
268
|
-
if (!type) type = 'M';
|
|
269
|
-
|
|
270
|
-
return {
|
|
271
|
-
root,
|
|
272
|
-
accidental,
|
|
273
|
-
type,
|
|
274
|
-
};
|
|
275
|
-
} else {
|
|
276
|
-
throw new RuntimeError('BadArguments', `Invalid key: ${keyString}`);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/** Note value associated to a note name. */
|
|
281
|
-
getNoteValue(noteString: string): number {
|
|
282
|
-
const value = Music.noteValues[noteString];
|
|
283
|
-
if (value === undefined) {
|
|
284
|
-
throw new RuntimeError('BadArguments', `Invalid note name: ${noteString}`);
|
|
285
|
-
}
|
|
286
|
-
return value.intVal;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/** Interval value associated to an interval name. */
|
|
290
|
-
getIntervalValue(intervalString: string): number {
|
|
291
|
-
const value = Music.intervals[intervalString];
|
|
292
|
-
if (value === undefined) {
|
|
293
|
-
throw new RuntimeError('BadArguments', `Invalid interval name: ${intervalString}`);
|
|
294
|
-
}
|
|
295
|
-
return value;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/** Canonical note name associated to a value. */
|
|
299
|
-
getCanonicalNoteName(noteValue: number): string {
|
|
300
|
-
if (!this.isValidNoteValue(noteValue)) {
|
|
301
|
-
throw new RuntimeError('BadArguments', `Invalid note value: ${noteValue}`);
|
|
302
|
-
}
|
|
303
|
-
return Music.canonicalNotes[noteValue];
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/** Interval name associated to a value. */
|
|
307
|
-
getCanonicalIntervalName(intervalValue: number): string {
|
|
308
|
-
if (!this.isValidIntervalValue(intervalValue)) {
|
|
309
|
-
throw new RuntimeError('BadArguments', `Invalid interval value: ${intervalValue}`);
|
|
310
|
-
}
|
|
311
|
-
return Music.diatonicIntervals[intervalValue];
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Given a note, interval, and interval direction, produce the relative note.
|
|
316
|
-
*/
|
|
317
|
-
getRelativeNoteValue(noteValue: number, intervalValue: number, direction: number = 1): number {
|
|
318
|
-
if (direction !== 1 && direction !== -1) {
|
|
319
|
-
throw new RuntimeError('BadArguments', `Invalid direction: ${direction}`);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
let sum = (noteValue + direction * intervalValue) % Music.NUM_TONES;
|
|
323
|
-
if (sum < 0) sum += Music.NUM_TONES;
|
|
324
|
-
|
|
325
|
-
return sum;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Given a root and note value, produce the relative note name.
|
|
330
|
-
*/
|
|
331
|
-
getRelativeNoteName(root: string, noteValue: number): string {
|
|
332
|
-
const parts = this.getNoteParts(root);
|
|
333
|
-
const rootValue = this.getNoteValue(parts.root);
|
|
334
|
-
let interval = noteValue - rootValue;
|
|
335
|
-
|
|
336
|
-
if (Math.abs(interval) > Music.NUM_TONES - 3) {
|
|
337
|
-
let multiplier = 1;
|
|
338
|
-
if (interval > 0) multiplier = -1;
|
|
339
|
-
|
|
340
|
-
// Possibly wrap around. (Add +1 for modulo operator)
|
|
341
|
-
const reverseInterval = ((noteValue + 1 + (rootValue + 1)) % Music.NUM_TONES) * multiplier;
|
|
342
|
-
|
|
343
|
-
if (Math.abs(reverseInterval) > 2) {
|
|
344
|
-
throw new RuntimeError('BadArguments', `Notes not related: ${root}, ${noteValue})`);
|
|
345
|
-
} else {
|
|
346
|
-
interval = reverseInterval;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
if (Math.abs(interval) > 2) {
|
|
351
|
-
throw new RuntimeError('BadArguments', `Notes not related: ${root}, ${noteValue})`);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
let relativeNoteName = parts.root;
|
|
355
|
-
if (interval > 0) {
|
|
356
|
-
for (let i = 1; i <= interval; ++i) {
|
|
357
|
-
relativeNoteName += '#';
|
|
358
|
-
}
|
|
359
|
-
} else if (interval < 0) {
|
|
360
|
-
for (let i = -1; i >= interval; --i) {
|
|
361
|
-
relativeNoteName += 'b';
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
return relativeNoteName;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Return scale tones, given intervals. Each successive interval is
|
|
370
|
-
* relative to the previous one, e.g., Major Scale:
|
|
371
|
-
*
|
|
372
|
-
* TTSTTTS = [2,2,1,2,2,2,1]
|
|
373
|
-
*
|
|
374
|
-
* When used with key = 0, returns C scale (which is isomorphic to
|
|
375
|
-
* interval list).
|
|
376
|
-
*/
|
|
377
|
-
getScaleTones(key: number, intervals: number[]): number[] {
|
|
378
|
-
const tones = [key];
|
|
379
|
-
|
|
380
|
-
let nextNote = key;
|
|
381
|
-
for (let i = 0; i < intervals.length; i++) {
|
|
382
|
-
nextNote = this.getRelativeNoteValue(nextNote, intervals[i]);
|
|
383
|
-
if (nextNote !== key) tones.push(nextNote);
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
return tones;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* Return the interval of a note, given a diatonic scale.
|
|
391
|
-
* e.g., given the scale C, and the note E, returns M3.
|
|
392
|
-
*/
|
|
393
|
-
getIntervalBetween(note1: number, note2: number, direction: number = 1): number {
|
|
394
|
-
if (direction !== 1 && direction !== -1) {
|
|
395
|
-
throw new RuntimeError('BadArguments', `Invalid direction: ${direction}`);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
if (!this.isValidNoteValue(note1) || !this.isValidNoteValue(note2)) {
|
|
399
|
-
throw new RuntimeError('BadArguments', `Invalid notes: ${note1}, ${note2}`);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
let difference = direction === 1 ? note2 - note1 : note1 - note2;
|
|
403
|
-
|
|
404
|
-
if (difference < 0) difference += Music.NUM_TONES;
|
|
405
|
-
|
|
406
|
-
return difference;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* Create a scale map that represents the pitch state for a
|
|
411
|
-
* `keySignature`. For example, passing a `G` to `keySignature` would
|
|
412
|
-
* return a scale map with every note naturalized except for `F` which
|
|
413
|
-
* has an `F#` state.
|
|
414
|
-
*/
|
|
415
|
-
createScaleMap(keySignature: string): Record<string, string> {
|
|
416
|
-
const keySigParts = this.getKeyParts(keySignature);
|
|
417
|
-
if (!keySigParts.type) throw new RuntimeError('BadArguments', 'Unsupported key type: undefined');
|
|
418
|
-
const scaleName = Music.scaleTypes[keySigParts.type];
|
|
419
|
-
|
|
420
|
-
let keySigString = keySigParts.root;
|
|
421
|
-
if (keySigParts.accidental) keySigString += keySigParts.accidental;
|
|
422
|
-
|
|
423
|
-
if (!scaleName) throw new RuntimeError('BadArguments', 'Unsupported key type: ' + keySignature);
|
|
424
|
-
|
|
425
|
-
const scale = this.getScaleTones(this.getNoteValue(keySigString), scaleName);
|
|
426
|
-
const noteLocation = Music.rootIndexes[keySigParts.root];
|
|
427
|
-
|
|
428
|
-
const scaleMap = {} as Record<string, string>;
|
|
429
|
-
for (let i = 0; i < Music.roots.length; ++i) {
|
|
430
|
-
const index = (noteLocation + i) % Music.roots.length;
|
|
431
|
-
const rootName = Music.roots[index];
|
|
432
|
-
let noteName = this.getRelativeNoteName(rootName, scale[i]);
|
|
433
|
-
|
|
434
|
-
if (noteName.length === 1) {
|
|
435
|
-
noteName += 'n';
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
scaleMap[rootName] = noteName;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
return scaleMap;
|
|
442
|
-
}
|
|
443
|
-
}
|