-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlsAndInput.js
354 lines (314 loc) · 9.79 KB
/
controlsAndInput.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//Constructor function to handle the onscreen menu, keyboard and mouse
//controls
function ControlsAndInput() {
this.menuDisplayed = false;
this.mainMenuDisplayed = false;
//playback button displayed in the top left of the screen
this.playbackButton = new PlaybackButton();
//frquencies used by the energyfunction to retrieve a value
//for each plot.
this.frequencyBins = ["bass", "lowMid", "highMid", "treble"];
//create an array amplitude values from the fft.
var spectrum = fourier.analyze();
// variable to store an amplitude reading
let level;
//array for menu item spacings
let newArr = [0, 110, 260, 420, 520, 655, 780, 905];
//current selected song
let selectSong;
//beat detection
let beatDecay = 0;
let threshhold = 0.05;
//marquee that will move accross the screen
let marquee = width;
//button names
let buttonNames = [
"Stomper reggae",
"Virtual Insanity",
"Digital Love",
"Nirvana",
"Blow me away",
"G.O.A.T",
"Odyssey",
"Custom",
];
//empty buttons array
let buttons = [];
//creats a new button for each button name
buttonNames.map((button, index) => {
let currentButton = createButton(button);
currentButton.position(width - 225, 150 + index * 50);
//button styling
currentButton.style(`border: none;
padding: 10px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
background-color: black;
color: red;
border: 2px solid #f44336;`);
currentButton.mouseOver(() =>
currentButton.style(` background-color: #f44336;
color: black;`)
);
currentButton.mouseOut(() =>
currentButton.style(` background-color: black;
color: red; `)
);
//push every button onto buttons array
buttons.push(currentButton);
});
//plays the selected song
this.soundPlaying = function (songId) {
if (songId != selectSong) {
//dont do if selected already selected song
selectSong = songId;
for (let i in buttons) {
sound[i].pause(); //pause all songs
}
fourier.setInput(sound[selectSong]); //set input to current song
amplitude.setInput(sound[selectSong]); //set amplitude to current song
amplitude.smooth(0.9);
if (this.playbackButton.playing) {
sound[selectSong].loop(); //play song if playbackbutton is on play
}
}
};
this.soundPlaying(0); //plays the first song on the list
//playback play and pause button
const playOrPause = (keycode = undefined) => {
if (this.playbackButton.hitCheck(keycode)) {
//checks if button or spacebar was pressed
if (sound[selectSong].isPlaying()) {
sound[selectSong].pause(); //pauses song if it was playing
} else {
sound[selectSong].loop(); //plays song if it was paused
}
}
//maps song list buttons to play their song from index
buttons.map((button, index) => {
button.mousePressed(() => this.soundPlaying(index));
});
};
//controls selected visuals
let visNumber = 0;
const keyboardController = (keycode) => {
if (keycode > 48 && keycode < 57) {
visNumber = keycode - 49;
vis.selectVisual(vis.visuals[visNumber].name); //show selected visual
this.mainMenuDisplayed = true; //small title is displayed
}
if (visNumber == 0) {
this.mainMenuDisplayed = false; //small title is not displayed on title visual
}
};
//skip button
const skip = () => {
if (this.playbackButton.hitCheckSkip()) {
//checks if button was clicked
if (selectSong > buttonNames.length - 2) {
sound[selectSong].pause();
sound[0].stop();
this.soundPlaying(0); //if at the end of the songs list loop back to the first song
} else {
sound[selectSong].pause();
sound[selectSong + 1].stop();
this.soundPlaying(selectSong + 1); //play the next song in the list from the begining
}
}
};
//rewind button
const rewind = () => {
if (this.playbackButton.hitCheckRewind()) {
//checks if button was clicked
if (selectSong < 1) {
sound[selectSong].pause();
sound[buttonNames.length - 1].stop();
this.soundPlaying(buttonNames.length - 1); //if at the start of the songs list loop forwards to the last song
} else {
sound[selectSong].pause();
sound[selectSong - 1].stop();
this.soundPlaying(selectSong - 1); //play the previous song in the list form the begining
}
}
};
//mouse presses button
this.mousePressed = playOrPause;
this.mousePressed1 = skip;
this.mousePressed2 = rewind;
//responds to keyboard presses
//@param keycode the ascii code of the keypressed
this.keyPressed = (keycode) => {
playOrPause(keycode);
keyboardController(keycode);
};
//beat detection
this.beatDetection = function (level) {
if (level > beatDecay && level > threshhold) {
beatDecay = level * 100; //beatdecay > level
return true; //beat detected if amplitude level is above threshhold and decay decays down enough
} else {
beatDecay *= 0.9; //decay decays
return false; //beat not detected
}
};
//main menu UI draw function
this.draw = function () {
//default style
push();
fill("white");
stroke("black");
strokeWeight(2);
textSize(20);
this.playbackButton.draw(); //playback button
sound[selectSong].setVolume(volSlider.value()); //volume slider
h = map(fourier.getEnergy(this.frequencyBins[0]), 0, 255, 0, 20) * 2; // maps bass
fill("red");
text("Show menu:", width - 150, 39); //show menu tickbox
if (!this.menuDisplayed) {
//do only if the show menu tickbox is ticked
//display options
this.options(h);
text("Custom song:", 25, 165);
input.show();
text("Mic input:", 25, 220);
checkbox.show();
text("Random vis on beat:", 25, 250);
randbox.show();
text("Volume:", 25, 280);
volSlider.show();
if (visNumber == 1) {
text("Spectrum:", 25, 365);
specSlider.show();
hueSlider.show();
text("Hue:", 25, 325);
} else {
specSlider.hide();
hueSlider.hide();
}
if (visNumber == 2) {
spaceSlider.show();
text("Spacing:", 25, 365);
hueSlider.show();
text("Hue:", 25, 325);
} else {
spaceSlider.hide();
}
if (visNumber == 3) {
spaceSlider.show();
text("Spacing:", 25, 365);
thickSlider.show();
text("Thickness:", 25, 325);
} else {
thickSlider.hide();
}
if (visNumber == 5) {
thickSlider.show();
text("Thickness:", 25, 325);
}
if (visNumber == 6) {
spaceSlider.show();
text("Spacing:", 25, 365);
thickSlider.show();
text("Thickness:", 25, 325);
}
if (visNumber == 7) {
thickSlider.show();
text("Thickness:", 25, 325);
}
//display song list
this.songlist(h);
for (let i in buttons) {
buttons[i].show();
}
//display small title (if not in title visual)
if (this.mainMenuDisplayed) {
h = map(fourier.getEnergy(this.frequencyBins[2]), 0, 255, 0, 20) * 2; // maps highMid
this.visSelect(h);
h = map(fourier.getEnergy(this.frequencyBins[3]), 0, 255, 0, 20) * 2; // maps treble
this.menu(h);
}
//marquee text for currently playing song
text(
"Currently Playing: " + buttonNames[selectSong],
(marquee -= 2),
height - 50
);
if (marquee < -width / 4) {
marquee = width;
}
} else {
//hides the entire menu if the show menu tickbox is not ticked
volSlider.hide();
specSlider.hide();
hueSlider.hide();
spaceSlider.hide();
thickSlider.hide();
checkbox.hide();
randbox.hide();
input.hide();
for (let i in buttons) {
buttons[i].hide();
}
}
pop();
};
//small title
this.visSelect = function (h) {
//hight = highMid
for (i = 0; i < h + 1; i++) {
push();
textSize(40);
fill(1 + i * 15, 50, 255); //as the title moves towards +z colour changes
translate(i * sin(millis() / 400), i * cos(millis() / 400), i); //moves title towards +z position in a circle dependant on lowMid
rotate(sin(millis() / 400) / 40); //rotates up and down
text("Select a visualisation:", 25, 50); //displays title
pop();
}
};
//small visual list
this.menu = function (h) {
//hight = treble
for (j = 0; j < h + 1; j++) {
push();
textSize(25);
fill(255, 1 + j * 15, 1 + j * 15); //as the list moves towards +z colour changes
translate(0, 0, j); //moves title towards +z dependant on treble
rotate(cos(millis() / 400) / 40); //rotates up and down
for (i = 0; i < 8; i++) {
text(i + 1 + ":" + vis.visuals[i].name, 25 + newArr[i], 95); //draw out menu items for each visualisation
}
pop();
}
};
//options text
this.options = function (h) {
for (i = 0; i < h + 1; i++) {
//hight = bass
push();
textSize(25);
fill(1 + i * 15, 50, 255); //change colour as it moves +z
translate(i, 0, i); //move +z and +x
rotate(cos(millis() / 400) / 40); //rotates up and down
text("Options:", 25, 140); //displays options text
pop();
}
};
//song list text
this.songlist = function (h) {
for (i = 0; i < h + 1; i++) {
//hight = bass
push();
textSize(25);
fill(1 + i * 15, 50, 255); //change colour as it moves
translate(width - 250 - i, 0, i); // moves +z and -x
rotate(cos(millis() / 400) / 40); //rotates up and down
text("Song list:", 25, 140); //displays song list text
pop();
}
};
}