-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
121 lines (91 loc) · 3.08 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
window.onerror = function (message, file, line, col, e) {
log(e.message);
return false;
};
function log(message) {
document.querySelector('#log').value = message;
}
var context = new (window.AudioContext || window.webkitAudioContext)();
var osc, canvas, canvasCtx, drawVisual;
window.onload = function () {
var freq = document.querySelector('#freq');
var current = document.querySelector('#current');
current.textContent = freq.value;
function createOscillator() {
osc = context.createOscillator();
osc.type = 'sine';
osc.frequency.value = freq.value;
osc.start();
}
createOscillator();
freq.oninput = function () {
current.textContent = this.value;
};
freq.onchange = function () {
createOscillator();
};
canvas = document.querySelector('#graph');
canvasCtx = canvas.getContext("2d");
document.querySelector('#play').onclick = function () {
var button = this;
osc.connect(context.destination);
button.disabled = true;
setTimeout(function () {
osc.disconnect(context.destination);
button.disabled = false;
}, 1000);
};
document.querySelector('#listen').onclick = function () {
if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({audio: true},
function (stream) {
analyzeStream(stream);
},
function () {
alert('Error capturing audio.');
}
);
} else {
alert('Not supported browser.');
}
};
};
function analyzeStream(stream) {
var micSource = context.createMediaStreamSource(stream);
var analyser = context.createAnalyser();
var filter = createFilter();
micSource.connect(filter);
analyser.fftSize = 1024;
micSource.connect(analyser);
var WIDTH = canvas.width;
var HEIGHT = canvas.height;
var bufferLength = analyser.frequencyBinCount;
var dataArray = new Uint8Array(bufferLength);
canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
function draw() {
drawVisual = requestAnimationFrame(draw);
analyser.getByteFrequencyData(dataArray);
canvasCtx.fillStyle = 'rgb(0, 0, 0)';
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
var barWidth = (WIDTH - bufferLength + 1) / bufferLength;
var barHeight;
var x = 0;
for(var i = 0; i < bufferLength; i++) {
barHeight = dataArray[i];
canvasCtx.fillStyle = 'rgb(' + (barHeight+100) + ',50,50)';
canvasCtx.fillRect(x,HEIGHT-barHeight/2,barWidth,barHeight/2);
x += barWidth + 1;
}
}
draw();
}
function createFilter() {
var filter = context.createBiquadFilter();
filter.frequency.value = 14900;
filter.type = 'highpass';
filter.Q.value = 1;
return filter;
}