-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
234 lines (200 loc) · 5.56 KB
/
sound.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
window.onload = () => {
const file = document.getElementById("file-input");
const canvas = document.getElementById("canvas");
const h3 = document.getElementById("name");
const audio = document.getElementById("audio");
const demo = document.querySelector(".demo");
const pause = document.getElementById('pause');
const play = document.getElementById('play');
/* Get the documentElement (<html>) to display the page in fullscreen */
var elem = document.documentElement;
// Create context
const ctx = canvas.getContext("2d");
const context = new AudioContext() || context.AudioContext|| webkit.AudioContext();
let src = context.createMediaElementSource(audio);
const analyser = context.createAnalyser();
let index = 0;
let next = document.getElementById("next");
next.addEventListener("click", () => {
console.log('next clicked')
if (index == 3) {
index = 0;
} else {
index++;
}
console.log(index)
visualize(index);
});
demo.onclick = () => {
console.log('demo clicked')
visualize(0);
};
function visualize(index) {
console.log('in visualize')
let name;
let musicSource = [
"./music/blueMonday.mp3",
"./music/onYourMind.mp3",
"./music/saveYourTears.mp3",
"./music/desire.mp3",
];
let songNames = ["Blue Monday", "On Your Mind", "Save Your Tears", "Desire"];
// playSongs(file, index);
// audio.src = musicSource[index];
name = songNames[index];
audio.src = musicSource[index]
h3.innerText = `${name}`;
src.connect(analyser);
analyser.connect(context.destination);
analyser.fftSize = 4096;
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
let barHeight;
let x = 0;
let angle = 2;
let y;
function renderFrame() {
context.resume();
// audio.play();
requestAnimationFrame(renderFrame);
analyser.getByteFrequencyData(dataArray);
let r, g, b;
let bars = 360;
let barWidth = 5;
let center_x = canvas.width / 2;
let center_y = canvas.height / 2;
let radius;
if (canvas.width < 700 || canvas.height < 500) {
radius = 40;
} else {
radius = 150;
}
var gradient = ctx.createLinearGradient(0, 50, 0, canvas.height);
gradient.addColorStop(0, "rgba(0, 0, 1, 1)");
gradient.addColorStop(1, "rgba(0, 0, 51, 1)");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let test2 = 0;
for (let i = 0; i < bars; i++) {
if (canvas.width < 700 || canvas.height < 400) {
barHeight = dataArray[i] * 0.3;
} else {
barHeight = dataArray[i] * 0.8;
}
//trying to figure out base
if (dataArray[i] > 300) {
r = 255;
g = 5;
b = 0;
} else if (dataArray[i] > 235) {
r = 250;
g = 5;
b = 100;
} else if (dataArray[i] > 210) {
r = 250;
g = 0;
b = 250;
} else if (dataArray[i] > 200) {
r = 250;
g = 255;
b = 0;
} else if (dataArray[i] > 180) {
r = 204;
g = 255;
b = 50;
} else if (dataArray[i] > 170) {
r = 0;
g = 50;
b = 255;
} else if (dataArray[i] > 150) {
r = 0;
g = 100;
b = 255;
} else if (dataArray[i] > 130) {
r = 0;
g = 200;
b = 255;
} else if (dataArray[i] > 110) {
r = 0;
g = 255;
b = 200;
} else if (dataArray[i] > 90) {
r = 0;
g = 255;
b = 100;
} else if (dataArray[i] > 75) {
r = 0;
g = 255;
b = 50;
} else if (dataArray[i] > 50) {
r = 0;
g = 255;
b = 0;
} else if (dataArray[i] > 25) {
r = 0;
g = 100;
b = 0;
} else {
r = 0;
g = 50;
b = 0;
}
let rads = (angle * Math.PI) / bars;
x = center_x + Math.cos(rads * i) * radius;
y = center_y + Math.sin(rads * i) * radius;
let x_end = center_x + Math.cos(rads * i) * (radius + barHeight);
let y_end = center_y + Math.sin(rads * i) * (radius + barHeight);
drawBar(x, y, x_end, y_end, barWidth, dataArray[i]);
function drawBar(x1, y1, x2, y2, width, frequency) {
var lineColor = `rgb(${r}, ${g}, ${b})`;
ctx.strokeStyle = lineColor;
ctx.lineWidth = width;
ctx.beginPath();
ctx.lineCap = "round";
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.fill();
}
}
}
audio.play();
renderFrame();
}
/* View in fullscreen */
function openFullscreen() {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) {
/* Firefox */
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
/* Chrome, Safari and Opera */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) {
/* IE/Edge */
elem.msRequestFullscreen();
}
}
/* Close fullscreen */
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
/* Firefox */
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
/* Chrome, Safari and Opera */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
/* IE/Edge */
document.msExitFullscreen();
}
}
document.getElementById("fullScreen").addEventListener("click", openFullscreen);
document
.getElementById("exitFullScreen")
.addEventListener("click", closeFullscreen);
}