-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
163 lines (146 loc) · 4.37 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
/*
TO DO:
1. Auto-advance notes after 4 iterations, advance manually with space bar.
2. Press enter to stop.
3. Press down arrow to play same note again manually.
*/
const tuning = {
standard: {
label: "Standard",
notes: ['e2', 'a2', 'd3', 'g3', 'b3', 'e4']
},
dropD: {
label: "Drop D",
notes: ['d2', 'a2', 'd3', 'g3', 'b3', 'e4']
},
dadgad: {
label: "DADGAD",
notes: ['d2', 'a2', 'd3', 'g3', 'a4', 'd4']
},
};
document.addEventListener('DOMContentLoaded', () => {
const strings = document.querySelectorAll('.string'),
audios = document.querySelectorAll('audio');
document.querySelectorAll('.string')
.forEach( function(button) {
button.onclick = function() {
var string_id = button.dataset.string;
for (let string of strings) {
if ( string != this ) {
string.classList.remove('active');
}
}
var repeat = function() {
document.getElementById(string_id).currentTime = 0;
document.getElementById(string_id).play();
};
if ( this.classList.contains('active') ) {
document.getElementById(string_id).currentTime = 0;
} else {
this.classList.add('active');
for (let audio of audios) {
audio.pause();
audio.currentTime = 0;
}
document.getElementById(string_id).play();
}
}
}
)
window.addEventListener("keydown", checkKeyPressed, false);
function checkKeyPressed(event) {
// number 1
if (event.keyCode == "49") {
eventFire(document.querySelector('.string:nth-of-type(1)'), 'click');
}
// number 2
if (event.keyCode == "50") {
eventFire(document.querySelector('.string:nth-of-type(2)'), 'click');
}
// number 3
if (event.keyCode == "51") {
eventFire(document.querySelector('.string:nth-of-type(3)'), 'click');
}
// number 4
if (event.keyCode == "52") {
eventFire(document.querySelector('.string:nth-of-type(4)'), 'click');
}
// number 5
if (event.keyCode == "53") {
eventFire(document.querySelector('.string:nth-of-type(5)'), 'click');
}
// number 6
if (event.keyCode == "54") {
eventFire(document.querySelector('.string:nth-of-type(6)'), 'click');
}
// space or right arrow
if (event.keyCode == "32" || event.keyCode == "39") {
var active = document.querySelector('.string.active');
if ( active ) {
if ( document.querySelector('.string.active + .string') == null ) {
eventFire( document.querySelector('.string:first-of-type'), 'click');
} else {
eventFire( document.querySelector('.string.active + .string'), 'click');
}
} else {
eventFire( document.querySelector('.string:first-of-type'), 'click');
}
}
// enter
if (event.keyCode == "13") {
stopAll();
}
// left arrow
if (event.keyCode == "37") {
var active = document.querySelector('.string.active');
if ( active ) {
if ( document.querySelector('.string:first-of-type').classList.contains('active') ) {
eventFire( document.querySelector('.string:last-of-type'), 'click');
} else {
eventFire( document.querySelector('.string.active').previousElementSibling, 'click');
}
} else {
eventFire( document.querySelector('.string:last-of-type'), 'click');
}
}
// down and up arrow
if (event.keyCode == "40" || event.keyCode == "38") {
var string_id = document.querySelector('.string.active').dataset.string;
document.getElementById(string_id).currentTime = 0;
}
}
function eventFire(el, etype){
if (el.fireEvent) {
el.fireEvent('on' + etype);
} else {
var evObj = document.createEvent('Events');
evObj.initEvent(etype, true, false);
el.dispatchEvent(evObj);
}
}
function stopAll() {
for (let string of strings) {
string.classList.remove('active');
}
for (let audio of audios) {
audio.pause();
audio.currentTime = 0;
}
}
document.addEventListener("click", (evt) => {
const flyoutElement = document.querySelector('.strings');
let targetElement = evt.target; // clicked element
do {
if (targetElement == flyoutElement) {
// This is a click inside. Do nothing, just return.
// console.log("Clicked inside!");
return;
}
// Go up the DOM
targetElement = targetElement.parentNode;
} while (targetElement);
// This is a click outside.
// console.log("Clicked outside!");
stopAll();
});
})