/
pumpkin.js
279 lines (248 loc) · 6.78 KB
/
pumpkin.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
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
var preloadedSounds = {};
var thTags;
var bg;
window.onload = function () {
bg = new Howl({
src: ["sounds/songs/Monster-Mash_no-chorus.mp3"]
});
// NOTE: each dictionary key is the folder name, each dictionary value is an array of filenames within the folder
var sounds = {
"mandolin": ["Am", "C", "D", "D7", "Am"],
"drums": ["Kick", "HiHatClosed", "Snare", "CymbalCrash", "CymbalRide", "SnareSideStick"],
"guitar": ["G-short", "G-long", "Em-short", "Em-long", "C-short", "C-long", "D-short", "D-long"],
"xylo": ["xylo-C-lo", "xylo-D", "xylo-E", "xylo-F", "xylo-G", "xylo-A", "xylo-B", "xylo-C-hi"],
"pumpkin": ["slap", "tap", "deep-slap"],
"halloween": ["bubbling", "cat", "ghost", "laugh", "scream", "witch", "wolf"],
"mash": ["chorus-he-did-the-mash-high", "chorus-he-did-the-mash", "chorus-he-did-the-mash-2", "chorus-the-monster-mash", "lead-caught-on-in-flash", "lead-graveyard-smash", "lead-he-did-the-monster-mash", "lead-monster-mash", "lead-then-you-can-monster-mash", "lead-you-do-the-mash"]
};
// map each the keyboard keys to the desired sounds
var keymap_monstermash = {
"left": "guitar/G-long",
"right": "guitar/Em-long",
"up": "guitar/C-long",
"down": "guitar/D-long",
"space": "mash/lead-you-do-the-mash",
"q": "halloween/cat",
"w": "halloween/ghost",
"e": "halloween/laugh",
"r": "halloween/scream",
"t": "halloween/witch",
"y": "halloween/wolf",
"a": "mash/chorus-he-did-the-mash",
"s": "mash/chorus-the-monster-mash",
"d": "mash/chorus-he-did-the-mash-2",
"f": "mash/chorus-he-did-the-mash-high",
"g": "drums/Kick",
"h": "drums/Snare",
}
// find elements to flash when triggered
thTags = document.getElementsByTagName("th");
// pre-load all sound clips
for (var group in sounds)
{
var s = sounds[group];
for (var i = 0; i < s.length; i++) {
var id = group + "/" + s[i];
preloadedSounds[id] = new Howl({
src: ['sounds/' + id + ".mp3"]
});
}
}
// populate menus with sound options
var menus = document.getElementsByClassName("menu");
for (var m = 0; m < menus.length; m++)
{
var menu = menus[m];
for (var group in sounds)
{
// show dictionary keys as option group headings
var optgroup = document.createElement("optgroup");
optgroup.label = group;
menu.options.add(optgroup, menu.options.length);
// show dictionary values as sound options
var s = sounds[group];
for (var i = 0; i < s.length; i++)
{
var option = document.createElement("option");
option.text = s[i];
option.data = group + "/" + s[i];
optgroup.appendChild(option);
}
}
menu.selectedIndex = m;
}
// assign each keyboard key a specific sound based on pre-defined mapping
var menus = document.getElementsByClassName("menu");
for (var key in keymap_monstermash)
{
var key_idx = keymap_monstermash[key];
var menu_id = "sound_" + key
var menu = document.getElementById(menu_id);
for(var i = 0; i < menu.options.length; i++)
{
if(menu.options[i].data === key_idx)
{
menu.selectedIndex = i;
break;
}
}
}
var bandNames = ["Touch the Pumpkins", "Pumpkin Parade", "Pumpkin Chuckin'", "Smacking Pumpkins", "The Pumpkings"];
var bandIndex = Math.floor(Math.random() * Math.floor(bandNames.length));
document.getElementById("band_name").innerHTML = bandNames[bandIndex];
}
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
var key = "";
var visual = "pumpkin";
console.log(e.keyCode);
switch (e.keyCode) {
case 38:
key = "up";
visual = "bat";
break;
case 40:
key = "down";
visual = "candle";
break;
case 37:
key = "left";
visual = "coffin";
break;
case 39:
key = "right";
visual = "frank";
break;
case 32:
key = "space";
visual = "ghost";
break;
case 13:
key = "enter";
visual = "grave";
break;
case 81:
key = "q";
visual = "hat";
break;
case 87:
key = "w";
visual = "house";
break;
case 69:
key = "e";
visual = "pot";
break;
case 82:
key = "r";
visual = "pumpkin-1";
break;
case 84:
key = "t";
visual = "pumpkin-2";
break;
case 89:
key = "y";
visual = "raven";
break;
case 65:
key = "a";
visual = "reaper";
break;
case 83:
key = "s";
visual = "skull";
break;
case 68:
key = "d";
visual = "spider-web";
break;
case 70:
key = "f";
visual = "vampire-1";
break;
case 71:
key = "g";
visual = "vampire-2";
break;
case 72:
key = "h";
visual = "scarecrow";
break;
}
console.log('key: ' + key);
if (key.length > 0)
{
elem = document.getElementById('sound_' + key);
var soundName = elem.options[elem.selectedIndex].data;
console.log(soundName);
var volume = document.getElementById('volume_' + key).value;
var pan = document.getElementById('pan_' + key).value;
preloadedSounds[soundName].play();
// TODO: can/should we set these values on change instead of every play?
// if more than one key is mapped to the same sound, then there might be some problems...
preloadedSounds[soundName].volume(volume / 100);
preloadedSounds[soundName].stereo(pan / 50);
// Flash pressed key UI
for (var i = 0; i < thTags.length; i++) {
if (thTags[i].textContent == key) {
thTags[i].setAttribute("class","flash");
setTimeout(function() {
thTags[i].setAttribute("class","");
}, 100);
break;
}
}
// deploy visual
var visual_dom = document.getElementById(visual);
visual_dom.className += " go";
setTimeout(function(){ visual_dom.classList.remove("go"); },200);
}
}
function changeTextValue(newVal, idToChange)
{
document.getElementById(idToChange).innerHTML = newVal;
}
function showAdvanced(cb) {
var elements = document.getElementsByClassName("advanced_options");
for (var e = 0; e < elements.length; e++) {
var elem = elements[e];
if (cb.checked)
{
elem.style.display = 'table-row';
}
else
{
elem.style.display = 'none';
}
}
}
var playing = false;
function changeBackingVolume(value) {
bg.volume(value / 100);
document.getElementById(idToChange).innerHTML = value;
}
function playBackingTrack(button) {
// TODO fix
if(playing) {
playing = false;
bg.stop();
button.innerHTML = "Play Backing Track";
return;
}
button.innerHTML = "Playing...";
playing = true;
bg.play();
}
function toggleDisplay(cb) {
if (document.getElementById("visuals").classList.contains("hidden")) {
document.getElementById("controls").setAttribute("class","hidden");
document.getElementById("visuals").setAttribute("class","");
document.getElementById("switch-button").textContent='Show Controls';
} else {
document.getElementById("controls").setAttribute("class","");
document.getElementById("visuals").setAttribute("class","hidden");
document.getElementById("switch-button").textContent='Show Visuals';
}
}