-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
60 lines (53 loc) · 1.58 KB
/
index.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
/*
TODO:
- add vu meter of microphone
- add optional warhol / treshold effect on video. or just css filters
*/
async function initVideo() {
const props = {
video: true,
};
try {
const stream = await navigator.mediaDevices.getUserMedia(props);
document.querySelector('#camera-feed').srcObject = stream;
}
catch(e) {
console.warn('no video');
document.querySelector('#camera').innerHTML = 'Camera not available';
}
}
function sampleMicLevel(stream) {
const audioContext = new AudioContext();
const mediaStreamAudioSourceNode = audioContext.createMediaStreamSource(stream);
const analyserNode = audioContext.createAnalyser();
mediaStreamAudioSourceNode.connect(analyserNode);
const pcmData = new Float32Array(analyserNode.fftSize);
const analyse = () => {
analyserNode.getFloatTimeDomainData(pcmData);
let sumSquares = 0.0;
for (const amplitude of pcmData) {
sumSquares += amplitude*amplitude;
}
const level = Math.sqrt(sumSquares / pcmData.length);
document.querySelector('#audio-level').innerHTML = parseInt(level * 100);
// volumeMeterEl.value = Math.sqrt(sumSquares / pcmData.length);
};
setInterval(analyse, 200);
}
async function initAudio() {
const props = { audio: true };
try {
const audio = await navigator.mediaDevices.getUserMedia(props);
console.log('got audio');
sampleMicLevel(audio);
}
catch(e) {
console.warn('no audio');
document.querySelector('#audio').innerHTML = 'Microphone not available';
}
}
function init() {
initVideo();
initAudio();
}
window.onload = init;