mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-20 08:45:46 +00:00
Bug 1339438 - Fix the test that checks that MediaElementAudioSourceNode are working correctly. r=pehrsons
MozReview-Commit-ID: Fazp4QsbxHk
This commit is contained in:
parent
fa67a95aa1
commit
f7e4a455e2
@ -27,6 +27,7 @@ support-files =
|
|||||||
ting-44.1k-2ch.wav
|
ting-44.1k-2ch.wav
|
||||||
ting-48k-1ch.wav
|
ting-48k-1ch.wav
|
||||||
ting-48k-2ch.wav
|
ting-48k-2ch.wav
|
||||||
|
sine-440-10s.opus
|
||||||
webaudio.js
|
webaudio.js
|
||||||
|
|
||||||
[test_analyserNode.html]
|
[test_analyserNode.html]
|
||||||
|
BIN
dom/media/webaudio/test/sine-440-10s.opus
Normal file
BIN
dom/media/webaudio/test/sine-440-10s.opus
Normal file
Binary file not shown.
@ -10,27 +10,6 @@
|
|||||||
<pre id="test">
|
<pre id="test">
|
||||||
<script class="testbody" type="text/javascript">
|
<script class="testbody" type="text/javascript">
|
||||||
SimpleTest.waitForExplicitFinish();
|
SimpleTest.waitForExplicitFinish();
|
||||||
// Get an Opus file containing a sine wave at maximum amplitude, of duration
|
|
||||||
// `lengthSeconds`, and of frequency `frequency`.
|
|
||||||
function getSineWaveFile(frequency, lengthSeconds, callback) {
|
|
||||||
var chunks = [];
|
|
||||||
var off = new OfflineAudioContext(1, lengthSeconds * 48000, 48000);
|
|
||||||
var osc = off.createOscillator();
|
|
||||||
var rec = new MediaRecorder(osc);
|
|
||||||
rec.ondataavailable = function(e) {
|
|
||||||
chunks.push(e.data);
|
|
||||||
};
|
|
||||||
rec.onstop = function(e) {
|
|
||||||
var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' });
|
|
||||||
callback(blob);
|
|
||||||
}
|
|
||||||
osc.frequency.value = 1.0;
|
|
||||||
osc.start();
|
|
||||||
rec.start();
|
|
||||||
off.startRendering().then(function(buffer) {
|
|
||||||
rec.stop();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function binIndexForFrequency(frequency, analyser) {
|
function binIndexForFrequency(frequency, analyser) {
|
||||||
return 1 + Math.round(frequency *
|
return 1 + Math.round(frequency *
|
||||||
@ -76,60 +55,74 @@ function checkFrequency(an) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// On the other hand, we should find a peak at 440Hz. Our sine wave is not
|
||||||
|
// attenuated, we're expecting the peak to reach 0dBFs.
|
||||||
|
index = binIndexForFrequency(440, an);
|
||||||
|
info("energy at 440: " + frequencyArray[index] + ", threshold " + (an.maxDecibels - 10));
|
||||||
|
if (frequencyArray[index] < (an.maxDecibels - 10)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function test() {
|
var audioElement = new Audio();
|
||||||
getSineWaveFile(440, 1, (blob) => {
|
audioElement.src = 'sine-440-10s.opus'
|
||||||
var audioElement = new Audio();
|
audioElement.loop = true;
|
||||||
audioElement.src = URL.createObjectURL(blob);
|
var ac = new AudioContext();
|
||||||
audioElement.loop = true;
|
var mediaElementSource = ac.createMediaElementSource(audioElement);
|
||||||
var ac = new AudioContext();
|
var an = ac.createAnalyser();
|
||||||
var mediaElementSource = ac.createMediaElementSource(audioElement);
|
frequencyArray = new Float32Array(an.frequencyBinCount);
|
||||||
var an = ac.createAnalyser();
|
|
||||||
frequencyArray = new Float32Array(an.frequencyBinCount);
|
|
||||||
|
|
||||||
// Uncomment this to check what the analyser is doing.
|
// Uncomment this to check what the analyser is doing.
|
||||||
// debugCanvas(an);
|
// debugCanvas(an);
|
||||||
|
|
||||||
mediaElementSource.connect(an);
|
mediaElementSource.connect(an)
|
||||||
|
|
||||||
audioElement.play();
|
audioElement.play();
|
||||||
// We want to check the we have the expected audio for at least one loop of
|
// We want to check the we have the expected audio for at least two loop of
|
||||||
// the HTMLMediaElement. The file is one second, and we use the default FFT
|
// the HTMLMediaElement, piped into an AudioContext. The file is ten seconds,
|
||||||
// size.
|
// and we use the default FFT size.
|
||||||
var lastCurrentTime = 0;
|
var lastCurrentTime = 0;
|
||||||
var looped = false;
|
var loopCount = 0;
|
||||||
audioElement.onplaying = function() {
|
audioElement.onplaying = function() {
|
||||||
audioElement.ontimeupdate = function() {
|
audioElement.ontimeupdate = function() {
|
||||||
if (checkFrequency(an)) {
|
// We don't run the analysis when close to loop point or at the
|
||||||
ok(true, "Found correct audio signal during analysis");
|
// beginning, since looping is not seamless, there could be an
|
||||||
dump(lastCurrentTime + " " + audioElement.currentTime + "\n");
|
// unpredictable amount of silence
|
||||||
if (lastCurrentTime > audioElement.currentTime) {
|
var rv = checkFrequency(an);
|
||||||
if (looped) {
|
info("currentTime: " + audioElement.currentTime);
|
||||||
audioElement.ontimeupdate = null;
|
if (audioElement.currentTime < 4 ||
|
||||||
audioElement.onplaying = null;
|
audioElement.currentTIme > 8){
|
||||||
SimpleTest.finish()
|
return;
|
||||||
}
|
}
|
||||||
lastCurrentTime = audioElement.currentTime;
|
if (!rv) {
|
||||||
looped = true;
|
ok(false, "Found unexpected noise during analysis.");
|
||||||
} else {
|
audioElement.ontimeupdate = null;
|
||||||
lastCurrentTime = audioElement.currentTime;
|
audioElement.onplaying = null;
|
||||||
}
|
ac.close();
|
||||||
} else {
|
audioElement.src = '';
|
||||||
ok(false, "Found unexpected noise during analysis.");
|
SimpleTest.finish()
|
||||||
audioElement.ontimeupdate = null;
|
return;
|
||||||
audioElement.onplaying = null;
|
}
|
||||||
ac.close();
|
ok(true, "Found correct audio signal during analysis");
|
||||||
audioElement.src = '';
|
info(lastCurrentTime + " " + audioElement.currentTime);
|
||||||
SimpleTest.finish()
|
if (lastCurrentTime > audioElement.currentTime) {
|
||||||
}
|
info("loopCount: " + loopCount);
|
||||||
}
|
if (loopCount > 1) {
|
||||||
|
audioElement.ontimeupdate = null;
|
||||||
|
audioElement.onplaying = null;
|
||||||
|
ac.close();
|
||||||
|
audioElement.src = '';
|
||||||
|
SimpleTest.finish();
|
||||||
}
|
}
|
||||||
});
|
lastCurrentTime = audioElement.currentTime;
|
||||||
|
loopCount++;
|
||||||
|
} else {
|
||||||
|
lastCurrentTime = audioElement.currentTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SpecialPowers.pushPrefEnv(
|
|
||||||
{'set': [['media.recorder.audio_node.enabled', true]]}, test);
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
Reference in New Issue
Block a user