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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* global config, AudioContext */
  2. /**
  3. * Provides statistics for the local stream.
  4. */
  5. var RTCBrowserType = require('../RTC/RTCBrowserType');
  6. /**
  7. * Size of the webaudio analyzer buffer.
  8. * @type {number}
  9. */
  10. var WEBAUDIO_ANALYZER_FFT_SIZE = 2048;
  11. /**
  12. * Value of the webaudio analyzer smoothing time parameter.
  13. * @type {number}
  14. */
  15. var WEBAUDIO_ANALYZER_SMOOTING_TIME = 0.8;
  16. /**
  17. * Converts time domain data array to audio level.
  18. * @param samples the time domain data array.
  19. * @returns {number} the audio level
  20. */
  21. function timeDomainDataToAudioLevel(samples) {
  22. var maxVolume = 0;
  23. var length = samples.length;
  24. for (var i = 0; i < length; i++) {
  25. if (maxVolume < samples[i])
  26. maxVolume = samples[i];
  27. }
  28. return parseFloat(((maxVolume - 127) / 128).toFixed(3));
  29. }
  30. /**
  31. * Animates audio level change
  32. * @param newLevel the new audio level
  33. * @param lastLevel the last audio level
  34. * @returns {Number} the audio level to be set
  35. */
  36. function animateLevel(newLevel, lastLevel) {
  37. var value = 0;
  38. var diff = lastLevel - newLevel;
  39. if(diff > 0.2) {
  40. value = lastLevel - 0.2;
  41. }
  42. else if(diff < -0.4) {
  43. value = lastLevel + 0.4;
  44. }
  45. else {
  46. value = newLevel;
  47. }
  48. return parseFloat(value.toFixed(3));
  49. }
  50. /**
  51. * <tt>LocalStatsCollector</tt> calculates statistics for the local stream.
  52. *
  53. * @param stream the local stream
  54. * @param interval stats refresh interval given in ms.
  55. * @constructor
  56. */
  57. function LocalStatsCollector(stream, interval,
  58. statisticsService, eventEmitter) {
  59. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  60. this.stream = stream;
  61. this.intervalId = null;
  62. this.intervalMilis = interval;
  63. this.eventEmitter = eventEmitter;
  64. this.audioLevel = 0;
  65. this.statisticsService = statisticsService;
  66. }
  67. /**
  68. * Starts the collecting the statistics.
  69. */
  70. LocalStatsCollector.prototype.start = function () {
  71. if (config.disableAudioLevels || !window.AudioContext ||
  72. RTCBrowserType.isTemasysPluginUsed())
  73. return;
  74. var context = new AudioContext();
  75. var analyser = context.createAnalyser();
  76. analyser.smoothingTimeConstant = WEBAUDIO_ANALYZER_SMOOTING_TIME;
  77. analyser.fftSize = WEBAUDIO_ANALYZER_FFT_SIZE;
  78. var source = context.createMediaStreamSource(this.stream);
  79. source.connect(analyser);
  80. var self = this;
  81. this.intervalId = setInterval(
  82. function () {
  83. var array = new Uint8Array(analyser.frequencyBinCount);
  84. analyser.getByteTimeDomainData(array);
  85. var audioLevel = timeDomainDataToAudioLevel(array);
  86. if (audioLevel != self.audioLevel) {
  87. self.audioLevel = animateLevel(audioLevel, self.audioLevel);
  88. self.eventEmitter.emit(
  89. "statistics.audioLevel",
  90. self.statisticsService.LOCAL_JID,
  91. self.audioLevel);
  92. }
  93. },
  94. this.intervalMilis
  95. );
  96. };
  97. /**
  98. * Stops collecting the statistics.
  99. */
  100. LocalStatsCollector.prototype.stop = function () {
  101. if (this.intervalId) {
  102. clearInterval(this.intervalId);
  103. this.intervalId = null;
  104. }
  105. };
  106. module.exports = LocalStatsCollector;