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

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