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
3 // found in the LICENSE file.
5 // Audio test utilities.
7 // Gathers |numSamples| samples at |frequency| number of times per second and
8 // calls back |callback| with an array with numbers in the [0, 32768] range.
9 function gatherAudioLevelSamples(peerConnection
, numSamples
, frequency
,
11 console
.log('Gathering ' + numSamples
+ ' audio samples...');
12 var audioLevelSamples
= []
13 var gatherSamples
= setInterval(function() {
14 peerConnection
.getStats(function(response
) {
15 audioLevelSamples
.push(getAudioLevelFromStats_(response
));
16 if (audioLevelSamples
.length
== numSamples
) {
17 console
.log('Gathered all samples.');
18 clearInterval(gatherSamples
);
19 callback(audioLevelSamples
);
25 // Tries to identify the beep-every-second signal generated by the fake audio
26 // media/audio/fake_audio_input_stream.cc. Fails the test if we can't see a
28 function verifyAudioIsPlaying(samples
) {
30 for (var i
= 0; i
< samples
.length
; ++i
)
31 average
+= samples
[i
] / samples
.length
;
34 for (var i
= 0; i
< samples
.length
; ++i
)
35 largest
= Math
.max(largest
, samples
[i
]);
37 console
.log('Average audio level: ' + average
+ ', largest: ' + largest
);
39 // TODO(phoglund): Make a more sophisticated curve-fitting algorithm. We want
40 // to see a number of peaks with relative silence between them. The following
41 // seems to work fine on a nexus 7.
42 if (average
< 3000 || average
> 8000)
43 throw 'Unexpected avg audio level: got ' + average
+ ', expected it ' +
44 'to be 4000 < avg < 8000.'
46 throw 'Too low max audio level: got ' + largest
+ ', expected > 30000.';
49 // If silent (like when muted), we should get very near zero audio level.
50 function verifyIsSilent(samples
) {
52 for (var i
= 0; i
< samples
.length
; ++i
)
53 average
+= samples
[i
] / samples
.length
;
55 console
.log('Average audio level: ' + average
);
57 throw 'Expected silence, but avg audio level was ' + average
;
63 function getAudioLevelFromStats_(response
) {
64 var reports
= response
.result();
65 var audioOutputLevels
= [];
66 for (var i
= 0; i
< reports
.length
; ++i
) {
67 var report
= reports
[i
];
68 if (report
.names().indexOf('audioOutputLevel') != -1) {
69 audioOutputLevels
.push(report
.stat('audioOutputLevel'));
72 // Should only be one audio level reported, otherwise we get confused.
73 expectEquals(1, audioOutputLevels
.length
);
75 return audioOutputLevels
[0];