-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
350 lines (289 loc) · 7.85 KB
/
sketch.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
var song, uploadedSong;
var mp3FileName;
var sliderVolumeDiv, sliderVolumeEmoji, sliderVolume;
var playPauseButton;
var amplitude, frequency, wave;
var hsb1;
var volumeHistory = [];
var bandWidth;
var peakDetect;
var emojis = [];
var em = [];
let canvas;
function setup() {
// GENERAL SETUP
let dimension = min(windowWidth / 1.5, windowHeight / 1.5);
canvas = createCanvas(dimension, dimension);
canvas.parent('canvas');
canvas.style("box-shadow: 0 0 0.5em 0 rgb(115, 121, 133);")
angleMode(DEGREES);
colorMode(HSB);
background(51);
hsb1 = 0;
// SET UP FOR SOUND FILE
song = loadSound("wonder.mp3", loaded); // little hack so it works
// SET UP FOR AMPLITUDE
amplitude = new p5.Amplitude(0.9); //0.9 is smoothing
// SET UP FOR FREQUENCY
let numberOfBands = 64;
frequency = new p5.FFT(0.9, numberOfBands); //0.9 is smoothing
bandWidth = width / numberOfBands;
peakDetect = new p5.PeakDetect();
// BUTTONS
playPauseButton = createButton("⏯");
playPauseButton.parent('canvas-util-div');
playPauseButton.class('playpause');
playPauseButton.attribute('disabled', '');
playPauseButton.mousePressed(togglePlaying);
// SLIDERS
sliderVolumeDiv = createDiv();
sliderVolumeDiv.id('volume-slider-div');
sliderVolumeDiv.parent('canvas-util-div');
sliderVolume = createSlider(0, 1, 0.5, 0.01);
sliderVolume.parent('volume-slider-div');
sliderVolume.class('volume-slider');
sliderVolume.mouseMoved(sliderEmoji);
sliderVolumeEmoji = createP('🔉'); //🔇🔈🔉🔊
sliderVolumeEmoji.id('volume-emoji');
sliderVolumeEmoji.style("font-size: 1.5rem; margin: 0 3% 0 3%; padding: 1%; cursor: default;");
sliderVolumeEmoji.parent('volume-slider-div');
}
// PAGE & SOUND
function loadDefaultMp3() {
song.pause();
playPauseButton.attribute('disabled', '');
song = loadSound("80degrees.mp3", loaded);
console.log('clicked');
}
function loadUploadedMp3(event) {
uploadedSong = URL.createObjectURL(event.target.files[0]);
song.pause();
playPauseButton.attribute('disabled', '');
song = loadSound(uploadedSong, loaded);
console.log('uploaded');
}
function togglePlaying() {
if (!song.isPlaying()) {
song.play();
playPauseButton.html("⏸");
} else {
song.pause();
playPauseButton.html("▶️");
}
}
function loaded() {
console.log("loaded");
playPauseButton.removeAttribute('disabled');
playPauseButton.html("⏯");
}
function sliderEmoji() {
if (sliderVolume.value() == 0) {
sliderVolumeEmoji.html('🔇');
}
else if (sliderVolume.value() == 1) {
sliderVolumeEmoji.html('🔊');
}
else if (sliderVolume.value() <= 0.5) {
sliderVolumeEmoji.html('🔈');
}
else {
sliderVolumeEmoji.html('🔉');
}
}
// VISUALIZER
function drawAmplitudeEllipse() {
var volume = amplitude.getLevel(); //range of volume between 0 and 0.3
var diameter = map(volume, 0, 0.3, 10, 200);
fill(255, 0, 255);
ellipse(width/2, height/2, diameter, diameter);
}
function drawFlatAmplitudeLevels() {
push();
var volume = amplitude.getLevel(); //range of volume between 0 and 0.3
if (song.isPlaying()){
volumeHistory.push(volume);
}
stroke(255);
noFill();
beginShape();
for (var i = 0; i < volumeHistory.length; i++) {
var y = map(volumeHistory[i], 0, 1, height / 2, 0);
vertex(i, y);
}
endShape();
pop();
if (volumeHistory.length > width - 50) {
volumeHistory.splice(0,1)
}
stroke(255, 0, 0);
line(volumeHistory.length, 0, volumeHistory.length, height);
fill(255, 0, 255);
}
function drawCircularAmplitudeLevels() {
var volume = amplitude.getLevel(); //range of volume between 0 and 0.3
if (song.isPlaying()){
volumeHistory.push(volume);
}
stroke(255);
//noFill();
var c = sliderColor.value()
fill(145,c,200);
// IDEA: slider to change color
translate(width / 2, height / 2);
beginShape();
for (var i = 0; i < 360; i++) {
var r = map(volumeHistory[i], 0, 1, 10, width / 2);
var x = r * cos(i);
var y = r * sin(i);
vertex(x, y);
}
endShape();
if (volumeHistory.length > 360) {
volumeHistory.splice(0,1);
}
}
function drawFlatFrequencyLevels() {
background(51);
var spectrum = frequency.analyze();
stroke(255);
for (var i = 0; i < spectrum.length; i++) {
var amp = spectrum[i];
let spectrumRange = 256;
var y = map(amp, 0, spectrumRange, height, 0);
rect(i * bandWidth, y, bandWidth - 2, height - y);
}
noFill();
}
function drawCircularFrequencyLevels() {
background(51);
var spectrum = frequency.analyze();
stroke(255);
translate(width/2, height/2); // translate to the center
beginShape();
for (var i = 0; i < spectrum.length; i++) {
var angle = map(i, 0, spectrum.length, 0, 360)
var amp = sp3[i];
let spectrumRange = 256;
var r = map(amp, 0, spectrumRange, 20, 100);
var x = r * cos(angle);
var y = r * sin(angle);
stroke(i, 255, 255);
line(0, 0, x, y);
}
endShape();
}
function checkNonZeroArray(array) {
for (let v of array) {
if (v != 0) {
return true;
}
}
return false;
}
function draw1() {
background(hsb1, 50, 100);
let spectrumRange = 256;
var spectrum = frequency.analyze();
peakDetect.update(frequency);
var l = spectrum.length;
var sp1 = spectrum.slice(0,l/4);
var sp2 = spectrum.slice(l/4, l/2);
var sp3 = spectrum.slice(l/2, 3*l/2);
if (checkNonZeroArray(spectrum)) { // array all zeroes
hsb1 = (hsb1 + 1) % 360;
}
// frequency animation
push();
var centerAnimation = spectrum.slice(l/4, 3*l/4);
translate(width/2, height/2); // translate to the center
beginShape();
for (var i = 0; i < centerAnimation.length; i++) {
var angle = map(i, 0, centerAnimation.length, 0, 360)
var amp = centerAnimation[i];
var r = map(amp, 0, spectrumRange, width/8, width/2); // tuning
var x = r * cos(angle);
var y = r * sin(angle);
stroke(i, 255, 255);
//scale(1.5);
//vertex(x, y);
line(0, 0, x, y);
}
endShape();
pop();
// amplitude animation
push();
var volume = amplitude.getLevel(); //range of volume between 0 and 0.3
if (song.isPlaying()){
volumeHistory.push(volume);
}
strokeWeight(3);
var hsb2 = (hsb1 + 180) % 360;
stroke(hsb2, 50, 100);
fill(hsb2, 50, 100);
beginShape();
vertex(0,0);
for (var i = 0; i < volumeHistory.length; i++) {
var y = map(volumeHistory[i], 0, 1, 0+height/100, height/4);
vertex(i, y);
}
vertex(width,0);
endShape();
beginShape();
vertex(width, height);
for (var i = 0; i < volumeHistory.length; i++) {
var y = map(volumeHistory[i], 0, 1, height-height/100, 3*height/4);
vertex(width-i, y);
}
vertex(0, height);
endShape();
if (volumeHistory.length > width) {
volumeHistory.splice(0,1);
}
pop();
// emojis
push();
if (mouseIsPressed && mouseX > 0 && mouseX <= width && mouseY > 0 && mouseY <= height) {
// find a way to make interval a bit bigger
addEmoji(mouseX, mouseY);
}
drawEmojis();
pop();
}
function drawEmojis() {
for (var i = 0; i < em.length; i++) {
let emoji = em[i];
push();
textSize(emoji.size);
text(emoji.emoji, emoji.x, emoji.y);
pop();
emoji.y = emoji.y0 + emoji.vy * emoji.t + 5 * emoji.t * emoji.t;
emoji.x = emoji.x0 + emoji.vx * emoji.t
emoji.t += 0.1;
if (emoji.x > width || emoji.y > height) {
em.splice(i,1);
}
}
}
function addEmoji(x, y) {
// idea: emoji palettes
em.push(
{
x0: x,
y0: y,
x: x,
y: y,
vx: getRandomFloat(-10, 10),
vy: getRandomFloat(-20,0),
t: 0,
emoji: random(['🔔', '🔊', '🎶', '💫', '💤', '🧿', '💣', '🌈', '🍩', '👁', '🫀', '🍄', '🪁', '💊', '🧩']),
size: getRandomFloat(10,30) //make it relative to canvas size
}
)
}
function getRandomFloat(min, max) {
return Math.random() * (max - min) + min;
}
function draw() {
song.setVolume(sliderVolume.value());
draw1();
}