forked from jamesfiltness/web-audio-mixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiometer.html
274 lines (188 loc) · 5.9 KB
/
audiometer.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
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
canvas {
background: #000;
fill :#000;
border-radius: 7px; }
@font-face{
font-family: 'digi';
src: url('digital-7_mono-webfont.eot');
src: url('digital-7_mono-webfont.eot?#iefix') format('embedded-opentype'),
url('digital-7_mono-webfont.woff') format('woff'),
url('digital-7_mono-webfont.ttf') format('truetype'),
url('digital-7_mono-webfont.svg#webfont') format('svg');
}
body {background: #000}
#mins,#secs,#milli {
float: left;
margin: 4px;
font-size: 28px;
color: #fff;
font-family: 'digi';
font-size: 50px;
}
#mins:after,#secs:after {
content: ":"
}
}
</style>
</head>
<body>
<div style="float: left; width: 250px;border: 1px solid #ccc;">
<div id="mins">00</div>
<div id="secs">00</div>
<div id="milli">00</div>
</div>
<div style="float: left; width: 200px;border: 1px solid #ccc;">
<button id="play">Play</button>
<button id="pause">Pause</button>
<button id="start">Rewind</button>
</div>
<div style="float: left; width: 200px;border: 1px solid #ccc;">
<canvas id="myCanvas" width="130" height="400">
Your browser does not support the HTML5 canvas tag.
</canvas>
</div>
<script>
(function() {
var startTime = +new Date();
var mi = document.getElementById('mins');
var secs = document.getElementById('secs');
var milli = document.getElementById('milli');
var currMins = 0;
var currSecs = 0;
function watchStart() {
setInterval(function() {
var elapsed = new Date() - startTime;
var curr = new Date(elapsed);
var tenths = (curr.getMilliseconds() / 10) % 100;
currSecs = curr.getSeconds();
currMins = curr.getMinutes();
if(curr.getSeconds() < 10) {
currSecs = "0" + curr.getSeconds();
}
if(curr.getMinutes() < 10) {
currMins = "0" + curr.getMinutes();
}
mi.innerHTML = currMins;
secs.innerHTML= currSecs;
milli.innerHTML= tenths.toFixed();
},10);
}
function getBuffer(context,fn) {
var request = new XMLHttpRequest();
request.open('GET', 'audio/Vox.mp3', true);
//xhr2 -
request.responseType = 'arraybuffer';
request.onload = function() {
// Asynchronously decode the audio file data in request.response
context.decodeAudioData(request.response,function(buff){
buffer = buff;
fn(buffer);
},function(error) {
console.log(error);
});
};
request.send();
};
function audioCtx() {
var audioCtx = (window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext);
if(audioCtx) {
return new audioCtx();
} else {
alert('Web Audio not supported in this browser. Please use a modern browser such as Chrome or Firefox');
return;
}
};
function Daw() {
var self = this;
this.ctx = audioCtx();
this.gain = this.ctx.createGain();
this.sources = [];
this.loadSources();
this.transport = new Transport(this.ctx,this);
this.analyserLeft = this.ctx.createAnalyser();
this.analyserLeft.smoothingTimeConstant = 0.3;
this.analyserLeft.fftSize = 1024;
this.analyserRight = this.ctx.createAnalyser();
this.analyserRight.smoothingTimeConstant = 0.3;
this.analyserRight.fftSize = 1024;
this.javascriptNode = this.ctx.createScriptProcessor(2048, 1, 1);
this.javascriptNode.connect(this.ctx.destination);
this.splitter = this.ctx.createChannelSplitter();
this.canvas = document.getElementById("myCanvas");
this.ctx = this.canvas.getContext("2d");
var on = true;
this.javascriptNode.onaudioprocess = function() {
if(on) {
self.ctx.clearRect(0, 0, 400, 400);
on = false;
} else {
on = true;
}
// get the average, bincount is fftsize / 2
var arrayLeft = new Uint8Array(self.analyserLeft.frequencyBinCount);
self.analyserLeft.getByteFrequencyData(arrayLeft);
var averageLeft = getAverageVolume(arrayLeft);
var arrayRight = new Uint8Array(self.analyserRight.frequencyBinCount);
self.analyserRight.getByteFrequencyData(arrayRight);
var averageRight = getAverageVolume(arrayRight);
var grd=self.ctx.createLinearGradient(0,0,0,250);
grd.addColorStop(0,"red");
grd.addColorStop(0.2,"orange");
grd.addColorStop(1,"green");
self.ctx.fillStyle = grd;
var top = 400 - (averageLeft * 4);
topRight = 400 - (averageRight * 4);
self.ctx.fillRect(70,top,50,400);
self.ctx.fillRect(10,topRight,50,400 );
}
};
function getAverageVolume(array) {
var values = 0;
var average;
var length = array.length;
for (var i = 0; i < length; i++) {
values += array[i];
}
average = values / length;
return average;
};
Daw.prototype.loadSources = function() {
}
function Transport(ctx,daw) {
var start = offset = elapsed = 0,
play = document.getElementById('play'),
pause = document.getElementById('pause'),
source,
playing,
buffer,
duration;
buffer = getBuffer(ctx,function(buffer) {
duration = buffer.duration * 1000,
play.addEventListener('click', function() {
source = ctx.createBufferSource();
currMin = currSecs = 0;
watchStart();
console.log('wewe');
source.buffer = buffer;
// connect the source to the analyser and the splitter
source.connect(daw.splitter)
daw.splitter.connect(daw.analyserLeft,0,0);
daw.splitter.connect(daw.analyserRight,1,0);
daw.analyserLeft.connect(daw.javascriptNode)
source.connect(ctx.destination);
source.start(0);
});
});
};
function init() {
new Daw();
};
window.addEventListener('load', init, false);
})();
</script>
</body>
</html>