-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
179 lines (162 loc) · 3.94 KB
/
common.ts
File metadata and controls
179 lines (162 loc) · 3.94 KB
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
export type Preset =
| "interval"
| "transposing_instrument"
| "key_signature"
| "key_signature_modulation"
| "key";
export const labelFromPreset: Record<Preset, string> = {
interval: "Interval",
transposing_instrument: "Transposing instrument",
key_signature: "Key signature",
key_signature_modulation: "Key signature modulator",
key: "Key",
};
export const rulersFromPreset: Record<Preset, RulerType[]> = {
interval: ["pitch", "interval"],
transposing_instrument: ["pitch", "pitch"],
key_signature: ["pitch", "position"],
key_signature_modulation: ["pitch", "position", "position"],
key: ["pitch", "position", "degree"],
};
export const itemsInFractionalHeight = (f: number) => {
// given a fraction f between 0 and 1,
// returns an approximate number of ruler items
// that can fit in f * height of screen
const itemHeight = 36;
return Math.floor((window.innerHeight * f) / itemHeight);
};
export type RulerType = "pitch" | "degree" | "position" | "interval";
export const getAt = (arr: string[], i: number): string => {
if (i >= 0 && i < arr.length) {
return arr[i];
}
if (i >= arr.length) {
return arr[i % arr.length];
}
// i < 0
return arr[arr.length - (-i % arr.length)];
};
export const buildReverseMap = (labels: string[]): Map<string, number> => {
const result = new Map();
labels.forEach((label, i) => result.set(label, i));
return result;
};
export const numberLabels = [
"7",
"♯6/♭7",
"6",
"♯5/♭6",
"5",
"♯4/♭5",
"4",
"3",
"♯2/♭3",
"2",
"♯1/♭2",
"1",
];
export const degreeLabels = numberLabels.map((item) => {
const openTag = "<span class='caret'>";
const closeTag = "</span>";
const split = item.split("/");
if (split.length === 1) {
return openTag + item + closeTag;
}
const firstNum = split[0].slice(1);
const secondNum = split[1].slice(1);
return `♯${openTag}${firstNum}${closeTag}/♭${openTag}${secondNum}${closeTag}`;
});
export const majorScalePositions = new Set(["1", "2", "3", "4", "5", "6", "7"]);
export const pitchLabels = [
"B",
"A♯/B♭",
"A",
"G♯/A♭",
"G",
"F♯/G♭",
"F",
"E",
"D♯/E♭",
"D",
"C♯/D♭",
"C",
];
export const intervalLabels = [
"P15",
"Maj14",
"m14",
"Maj13",
"m13",
"P12",
"A11/d12",
"P11",
"Maj10",
"m10",
"Maj9",
"m9",
"P8",
"Maj7",
"m7",
"Maj6",
"m6",
"P5",
"A4/d5",
"P4",
"Maj3",
"m3",
"Maj2",
"m2",
"P1",
];
export const numberToIndex = buildReverseMap(numberLabels);
export const pitchToIndex = buildReverseMap(pitchLabels);
export const intervalToIndex = buildReverseMap(intervalLabels);
export const labelsFromRulerType: Record<RulerType, string[]> = {
pitch: pitchLabels,
degree: numberLabels,
position: numberLabels,
interval: intervalLabels,
};
export const reverseMapFromRulerType: Record<RulerType, Map<string, number>> = {
pitch: pitchToIndex,
degree: numberToIndex,
position: numberToIndex,
interval: intervalToIndex,
};
export const defaultZerothItems: Record<RulerType, string> = {
pitch: pitchLabels[0],
degree: numberLabels[0],
position: numberLabels[0],
interval: intervalLabels[0],
};
export const getItemsAtOffset = (
itemsAtZero: string[],
rulerTypes: RulerType[],
offset: number
): string[] => {
let underCursor: string[] = [];
itemsAtZero.forEach((item, i) => {
const indexOfItemAtZero = reverseMapFromRulerType[rulerTypes[i]].get(item);
if (typeof indexOfItemAtZero === "number") {
underCursor.push(
getAt(labelsFromRulerType[rulerTypes[i]], indexOfItemAtZero + offset)
);
}
});
return underCursor;
};
// https://en.wikipedia.org/wiki/Relative_key#List
export const minorKeyFromMajor: Map<string, string> = new Map([
["B", "G♯/A♭"],
["A♯/B♭", "G"],
["A", "F♯"],
["G♯/A♭", "F"],
["G", "E"],
["F♯/G♭", "D♯/E♭"],
["F", "D"],
["E", "C♯"],
["D♯/E♭", "C"],
["D", "B"],
["C♯/D♭", "A♯/B♭"],
["C", "A"],
]);