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