nan-param.html (3163B)
1 <!doctype html> 2 <html> 3 <head> 4 <title>Test Flushing of NaN to Zero in AudioParams</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="/webaudio/resources/audit-util.js"></script> 8 </head> 9 10 <body> 11 <script> 12 // See 13 // https://webaudio.github.io/web-audio-api/#computation-of-value. 14 // 15 // The computed value must replace NaN values in the output with 16 // the default value of the param. 17 promise_test(async () => { 18 // For testing, we only need a small number of frames; and 19 // a low sample rate is perfectly fine. Use two channels. 20 // The first channel is for the AudioParam output. The 21 // second channel is for the AudioParam input. 22 const context = new OfflineAudioContext( 23 {numberOfChannels: 2, length: 256, sampleRate: 8192}); 24 25 const merger = new ChannelMergerNode( 26 context, {numberOfInputs: context.destination.channelCount}); 27 merger.connect(context.destination); 28 29 // A constant source with a huge value. 30 const mod = new ConstantSourceNode(context, {offset: 1e30}); 31 32 // Gain nodes with a huge positive gain and huge negative 33 // gain. Combined with the huge offset in |mod|, the 34 // output of the gain nodes are +Infinity and -Infinity. 35 const gainPos = new GainNode(context, {gain: 1e30}); 36 const gainNeg = new GainNode(context, {gain: -1e30}); 37 38 mod.connect(gainPos); 39 mod.connect(gainNeg); 40 41 // Connect these to the second merger channel. This is a 42 // sanity check that the AudioParam input really is NaN. 43 gainPos.connect(merger, 0, 1); 44 gainNeg.connect(merger, 0, 1); 45 46 // Source whose AudioParam is connected to the graph 47 // that produces NaN values. Use a non-default value offset 48 // just in case something is wrong we get default for some 49 // other reason. 50 const src = new ConstantSourceNode(context, {offset: 100}); 51 52 gainPos.connect(src.offset); 53 gainNeg.connect(src.offset); 54 55 // AudioParam output goes to channel 1 of the destination. 56 src.connect(merger, 0, 0); 57 58 // Let's go! 59 mod.start(); 60 src.start(); 61 62 const buffer = await context.startRendering(); 63 64 const input = buffer.getChannelData(1); 65 const output = buffer.getChannelData(0); 66 67 // Have to test manually for NaN values in the input because 68 // NaN fails all comparisons. 69 let isNaN = true; 70 for (let k = 0; k < input.length; ++k) { 71 if (!Number.isNaN(input[k])) { 72 isNaN = false; 73 break; 74 } 75 } 76 77 assert_true(isNaN, 'AudioParam input contains only NaN'); 78 79 // Output of the AudioParam should have all NaN values 80 // replaced by the default. 81 assert_constant_value( 82 output, src.offset.defaultValue, 83 'AudioParam output should flush NaN to default'); 84 }, 'AudioParam NaN should be flushed to default value'); 85 </script> 86 </body> 87 </html>