periodicWave.html (5031B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title> 5 Test Constructor: PeriodicWave 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 <body> 13 <script id="layout-test-code"> 14 // real and imag are used in separate PeriodicWaves to make their peak values 15 // easy to determine. 16 const realMax = 99; 17 var real = new Float32Array(realMax + 1); 18 real[1] = 2.0; // fundamental 19 real[realMax] = 3.0; 20 const realPeak = real[1] + real[realMax]; 21 const realFundamental = 19.0; 22 var imag = new Float32Array(4); 23 imag[0] = 6.0; // should be ignored. 24 imag[3] = 0.5; 25 const imagPeak = imag[3]; 26 const imagFundamental = 551.0; 27 28 const testLength = 8192; 29 let context = new AudioContext(); 30 31 let audit = Audit.createTaskRunner(); 32 33 // Create with the factory method 34 35 audit.define('create with factory method', (task, should) => { 36 should(() => { 37 context.createPeriodicWave(new Float32Array(testLength), new Float32Array(testLength)); 38 }, 'context.createPeriodicWave(new Float32Array(' + testLength + '), ' + 39 'new Float32Array(' + testLength + '))').notThrow(); 40 task.done(); 41 }); 42 43 audit.define('different length with factory method', (task, should) => { 44 should(() => { 45 context.createPeriodicWave(new Float32Array(512), new Float32Array(4)); 46 }, 'context.createPeriodicWave(new Float32Array(512), ' + 47 'new Float32Array(4))').throw(DOMException, "IndexSizeError"); 48 task.done(); 49 }); 50 51 audit.define('too small with factory method', (task, should) => { 52 should(() => { 53 context.createPeriodicWave(new Float32Array(1), new Float32Array(1)); 54 }, 'context.createPeriodicWave(new Float32Array(1), ' + 55 'new Float32Array(1))').throw(DOMException, "IndexSizeError"); 56 task.done(); 57 }); 58 59 // Create with the constructor 60 61 audit.define('create with constructor', (task, should) => { 62 should(() => { 63 new PeriodicWave(context, { real: new Float32Array(testLength), imag: new Float32Array(testLength) }); 64 }, 'new PeriodicWave(context, { real : new Float32Array(' + testLength + '), ' + 65 'imag : new Float32Array(' + testLength + ') })').notThrow(); 66 task.done(); 67 }); 68 69 audit.define('different length with constructor', (task, should) => { 70 should(() => { 71 new PeriodicWave(context, { real: new Float32Array(testLength), imag: new Float32Array(4) }); 72 }, 'new PeriodicWave(context, { real : new Float32Array(' + testLength + '), ' + 73 'imag : new Float32Array(4) })').throw(DOMException, "IndexSizeError"); 74 task.done(); 75 }); 76 77 audit.define('too small with constructor', (task, should) => { 78 should(() => { 79 new PeriodicWave(context, { real: new Float32Array(1), imag: new Float32Array(1) }); 80 }, 'new PeriodicWave(context, { real : new Float32Array(1), ' + 81 'imag : new Float32Array(1) })').throw(DOMException, "IndexSizeError"); 82 task.done(); 83 }); 84 85 audit.define('output test', (task, should) => { 86 let context = new OfflineAudioContext(2, testLength, 44100); 87 // Create the expected output buffer 88 let expectations = context.createBuffer(2, testLength, context.sampleRate); 89 for (var i = 0; i < expectations.length; ++i) { 90 91 expectations.getChannelData(0)[i] = 1.0 / realPeak * 92 (real[1] * Math.cos(2 * Math.PI * realFundamental * i / 93 context.sampleRate) + 94 real[realMax] * Math.cos(2 * Math.PI * realMax * realFundamental * i / 95 context.sampleRate)); 96 97 expectations.getChannelData(1)[i] = 1.0 / imagPeak * 98 imag[3] * Math.sin(2 * Math.PI * 3 * imagFundamental * i / 99 context.sampleRate); 100 } 101 102 // Create the real output buffer 103 let merger = context.createChannelMerger(); 104 105 let osc1 = context.createOscillator(); 106 let osc2 = context.createOscillator(); 107 108 osc1.setPeriodicWave(context.createPeriodicWave( 109 real, new Float32Array(real.length))); 110 osc2.setPeriodicWave(context.createPeriodicWave( 111 new Float32Array(imag.length), imag)); 112 osc1.frequency.value = realFundamental; 113 osc2.frequency.value = imagFundamental; 114 115 osc1.start(); 116 osc2.start(); 117 118 osc1.connect(merger, 0, 0); 119 osc2.connect(merger, 0, 1); 120 121 context.startRendering().then(reality => { 122 should(reality, 'rendering PeriodicWave').beEqualToArray(expectations); 123 task.done(); 124 }); 125 }); 126 127 audit.run(); 128 </script> 129 </body> 130 </html>