Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LocalStatsCollector.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Provides statistics for the local stream.
  3. */
  4. var RTCBrowserType = require('../RTC/RTCBrowserType');
  5. /**
  6. * Size of the webaudio analyzer buffer.
  7. * @type {number}
  8. */
  9. var WEBAUDIO_ANALYZER_FFT_SIZE = 2048;
  10. /**
  11. * Value of the webaudio analyzer smoothing time parameter.
  12. * @type {number}
  13. */
  14. var WEBAUDIO_ANALYZER_SMOOTING_TIME = 0.8;
  15. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  16. var context = null;
  17. if(window.AudioContext) {
  18. context = new AudioContext();
  19. context.suspend();
  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. context.resume();
  78. var analyser = context.createAnalyser();
  79. analyser.smoothingTimeConstant = WEBAUDIO_ANALYZER_SMOOTING_TIME;
  80. analyser.fftSize = WEBAUDIO_ANALYZER_FFT_SIZE;
  81. var source = context.createMediaStreamSource(this.stream);
  82. source.connect(analyser);
  83. var self = this;
  84. this.intervalId = setInterval(
  85. function () {
  86. var array = new Uint8Array(analyser.frequencyBinCount);
  87. analyser.getByteTimeDomainData(array);
  88. var audioLevel = timeDomainDataToAudioLevel(array);
  89. if (audioLevel != self.audioLevel) {
  90. self.audioLevel = animateLevel(audioLevel, self.audioLevel);
  91. self.callback(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;