1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
export function naturalNotesBetween(a, b) {
const [tonicA, tonicB] = [a[0], b[0]]
const abs = tonicB.charCodeAt() - tonicA.charCodeAt();
if (abs < 0) {
return abs + 7;
} else {
return abs;
}
}
export function chromaticNotesBetween(a, b) {
console.log('chromaticNotesBetween', a, b);
const [aNote, bNote] = [a, b].map(x => Note.fromString(x));
console.log(' -- aNote', aNote);
console.log(' -- bNote', bNote);
const aIndex = Note.noteRange.findIndex(x => aNote.toString() == x.toString());
const bIndex = Note.noteRange.findIndex(x => bNote.toString() == x.toString());
console.log(' -- aNote', aIndex);
console.log(' -- bNote', bIndex);
return aIndex - bIndex;
}
export function toCents([aNote, aChord], [bNote, bChord]) {
console.debug('toCents', [aNote, aChord], [bNote, bChord]);
console.debug('- a', [aNote, aChord]);
console.debug('- b', [bNote, bChord]);
const offset = (aNote.distanceTo(bNote)) * 100;
console.debug('- offset', offset);
const scale = (bChord - aChord) * 1200;
console.debug('- scale', scale);
return scale + offset;
}
export class Note {
static natural = '♮';
static sharp = '♯';
static flat = '♭';
static range = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
static get first() {
return this.range.at(0);
}
static get last() {
return this.range.at(-1);
}
static noSharps = ['B', 'E'];
static get noFlats() {
return this.noSharps.map(n => String.fromCharCode(n.charCodeAt()+1))
}
static get noteRange() {
return this.range.flatMap(n => {
if (this.noSharps.includes(n)) {
return new Note(n);
} else {
return [new Note(n, false), new Note(n, true)];
}
})
}
static fromString(string) {
const [root, accidental] = [string[0], string[1]];
switch (accidental) {
case this.sharp:
if (this.noSharps.includes(root)) {
return new this(String.fromCharCode(root.charCodeAt()+1))
} else {
return new this(root, true);
}
case this.flat:
if (this.noFlats.includes(root)) {
return new this(String.fromCharCode(root.charCodeAt()-1));
} else if (root === Note.first) {
// i don't want to make a ring buffer that accepts
// negative indices, ok???
return new this(Note.last, true);
} else {
const code = root.charCodeAt();
return new this(String.fromCharCode(code-1), true);
}
case undefined:
case this.natural:
return new this(root);
default:
throw new Error('wtf is that accidental', accidental);
}
}
constructor(root, isSharp=false) {
if (isSharp && Note.noSharps.includes(root)) {
throw new Error(`bad note: ${root}${Note.sharp}`);
}
this.root = root;
this.isSharp = isSharp;
}
distanceTo(dest) {
const srcFromC = Note.noteRange.findIndex(t =>
t.toString() === this.toString());
const destFromC = Note.noteRange.findIndex(t =>
t.toString() === dest.toString());
return destFromC - srcFromC;
}
toString() {
return this.root + (this.isSharp ? Note.sharp : '');
}
toAlternateString() {
if (this.isSharp) {
const next = (this.root === Note.last) ? Note.first : String.fromCharCode(this.root.charCodeAt()+1);
if (Note.noSharps.includes(this.root)) {
return next;
} else {
return next + Note.flat;
}
} else if (Note.noFlats.includes(this.root)) {
const prev = (this.root === Note.first) ? Note.last : String.fromCharCode(this.root.charCodeAt()-1);
return prev + Note.sharp;
} else {
return this.toString();
}
}
toRelativeString(other) {
const alternate = this.toAlternateString();
const otherStr = other.toString();
const delta = otherStr.charCodeAt() - alternate.charCodeAt();
// if (other.root == 'G' && other.isSharp == true) {
//console.debug('bjc', 'other', otherStr, 'me', this.toString(), 'alt', alternate, 'delta', delta);
// }
if (delta === -1 || delta === 1) {
return alternate;
} else {
return this.toString();
}
}
}
const ringHandler = {
get(target, prop) {
return target[prop % target.length] || Reflect.get(...arguments);
}
};
export const chromaticScale = new Proxy(
['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B'],
ringHandler);
// todo: handle tonics with a flat ‘♭’, so we can deal with [BE]♭.
function scaleFromIntervals(tonic, intervals) {
const scaleIndex = chromaticScale.indexOf(tonic);
if (scaleIndex < 0) {
throw new Error(`tonic ${tonic} not found in scale ${chromaticScale.toString()}`);
}
let scale = [tonic];
let steps = 0;
let lastBase = tonic[0];
for (let i = 0; i < intervals.length; i++) {
steps += intervals[i];
const note = chromaticScale[scaleIndex + steps];
// don't display two base notes in a row by changing
// accidentals.
const delta = naturalNotesBetween(lastBase, note);
if (delta === 0) {
const nextBase = chromaticScale[scaleIndex + steps + 1][0];
//console.log('0', 'prev', lastBase, 'next', nextBase, 'note', note)
scale.push(`${nextBase}♭`)
} else if (delta === 1) {
//console.log('1', 'note', note)
scale.push(`${note}`)
} else {
// we can only be two away, so take the sharp of the
// previous note.
const nextBase = chromaticScale[scaleIndex + steps - 1][0];
//console.log('other', 'prev', lastBase, 'next', nextBase, 'note', note);
scale.push(`${nextBase}♯`)
}
lastBase = scale[scale.length-1];
}
return new Proxy(scale, ringHandler);
}
class Scale {
constructor(tonic, intervals) {
this.scale = scaleFromIntervals(tonic, intervals);
return new Proxy(this, {
get(target, prop) {
if (prop in target) {
return target[prop];
} else {
return target.scale[prop] || Reflect.get(...arguments);
}
}
});
}
get tonic() {
return this.scale[0];
}
get third() {
return this.scale[2];
}
get fifth() {
return this.scale[4];
}
get length() {
return this.scale.length;
}
toString() {
return `[${this.scale.join(", ")}]`;
}
}
export function MajorScale(tonic) {
// console.debug(`MajorScale(${tonic})`);
const intervals = [2, 2, 1, 2, 2, 2];
return new Scale(tonic, intervals);
}
export function MinorScale(tonic) {
// console.debug(`MinorScale(${tonic})`);
const intervals = [2, 1, 2, 2, 1, 2];
return new Scale(tonic, intervals);
}
export function AugmentedScale(tonic) {
// console.debug(`AugmentedScale(${tonic})`);
const intervals = [2, 2, 1, 1, 2, 2];
// C D Eb F Gb Ab, C
return new Scale(tonic, intervals);
}
export function DiminishedScale(tonic) {
// console.debug(`DiminishedScale(${tonic})`);
const intervals = [2, 1, 2, 1, 2, 1];
// C D Eb F Gb Ab, Bb
return new Scale(tonic, intervals);
}
// todo: add sus4/sus2
// well, a lot of them.
export const allDiatonicScales = chromaticScale.flatMap(tonic => {
return [
MajorScale(tonic),
MinorScale(tonic)
];
})
|