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.

LocalStatsCollector.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * Provides statistics for the local stream.
  18. */
  19. /**
  20. * Size of the webaudio analizer buffer.
  21. * @type {number}
  22. */
  23. var WEBAUDIO_ANALIZER_FFT_SIZE = 2048;
  24. /**
  25. * Value of the webaudio analizer smoothing time parameter.
  26. * @type {number}
  27. */
  28. var WEBAUDIO_ANALIZER_SMOOTING_TIME = 0.8;
  29. /**
  30. * Converts time domain data array to audio level.
  31. * @param array the time domain data array.
  32. * @returns {number} the audio level
  33. */
  34. function timeDomainDataToAudioLevel(samples) {
  35. var maxVolume = 0;
  36. var length = samples.length;
  37. for (var i = 0; i < length; i++) {
  38. if (maxVolume < samples[i])
  39. maxVolume = samples[i];
  40. }
  41. return parseFloat(((maxVolume - 127) / 128).toFixed(3));
  42. };
  43. /**
  44. * Animates audio level change
  45. * @param newLevel the new audio level
  46. * @param lastLevel the last audio level
  47. * @returns {Number} the audio level to be set
  48. */
  49. function animateLevel(newLevel, lastLevel)
  50. {
  51. var value = 0;
  52. var diff = lastLevel - newLevel;
  53. if(diff > 0.2)
  54. {
  55. value = lastLevel - 0.2;
  56. }
  57. else if(diff < -0.4)
  58. {
  59. value = lastLevel + 0.4;
  60. }
  61. else
  62. {
  63. value = newLevel;
  64. }
  65. return parseFloat(value.toFixed(3));
  66. }
  67. /**
  68. * <tt>LocalStatsCollector</tt> calculates statistics for the local stream.
  69. *
  70. * @param stream the local stream
  71. * @param interval stats refresh interval given in ms.
  72. * @param {function(LocalStatsCollector)} updateCallback the callback called on stats
  73. * update.
  74. * @constructor
  75. */
  76. function LocalStatsCollector(stream, interval, statisticsService, eventEmitter) {
  77. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  78. this.stream = stream;
  79. this.intervalId = null;
  80. this.intervalMilis = interval;
  81. this.eventEmitter = eventEmitter;
  82. this.audioLevel = 0;
  83. this.statisticsService = statisticsService;
  84. }
  85. /**
  86. * Starts the collecting the statistics.
  87. */
  88. LocalStatsCollector.prototype.start = function () {
  89. if (config.disableAudioLevels || !window.AudioContext)
  90. return;
  91. var context = new AudioContext();
  92. var analyser = context.createAnalyser();
  93. analyser.smoothingTimeConstant = WEBAUDIO_ANALIZER_SMOOTING_TIME;
  94. analyser.fftSize = WEBAUDIO_ANALIZER_FFT_SIZE;
  95. var source = context.createMediaStreamSource(this.stream);
  96. source.connect(analyser);
  97. var self = this;
  98. this.intervalId = setInterval(
  99. function () {
  100. var array = new Uint8Array(analyser.frequencyBinCount);
  101. analyser.getByteTimeDomainData(array);
  102. var audioLevel = timeDomainDataToAudioLevel(array);
  103. if(audioLevel != self.audioLevel) {
  104. self.audioLevel = animateLevel(audioLevel, self.audioLevel);
  105. self.eventEmitter.emit(
  106. "statistics.audioLevel",
  107. self.statisticsService.LOCAL_JID,
  108. self.audioLevel);
  109. }
  110. },
  111. this.intervalMilis
  112. );
  113. };
  114. /**
  115. * Stops collecting the statistics.
  116. */
  117. LocalStatsCollector.prototype.stop = function () {
  118. if (this.intervalId) {
  119. clearInterval(this.intervalId);
  120. this.intervalId = null;
  121. }
  122. };
  123. module.exports = LocalStatsCollector;