transferred-buffer-output.html (3611B)
1 <!doctype html> 2 <html> 3 <head> 4 <title> 5 Test Convolver Output with Transferred Buffer 6 </title> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="/webaudio/resources/audit-util.js"></script> 10 <script src="/webaudio/resources/audit.js"></script> 11 </head> 12 13 <body> 14 <script> 15 // Arbitrary sample rate. 16 const sampleRate = 16000; 17 18 // Number of frames to render. Just need to have at least 2 render 19 // quanta. 20 const lengthInFrames = 10 * RENDER_QUANTUM_FRAMES; 21 22 let audit = Audit.createTaskRunner(); 23 24 // Buffer to use for the impulse response of a ConvolverNode. 25 let impulseBuffer; 26 27 // This sets up a worker to receive one channel of an AudioBuffer. 28 function setUpWorkerForTest() { 29 impulseBuffer = new AudioBuffer({ 30 numberOfChannels: 2, 31 length: 2 * RENDER_QUANTUM_FRAMES, 32 sampleRate: sampleRate 33 }); 34 35 // Just fill the buffer with a constant value; the contents shouldn't 36 // matter for this test since we're transferring one of the channels. 37 impulseBuffer.getChannelData(0).fill(1); 38 impulseBuffer.getChannelData(1).fill(2); 39 40 // We're going to transfer channel 0 to the worker, making it 41 // unavailable for the convolver 42 let data = impulseBuffer.getChannelData(0).buffer; 43 44 let string = [ 45 'onmessage = function(e) {', ' postMessage(\'done\');', '};' 46 ].join('\n'); 47 48 let blobURL = URL.createObjectURL(new Blob([string])); 49 let worker = new Worker(blobURL); 50 worker.onmessage = workerReply; 51 worker.postMessage(data, [data]); 52 } 53 54 function workerReply() { 55 // Worker has received the message. Run the test. 56 audit.run(); 57 } 58 59 audit.define( 60 { 61 label: 'Test Convolver with transferred buffer', 62 description: 'Output should be all zeroes' 63 }, 64 async (task, should) => { 65 // Two channels so we can capture the output of the convolver with a 66 // stereo convolver. 67 let context = new OfflineAudioContext({ 68 numberOfChannels: 2, 69 length: lengthInFrames, 70 sampleRate: sampleRate 71 }); 72 73 // Use a simple constant source so we easily check that the 74 // convolver output is correct. 75 let source = new ConstantSourceNode(context); 76 77 // Create the convolver with the desired impulse response and 78 // disable normalization so we can easily check the output. 79 let conv = new ConvolverNode( 80 context, {disableNormalization: true, buffer: impulseBuffer}); 81 82 source.connect(conv).connect(context.destination); 83 84 source.start(); 85 86 let renderedBuffer = await context.startRendering(); 87 88 // Get the actual data 89 let c0 = renderedBuffer.getChannelData(0); 90 let c1 = renderedBuffer.getChannelData(1); 91 92 // Since one channel was transferred, we must behave as if all were 93 // transferred. Hence, the output should be all zeroes for both 94 // channels. 95 should(c0, `Convolver channel 0 output[0:${c0.length - 1}]`) 96 .beConstantValueOf(0); 97 98 should(c1, `Convolver channel 1 output[0:${c1.length - 1}]`) 99 .beConstantValueOf(0); 100 101 task.done(); 102 }); 103 104 setUpWorkerForTest(); 105 </script> 106 </body> 107 </html>