OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Audio test utilities. | 5 // Audio test utilities. |
6 | 6 |
7 // GetStats reports audio output energy in the [0, 32768] range. | 7 // GetStats reports audio output energy in the [0, 32768] range. |
8 var MAX_AUDIO_OUTPUT_ENERGY = 32768; | 8 var MAX_AUDIO_OUTPUT_ENERGY = 32768; |
9 | 9 |
10 // Queries WebRTC stats on |peerConnection| to find out whether audio is playing | 10 // Queries WebRTC stats on |peerConnection| to find out whether audio is playing |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 127 |
128 var expectedPeaks = 3; | 128 var expectedPeaks = 3; |
129 console.log(numPeaks + '/' + expectedPeaks + ' signal peaks identified.'); | 129 console.log(numPeaks + '/' + expectedPeaks + ' signal peaks identified.'); |
130 return numPeaks >= expectedPeaks; | 130 return numPeaks >= expectedPeaks; |
131 } | 131 } |
132 | 132 |
133 /** | 133 /** |
134 * @private | 134 * @private |
135 */ | 135 */ |
136 function identifySilence_(samples) { | 136 function identifySilence_(samples) { |
| 137 // Look at the last 10K samples only to make detection a bit faster. |
| 138 var window = samples.slice(-10000); |
| 139 |
137 var average = 0; | 140 var average = 0; |
138 for (var i = 0; i < samples.length; ++i) | 141 for (var i = 0; i < window.length; ++i) |
139 average += samples[i] / samples.length; | 142 average += window[i] / window.length; |
140 | 143 |
141 // If silent (like when muted), we should get very near zero audio level. | 144 // If silent (like when muted), we should get very near zero audio level. |
142 console.log('Average audio level: ' + average); | 145 console.log('Average audio level (last 10k samples): ' + average); |
143 | 146 |
144 return average < 0.01 * MAX_AUDIO_OUTPUT_ENERGY; | 147 return average < 0.01 * MAX_AUDIO_OUTPUT_ENERGY; |
145 } | 148 } |
146 | 149 |
147 /** | 150 /** |
148 * @private | 151 * @private |
149 */ | 152 */ |
150 function getAudioLevelFromStats_(response) { | 153 function getAudioLevelFromStats_(response) { |
151 var reports = response.result(); | 154 var reports = response.result(); |
152 var audioOutputLevels = []; | 155 var audioOutputLevels = []; |
153 for (var i = 0; i < reports.length; ++i) { | 156 for (var i = 0; i < reports.length; ++i) { |
154 var report = reports[i]; | 157 var report = reports[i]; |
155 if (report.names().indexOf('audioOutputLevel') != -1) { | 158 if (report.names().indexOf('audioOutputLevel') != -1) { |
156 audioOutputLevels.push(report.stat('audioOutputLevel')); | 159 audioOutputLevels.push(report.stat('audioOutputLevel')); |
157 } | 160 } |
158 } | 161 } |
159 return audioOutputLevels; | 162 return audioOutputLevels; |
160 } | 163 } |
OLD | NEW |