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