Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AudioLevels.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* global interfaceConfig */
  2. import UIUtil from "../util/UIUtil";
  3. /**
  4. * Responsible for drawing audio levels.
  5. */
  6. const AudioLevels = {
  7. /**
  8. * The number of dots.
  9. *
  10. * IMPORTANT: functions below assume that this is an odd number.
  11. */
  12. _AUDIO_LEVEL_DOTS: 5,
  13. /**
  14. * Creates the audio level indicator span element.
  15. *
  16. * IMPORTANT: This function assumes that the number of dots is an
  17. * odd number.
  18. *
  19. * @return {Element} the document element representing audio levels
  20. */
  21. createThumbnailAudioLevelIndicator() {
  22. let audioSpan = document.createElement('span');
  23. audioSpan.className = 'audioindicator';
  24. this.sideDotsCount = Math.floor(this._AUDIO_LEVEL_DOTS/2);
  25. for (let i = 0; i < this._AUDIO_LEVEL_DOTS; i++) {
  26. let audioDot = document.createElement('span');
  27. // The median index will be equal to the number of dots on each
  28. // side.
  29. if (i === this.sideDotsCount)
  30. audioDot.className = "audiodot-middle";
  31. else
  32. audioDot.className = (i < this.sideDotsCount)
  33. ? "audiodot-top"
  34. : "audiodot-bottom";
  35. audioSpan.appendChild(audioDot);
  36. }
  37. return audioSpan;
  38. },
  39. /**
  40. * Updates the audio level UI for the given id.
  41. *
  42. * @param {string} id id of the user for whom we draw the audio level
  43. * @param {number} audioLevel the newAudio level to render
  44. */
  45. updateThumbnailAudioLevel (id, audioLevel) {
  46. // First make sure we are sensitive enough.
  47. audioLevel *= 1.2;
  48. audioLevel = Math.min(audioLevel, 1);
  49. // Let's now stretch the audio level over the number of dots we have.
  50. let stretchedAudioLevel = (this.sideDotsCount + 1) * audioLevel;
  51. let dotLevel = 0.0;
  52. for (let i = 0; i < (this.sideDotsCount + 1); i++) {
  53. dotLevel = Math.min(1, Math.max(0, (stretchedAudioLevel - i)));
  54. this._setDotLevel(id, i, dotLevel);
  55. }
  56. },
  57. /**
  58. * Fills the dot(s) with the specified "index", with as much opacity as
  59. * indicated by "opacity".
  60. *
  61. * @param {string} elementID the parent audio indicator span element
  62. * @param {number} index the index of the dots to fill, where 0 indicates
  63. * the middle dot and the following increments point toward the
  64. * corresponding pair of dots.
  65. * @param {number} opacity the opacity to set for the specified dot.
  66. */
  67. _setDotLevel(elementID, index, opacity) {
  68. let audioSpan = document.getElementById(elementID)
  69. .getElementsByClassName("audioindicator");
  70. // Make sure the audio span is still around.
  71. if (audioSpan && audioSpan.length > 0)
  72. audioSpan = audioSpan[0];
  73. else
  74. return;
  75. let audioTopDots
  76. = audioSpan.getElementsByClassName("audiodot-top");
  77. let audioDotMiddle
  78. = audioSpan.getElementsByClassName("audiodot-middle");
  79. let audioBottomDots
  80. = audioSpan.getElementsByClassName("audiodot-bottom");
  81. // First take care of the middle dot case.
  82. if (index === 0){
  83. audioDotMiddle[0].style.opacity = opacity;
  84. return;
  85. }
  86. // Index > 0 : we are setting non-middle dots.
  87. index--;
  88. audioBottomDots[index].style.opacity = opacity;
  89. audioTopDots[this.sideDotsCount - index - 1].style.opacity = opacity;
  90. },
  91. /**
  92. * Updates the audio level of the large video.
  93. *
  94. * @param audioLevel the new audio level to set.
  95. */
  96. updateLargeVideoAudioLevel(elementId, audioLevel) {
  97. let element = document.getElementById(elementId);
  98. if(!UIUtil.isVisible(element))
  99. return;
  100. let level = parseFloat(audioLevel);
  101. level = isNaN(level) ? 0 : level;
  102. let shadowElement = element.getElementsByClassName("dynamic-shadow");
  103. if (shadowElement && shadowElement.length > 0)
  104. shadowElement = shadowElement[0];
  105. shadowElement.style.boxShadow = this._updateLargeVideoShadow(level);
  106. },
  107. /**
  108. * Updates the large video shadow effect.
  109. */
  110. _updateLargeVideoShadow (level) {
  111. var scale = 2,
  112. // Internal circle audio level.
  113. int = {
  114. level: level > 0.15 ? 20 : 0,
  115. color: interfaceConfig.AUDIO_LEVEL_PRIMARY_COLOR
  116. },
  117. // External circle audio level.
  118. ext = {
  119. level: (int.level * scale * level + int.level).toFixed(0),
  120. color: interfaceConfig.AUDIO_LEVEL_SECONDARY_COLOR
  121. };
  122. // Internal blur.
  123. int.blur = int.level ? 2 : 0;
  124. // External blur.
  125. ext.blur = ext.level ? 6 : 0;
  126. return [
  127. `0 0 ${ int.blur }px ${ int.level }px ${ int.color }`,
  128. `0 0 ${ ext.blur }px ${ ext.level }px ${ ext.color }`
  129. ].join(', ');
  130. }
  131. };
  132. export default AudioLevels;