-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebaudio.html
305 lines (260 loc) · 9.03 KB
/
webaudio.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Web Audio Visualizer</title>
<style>
body {
background: #eeeeee;
font-family: tahoma, verdana, sans serif;
}
canvas {
margin-left:10px;
margin-top:10px;
box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
background: black;
}
#controls{
margin-left:10px;
margin-top:10px;
}
</style>
<script>
// An IIFE ("Iffy") - see the notes in mycourses
(function(){
"use strict";
var NUM_SAMPLES = 256;
var SOUND_1 = 'media/New Adventure Theme.mp3';
var SOUND_2 = 'media/Peanuts Theme.mp3';
var SOUND_3 = 'media/The Picard Song.mp3';
var audioElement;
var analyserNode;
var canvas,ctx,maxRadius;
var invert = true, tintRed = false, noise = true, lines = false, custom = false;
function init(){
// set up canvas stuff
canvas = document.querySelector('canvas');
ctx = canvas.getContext("2d");
// get reference to <audio> element on page
audioElement = document.querySelector('audio');
// call our helper function and get an analyser node
analyserNode = createWebAudioContextWithAnalyserNode(audioElement);
// get sound track <select> and Full Screen button working
setupUI();
var radiusSlider = document.getElementById('radiusSlider');
maxRadius = radiusSlider.value;
radiusSlider.onchange = function(e){
maxRadius = e.target.value;
}
var noiseButton = document.getElementById('noise');
noiseButton.onchange = function(e){
noise = e.target.checked;
}
var tintRedButton = document.getElementById('tintRed');
tintRedButton.onchange = function(e){
tintRed = e.target.checked;
}
var invertButton = document.getElementById('invert');
invertButton.onchange = function(e){
invert = e.target.checked;
}
var linesButton = document.getElementById('lines');
linesButton.onchange = function(e){
lines = e.target.checked;
}
var customButton = document.getElementById('custom');
customButton.onchange = function(e){
custom = e.target.checked;
}
// load and play default sound into audio element
playStream(audioElement,SOUND_1);
// start animation loop
update();
}
function createWebAudioContextWithAnalyserNode(audioElement) {
var audioCtx, analyserNode, sourceNode;
// create new AudioContext
// The || is because WebAudio has not been standardized across browsers yet
// http://webaudio.github.io/web-audio-api/#the-audiocontext-interface
audioCtx = new (window.AudioContext || window.webkitAudioContext);
// create an analyser node
analyserNode = audioCtx.createAnalyser();
/*
We will request NUM_SAMPLES number of samples or "bins" spaced equally
across the sound spectrum.
If NUM_SAMPLES (fftSize) is 256, then the first bin is 0 Hz, the second is 172 Hz,
the third is 344Hz. Each bin contains a number between 0-255 representing
the amplitude of that frequency.
*/
// fft stands for Fast Fourier Transform
analyserNode.fftSize = NUM_SAMPLES;
// this is where we hook up the <audio> element to the analyserNode
sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(analyserNode);
// here we connect to the destination i.e. speakers
analyserNode.connect(audioCtx.destination);
return analyserNode;
}
function setupUI(){
document.querySelector("#trackSelect").onchange = function(e){
playStream(audioElement,e.target.value);
};
document.querySelector("#fsButton").onclick = function(){
requestFullscreen(canvas);
};
}
function playStream(audioElement,path){
audioElement.src = path;
audioElement.play();
audioElement.volume = 0.2;
document.querySelector('#status').innerHTML = "Now playing: " + path;
}
function update() {
// this schedules a call to the update() method in 1/60 seconds
requestAnimationFrame(update);
/*
Nyquist Theorem
http://whatis.techtarget.com/definition/Nyquist-Theorem
The array of data we get back is 1/2 the size of the sample rate
*/
// create a new array of 8-bit integers (0-255)
var data = new Uint8Array(NUM_SAMPLES/2);
// populate the array with the frequency data
// notice these arrays can be passed "by reference"
analyserNode.getByteFrequencyData(data);
// OR
//analyserNode.getByteTimeDomainData(data); // waveform data
// DRAW!
ctx.clearRect(0,0,800,600);
var barWidth = 4;
var barSpacing = 1;
var barHeight = 100;
var topSpacing = 50;
// loop through the data and draw!
for(var i=0; i<data.length; i++) {
ctx.fillStyle = 'rgba(0,255,0,0.6)';
// the higher the amplitude of the sample (bin) the taller the bar
// remember we have to draw our bars left-to-right and top-down
//ctx.fillRect(i * (barWidth + barSpacing),topSpacing + 256-data[i],barWidth,barHeight);
//ctx.fillRect(640 - i * (barWidth + barSpacing), topSpacing + 256 - data[i] - 20, barWidth, barHeight);
if(data[i] != 0) {
ctx.strokeStyle = makeColor(data[i] , 111, 255 - data[i] , .6);
ctx.beginPath();
ctx.moveTo(0, data[i] + 100);
ctx.quadraticCurveTo(canvas.width / 2,canvas.height / 2 , canvas.width, canvas.height - data[i] - 100);
ctx.stroke();
//ctx.fill();
ctx.closePath();
}
if(data[i] != 0) {
ctx.strokeStyle = makeColor(255 - data[i] , 111, data[i] , .6);
ctx.beginPath();
ctx.moveTo(data[i] + 100, 0);
ctx.quadraticCurveTo(canvas.width / 2,canvas.height / 2 , canvas.width - data[i] - 100 , canvas.height);
ctx.stroke();
//ctx.fill();
ctx.closePath();
}
// Red-ish circles
var percent = data[i] / 255;
var circleRadius = percent * maxRadius;
ctx.beginPath();
ctx.fillStyle = makeColor(255, 111, 111, .34 - percent / 3.0);
ctx.arc(canvas.width / 2, canvas.height / 2, circleRadius, 0, 2 * Math.PI, false);
ctx.fill();
ctx.closePath();
// blue-ish circles, bigger, more transparent
ctx.beginPath();
ctx.fillStyle = makeColor(0,0,255, .10 - percent/10.0);
ctx.arc(canvas.width / 2, canvas.height / 2, circleRadius * 1.5, 0, 2 * Math.PI, false);
ctx.fill();
ctx.closePath();
// yellow-ish circles, smaller
ctx.save();
ctx.beginPath();
ctx.fillStyle = makeColor(200, 200, 0, .5 - percent / 5.0);
ctx.arc(canvas.width / 2, canvas.height / 2, circleRadius * .5, 0, 2 * Math.PI, false);
ctx.fill();
ctx.closePath();
ctx.restore();
}
manipulatePixels();
}
function manipulatePixels() {
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
var length = data.length;
var width = imageData.width;
for(var i = 0; i < length; i += 4) {
if(tintRed) {
data[i] = data[i] + 100;
}
if(invert){
var red = data[i], green = data[i + 1], blue = data[i + 2];
data[i] = 255 - red;
data[i+1] = 255 - green;
data[i+2] = 255 - blue;
}
if(noise && Math.random() < .10) {
data[i] = data[i + 1] = data[i + 2] = 128; //gray noise
}
if(lines) {
var row = Math.floor(i/4/width);
if(row % 50 == 0) {
data[i] = data[i + 1] = data[i + 2] = data[i + 3] = 255;
data[i + width*4] = data[i + width*4 + 1] = data[i + width*4 + 2] = data[i + (width*4) + 3] = 255;
}
}
if(custom) {
if(i != 0 && i != length - 1 ) {
data[i] = data[i - 4];
data[i + 1] = data[i + 5];
}
}
}
ctx.putImageData(imageData, 0, 0);
}
// HELPER
function makeColor(red, green, blue, alpha){
var color='rgba('+red+','+green+','+blue+', '+alpha+')';
return color;
}
// FULL SCREEN MODE
function requestFullscreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullscreen) {
element.mozRequestFullscreen();
} else if (element.mozRequestFullScreen) { // camel-cased 'S' was changed to 's' in spec
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
}
// .. and do nothing if the method is not supported
};
window.addEventListener("load",init);
}());
</script>
</head>
<body>
<canvas id="canvas" width="640" height="400"></canvas>
<div id="controls">
<audio controls loop></audio>
<label>Track:
<select id="trackSelect" >
<option value="media/New Adventure Theme.mp3">New Adventure Theme</option>
<option value="media/Peanuts Theme.mp3">Peanuts Theme</option>
<option value="media/The Picard Song.mp3">The Picard Song</option>
</select>
</label>
<input id="radiusSlider" type="range" name="points" min="100" max="400">
<input id="tintRed" type="checkbox">Red Tint
<input id="invert" type="checkbox" checked >Invert
<input id="noise" type="checkbox" checked >Noise
<input id="lines" type="checkbox">Lines
<input id="custom" type="checkbox">Custom
<button id="fsButton">Go Full Screen</button><br>
<p id="status">???</p>
</div>
</body>
</html>