Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <title>Test left/right symmetry and block-offset invariance of HRTF panner</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 | const blockSize = 128; |
michael@0 | 16 | const bufferSize = 4096; // > HRTF panner latency |
michael@0 | 17 | |
michael@0 | 18 | var ctx = new AudioContext(); |
michael@0 | 19 | |
michael@0 | 20 | function isChannelSilent(channel) { |
michael@0 | 21 | for (var i = 0; i < channel.length; ++i) { |
michael@0 | 22 | if (channel[i] != 0.0) { |
michael@0 | 23 | return false; |
michael@0 | 24 | } |
michael@0 | 25 | } |
michael@0 | 26 | return true; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | function startTest() { |
michael@0 | 30 | var leftPanner = ctx.createPanner(); |
michael@0 | 31 | var rightPanner = ctx.createPanner(); |
michael@0 | 32 | leftPanner.setPosition(-1,0,0); |
michael@0 | 33 | rightPanner.setPosition(1,0,0); |
michael@0 | 34 | |
michael@0 | 35 | // Test that PannerNode processes the signal consistently irrespective of |
michael@0 | 36 | // the offset in the processing block. This is done by inserting a delay of |
michael@0 | 37 | // less than a block size before one panner. |
michael@0 | 38 | const delayTime = 0.7 * blockSize / ctx.sampleRate; |
michael@0 | 39 | var leftDelay = ctx.createDelay(delayTime); |
michael@0 | 40 | leftDelay.delayTime.value = delayTime; |
michael@0 | 41 | leftDelay.connect(leftPanner); |
michael@0 | 42 | // and compensating for the delay after the other. |
michael@0 | 43 | var rightDelay = ctx.createDelay(delayTime); |
michael@0 | 44 | rightDelay.delayTime.value = delayTime; |
michael@0 | 45 | rightPanner.connect(rightDelay); |
michael@0 | 46 | |
michael@0 | 47 | // Feed the panners with a signal having some harmonics to fill the spectrum. |
michael@0 | 48 | var oscillator = ctx.createOscillator(); |
michael@0 | 49 | oscillator.frequency.value = 110; |
michael@0 | 50 | oscillator.type = "sawtooth"; |
michael@0 | 51 | oscillator.connect(leftDelay); |
michael@0 | 52 | oscillator.connect(rightPanner); |
michael@0 | 53 | oscillator.start(0); |
michael@0 | 54 | |
michael@0 | 55 | // Switch the channels on one panner output, and it should match the other. |
michael@0 | 56 | var splitter = ctx.createChannelSplitter(); |
michael@0 | 57 | leftPanner.connect(splitter); |
michael@0 | 58 | var merger = ctx.createChannelMerger(); |
michael@0 | 59 | splitter.connect(merger, 0, 1); |
michael@0 | 60 | splitter.connect(merger, 1, 0); |
michael@0 | 61 | |
michael@0 | 62 | // Invert one signal so that mixing with the other will find the difference. |
michael@0 | 63 | var gain = ctx.createGain(); |
michael@0 | 64 | gain.gain.value = -1.0; |
michael@0 | 65 | merger.connect(gain); |
michael@0 | 66 | |
michael@0 | 67 | var processor = ctx.createScriptProcessor(bufferSize, 2, 0); |
michael@0 | 68 | gain.connect(processor); |
michael@0 | 69 | rightDelay.connect(processor); |
michael@0 | 70 | processor.onaudioprocess = |
michael@0 | 71 | function(e) { |
michael@0 | 72 | compareBuffers(e.inputBuffer, |
michael@0 | 73 | ctx.createBuffer(2, bufferSize, ctx.sampleRate)); |
michael@0 | 74 | e.target.onaudioprocess = null; |
michael@0 | 75 | SimpleTest.finish(); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | function prepareTest() { |
michael@0 | 80 | // A PannerNode will produce no output until it has loaded its HRIR |
michael@0 | 81 | // database. Wait for this to load before starting the test. |
michael@0 | 82 | var processor = ctx.createScriptProcessor(bufferSize, 2, 0); |
michael@0 | 83 | var panner = ctx.createPanner(); |
michael@0 | 84 | panner.connect(processor); |
michael@0 | 85 | var oscillator = ctx.createOscillator(); |
michael@0 | 86 | oscillator.connect(panner); |
michael@0 | 87 | oscillator.start(0); |
michael@0 | 88 | |
michael@0 | 89 | processor.onaudioprocess = |
michael@0 | 90 | function(e) { |
michael@0 | 91 | if (isChannelSilent(e.inputBuffer.getChannelData(0))) |
michael@0 | 92 | return; |
michael@0 | 93 | |
michael@0 | 94 | oscillator.stop(0); |
michael@0 | 95 | panner.disconnect(); |
michael@0 | 96 | e.target.onaudioprocess = null; |
michael@0 | 97 | startTest(); |
michael@0 | 98 | }; |
michael@0 | 99 | } |
michael@0 | 100 | prepareTest(); |
michael@0 | 101 | </script> |
michael@0 | 102 | </pre> |
michael@0 | 103 | </body> |
michael@0 | 104 | </html> |