-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
173 lines (146 loc) · 4.92 KB
/
script.js
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
document.addEventListener("DOMContentLoaded", function () {
const audio = document.querySelector("audio");
const chords = document.querySelectorAll("#chords li");
const transposeCounter = document.getElementById("transpose-counter");
const transposeUpButton = document.getElementById("transpose-up");
const transposeDownButton = document.getElementById("transpose-down");
const capoCounter = document.getElementById("capo-counter");
const chordDiagramCurrent = document.getElementById("chord-diagram-current");
const chordDiagramNext = document.getElementById("chord-diagram-next");
const chordCurrent = document.getElementById("chord-current");
const chordNext = document.getElementById("chord-next");
let maxChordLength = 0;
let instrument = "guitar";
/* lisetner for chords, when click, jump the audio to the time of the id element clicked */
chords.forEach((chord) => {
chord.addEventListener("click", function () {
audio.currentTime = chord.id;
audio.play();
});
});
/* handle <input type="radio" name="instrument" /> */
document.querySelectorAll('input[name="instrument"]').forEach((input) => {
input.addEventListener("change", function () {
instrument = this.value;
});
});
/* add atriuite of length of each chord */
chords.forEach((chord, index) => {
var nextChord = chords[index + 1];
if (nextChord) {
var length = nextChord.id - chord.id;
chord.setAttribute("length", length);
if (length > maxChordLength) {
maxChordLength = length;
}
}
});
/* set width of each chord */
chords.forEach((chord) => {
let length = chord.getAttribute("length");
const n = 3;
length = parseFloat(length).toFixed(n);
let w = (length / maxChordLength.toFixed(n)) * 400;
chord.style.width = w + "px";
});
/* chords.forEach setProperty('--animation-duration', length + 's'); */
chords.forEach((chord) => {
let length = chord.getAttribute("length");
chord.style.setProperty("--animation-duration", length + "s");
});
/* scale the width of each chord by x */
function scaleChordWidth(x) {
chords.forEach((chord) => {
chord.style.width = chord.style.width.replace("px", "") * x + "px";
});
}
/* handle for #zoom-in and #zoom-out */
document.getElementById("zoom-in").addEventListener("click", function () {
scaleChordWidth(1.5);
});
document.getElementById("zoom-out").addEventListener("click", function () {
scaleChordWidth(0.8);
});
transposeUpButton.addEventListener("click", function () {
transposeChords(1);
transposeCounter.innerHTML = parseInt(transposeCounter.innerHTML) + 1;
capoCounter.innerHTML = transposeCounter.innerHTML * -1;
});
transposeDownButton.addEventListener("click", function () {
transposeChords(-1);
transposeCounter.innerHTML = parseInt(transposeCounter.innerHTML) - 1;
capoCounter.innerHTML = transposeCounter.innerHTML * -1;
});
setInterval(function () {
const currentTime = audio.currentTime;
console.log(currentTime);
chords.forEach((chord) => {
if (chord.id <= currentTime) {
chord.classList.add("actived");
} else {
chord.classList.remove("actived");
chord.classList.remove("active");
}
if (chord.id - currentTime <= 0.3 && chord.id - currentTime >= 0) {
chord.classList.add("active");
if (chordCurrent.innerHTML != chord.innerHTML) {
chordCurrent.innerHTML = chord.innerHTML;
chordDiagramCurrent.src = `${instrument}/${encodeURIComponent(
simplifyChord(chord.innerHTML)
)}.png`;
}
if (chordNext.innerHTML != chord.nextElementSibling.innerHTML) {
chordNext.innerHTML = chord.nextElementSibling.innerHTML;
chordDiagramNext.src = `${instrument}/${encodeURIComponent(
simplifyChord(chord.nextElementSibling.innerHTML)
)}.png`;
}
} else {
/* chord.classList.remove("active"); */
}
});
}, 150);
function transposeChords(amount) {
chords.forEach((chord) => {
chord.innerHTML = transposeChord(chord.innerHTML, amount);
});
}
function transposeChord(chord, amount) {
var scale = [
"C",
"C#",
"D",
"D#",
"E",
"F",
"F#",
"G",
"G#",
"A",
"A#",
"B",
];
var normalizeMap = {
Cb: "B",
Db: "C#",
Eb: "D#",
Fb: "E",
Gb: "F#",
Ab: "G#",
Bb: "A#",
"E#": "F",
"B#": "C",
};
return chord.replace(/[CDEFGAB](b|#)?/g, function (match) {
var i =
(scale.indexOf(normalizeMap[match] ? normalizeMap[match] : match) +
amount) %
scale.length;
return scale[i < 0 ? i + scale.length : i];
});
}
/* simplfy chord: remove after slash (include the slash) */
function simplifyChord(chord) {
return chord.replace(/\/.*/, "").replace("A#", "Bb").replace("D#", "Eb");
}
});