content/media/webaudio/test/test_AudioBuffer.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <title>Test whether we can create an AudioContext interface</title>
michael@0 5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 7 </head>
michael@0 8 <body>
michael@0 9 <pre id="test">
michael@0 10 <script src="webaudio.js" type="text/javascript"></script>
michael@0 11 <script class="testbody" type="text/javascript">
michael@0 12
michael@0 13 SimpleTest.waitForExplicitFinish();
michael@0 14 addLoadEvent(function() {
michael@0 15 var context = new AudioContext();
michael@0 16 var buffer = context.createBuffer(2, 2048, context.sampleRate);
michael@0 17 SpecialPowers.gc(); // Make sure that our channels are accessible after GC
michael@0 18 ok(buffer, "Buffer was allocated successfully");
michael@0 19 is(buffer.sampleRate, context.sampleRate, "Correct sample rate");
michael@0 20 is(buffer.length, 2048, "Correct length");
michael@0 21 ok(Math.abs(buffer.duration - 2048 / context.sampleRate) < 0.0001, "Correct duration");
michael@0 22 is(buffer.numberOfChannels, 2, "Correct number of channels");
michael@0 23 for (var i = 0; i < buffer.numberOfChannels; ++i) {
michael@0 24 var buf = buffer.getChannelData(i);
michael@0 25 ok(buf, "Buffer index " + i + " exists");
michael@0 26 ok(buf instanceof Float32Array, "Result is a typed array");
michael@0 27 is(buf.length, buffer.length, "Correct length");
michael@0 28 var foundNonZero = false;
michael@0 29 for (var j = 0; j < buf.length; ++j) {
michael@0 30 if (buf[j] != 0) {
michael@0 31 foundNonZero = true;
michael@0 32 break;
michael@0 33 }
michael@0 34 buf[j] = j;
michael@0 35 }
michael@0 36 ok(!foundNonZero, "Buffer " + i + " should be initialized to 0");
michael@0 37 }
michael@0 38
michael@0 39 // Now test copying the channel data out of a normal buffer
michael@0 40 var copy = new Float32Array(100);
michael@0 41 buffer.copyFromChannel(copy, 0, 1024);
michael@0 42 for (var i = 0; i < copy.length; ++i) {
michael@0 43 is(copy[i], 1024 + i, "Correct sample");
michael@0 44 }
michael@0 45
michael@0 46 // Test copying the channel data out of a playing buffer
michael@0 47 var srcNode = context.createBufferSource();
michael@0 48 srcNode.buffer = buffer;
michael@0 49 srcNode.start(0);
michael@0 50 copy = new Float32Array(100);
michael@0 51 buffer.copyFromChannel(copy, 0, 1024);
michael@0 52 for (var i = 0; i < copy.length; ++i) {
michael@0 53 is(copy[i], 1024 + i, "Correct sample");
michael@0 54 }
michael@0 55
michael@0 56 // Test copying to the channel data
michael@0 57 var newData = new Float32Array(200);
michael@0 58 buffer.copyToChannel(newData, 0, 100);
michael@0 59 var changedData = buffer.getChannelData(0);
michael@0 60 for (var i = 0; i < changedData.length; ++i) {
michael@0 61 if (i < 100 || i >= 300) {
michael@0 62 is(changedData[i], i, "Untouched sample");
michael@0 63 } else {
michael@0 64 is(changedData[i], 0, "Correct sample");
michael@0 65 }
michael@0 66 }
michael@0 67
michael@0 68 // Now, neuter the array buffer
michael@0 69 var worker = new Worker("audioBufferSourceNodeNeutered_worker.js");
michael@0 70 var data = buffer.getChannelData(0).buffer;
michael@0 71 worker.postMessage(data, [data]);
michael@0 72 SpecialPowers.gc();
michael@0 73
michael@0 74 expectException(function() {
michael@0 75 buffer.copyFromChannel(copy, 0, 1024);
michael@0 76 }, DOMException.INDEX_SIZE_ERR);
michael@0 77
michael@0 78 expectException(function() {
michael@0 79 buffer.copyToChannel(newData, 0, 100);
michael@0 80 }, DOMException.INDEX_SIZE_ERR);
michael@0 81
michael@0 82 expectException(function() {
michael@0 83 context.createBuffer(2, 2048, 7999);
michael@0 84 }, DOMException.INDEX_SIZE_ERR);
michael@0 85 expectException(function() {
michael@0 86 context.createBuffer(2, 2048, 192001);
michael@0 87 }, DOMException.INDEX_SIZE_ERR);
michael@0 88 context.createBuffer(2, 2048, 8000); // no exception
michael@0 89 context.createBuffer(2, 2048, 192000); // no exception
michael@0 90 context.createBuffer(32, 2048, 48000); // no exception
michael@0 91 // Null length
michael@0 92 expectException(function() {
michael@0 93 context.createBuffer(2, 0, 48000);
michael@0 94 }, DOMException.INDEX_SIZE_ERR);
michael@0 95 // Null number of channels
michael@0 96 expectException(function() {
michael@0 97 context.createBuffer(0, 2048, 48000);
michael@0 98 }, DOMException.INDEX_SIZE_ERR);
michael@0 99 SimpleTest.finish();
michael@0 100 });
michael@0 101
michael@0 102 </script>
michael@0 103 </pre>
michael@0 104 </body>
michael@0 105 </html>

mercurial