Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!doctype html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <title>Test Custom Oscillator at Very Low Frequency</title> | |
| 5 <script src="../resources/js-test.js"></script> | |
| 6 <script src="resources/compatibility.js"></script> | |
| 7 <script src="resources/audio-testing.js"></script> | |
| 8 </head> | |
| 9 | |
| 10 <body> | |
| 11 <script> | |
| 12 description("Test Custom Oscillator at Very Low Frequency"); | |
| 13 window.jsTestIsAsync = true; | |
| 14 | |
| 15 // Create a custom oscillator and verify that the parts of a periodic wave that should be | |
| 16 // ignored really are ignored. | |
| 17 | |
| 18 var sampleRate = 48000; | |
| 19 var highSampleRate = 192000; | |
|
hongchan
2015/07/22 21:01:19
This is unused.
Raymond Toy
2015/07/23 15:33:09
Done.
| |
| 20 | |
| 21 // The desired frequency of the oscillator. The value to be used depends on the | |
| 22 // implementation of the PeriodicWave and should be less than then lowest fundamental | |
| 23 // frequency. The lowest frequency is the Nyquist frequency divided by the max number of | |
| 24 // partials used for the FFT. In the current implementation, the max numbe r of partials is | |
| 25 // 2048 so the lowest frequency is 24000/2048 = 11.78 Hz. | |
|
hongchan
2015/07/22 21:01:19
Partials are not linearly arranged over the freque
Raymond Toy
2015/07/23 15:33:09
I've rewritten the comment. Let me if it's better.
| |
| 26 var desiredFrequencyHz = 1; | |
| 27 | |
| 28 // Minimum allowed SNR between the actual oscillator and the expected resu lt. Experimentally | |
| 29 // determined. | |
| 30 var snrThreshold = 130; | |
| 31 | |
| 32 var context; | |
| 33 var osc; | |
| 34 var actual; | |
| 35 | |
| 36 var audit = Audit.createTaskRunner(); | |
| 37 | |
| 38 function checkSineResult(result, freq, rate) { | |
|
hongchan
2015/07/22 21:01:19
Perhaps would |checkCosineResult| fit better for t
Raymond Toy
2015/07/23 15:33:09
Done.
| |
| 39 // Compute the SNR between the actual result and expected cosine wave | |
|
hongchan
2015/07/22 21:01:19
How about moving this to the line 37? Also a perio
Raymond Toy
2015/07/23 15:33:09
Done.
| |
| 40 var signal = 0; | |
| 41 var noise = 0; | |
| 42 var omega = 2 * Math.PI * freq / rate; | |
| 43 | |
| 44 actual = result.getChannelData(0); | |
| 45 | |
| 46 for (var k = 0; k < actual.length; ++k) { | |
| 47 var x = Math.cos(omega * k); | |
| 48 var diff = x - actual[k]; | |
| 49 signal += x * x; | |
| 50 noise += diff * diff; | |
| 51 } | |
| 52 | |
| 53 var snr = 10 * Math.log10(signal / noise); | |
| 54 | |
| 55 Should("SNR of " + desiredFrequencyHz + " Hz sine wave", snr).beGreaterT hanOrEqualTo(snrThreshold); | |
| 56 testPassed("PeriodicWave coefficients that must be ignored were correctl y ignored."); | |
| 57 } | |
| 58 | |
| 59 function runTest() { | |
| 60 context = new OfflineAudioContext(1, sampleRate, sampleRate); | |
| 61 osc = context.createOscillator(); | |
| 62 | |
| 63 // Create the custom oscillator. For simplicity of testing, we use just a cosine wave, but | |
| 64 // the initial elements of the real and imaginary parts are explicitly s et to non-zero to | |
| 65 // test that they are ignored. | |
| 66 var r = new Float32Array(2); | |
| 67 var i = new Float32Array(2); | |
| 68 r[0] = 1; // DC component to be ignored | |
| 69 r[1] = 1; // Fundamental | |
| 70 i[0] = 1; // Sine term that doesn't actually exist in a Fourier series | |
| 71 i[1] = 0; | |
| 72 var wave = context.createPeriodicWave(r, i); | |
| 73 | |
| 74 osc.setPeriodicWave(wave); | |
| 75 osc.frequency.value = desiredFrequencyHz; | |
| 76 osc.connect(context.destination); | |
| 77 osc.start(); | |
| 78 context.startRendering().then(function (b) { | |
|
hongchan
2015/07/22 21:01:19
Let's use |buffer| instead of |b|.
| |
| 79 checkSineResult(b, desiredFrequencyHz, sampleRate); | |
| 80 }).then(finishJSTest); | |
| 81 }; | |
| 82 | |
| 83 runTest(); | |
| 84 successfullyParsed = true; | |
| 85 </script> | |
| 86 </body> | |
| 87 </html> | |
| OLD | NEW |