OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <title>Test ConstantSourceNode Output</title> |
| 5 <script src="../resources/testharness.js"></script> |
| 6 <script src="../resources/testharnessreport.js"></script> |
| 7 <script src="resources/audio-testing.js"></script> |
| 8 <script src="resources/audioparam-testing.js"></script> |
| 9 </head> |
| 10 |
| 11 <body> |
| 12 <script> |
| 13 var sampleRate = 48000; |
| 14 var renderDuration = 0.125; |
| 15 var renderFrames = sampleRate * renderDuration; |
| 16 |
| 17 var audit = Audit.createTaskRunner(); |
| 18 |
| 19 audit.defineTask("constant source", function (taskDone) { |
| 20 // Verify a constant source outputs the correct (fixed) constant. |
| 21 var context = new OfflineAudioContext(1, renderFrames, sampleRate); |
| 22 var node = new ConstantSourceNode(context, { |
| 23 offset: 0.5 |
| 24 }); |
| 25 node.connect(context.destination); |
| 26 node.start(); |
| 27 |
| 28 context.startRendering().then(function (buffer) { |
| 29 var actual = buffer.getChannelData(0); |
| 30 var expected = new Float32Array(actual.length); |
| 31 expected.fill(node.offset.value); |
| 32 |
| 33 Should("ConstantSourceNode({offset: 0.5})", actual) |
| 34 .beEqualToArray(expected); |
| 35 }).then(taskDone); |
| 36 }); |
| 37 |
| 38 audit.defineTask("start/stop", function (taskDone) { |
| 39 // Verify a constant source starts and stops at the correct time and has |
| 40 // the correct (fixed) value. |
| 41 var context = new OfflineAudioContext(1, renderFrames, sampleRate); |
| 42 var node = new ConstantSourceNode(context, { |
| 43 offset: 1 |
| 44 }); |
| 45 node.connect(context.destination); |
| 46 |
| 47 var startFrame = 10; |
| 48 var stopFrame = 300; |
| 49 |
| 50 node.start(startFrame / context.sampleRate); |
| 51 node.stop(stopFrame / context.sampleRate); |
| 52 |
| 53 context.startRendering().then(function (buffer) { |
| 54 var actual = buffer.getChannelData(0); |
| 55 var expected = new Float32Array(actual.length); |
| 56 // The expected output is all 1s from start to stop time. |
| 57 expected.fill(0); |
| 58 |
| 59 for (var k = startFrame; k < stopFrame; ++k) { |
| 60 expected[k] = node.offset.value; |
| 61 } |
| 62 |
| 63 var success = Should("ConstantSourceNode frames [0, " + |
| 64 startFrame + ")", |
| 65 actual.slice(0, startFrame)) |
| 66 .beConstantValueOf(0); |
| 67 |
| 68 success = Should("ConstantSourceNode frames [" + startFrame + |
| 69 ", " + |
| 70 stopFrame + ")", |
| 71 actual.slice(startFrame, stopFrame)) |
| 72 .beConstantValueOf(1) && success; |
| 73 |
| 74 success = Should("ConstantSourceNode frames [" + stopFrame + ", " + |
| 75 renderFrames + ")", |
| 76 actual.slice(stopFrame)) |
| 77 .beConstantValueOf(0) && success; |
| 78 |
| 79 Should("ConstantSourceNode started and stopped", success) |
| 80 .summarize( |
| 81 "at the correct times with the correct values", |
| 82 "with the incorrect times or values"); |
| 83 }).then(taskDone); |
| 84 |
| 85 }); |
| 86 |
| 87 audit.defineTask("basic automation", function (taskDone) { |
| 88 // Verify that automation works as expected. |
| 89 var context = new OfflineAudioContext(1, renderFrames, sampleRate); |
| 90 var source = context.createConstantSource(); |
| 91 source.connect(context.destination); |
| 92 |
| 93 var rampEndTime = renderDuration / 2; |
| 94 source.offset.setValueAtTime(0.5, 0); |
| 95 source.offset.linearRampToValueAtTime(1, rampEndTime); |
| 96 |
| 97 source.start(); |
| 98 |
| 99 context.startRendering() |
| 100 .then(function (buffer) { |
| 101 var actual = buffer.getChannelData(0); |
| 102 var expected = createLinearRampArray(0, rampEndTime, 0.5, 1, |
| 103 context.sampleRate); |
| 104 |
| 105 var rampEndFrame = Math.ceil(rampEndTime * context.sampleRate); |
| 106 var success = Should("ConstantSourceNode.linearRamp(1, 0.5)", |
| 107 actual.slice(0, rampEndFrame)) |
| 108 .beCloseToArray(expected, { |
| 109 // Experimentally determined threshold.. |
| 110 relativeThreshold: 7.1610e-7 |
| 111 }); |
| 112 |
| 113 success = Should("ConstantSourceNode after ramp", |
| 114 actual.slice(rampEndFrame)) |
| 115 .beConstantValueOf(1) && success; |
| 116 |
| 117 Should("ConstantSourceNode automation", success) |
| 118 .summarize( |
| 119 "produced the correct values", |
| 120 "did not produce the correct values"); |
| 121 }) |
| 122 .then(taskDone); |
| 123 }); |
| 124 |
| 125 audit.defineTask("connected audioparam", function (taskDone) { |
| 126 // Verify the constant source output with connected AudioParam produces |
| 127 // the correct output. |
| 128 var context = new OfflineAudioContext(2, renderFrames, sampleRate) |
| 129 context.destination.channelInterpretation = "discrete"; |
| 130 var source = new ConstantSourceNode(context, { |
| 131 offset: 1 |
| 132 }); |
| 133 var osc = context.createOscillator(); |
| 134 var merger = context.createChannelMerger(2); |
| 135 merger.connect(context.destination); |
| 136 |
| 137 source.connect(merger, 0, 0); |
| 138 osc.connect(merger, 0, 1); |
| 139 osc.connect(source.offset); |
| 140 |
| 141 osc.start(); |
| 142 var sourceStartFrame = 10; |
| 143 source.start(sourceStartFrame / context.sampleRate); |
| 144 |
| 145 context.startRendering() |
| 146 .then(function (buffer) { |
| 147 // Channel 0 and 1 should be identical, except channel 0 (the |
| 148 // source) is silent at the beginning. |
| 149 var actual = buffer.getChannelData(0); |
| 150 var expected = buffer.getChannelData(1); |
| 151 // The expected output should be oscillator + 1 because offset |
| 152 // is 1. |
| 153 expected = expected.map(x => 1 + x); |
| 154 var success = true; |
| 155 |
| 156 // The initial part of the output should be silent because the |
| 157 // source node hasn't started yet. |
| 158 success = Should("ConstantSourceNode frames [0, " + |
| 159 sourceStartFrame + ")", actual.slice(0, sourceStartFrame) |
| 160 ) |
| 161 .beConstantValueOf(0); |
| 162 // The rest of the output should be the same as the oscillator (in |
| 163 // channel 1) |
| 164 success = Should("ConstantSourceNode frames [" + |
| 165 sourceStartFrame + ", " + renderFrames + ")", |
| 166 actual.slice(sourceStartFrame)) |
| 167 .beCloseToArray(expected.slice(sourceStartFrame), 0); |
| 168 |
| 169 Should("ConstantSourceNode with connected AudioParam", |
| 170 success) |
| 171 .summarize( |
| 172 "had the expected output", |
| 173 "did not have the expected output"); |
| 174 }) |
| 175 .then(taskDone); |
| 176 }); |
| 177 |
| 178 audit.runTasks(); |
| 179 </script> |
| 180 </body> |
| 181 </html> |
OLD | NEW |