-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
352 lines (288 loc) · 11.6 KB
/
index.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
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>equalizer.js</title>
<link type="text/css" rel="stylesheet" href="./styles/main.css" />
<link type="image/png" rel="icon" href="./images/favicon.png" />
<!-- Primary Meta Tags -->
<title>equalizer.js</title>
<meta name="title" content="equalizer.js">
<meta name="description" content="A web-based audio analysis tool for real-time and choreographed visualizations; written in JavaScript.">
<!-- Open Graph / Facebook -->
<meta property="og:type" content="website">
<meta property="og:url" content="https://jonobr1.com/equalizer.js/">
<meta property="og:title" content="equalizer.js">
<meta property="og:description" content="A web-based audio analysis tool for real-time and choreographed visualizations; written in JavaScript.">
<meta property="og:image" content="https://jonobr1.com/equalizer.js/images/thumbnail.png">
<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:url" content="https://jonobr1.com/equalizer.js/">
<meta property="twitter:title" content="equalizer.js">
<meta property="twitter:description" content="A web-based audio analysis tool for real-time and choreographed visualizations; written in JavaScript.">
<meta property="twitter:image" content="https://jonobr1.com/equalizer.js/images/thumbnail.png">
</head>
<body>
<div id="content">
<div id="tool" class="column"></div>
<div id="description" class="column">
<h1 id="title">equalizer.js</h1>
<p>
<span class="symbols">O</span>An audio analysis tool for real-time and choreographed visualizations.
</p>
<p id="links">
<a href="https://npmjs.com/package/equalizer.js">NPM</a> · <a href="http://github.com/jonobr1/equalizer">Source Code</a> · <a href="https://github.com/jonobr1/equalizer/blob/master/LICENSE" target="_blank">MIT</a>
</p>
<p id="instructions">
Start by dragging an audio file — preferably a wav, mp3, or ogg — onto the gray outline box above. <!-- Once loaded a rotated <a class="explanation" href="https://en.wikipedia.org/wiki/Staff_(music)" target="_blank">staff</a> will be displayed.--> Hit the <span id="spacebar" class="action">spacebar</span> to play and pause the music. Once audio starts playing you'll notice some visual activity, <span id="bands"></span> bands representing different amplitudes at equally spaced frequency bandwidths.
</p>
<p>
You can also <span id="export" class="action">export</span> and <span id="import" class="action">import</span> data as JSON with the below text field:
<br />
<textarea width="300" height="200"></textarea>
<br />
<button class="download hidden">Download</button>
</p>
<br />
<!-- <h2 id="examples">
Examples
</h2>
<p>
This page is one example of how to use Equalizer, but there are other ways it can be used. Check them out below:
</p>
<ul>
<li>Real-time Analysis</li>
<li>Choreographed Data</li>
</ul> -->
<p id="post-scriptum">
Created <span id="created-date"></span> and updated <span id="updated-date"></span>.
<br />
A free and open source tool by <a href="http://jono.fyi/" target="_blank">Jono Brandel</a>
<br />
</p>
</div>
</div>
<div class="scripts">
<script type="module">
import * as qp from './third-party/query-params.js';
import { Equalizer } from './src/equalizer.js';
import { Sound } from './src/sound.js';
if (!Sound.has) {
throw new Error('Sound: Unable to identify the Web Audio API');
}
var params = qp.get();
var $ = {
tool: document.querySelector('#tool'),
spacebar: document.querySelector('#spacebar'),
bands: document.querySelector('#bands'),
export: document.querySelector('#export'),
import: document.querySelector('#import'),
textarea: document.querySelector('textarea'),
download: document.querySelector('button.download'),
updatedAt: document.querySelector('#updated-date'),
createdAt: document.querySelector('#created-date')
};
Equalizer.Resolution = typeof params.resolution === 'number' ? params.resolution : 16;
Equalizer.FrameRate = typeof params.frameRate === 'number' ? params.frameRate : 30;
var context = new AudioContext();
var equalizer = new Equalizer(context).appendTo($.tool);
var elem = equalizer.domElement;
var sound, data;
elem.style.display = 'inline-block';
elem.style.margin = '20px';
elem.style.border = '1px solid #ccc';
$.bands.textContent = Equalizer.Resolution;
elem.addEventListener('drop', drop, false);
elem.addEventListener('dragover', dragover, false);
elem.addEventListener('dragleave', dragleave);
elem.addEventListener('click', toggle, false);
$.spacebar.addEventListener('click', toggle, false);
$.export.addEventListener('click', generateAnalysis, false);
$.import.addEventListener('click', importAnalysis, false);
$.download.addEventListener('click', download, false);
window.addEventListener('keyup', keyup, false);
equalizer.update();
updateStats();
function setup() {
equalizer.add(sound.gain);
if (!loop.init) {
loop();
loop.init = true;
}
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
elem.style.background = 'transparent';
var file = e.dataTransfer.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
data = e.target.result;
if (sound) {
sound.stop();
}
sound = new Sound(context, data.slice(0), setup);
sound.src = file.name;
equalizer.reset();
};
reader.readAsArrayBuffer(file);
}
function dragover(e) {
e.stopPropagation();
e.preventDefault();
elem.style.background = 'rgba(200, 255, 150, 0.33)';
}
function dragleave(e) {
e.stopPropagation();
e.preventDefault();
elem.style.background = 'transparent';
}
function keyup(e) {
switch (e.which) {
case 32:
toggle();
break;
}
}
function loop() {
requestAnimationFrame(loop);
if (sound.playing) {
equalizer.update(sound.currentTime);
}
}
function toggle() {
if (!sound) {
alert('You need to drop an audio file before the equalizer can run.');
return;
}
if (sound.playing) {
sound.pause();
} else {
sound.play();
}
}
function generateAnalysis(e) {
if (!sound) {
alert('You need to drop an audio file before the equalizer can run.');
return;
} else if ($.textarea.classList.contains('processing')) {
alert(`Already processing ${sound.src}`);
return;
}
var result = {
frameRate: Equalizer.FrameRate,
resolution: Equalizer.Resolution,
samples: []
};
var context = new AudioContext();
var equalizer = new Equalizer(context);
var s = new Sound(context, data.slice(0), loaded);
var pass = context.createGain();
var elapsed = 0;
$.textarea.classList.add('processing');
function loaded() {
equalizer.add(pass);
s.applyFilter(pass);
s.volume = 0;
s.gain.disconnect(context.destination);
render();
}
function render(){
var pct = Math.round(100 * elapsed / s.duration);
$.textarea.value = `Generating Analysis\n${pct}%`;
if (elapsed >= s.duration) {
complete();
return;
}
s.play({
offset: elapsed
});
equalizer.update();
var sample = [];
for (var i = 0; i < equalizer.bands.length; i++) {
var band = equalizer.bands[i];
sample.push(Math.round(band.value));
}
console.log(sample.join(', '));
result.samples.push(sample);
elapsed += 1 / result.frameRate;
requestAnimationFrame(render);
}
function complete() {
$.textarea.classList.remove('processing');
$.textarea.value = JSON.stringify(result);
$.download.classList.remove('hidden');
$.download.result = result;
}
}
function importAnalysis(e) {
try {
var result = JSON.parse($.textarea.value);
$.download.result = result;
equalizer.analysed = result;
} catch(e) {
console.error(e);
alert('Unable to parse JSON in textarea');
}
}
function download() {
var filename = sound.src.replace(/\.[a-z0-9]*$/i, '.json');
saveTemplateAsFile(filename, $.download.result);
}
function updateStats() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.github.com/repos/jonobr1/equalizer');
xhr.onreadystatechange = function() {
if (!(xhr.readyState === 4 && xhr.status === 200)) {
return;
}
var resp = JSON.parse(xhr.responseText);
$.updatedAt.textContent = formatDate(resp.pushed_at);
$.createdAt.textContent = formatDate(resp.created_at);
};
xhr.send();
}
function formatDate(time) {
var date = new Date(time);
var suffices = ['st', 'nd', 'rd'];
suffices.getIndex = function(n) {
var index = parseInt((n + '').slice(-1));
return index - 1;
};
var months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
return [
months[date.getMonth()],
date.getDate() + ',',
date.getFullYear()
].join(' ')
}
function saveTemplateAsFile(filename, dataObjToWrite) {
var blob = new Blob([JSON.stringify(dataObjToWrite)], { type: 'text/json' });
var link = document.createElement('a');
link.download = filename;
link.href = window.URL.createObjectURL(blob);
link.dataset.downloadurl = ['text/json', link.download, link.href].join(':');
var e = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
link.dispatchEvent(e);
link.remove()
};
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-QVBN5WNFBR"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-QVBN5WNFBR');
</script>
</div>
</body>
</html>