tenfourfox/dom/media/webaudio/test/test_AudioBuffer.html

106 lines
3.4 KiB
HTML
Raw Normal View History

2017-04-19 07:56:45 +00:00
<!DOCTYPE HTML>
<html>
<head>
<title>Test whether we can create an AudioContext interface</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script src="webaudio.js" type="text/javascript"></script>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
var context = new AudioContext();
var buffer = context.createBuffer(2, 2048, context.sampleRate);
SpecialPowers.gc(); // Make sure that our channels are accessible after GC
ok(buffer, "Buffer was allocated successfully");
is(buffer.sampleRate, context.sampleRate, "Correct sample rate");
is(buffer.length, 2048, "Correct length");
ok(Math.abs(buffer.duration - 2048 / context.sampleRate) < 0.0001, "Correct duration");
is(buffer.numberOfChannels, 2, "Correct number of channels");
for (var i = 0; i < buffer.numberOfChannels; ++i) {
var buf = buffer.getChannelData(i);
ok(buf, "Buffer index " + i + " exists");
ok(buf instanceof Float32Array, "Result is a typed array");
is(buf.length, buffer.length, "Correct length");
var foundNonZero = false;
for (var j = 0; j < buf.length; ++j) {
if (buf[j] != 0) {
foundNonZero = true;
break;
}
buf[j] = j;
}
ok(!foundNonZero, "Buffer " + i + " should be initialized to 0");
}
// Now test copying the channel data out of a normal buffer
var copy = new Float32Array(100);
buffer.copyFromChannel(copy, 0, 1024);
for (var i = 0; i < copy.length; ++i) {
is(copy[i], 1024 + i, "Correct sample");
}
// Test copying the channel data out of a playing buffer
var srcNode = context.createBufferSource();
srcNode.buffer = buffer;
srcNode.start(0);
copy = new Float32Array(100);
buffer.copyFromChannel(copy, 0, 1024);
for (var i = 0; i < copy.length; ++i) {
is(copy[i], 1024 + i, "Correct sample");
}
// Test copying to the channel data
var newData = new Float32Array(200);
buffer.copyToChannel(newData, 0, 100);
var changedData = buffer.getChannelData(0);
for (var i = 0; i < changedData.length; ++i) {
if (i < 100 || i >= 300) {
is(changedData[i], i, "Untouched sample");
} else {
is(changedData[i], 0, "Correct sample");
}
}
// Now, neuter the array buffer
var worker = new Worker("audioBufferSourceNodeNeutered_worker.js");
var data = buffer.getChannelData(0).buffer;
worker.postMessage(data, [data]);
SpecialPowers.gc();
expectException(function() {
buffer.copyFromChannel(copy, 0, 1024);
}, DOMException.INDEX_SIZE_ERR);
expectException(function() {
buffer.copyToChannel(newData, 0, 100);
}, DOMException.INDEX_SIZE_ERR);
expectException(function() {
context.createBuffer(2, 2048, 7999);
}, DOMException.INDEX_SIZE_ERR);
expectException(function() {
context.createBuffer(2, 2048, 192001);
}, DOMException.INDEX_SIZE_ERR);
context.createBuffer(2, 2048, 8000); // no exception
context.createBuffer(2, 2048, 192000); // no exception
context.createBuffer(32, 2048, 48000); // no exception
// Null length
expectException(function() {
context.createBuffer(2, 0, 48000);
}, DOMException.INDEX_SIZE_ERR);
// Null number of channels
expectException(function() {
context.createBuffer(0, 2048, 48000);
}, DOMException.INDEX_SIZE_ERR);
SimpleTest.finish();
});
</script>
</pre>
</body>
</html>