-
Notifications
You must be signed in to change notification settings - Fork 0
/
testFilters.html
305 lines (249 loc) · 8.65 KB
/
testFilters.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>
<head>
<script type="text/javascript" src="js/pitch.js"></script>
<script src="https://rawgithub.com/mrdoob/three.js/master/build/three.js"></script>
<script src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
var audioContext;
var audioSource;
var analyser;
var freqDomain;
var timeDomain;
var lowpassfilter;
var highpassfilter;
var bandpassfilter;
var allpassfilter;
var peakingfilter;
var gainNode;
var canvas;
var ctxt;
var GRID_WIDTH = 32;
var GRID_HEIGHT = 32;
var SAMPLE_MAX = 256;
var WIDTH = 600;
var HEIGHT = 600;
var scene;
var camera;
var renderer;
$(document).ready(function() {
var circleFill = function(context) {
context.beginPath();
context.arc(0, 0, 0.5, 0, 2 * Math.PI, true);
context.fill();
}
init();
function init() {
canvas = document.getElementById('canvas');
ctxt = canvas.getContext('2d');
canvas.width = WIDTH;
canvas.height = HEIGHT;
try {
// Fix up for prefixing
window.AudioContext = window.AudioContext
||window.webkitAudioContext;
audioContext = new AudioContext();
}
catch(e) {
alert('Web Audio API is not supported in this browser');
}
ctxt.strokeRect(0, 0, canvas.width, canvas.height);
// $('#canvas').css('padding-left', '300px');
lowpassfilter = audioContext.createBiquadFilter();
lowpassfilter.type = lowpassfilter.LOWPASS;
lowpassfilter.frequency.value = 100;
highpassfilter = audioContext.createBiquadFilter();
highpassfilter.type = highpassfilter.HIGHPASS;
highpassfilter.frequency.value = 100;
bandpassfilter = audioContext.createBiquadFilter();
bandpassfilter.type = bandpassfilter.BANDPASS;
bandpassfilter.frequency.value = 100;
allpassfilter = audioContext.createBiquadFilter();
allpassfilter.type = allpassfilter.ALLPASS;
allpassfilter.frequency.value = 100;
peakingfilter = audioContext.createBiquadFilter();
peakingfilter.type = peakingfilter.PEAKING;
peakingfilter.frequency.value = 100;
gainNode = audioContext.createGainNode();
$("#soundIn").change(loadSound);
// check box listeners
$("#lowpass").change(updateSound);
$("#highpass").change(updateSound);
$("#bandpass").change(updateSound);
$("#allpass").change(updateSound);
$("#peaking").change(updateSound);
$("#gain").change(updateSound);
// range input listeners
$("#lowpassrange").change(changeFrequency);
$("#highpassrange").change(changeFrequency);
$("#bandpassrage").change(changeFrequency);
$("#allpassrange").change(changeFrequency);
$("#peakingrange").change(changeFrequency);
$("#gainrange").change(changeGain);
$('#analyze').click(analyzeSound);
}
// load sound from input mp3
function loadSound() {
var file = $("#soundIn").get(0).files[0];
var reader = new FileReader();
reader.onload = function(e) {
var buff = e.target.result;
playSound(buff);
}
reader.readAsArrayBuffer(file);
}
function updateSound() {
if (audioSource) {
// disconnect everything
audioSource.disconnect(0);
lowpassfilter.disconnect(0);
highpassfilter.disconnect(0);
allpassfilter.disconnect(0);
bandpassfilter.disconnect(0);
peakingfilter.disconnect(0);
gainNode.disconnect(0);
var source = audioSource;
if ($("#lowpass").prop('checked')) {
audioSource.connect(lowpassfilter);
source = lowpassfilter;
}
if ($("#highpass").prop('checked')) {
source.connect(highpassfilter);
source = highpassfilter;
}
if ($("#bandpass").prop('checked')) {
source.connect(bandpassfilter);
source = bandpassfilter;
}
if ($("#allpass").prop('checked')) {
source.connect(allpassfilter);
source = allpassfilter;
}
if ($("#peaking").prop('checked')) {
source.connect(peakingfilter);
source = peakingfilter;
}
if ($("#gain").prop('checked')) {
source.connect(gainNode);
source = gainNode;
}
source.connect(analyser);
analyser.connect(audioContext.destination);
source.connect(audioContext.destination);
}
}
// analyze qualities of the sound
function analyzeSound() {
requestAnimationFrame(analyzeSound);
ctxt.clearRect(0,0, WIDTH, HEIGHT);
console.log('analyzing...');
var timeDomain = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteTimeDomainData(timeDomain);
var freqDomain = new Float32Array(analyser.frequencyBinCount);
analyser.getFloatFrequencyData(freqDomain);
// console.log(freqDomain);
for (var freq = 0; freq < freqDomain.length; freq++) {
var x = Math.floor(freq % GRID_WIDTH) * WIDTH/GRID_WIDTH + 10;
var y = Math.floor(freq / GRID_WIDTH) * HEIGHT/GRID_HEIGHT + 10;
var amp = freqDomain[freq];
var r = Math.abs(amp/SAMPLE_MAX) * 10;
var grd = ctxt.createRadialGradient(x, y, r/2, x, y, r);
grd.addColorStop(0, "red");
grd.addColorStop(0, "blue");
// ctxt.fillStyle = '#FF0000';
ctxt.fillStyle = grd;
ctxt.beginPath();
ctxt.arc(x, y, r, 0, Math.PI * 2);
ctxt.fill();
}
// renderer.render(scene, camera);
}
// gets the value of the inputted frequency
function getFrequencyValue(frequency) {
var nyquist = audioContext.sampleRate/2;
var index = Math.round(frequency/nyquist * freqDomain.length);
return freqDomain[index];
}
// play sound from ArrayBuffer
function playSound(buffer) {
// disconnect if there is already a song uploaded
if (audioSource) {
audioSource.stop();
audioSource.disconnect(0);
}
audioSource = audioContext.createBufferSource();
audioSource.buffer = audioContext.createBuffer(buffer,false);
// set up the AudioAnalserNode
analyser = audioContext.createAnalyser();
audioSource.connect(analyser);
analyser.connect(audioContext.destination);
audioSource.connect(audioContext.destination);
audioSource.start(0);
analyzeSound();
}
function changeFrequency() {
var minValue = 40;
var maxValue = audioContext.sampleRate / 2;
var id = $(this).attr('id');
var newvalue = $(this).val();
console.log("newvalue = " + newvalue);
var filter;
if (id == "lowpassrange") {
filter = lowpassfilter;
}
else if (id == "highpassrange") {
filter = highpassfilter;
}
else if (id == "bandpassrange") {
filter = bandpassfilter;
}
else if (id == "allpassrange") {
filter = allpassfilter;
}
else if (id == "peakingrange") {
filter = peakingfilter;
}
// Logarithm (base 2) to compute how many octaves fall in the range.
var numberOfOctaves = Math.log(maxValue / minValue) / Math.LN2;
// Compute a multiplier from 0 to 1 based on an exponential scale.
var multiplier = Math.pow(2, numberOfOctaves * (newvalue - 1.0));
//filter.frequency.value = maxValue * multiplier;
filter.frequency.value = newvalue;
analyzeSound();
}
function changeGain(){
console.log("updating gain to " + $("#gainrange").val());
gainNode.gain.value = $("#gainrange").val();
}
});
</script>
</head>
<body>
<div id="titlediv">
SOUND SCENE
</div>
<div id="inputdiv">
<input type="file" id="soundIn" accept="audio/*"></input>
</div>
<input type = "checkbox" id = "lowpass" name = "" > Low pass
<input type = "range" id = "lowpassrange" min="100" max="200" value = "100">
<canvas id="canvas"></canvas>
<br></br>
<input type = "checkbox" id = "highpass" name = "" > High pass
<input type = "range" id = "highpassrange" min="100" max = "400" value = "100">
<br></br>
<input type = "checkbox" id = "bandpass" name = "" > Band pass
<input type = "range" id = "bandpassrange" min="100" max = "400" value="100">
<br></br>
<input type = "checkbox" id = "allpass" name = "" > All pass
<input type = "range" id = "allpassrange" min="100" max = "400" value="100">
<br></br>
<input type = "checkbox" id = "peaking" name = "" > Peaking
<input type = "range" id = "peakingrange" min="100" max = "400" value="100">
<br></br>
<input type = "checkbox" id = "gain" name = "" > Gain
<input type = "range" id = "gainrange" min="0" max = "1" value="0" step="0.01" >
<br></br>
<button type="submit" id = "analyze" >Analyze</button>
</body>
</html>