set-target-conv.html (2820B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Test convergence of setTargetAtTime</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src='/webaudio/resources/audio-param.js'></script> 8 <script src="/webaudio/resources/audit-util.js"></script> 9 </head> 10 11 <body> 12 <script> 13 promise_test(async () => { 14 // Two channels: 15 // 0 - actual result 16 // 1 - expected result 17 const context = new OfflineAudioContext( 18 {numberOfChannels: 2, sampleRate: 8000, length: 8000}); 19 20 const merger = new ChannelMergerNode( 21 context, {numberOfChannels: context.destination.channelCount}); 22 merger.connect(context.destination); 23 24 // Construct test source that will have the AudioParams being tested 25 // to verify that the AudioParams are working correctly. 26 const src = new ConstantSourceNode(context); 27 28 src.connect(merger, 0, 0); 29 src.offset.setValueAtTime(1, 0); 30 31 const timeConstant = 0.01; 32 33 // testTime must be at least 10*timeConstant. Also, this must not 34 // lie on a render boundary. 35 const testTime = 0.15; 36 const rampEnd = testTime + 0.001; 37 38 src.offset.setTargetAtTime(0.5, 0.01, timeConstant); 39 src.offset.setValueAtTime(0.5, testTime); 40 src.offset.linearRampToValueAtTime(1, rampEnd); 41 42 // The reference node that will generate the expected output. We do 43 // the same automations, except we don't apply the setTarget 44 // automation. 45 const refSrc = new ConstantSourceNode(context); 46 refSrc.connect(merger, 0, 1); 47 48 refSrc.offset.setValueAtTime(0.5, 0); 49 refSrc.offset.setValueAtTime(0.5, testTime); 50 refSrc.offset.linearRampToValueAtTime(1, rampEnd); 51 52 src.start(); 53 refSrc.start(); 54 55 const resultBuffer = await context.startRendering(); 56 const actual = resultBuffer.getChannelData(0); 57 const expected = resultBuffer.getChannelData(1); 58 59 // Just verify that the actual output matches the expected 60 // starting a little bit before testTime. 61 const testFrame = Math.floor(testTime * context.sampleRate) - 128; 62 const actualSlice = actual.slice(testFrame); 63 const expectedSlice = expected.slice(testFrame); 64 65 assert_equals( 66 actualSlice.length, expectedSlice.length, 67 `output[${testFrame}:] length`); 68 69 const relThreshold = 4.1724e-6; 70 assert_array_equal_within_eps( 71 actualSlice, 72 expectedSlice, 73 relThreshold, 74 `output[${testFrame}] approx expected (rel ${relThreshold})`); 75 }, 'setTargetAtTime: convergence handled correctly'); 76 </script> 77 </body> 78 </html>