test-constantsourcenode.html (3850B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>Test the ConstantSourceNode Interface</title> 4 <script src=/resources/testharness.js></script> 5 <script src=/resources/testharnessreport.js></script> 6 <script> 7 test(function(t) { 8 var ac = new AudioContext(); 9 10 var csn = ac.createConstantSource(); 11 assert_equals(csn.offset.value, 1.0, "Default offset is 1.0"); 12 13 csn = new ConstantSourceNode(ac); 14 assert_equals(csn.offset.value, 1.0, "Default offset is 1.0"); 15 16 csn = new ConstantSourceNode(ac, {offset: -0.25}); 17 assert_equals(csn.offset.value, -0.25, "Offset can be set during construction"); 18 }, "ConstantSourceNode can be constructed"); 19 20 test(function(t) { 21 var ac = new AudioContext(); 22 23 var csn = ac.createConstantSource(); 24 25 assert_throws_dom("InvalidStateError", function() { 26 csn.stop(1); 27 }, "Start must be called before stop"); 28 29 assert_throws_js(RangeError, function() { 30 csn.start(-1); 31 }, "When can not be negative"); 32 33 csn.start(0); 34 assert_throws_js(RangeError, function() { 35 csn.stop(-1); 36 }, "When can not be negative"); 37 }, "ConstantSourceNode stop and start"); 38 39 async_test(function(t) { 40 var ac = new OfflineAudioContext(1, 2048, 44100); 41 var csn = ac.createConstantSource(); 42 csn.connect(ac.destination); 43 csn.start() 44 csn.stop(1024/44100) 45 csn.onended = function(e) { 46 t.step(function() { 47 assert_equals(e.type, "ended", "Event type should be 'ended', received: " + e.type); 48 }); 49 t.done(); 50 } 51 ac.startRendering(); 52 }, "ConstantSourceNode onended event"); 53 54 async_test(function(t) { 55 var ac = new OfflineAudioContext(1, 2048, 44100); 56 var csn = ac.createConstantSource(); 57 csn.connect(ac.destination); 58 csn.start(512/44100) 59 csn.stop(1024/44100) 60 61 ac.oncomplete = function(e) { 62 t.step(function() { 63 var result = e.renderedBuffer.getChannelData(0); 64 for (var i = 0; i < 2048; ++i) { 65 if (i >= 512 && i < 1024) { 66 assert_equals(result[i], 1.0, "sample " + i + " should equal 1.0"); 67 } else { 68 assert_equals(result[i], 0.0, "sample " + i + " should equal 0.0"); 69 } 70 } 71 }); 72 t.done(); 73 } 74 75 ac.startRendering(); 76 }, "ConstantSourceNode start and stop when work"); 77 78 async_test(function(t) { 79 var ac = new OfflineAudioContext(1, 2048, 44100); 80 var csn = ac.createConstantSource(); 81 csn.offset.value = 0.25; 82 csn.connect(ac.destination); 83 csn.start() 84 85 ac.oncomplete = function(e) { 86 t.step(function() { 87 var result = e.renderedBuffer.getChannelData(0); 88 for (var i = 0; i < 2048; ++i) { 89 assert_equals(result[i], 0.25, "sample " + i + " should equal 0.25"); 90 } 91 }); 92 t.done(); 93 } 94 95 ac.startRendering(); 96 }, "ConstantSourceNode with no automation"); 97 98 async_test(function(t) { 99 var ac = new OfflineAudioContext(1, 2048, 44100); 100 101 var timeConstant = 2.0; 102 var offsetStart = 0.25; 103 var offsetEnd = 0.1; 104 105 var csn = ac.createConstantSource(); 106 csn.offset.value = offsetStart; 107 csn.offset.setTargetAtTime(offsetEnd, 1024/ac.sampleRate, timeConstant); 108 csn.connect(ac.destination); 109 csn.start() 110 111 ac.oncomplete = function(e) { 112 t.step(function() { 113 // create buffer with expected values 114 var buffer = ac.createBuffer(1, 2048, ac.sampleRate); 115 for (var i = 0; i < 2048; ++i) { 116 if (i < 1024) { 117 buffer.getChannelData(0)[i] = offsetStart; 118 } else { 119 time = (i-1024)/ac.sampleRate; 120 buffer.getChannelData(0)[i] = offsetEnd + (offsetStart - offsetEnd)*Math.exp(-time/timeConstant); 121 } 122 } 123 124 var result = e.renderedBuffer.getChannelData(0); 125 var expected = buffer.getChannelData(0); 126 for (var i = 0; i < 2048; ++i) { 127 assert_approx_equals(result[i], expected[i], 1.342e-6, "sample " + i); 128 } 129 }); 130 t.done(); 131 } 132 133 ac.startRendering(); 134 }, "ConstantSourceNode with automation"); 135 </script>