You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5KB

  1. lastNoise: {}
  2. function isNoisy(username) {
  3. const now = +new Date()
  4. const then = VideoShare.lastNoise[username] || 0
  5. console.log(now - then)
  6. }
  7. function notifySound(stream) {
  8. const audioContext = new AudioContext()
  9. const analyser = audioContext.createAnalyser()
  10. const microphone = audioContext.createMediaStreamSource(stream)
  11. const scriptProcessor = audioContext.createScriptProcessor(0, 1, 1)
  12. analyser.smoothingTimeConstant = 0.3
  13. analyser.fftSize = 1024
  14. microphone.connect(analyser)
  15. analyser.connect(scriptProcessor)
  16. scriptProcessor.connect(audioContext.destination)
  17. scriptProcessor.onaudioprocess = () => {
  18. const array = new Uint8Array(analyser.frequencyBinCount)
  19. analyser.getByteFrequencyData(array)
  20. const isLoud = array.reduce((a, b) => a + b) / array.length | 0
  21. if(isLoud) {
  22. wire({kind: 'noise'})
  23. }
  24. }
  25. }
  26. function showNoise() {
  27. const now = +new Date()
  28. for([username, ts] of Object.entries(VideoShare.lastNoise)) {
  29. const isNoisy = (now - ts) < 100
  30. const query = `.label-${username}`
  31. const el = document.querySelector(query)
  32. if(el && isNoisy) {
  33. el.style.color = 'lime'
  34. }
  35. else if (el) {
  36. el.style.color = 'white'
  37. }
  38. }
  39. }
  40. addEventListener('noise', ({detail: {source}}) => {
  41. VideoShare.lastNoise[source] = +new Date()
  42. })
  43. addEventListener('load', () => {
  44. setInterval(showNoise, 100)
  45. doNotLog.add('noise')
  46. })
  47. // VideoShare.notifySound(stream)