-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
318 lines (273 loc) · 9.64 KB
/
Copy pathapp.js
File metadata and controls
318 lines (273 loc) · 9.64 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// app.js
//let gentext = "";
// Intervals
// Populate dropdowns
function populateScaleDropdown(sel) {
sel.innerHTML = "";
CIRCLE_OF_FIFTHS.forEach(n => {
const opt = document.createElement("option");
opt.value = n;
opt.textContent = n;
sel.appendChild(opt);
});
}
function populateChordDropdown(sel) {
const list = [
"C",
"C#","Db",
"D",
"D#","Eb",
"E",
"F",
"F#","Gb",
"G",
"G#","Ab",
"A",
"A#","Bb",
"B"
];
sel.innerHTML = "";
list.forEach(n => {
const opt = document.createElement("option");
opt.value = n;
opt.textContent = n;
sel.appendChild(opt);
});
}
function populateVoiceDropdown(sel, voicings) {
sel.innerHTML = "";
voicings.forEach((n, i) => {
const opt = document.createElement("option");
opt.value = i;
opt.textContent = n.frets;
sel.appendChild(opt);
});
}
let voicings = null;
function updateVoicings(tuning, reverse, usedDict=false) {
console.log("tuning:", tuning);
//const scaleRoot = scaleRootSel.value;
//console.log("Scale Root:", scaleRoot);
//const scaleType = scaleTypeSel.value;
const chordRootSelVal = chordRootSel.value;
const chordType = chordTypeSel.value;
//const useFlats = autoUseFlats(scaleRoot);
// Normalize roots to internal sharp names
//const scaleRootSharp = normalizeRootToSharp(scaleRoot);
const chordRootSharp = normalizeRootToSharp(chordRootSelVal);
//const scaleIntervals = MODES[scaleType] || MODES.major;
const chordIntervals = CHORD_INTERVALS[chordType] || CHORD_INTERVALS.maj;
//const scaleNotes = getNotes(scaleRootSharp, scaleIntervals);
const chordNotes = getNotes(chordRootSharp, chordIntervals);
if (usedDict) {
voicings = getDictionaryChords(chordRootSharp, chordNotes, tuning, reverse, {
chordType: chordType
});
} else {
// Generate voicings
voicings = generateChordVoicings(chordRootSharp, chordNotes, tuning, reverse, {
minFret: 0,
maxFret: 15,
maxSpan: 3,
minNotes: chordIntervals.length > 6 ? 6: chordIntervals.length,
maxResults: 50
});
}
console.log("Found", voicings.length, " voicings");
populateVoiceDropdown(chordVoiceSel, voicings);
}
// Map chord root selection to internal sharp name
function normalizeRootToSharp(root) {
// If already sharp or natural and exists, use directly
if (NOTES_SHARP.includes(root)) return root;
// If flat, map via NOTES_FLAT index
const flatIdx = NOTES_FLAT.indexOf(root);
if (flatIdx !== -1) return NOTES_SHARP[flatIdx];
// Fallback: return as-is
return root;
}
// Init
const outputDiv = document.getElementById("output");
function textout(msg) {
outputDiv.textContent = msg == null ? "" : String(msg);
}
function htmlout(html) {
outputDiv.innerHTML = html == null ? "" : String(html);
}
window.addEventListener("DOMContentLoaded", () => {
const canvas = document.getElementById("fretboard");
const scaleRootSel = document.getElementById("scaleRootSel");
const scaleTypeSel = document.getElementById("scaleTypeSel");
const chordRootSel = document.getElementById("chordRootSel");
const chordTypeSel = document.getElementById("chordTypeSel");
const hideScale = document.getElementById("hideScale");
const hideChord = document.getElementById("hideChord");
const flipStrings = document.getElementById("flipStrings");
const showScale = document.getElementById("showScale");
const tuningSel = document.getElementById("tuning");
const showCaged = document.getElementById("showCaged");
const showVoice = document.getElementById("showVoice");
const chordVoiceSel = document.getElementById("chordVoiceSel");
const jazzNotation = document.getElementById("jazzNotation");
const useDict = document.getElementById("useDict");
populateScaleDropdown(scaleRootSel);
populateChordDropdown(chordRootSel);
scaleRootSel.value = "C";
chordRootSel.value = "C";
// chord types
CHORD_INTERVAL_ORDER.forEach(c => {
chordTypeSel.add(new Option(c, c));
});
// scale modes
Object.keys(MODES).forEach(m =>
scaleTypeSel.add(new Option(m,m))
);
Object.keys(TUNINGS).forEach(t =>
tuningSel.add(new Option(t,t))
);
console.log("TUNINGS", tuningSel.value, TUNINGS);
const fb = new Fretboard(canvas, 15, TUNINGS[tuningSel.value]);
fb.reverseStrings(flipStrings.checked);
updateVoicings(TUNINGS[tuningSel.value], flipStrings.checked, useDict.checked);
function update() {
const scaleRoot = scaleRootSel.value;
//console.log("Scale Root:", scaleRoot);
const scaleType = scaleTypeSel.value;
const chordRootSelVal = chordRootSel.value;
const chordType = chordTypeSel.value;
const useFlats = autoUseFlats(scaleRoot);
// Normalize roots to internal sharp names
const scaleRootSharp = normalizeRootToSharp(scaleRoot);
const chordRootSharp = normalizeRootToSharp(chordRootSelVal);
const scaleIntervals = MODES[scaleType] || MODES.major;
const chordIntervals = CHORD_INTERVALS[chordType] || CHORD_INTERVALS.maj;
const scaleNotes = getNotes(scaleRootSharp, scaleIntervals);
const chordNotes = getNotes(chordRootSharp, chordIntervals);
fb.clear();
fb.drawBoard(flipStrings.checked, { fretover: false });
console.log("Intervals", chordIntervals, ", Notes", chordNotes, );
//fb.draw(scaleNotes, chordNotes, useFlats);
if (!hideScale.checked) {
fb.plotNotes(scaleNotes, "#0af", useFlats, { //#3af
highlightRoot: showScale.checked,
rootNote: scaleRootSharp,
rootColor: "#0f0",
rootRadius: 22,
alpha: showScale.checked ? 1.0 : 0.5,
border: false,
});
}
if (!hideChord.checked) {
fb.plotNotes(chordNotes, showScale.checked ? "#ff0" : "#f60", useFlats, {
highlightRoot: !showScale.checked,
rootNote: chordRootSharp,
rootColor: "#fff",
rootRadius: 24,
alpha: showScale.checked ? 0.33 : 1.0,
border: true,
});
}
if (false) { // test
const openC = [null, 3, 2, 0, 1, 0]; // x32010
//if (flipStrings.checked) openC.reverse();
fb.plotVoicing(openC, "#f60", useFlats, {
highlightRoot: true,
rootNote: "C",
rootColor: "#fff",
rootRadius: 26
});
} else {
if (showVoice.checked) {
if (true && (voicings && voicings.length)) {
// Pick one to plot (e.g., first)
const v = voicings[chordVoiceSel.value];
//console.log("Chord Voice Select:", chordVoiceSel.value);
const frets = flipStrings.checked
? [...v.frets].reverse()
: v.frets;
fb.plotVoicing(v.frets, getRandomColor(), useFlats, {
highlightRoot: true,
rootNote: chordRootSharp,
rootColor: "#fff",
rootRadius: 26
});
} else if (voicings) {
for (const v of voicings) {
fb.plotVoicing(v.frets, getRandomColor(), useFlats, {
highlightRoot: true,
rootNote: chordRootSharp,
rootColor: "#fff",
rootRadius: 26,
alpha: 0.4,
});
}
}
}
}
if (showCaged.checked) {
const shapes = resolveCAGEDShapes(TUNINGS[tuningSel.value], chordRootSharp, chordNotes, flipStrings.checked, fb.frets);
console.log("Shapes:", shapes);
drawCAGEDOverlay(fb, shapes);
}
const keytriadIndex = Object.fromEntries(
KEY_TRIADS.map((row, i) => [row[0], i])
);
const idx = keytriadIndex[scaleRoot];
const triads = KEY_TRIADS[idx];
textout(
//`Chord: ${fbDisplayNote(chordRootSel.value, useFlats.checked)} ${chordTypeSel.value}\n` +
//`Scale: ${fbDisplayNote(scaleRootSel.value, useFlats.checked)} ${scaleTypeSel.value}\n` +
//`Tuning: ${tuningSel.value}\n\n` +
`${triads}`
);
}
scaleRootSel.addEventListener("change", update);
scaleTypeSel.addEventListener("change", update);
//chordRootSel.addEventListener("change", update);
//chordTypeSel.addEventListener("change", update);
showScale.addEventListener("change", update);
showCaged.addEventListener("change", update);
//showVoice.addEventListener("change", update);
chordVoiceSel.addEventListener("change", update);
hideScale.addEventListener("change", (e) => {
//console.log("Hide Scale", hideScale.checked);
fb.hideScale(hideScale.checked);
update();
});
showVoice.addEventListener("change", (e) => {
updateVoicings(TUNINGS[tuningSel.value], flipStrings.checked, useDict.checked);
update();
});
useDict.addEventListener("change", (e) => {
updateVoicings(TUNINGS[tuningSel.value], flipStrings.checked, useDict.checked);
update();
});
chordRootSel.addEventListener("change", (e) => {
updateVoicings(TUNINGS[tuningSel.value], flipStrings.checked, useDict.checked);
update();
});
chordTypeSel.addEventListener("change", (e) => {
updateVoicings(TUNINGS[tuningSel.value], flipStrings.checked, useDict.checked);
update();
});
hideChord.addEventListener("change", (e) => {
//console.log("Hide Chord", hideChord.checked);
fb.hideChord(hideChord.checked);
update();
});
flipStrings.addEventListener("change", (e) => {
fb.reverseStrings(flipStrings.checked);
updateVoicings(TUNINGS[tuningSel.value], flipStrings.checked, useDict.checked);
update();
});
tuningSel.addEventListener("change", (e) => {
console.log("TUNINGS[tuningSel.value]", TUNINGS[tuningSel.value]);
fb.setTuning(TUNINGS[tuningSel.value]);
update();
});
jazzNotation.addEventListener("change", (e) => {
fb.setNotation(jazzNotation.checked);
update();
});
update();
});