-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
202 lines (163 loc) · 5.56 KB
/
app.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
window.onload = function() {
var audio,
analyser,
audioContext,
soundSource,
soundBuffer,
sourceNode,
cycle,
transparency,
freqArray,
stream;
var hue = hue || 280;
var display = "circles";
var audioInput = document.getElementById('audiofile');
audioInput.addEventListener('change', function(event) {
if (!audioContext || audioContext.state !== "running"){
stream = URL.createObjectURL(event.target.files[0]);
audio = new Audio();
console.log(audio);
audio.src = stream;
setup();
}
});
var sample = document.getElementsByClassName('sample')[0];
sample.addEventListener('click', function (){
audio = new Audio('ready_to_howl.mp3');
setup();
});
function startSound() {
sourceNode = audioContext.createMediaElementSource(audio);
sourceNode.connect(analyser);
sourceNode.connect(audioContext.destination);
audio.play();
update();
}
function setup() {
audio.addEventListener('canplay', function () {
audioContext = audioContext || new AudioContext();
analyser = (analyser || audioContext.createAnalyser());
analyser.smoothingTimeConstant = 0.1;
analyser.fftSize = 512;
startSound();
});
}
document.getElementsByClassName('play')[0].addEventListener('click', playSound.bind(null,sourceNode));
document.getElementsByClassName('stop')[0].addEventListener('click', stopSound);
function playSound() {
if (audioContext){
if (audioContext.state === 'suspended') {
audioContext.resume();
}
}
}
function stopSound() {
if (audioContext){
if (audioContext.state === 'running'){
audioContext.suspend();
}
}
}
function drawCircle(context, freqValue, freqSequence) {
var x = ((width / 2)),
y = ((height / 2));
if (false){
transparency = 0.001;
} else {
transparency = 0.08;
}
context.beginPath();
context.arc(x-(freqValue / 8), y-(freqValue / 8), (Math.abs(freqValue-120)) * 5 , 0, 2 * Math.PI, false);
context.strokeStyle = 'hsla(' + hue + ', ' + 100 + '%,' + 40 + '%,' + transparency + ')';
context.fillStyle = "hsla(" + hue + ", 100%, 40%, .01)";
context.fill();
context.lineWidth = 2;
context.stroke();
}
function drawOscillo(context, freqArray) {
context.beginPath();
var sliceWidth = canvas.width * 1.0/freqArray.length;
var oscilloX = 0;
for (var i = 0; i < freqArray.length; i++){
var v = freqArray[i]/86.0;
var y = v * 200/2;
if(i === 0){
context.moveTo(oscilloX, y);
} else {
context.lineTo(oscilloX, y);
}
oscilloX += sliceWidth;
}
if (false){
transparency = 0.001;
} else {
transparency = 0.08;
}
context.strokeStyle = 'hsla(' + hue + ', ' + 100 + '%,' + 40 + '%,' + 0.9 + ')';
context.lineTo(canvas.width, canvas.height/2);
context.lineWidth = 2;
context.stroke();
}
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight,
c = 0;
function update() {
context.clearRect(0, 0, width, height);
freqArray = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteTimeDomainData(freqArray);
oscilloX = 0;
if (display === "circles"){
for (var i = 0; i < freqArray.length; i += 1) {
var point = freqArray[i];
drawCircle(context, point, i);
}
} else if (display === "oscillo"){
drawOscillo(context, freqArray);
}
if (cycle && audioContext.state === 'running'){
hue += 1;
}
requestAnimationFrame(update);
}
var options = document.getElementsByClassName('options-header')[0];
options.addEventListener("click", function() {
var accordion = document.getElementsByClassName('options-list')[0];
accordion.classList.toggle("hiding");
})
var colorpicker = document.getElementsByClassName('rainbow')[0];
colorpicker.addEventListener("click", function(event){
var rect = colorpicker.getBoundingClientRect();
var percent = (event.pageX - rect.left)/colorpicker.offsetWidth;
hue = 360 * percent;
console.log(event.pageX - rect.left);
})
var cycleButton = document.getElementsByClassName('cycle')[0];
cycleButton.addEventListener("click", function() {
if (cycle){
cycle = false;
} else {
cycle = true;
}
})
var oscilloButton = document.getElementById('oscillo');
var circlesButton = document.getElementById('circles');
oscilloButton.addEventListener("click", function(){
display = "oscillo";
console.log(display);
});
circlesButton.addEventListener("click", function() {
display = "circles";
console.log(display);
})
var question = document.getElementsByClassName('help')[0];
var closeButton = document.getElementsByClassName('close')[0];
var modal = document.getElementsByClassName('modal')[0];
closeButton.addEventListener("click", function(){
modal.classList.add("closed");
})
question.addEventListener("click", function() {
modal.classList.remove("closed");
})
};