|
@@ -0,0 +1,84 @@
|
|
1
|
+import { chords } from './chords.mjs'; // it is weird that we need to provide extension
|
|
2
|
+
|
|
3
|
+class FString { // becase String is taken
|
|
4
|
+ constructor(tune, threads) {
|
|
5
|
+ this.tune = tune % 12;
|
|
6
|
+ this.threads = Array(threads).fill(null);
|
|
7
|
+ }
|
|
8
|
+
|
|
9
|
+ highlightNote(i, symbol) {
|
|
10
|
+ this.threads[i] = symbol;
|
|
11
|
+ }
|
|
12
|
+}
|
|
13
|
+
|
|
14
|
+export default class Fingerboard {
|
|
15
|
+ constructor(threads) {
|
|
16
|
+ this.threads = threads;
|
|
17
|
+ this.iv = new FString(7, threads);
|
|
18
|
+ this.iii = new FString(14, threads);
|
|
19
|
+ this.ii = new FString(21, threads);
|
|
20
|
+ this.i = new FString(28, threads);
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ draw() {
|
|
24
|
+ let fingerboard = 'g d a e\n\n'
|
|
25
|
+ for (let i = 0; i < this.threads; i++) {
|
|
26
|
+ fingerboard += this.drawThread(i) + '\n';
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ return fingerboard;
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ drawThread(i) {
|
|
33
|
+ const empty = (i === 0) ? ' ' : '.';
|
|
34
|
+ const drawPoint = (string, i, empty) => string.threads[i] === null ? empty : string.threads[i];
|
|
35
|
+ return `${drawPoint(this.iv, i, empty)} ${drawPoint(this.iii, i, empty)} ${drawPoint(this.ii, i, empty)} ${drawPoint(this.i, i, empty)}`;
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ highlightNote(base, interval) {
|
|
39
|
+ [this.iv, this.iii, this.ii, this.i].forEach((string) => {
|
|
40
|
+ for (let i = 0; i < this.threads; i++) {
|
|
41
|
+ // console.log(i, string.tune, base, interval)
|
|
42
|
+ if ((i + string.tune) % 12 === (base + interval) % 12) {
|
|
43
|
+ string.highlightNote(i, interval.toString(16))
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+ });
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ highlightChord(chord) {
|
|
50
|
+ const [base, intervals] = chord2numbers(chord);
|
|
51
|
+ intervals.forEach(interval => this.highlightNote(base, interval));
|
|
52
|
+ }
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+function note2number(note) {
|
|
56
|
+ const notes = {
|
|
57
|
+ c: 0,
|
|
58
|
+ d: 2,
|
|
59
|
+ e: 4,
|
|
60
|
+ f: 5,
|
|
61
|
+ g: 7,
|
|
62
|
+ a: 9,
|
|
63
|
+ b: 10,
|
|
64
|
+ h: 11,
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ if (note.length === 1) return notes[note];
|
|
68
|
+
|
|
69
|
+ const accidental2number = accidental => {
|
|
70
|
+ if (accidental === '#') return 1;
|
|
71
|
+ if (accidental === 'b') return 11;
|
|
72
|
+ throw new Error('invalid accidental (#, b)');
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ if (note.length === 2) return notes[note[0]] + accidental2number(note[1]) % 12;
|
|
76
|
+ throw new Error('invalid note name (c, d, e, f, g, a, b, h)');
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+function chord2numbers(chord) {
|
|
80
|
+ const separator = (['#', 'b'].includes(chord[1])) ? 2 : 1;
|
|
81
|
+ const note = chord.slice(0, separator);
|
|
82
|
+ const chordType = chord.slice(separator);
|
|
83
|
+ return [note2number(note), chords[chordType]];
|
|
84
|
+}
|