1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/test/test_AudioBuffer.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<head> 1.7 + <title>Test whether we can create an AudioContext interface</title> 1.8 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.9 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.10 +</head> 1.11 +<body> 1.12 +<pre id="test"> 1.13 +<script src="webaudio.js" type="text/javascript"></script> 1.14 +<script class="testbody" type="text/javascript"> 1.15 + 1.16 +SimpleTest.waitForExplicitFinish(); 1.17 +addLoadEvent(function() { 1.18 + var context = new AudioContext(); 1.19 + var buffer = context.createBuffer(2, 2048, context.sampleRate); 1.20 + SpecialPowers.gc(); // Make sure that our channels are accessible after GC 1.21 + ok(buffer, "Buffer was allocated successfully"); 1.22 + is(buffer.sampleRate, context.sampleRate, "Correct sample rate"); 1.23 + is(buffer.length, 2048, "Correct length"); 1.24 + ok(Math.abs(buffer.duration - 2048 / context.sampleRate) < 0.0001, "Correct duration"); 1.25 + is(buffer.numberOfChannels, 2, "Correct number of channels"); 1.26 + for (var i = 0; i < buffer.numberOfChannels; ++i) { 1.27 + var buf = buffer.getChannelData(i); 1.28 + ok(buf, "Buffer index " + i + " exists"); 1.29 + ok(buf instanceof Float32Array, "Result is a typed array"); 1.30 + is(buf.length, buffer.length, "Correct length"); 1.31 + var foundNonZero = false; 1.32 + for (var j = 0; j < buf.length; ++j) { 1.33 + if (buf[j] != 0) { 1.34 + foundNonZero = true; 1.35 + break; 1.36 + } 1.37 + buf[j] = j; 1.38 + } 1.39 + ok(!foundNonZero, "Buffer " + i + " should be initialized to 0"); 1.40 + } 1.41 + 1.42 + // Now test copying the channel data out of a normal buffer 1.43 + var copy = new Float32Array(100); 1.44 + buffer.copyFromChannel(copy, 0, 1024); 1.45 + for (var i = 0; i < copy.length; ++i) { 1.46 + is(copy[i], 1024 + i, "Correct sample"); 1.47 + } 1.48 + 1.49 + // Test copying the channel data out of a playing buffer 1.50 + var srcNode = context.createBufferSource(); 1.51 + srcNode.buffer = buffer; 1.52 + srcNode.start(0); 1.53 + copy = new Float32Array(100); 1.54 + buffer.copyFromChannel(copy, 0, 1024); 1.55 + for (var i = 0; i < copy.length; ++i) { 1.56 + is(copy[i], 1024 + i, "Correct sample"); 1.57 + } 1.58 + 1.59 + // Test copying to the channel data 1.60 + var newData = new Float32Array(200); 1.61 + buffer.copyToChannel(newData, 0, 100); 1.62 + var changedData = buffer.getChannelData(0); 1.63 + for (var i = 0; i < changedData.length; ++i) { 1.64 + if (i < 100 || i >= 300) { 1.65 + is(changedData[i], i, "Untouched sample"); 1.66 + } else { 1.67 + is(changedData[i], 0, "Correct sample"); 1.68 + } 1.69 + } 1.70 + 1.71 + // Now, neuter the array buffer 1.72 + var worker = new Worker("audioBufferSourceNodeNeutered_worker.js"); 1.73 + var data = buffer.getChannelData(0).buffer; 1.74 + worker.postMessage(data, [data]); 1.75 + SpecialPowers.gc(); 1.76 + 1.77 + expectException(function() { 1.78 + buffer.copyFromChannel(copy, 0, 1024); 1.79 + }, DOMException.INDEX_SIZE_ERR); 1.80 + 1.81 + expectException(function() { 1.82 + buffer.copyToChannel(newData, 0, 100); 1.83 + }, DOMException.INDEX_SIZE_ERR); 1.84 + 1.85 + expectException(function() { 1.86 + context.createBuffer(2, 2048, 7999); 1.87 + }, DOMException.INDEX_SIZE_ERR); 1.88 + expectException(function() { 1.89 + context.createBuffer(2, 2048, 192001); 1.90 + }, DOMException.INDEX_SIZE_ERR); 1.91 + context.createBuffer(2, 2048, 8000); // no exception 1.92 + context.createBuffer(2, 2048, 192000); // no exception 1.93 + context.createBuffer(32, 2048, 48000); // no exception 1.94 + // Null length 1.95 + expectException(function() { 1.96 + context.createBuffer(2, 0, 48000); 1.97 + }, DOMException.INDEX_SIZE_ERR); 1.98 + // Null number of channels 1.99 + expectException(function() { 1.100 + context.createBuffer(0, 2048, 48000); 1.101 + }, DOMException.INDEX_SIZE_ERR); 1.102 + SimpleTest.finish(); 1.103 +}); 1.104 + 1.105 +</script> 1.106 +</pre> 1.107 +</body> 1.108 +</html>