waveshaper-copy-curve.html (3130B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title> 5 Test WaveShaper Copies Curve Data 6 </title> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="../../resources/audit-util.js"></script> 10 </head> 11 <body> 12 <script> 13 // Sample rate and number of frames are fairly arbitrary. We need to 14 // render, however, at least 384 frames. 1024 is a nice small value. 15 const sampleRate = 16000; 16 const renderFrames = 1024; 17 18 promise_test(async () => { 19 // Two-channel context; channel 0 contains the test data and channel 20 // 1 contains the expected result. Channel 1 has the normal 21 // WaveShaper output and channel 0 has the WaveShaper output with a 22 // modified curve. 23 const context = new OfflineAudioContext(2, renderFrames, sampleRate); 24 25 // Wave shaper curves. Doesn't really matter what we use as long as 26 // it modifies the input in some way. Thus, keep it simple and just 27 // invert the input. 28 const desiredCurve = [1, 0, -1]; 29 const curve0 = Float32Array.from(desiredCurve); 30 const curve1 = Float32Array.from(desiredCurve); 31 32 // Just use a default oscillator as the source. Doesn't really 33 // matter what we use. 34 const src = new OscillatorNode(context, {type: 'sawtooth'}); 35 36 // Create the wave shapers: ws0 is the test shaper, and ws1 is the 37 // reference wave shaper. 38 const ws0 = new WaveShaperNode(context, {curve: curve0}); 39 const ws1 = new WaveShaperNode(context, {curve: curve1}); 40 41 // Channel merger for comparing outputs (ch 0: modified-curve shaper, 42 // ch 1: reference shaper). 43 const merger = new ChannelMergerNode(context, {numberOfInputs: 2}); 44 45 src.connect(ws0).connect(merger, 0, 0); 46 src.connect(ws1).connect(merger, 0, 1); 47 48 merger.connect(context.destination); 49 50 // Let the context run for a bit and then modify the curve for ws0. 51 // Doesn't really matter what we modify the curve to as long as it's 52 // different. 53 const suspendTime = 256 / context.sampleRate; 54 context.suspend(suspendTime).then(() => { 55 // Mutate the original array we passed to ws0; 56 // node should have copied it. 57 curve0[0] = -0.5; 58 curve0[1] = 0.125; 59 curve0[2] = 0.75; 60 return context.resume(); 61 }); 62 63 src.start(); 64 65 const renderedBuffer = await context.startRendering(); 66 const actual = renderedBuffer.getChannelData(0); 67 const expected = renderedBuffer.getChannelData(1); 68 69 // Modifying the wave shaper curve should not modify the 70 // output so the outputs from the two wave shaper nodes should 71 // be exactly identical. 72 assert_array_equals( 73 actual, 74 expected, 75 'Output of WaveShaper with modified curve ' + 76 'should equal reference output'); 77 }, `test copying: Modifying curve should not modify WaveShaper`); 78 </script> 79 </body> 80 </html>