|
@@ -6,13 +6,13 @@ var LocalStatsCollector = (function() {
|
6
|
6
|
* Size of the webaudio analizer buffer.
|
7
|
7
|
* @type {number}
|
8
|
8
|
*/
|
9
|
|
- var WEBAUDIO_ANALIZER_FFT_SIZE = 512;
|
|
9
|
+ var WEBAUDIO_ANALIZER_FFT_SIZE = 2048;
|
10
|
10
|
|
11
|
11
|
/**
|
12
|
12
|
* Value of the webaudio analizer smoothing time parameter.
|
13
|
13
|
* @type {number}
|
14
|
14
|
*/
|
15
|
|
- var WEBAUDIO_ANALIZER_SMOOTING_TIME = 0.1;
|
|
15
|
+ var WEBAUDIO_ANALIZER_SMOOTING_TIME = 0.8;
|
16
|
16
|
|
17
|
17
|
/**
|
18
|
18
|
* <tt>LocalStatsCollector</tt> calculates statistics for the local stream.
|
|
@@ -54,12 +54,16 @@ var LocalStatsCollector = (function() {
|
54
|
54
|
this.intervalId = setInterval(
|
55
|
55
|
function () {
|
56
|
56
|
var array = new Uint8Array(analyser.frequencyBinCount);
|
57
|
|
- analyser.getByteFrequencyData(array);
|
58
|
|
- self.audioLevel = FrequencyDataToAudioLevel(array);
|
59
|
|
- self.updateCallback(self);
|
|
57
|
+ analyser.getByteTimeDomainData(array);
|
|
58
|
+ var audioLevel = TimeDomainDataToAudioLevel(array);
|
|
59
|
+ if(audioLevel != self.audioLevel) {
|
|
60
|
+ self.audioLevel = animateLevel(audioLevel, self.audioLevel);
|
|
61
|
+ self.updateCallback(LocalStatsCollectorProto.LOCAL_JID, self.audioLevel);
|
|
62
|
+ }
|
60
|
63
|
},
|
61
|
64
|
this.intervalMilis
|
62
|
65
|
);
|
|
66
|
+
|
63
|
67
|
};
|
64
|
68
|
|
65
|
69
|
/**
|
|
@@ -73,22 +77,55 @@ var LocalStatsCollector = (function() {
|
73
|
77
|
};
|
74
|
78
|
|
75
|
79
|
/**
|
76
|
|
- * Converts frequency data array to audio level.
|
77
|
|
- * @param array the frequency data array.
|
|
80
|
+ * Converts time domain data array to audio level.
|
|
81
|
+ * @param array the time domain data array.
|
78
|
82
|
* @returns {number} the audio level
|
79
|
83
|
*/
|
80
|
|
- var FrequencyDataToAudioLevel = function (array) {
|
|
84
|
+ var TimeDomainDataToAudioLevel = function (samples) {
|
|
85
|
+
|
81
|
86
|
var maxVolume = 0;
|
82
|
87
|
|
83
|
|
- var length = array.length;
|
|
88
|
+ var length = samples.length;
|
84
|
89
|
|
85
|
90
|
for (var i = 0; i < length; i++) {
|
86
|
|
- if (maxVolume < array[i])
|
87
|
|
- maxVolume = array[i];
|
|
91
|
+ if (maxVolume < samples[i])
|
|
92
|
+ maxVolume = samples[i];
|
88
|
93
|
}
|
89
|
94
|
|
90
|
|
- return maxVolume / 255;
|
|
95
|
+ return parseFloat(((maxVolume - 127) / 128).toFixed(3));
|
91
|
96
|
};
|
92
|
97
|
|
|
98
|
+ /**
|
|
99
|
+ * Animates audio level change
|
|
100
|
+ * @param newLevel the new audio level
|
|
101
|
+ * @param lastLevel the last audio level
|
|
102
|
+ * @returns {Number} the audio level to be set
|
|
103
|
+ */
|
|
104
|
+ function animateLevel(newLevel, lastLevel)
|
|
105
|
+ {
|
|
106
|
+ var value = 0;
|
|
107
|
+ var diff = lastLevel - newLevel;
|
|
108
|
+ if(diff > 0.2)
|
|
109
|
+ {
|
|
110
|
+ value = lastLevel - 0.2;
|
|
111
|
+ }
|
|
112
|
+ else if(diff < -0.4)
|
|
113
|
+ {
|
|
114
|
+ value = lastLevel + 0.4;
|
|
115
|
+ }
|
|
116
|
+ else
|
|
117
|
+ {
|
|
118
|
+ value = newLevel;
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ return parseFloat(value.toFixed(3));
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ /**
|
|
125
|
+ * Indicates that this audio level is for local jid.
|
|
126
|
+ * @type {string}
|
|
127
|
+ */
|
|
128
|
+ LocalStatsCollectorProto.LOCAL_JID = 'local';
|
|
129
|
+
|
93
|
130
|
return LocalStatsCollectorProto;
|
94
|
131
|
})();
|