audioparam-connect-audioratesignal.html (3474B)
1 <!DOCTYPE html> 2 <!-- 3 Tests that an audio-rate signal (AudioNode output) can be connected to an 4 AudioParam. Specifically, this tests that an audio-rate signal coming from an 5 AudioBufferSourceNode playing an AudioBuffer containing a specific curve can be 6 connected to an AudioGainNode's .gain attribute (an AudioParam). Another 7 AudioBufferSourceNode will be the audio source having its gain changed. We load 8 this one with an AudioBuffer containing a constant value of 1. Thus it's easy 9 to check that the resultant signal should be equal to the gain-scaling curve. 10 --> 11 <html> 12 <head> 13 <title> 14 audioparam-connect-audioratesignal.html 15 </title> 16 <script src="/resources/testharness.js"></script> 17 <script src="/resources/testharnessreport.js"></script> 18 <script src="/webaudio/resources/audit-util.js"></script> 19 </head> 20 <body> 21 <script> 22 const sampleRate = 44100.0; 23 const lengthInSeconds = 1; 24 25 let context; 26 let constantOneBuffer; 27 let linearRampBuffer; 28 29 function checkResult(renderedBuffer) { 30 const renderedData = renderedBuffer.getChannelData(0); 31 const expectedData = linearRampBuffer.getChannelData(0); 32 const n = renderedBuffer.length; 33 34 assert_equals( 35 n, 36 linearRampBuffer.length, 37 'Rendered signal length should match control buffer length'); 38 39 // Check that the rendered result exactly matches the buffer used to 40 // control gain. This is because we're changing the gain of a signal 41 // having constant value 1. 42 let success = true; 43 for (let i = 0; i < n; ++i) { 44 if (renderedData[i] != expectedData[i]) { 45 success = false; 46 break; 47 } 48 } 49 50 assert_true( 51 success, 52 `Rendered signal exactly matches ` + 53 `the audio-rate gain changing signal`); 54 } 55 56 promise_test(async t => { 57 const sampleFrameLength = sampleRate * lengthInSeconds; 58 59 // Create offline audio context. 60 context = new OfflineAudioContext(1, sampleFrameLength, sampleRate); 61 62 // Create buffer used by the source which will have its gain controlled. 63 constantOneBuffer = createConstantBuffer(context, sampleFrameLength, 1); 64 65 // Create buffer used to control gain. 66 linearRampBuffer = createLinearRampBuffer(context, sampleFrameLength); 67 68 // Create the two sources. 69 70 const constantSource = new AudioBufferSourceNode(context, { 71 buffer: constantOneBuffer, 72 }); 73 74 const gainChangingSource = new AudioBufferSourceNode(context, { 75 buffer: linearRampBuffer, 76 }); 77 78 // Create a gain node controlling the gain of constantSource and make 79 // the connections. 80 const gainNode = new GainNode(context, {gain: 0}); 81 82 constantSource.connect(gainNode).connect(context.destination); 83 84 // Connect an audio-rate signal to control the .gain AudioParam. 85 // This is the heart of what is being tested. 86 gainChangingSource.connect(gainNode.gain); 87 88 // Start both sources at time 0. 89 constantSource.start(); 90 gainChangingSource.start(); 91 92 const renderedBuffer = await context.startRendering(); 93 checkResult(renderedBuffer); 94 }, `AudioParam accepts an audio-rate signal from an AudioNode` + 95 ` and scales a constant source exactly`); 96 </script> 97 </body> 98 </html>