| lastNoise: {} | |||||
| function isNoisy(username) { | |||||
| const now = +new Date() | |||||
| const then = VideoShare.lastNoise[username] || 0 | |||||
| console.log(now - then) | |||||
| } | |||||
| function notifySound(stream) { | |||||
| const audioContext = new AudioContext() | |||||
| const analyser = audioContext.createAnalyser() | |||||
| const microphone = audioContext.createMediaStreamSource(stream) | |||||
| const scriptProcessor = audioContext.createScriptProcessor(0, 1, 1) | |||||
| analyser.smoothingTimeConstant = 0.3 | |||||
| analyser.fftSize = 1024 | |||||
| microphone.connect(analyser) | |||||
| analyser.connect(scriptProcessor) | |||||
| scriptProcessor.connect(audioContext.destination) | |||||
| scriptProcessor.onaudioprocess = () => { | |||||
| const array = new Uint8Array(analyser.frequencyBinCount) | |||||
| analyser.getByteFrequencyData(array) | |||||
| const isLoud = array.reduce((a, b) => a + b) / array.length | 0 | |||||
| if(isLoud) { | |||||
| wire({kind: 'noise'}) | |||||
| } | |||||
| } | |||||
| } | |||||
| function showNoise() { | |||||
| const now = +new Date() | |||||
| for([username, ts] of Object.entries(VideoShare.lastNoise)) { | |||||
| const isNoisy = (now - ts) < 100 | |||||
| const query = `.label-${username}` | |||||
| const el = document.querySelector(query) | |||||
| if(el && isNoisy) { | |||||
| el.style.color = 'lime' | |||||
| } | |||||
| else if (el) { | |||||
| el.style.color = 'white' | |||||
| } | |||||
| } | |||||
| } | |||||
| addEventListener('noise', ({detail: {source}}) => { | |||||
| VideoShare.lastNoise[source] = +new Date() | |||||
| }) | |||||
| addEventListener('load', () => { | |||||
| setInterval(showNoise, 100) | |||||
| doNotLog.add('noise') | |||||
| }) | |||||
| // VideoShare.notifySound(stream) |