tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_periodicWave.html (4175B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test the PeriodicWave interface</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <script type="text/javascript" src="webaudio.js"></script>
      7  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      8 </head>
      9 <body>
     10 <pre id="test">
     11 <script class="testbody" type="text/javascript">
     12 
     13 SimpleTest.waitForExplicitFinish();
     14 
     15 // real and imag are used in separate PeriodicWaves to make their peak values
     16 // easy to determine.
     17 const realMax = 99;
     18 var real = new Float32Array(realMax + 1);
     19 real[1] = 2.0; // fundamental
     20 real[realMax] = 3.0;
     21 const realPeak = real[1] + real[realMax];
     22 const realFundamental = 19.0;
     23 var imag = new Float32Array(4);
     24 imag[0] = 6.0; // should be ignored.
     25 imag[3] = 0.5;
     26 const imagPeak = imag[3];
     27 const imagFundamental = 551.0;
     28 
     29 const testLength = 4096;
     30 
     31 addLoadEvent(function() {
     32  var ac = new AudioContext();
     33  ac.createPeriodicWave(new Float32Array(4096), new Float32Array(4096));
     34  expectException(function() {
     35    ac.createPeriodicWave(new Float32Array(512), imag);
     36  }, DOMException.INDEX_SIZE_ERR);
     37  expectException(function() {
     38    ac.createPeriodicWave(new Float32Array(0), new Float32Array(0));
     39  }, DOMException.INDEX_SIZE_ERR);
     40  expectException(function() {
     41    ac.createPeriodicWave(new Float32Array(1), new Float32Array(1));
     42  }, DOMException.INDEX_SIZE_ERR);
     43  expectNoException(function() {
     44    ac.createPeriodicWave(new Float32Array(4097), new Float32Array(4097));
     45  });
     46 
     47  expectNoException(function() {
     48    new PeriodicWave(ac, {});
     49  });
     50 
     51  // real.size == imag.size
     52  expectException(function() {
     53    new PeriodicWave(ac, {real: new Float32Array(10), imag: new Float32Array(9)});
     54  }, DOMException.INDEX_SIZE_ERR);
     55 
     56  // size lower than 2 is not allowed
     57  expectException(function() {
     58    new PeriodicWave(ac, {real: new Float32Array(0)});
     59  }, DOMException.INDEX_SIZE_ERR);
     60  expectException(function() {
     61    new PeriodicWave(ac, {imag: new Float32Array(0)});
     62  }, DOMException.INDEX_SIZE_ERR);
     63  expectException(function() {
     64    new PeriodicWave(ac, {real: new Float32Array(1)});
     65  }, DOMException.INDEX_SIZE_ERR);
     66  expectException(function() {
     67    new PeriodicWave(ac, {imag: new Float32Array(1)});
     68  }, DOMException.INDEX_SIZE_ERR);
     69  expectException(function() {
     70    new PeriodicWave(ac, {real: new Float32Array(0), imag: new Float32Array(0)});
     71  }, DOMException.INDEX_SIZE_ERR);
     72  expectException(function() {
     73    new PeriodicWave(ac, {real: new Float32Array(1), imag: new Float32Array(1)});
     74  }, DOMException.INDEX_SIZE_ERR);
     75 
     76  new PeriodicWave(ac, {real: new Float32Array(4096), imag: new Float32Array(4096)});
     77  new PeriodicWave(ac, {real: new Float32Array(4096) });
     78  new PeriodicWave(ac, {imag: new Float32Array(4096) });
     79 
     80  runTest();
     81 });
     82 
     83 var gTest = {
     84  createGraph(context) {
     85    var merger = context.createChannelMerger();
     86 
     87    var osc0 = context.createOscillator();
     88    var osc1 = context.createOscillator();
     89 
     90    osc0.setPeriodicWave(context.
     91                         createPeriodicWave(real,
     92                                            new Float32Array(real.length)));
     93    osc1.setPeriodicWave(context.
     94                         createPeriodicWave(new Float32Array(imag.length),
     95                                            imag));
     96 
     97    osc0.frequency.value = realFundamental;
     98    osc1.frequency.value = imagFundamental;
     99 
    100    osc0.start();
    101    osc1.start();
    102 
    103    osc0.connect(merger, 0, 0);
    104    osc1.connect(merger, 0, 1);
    105 
    106    return merger;
    107  },
    108  createExpectedBuffers(context) {
    109    var buffer = context.createBuffer(2, testLength, context.sampleRate);
    110 
    111    for (var i = 0; i < buffer.length; ++i) {
    112 
    113      buffer.getChannelData(0)[i] = 1.0 / realPeak *
    114        (real[1] * Math.cos(2 * Math.PI * realFundamental * i /
    115                            context.sampleRate) +
    116         real[realMax] * Math.cos(2 * Math.PI * realMax * realFundamental * i /
    117                            context.sampleRate));
    118 
    119      buffer.getChannelData(1)[i] = 1.0 / imagPeak *
    120         imag[3] * Math.sin(2 * Math.PI * 3 * imagFundamental * i /
    121                            context.sampleRate);
    122    }
    123    return buffer;
    124  },
    125 };
    126 
    127 </script>
    128 </pre>
    129 </body>
    130 </html>