content/media/webaudio/test/test_periodicWave.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial