-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
93 lines (77 loc) · 2.5 KB
/
main.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
var audioContext = null;
var meter = null;
var canvasContext = null;
var WIDTH=500;
var HEIGHT=50;
var rafID = null;
const buttonEl = document.querySelector('button');
window.onload = function() {
// grab our canvas
canvasContext = document.getElementById( "meter" ).getContext("2d");
// monkeypatch Web Audio
window.AudioContext = window.AudioContext || window.webkitAudioContext;
// grab an audio context
audioContext = new AudioContext();
// Attempt to get audio input
try {
// monkeypatch getUserMedia
navigator.getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
// ask for an audio input
navigator.getUserMedia(
{
"audio": {
"mandatory": {
"googEchoCancellation": "false",
"googAutoGainControl": "false",
"googNoiseSuppression": "false",
"googHighpassFilter": "false"
},
"optional": []
},
video: { width: 1000, height: 500 }
}, gotStream, didntGetStream);
} catch (e) {
alert('getUserMedia threw exception :' + e);
}
}
// НЕУДАЧА
function didntGetStream() {
alert('Stream generation failed. Do you have at least one camera connected?');
}
var mediaStreamSource = null;
function gotStream(stream) {
// Create an AudioNode from the stream.
var video = document.querySelector('video');
video.srcObject = stream;
mediaStreamSource = audioContext.createMediaStreamSource(video.srcObject);
video.play();
// Create a new volume meter and connect it.
meter = createAudioMeter(audioContext);
mediaStreamSource.connect(meter);
// kick off the visual updating
drawLoop();
}
function drawLoop( time ) {
// clear the background
canvasContext.clearRect(0,0,WIDTH,HEIGHT);
// check if we're currently clipping
if (meter.checkClipping())
canvasContext.fillStyle = "red";
else
canvasContext.fillStyle = "green";
// draw a bar based on the current volume
canvasContext.fillRect(0, 5, meter.volume*WIDTH*2.4, HEIGHT);
// set up the next visual callback
rafID = window.requestAnimationFrame( drawLoop );
}
buttonEl.addEventListener('click', function() {
if (audioContext.state === 'suspended') {
audioContext.resume();
}
if (audioContext.state === 'running') {
audioContext.suspend();
}
})