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/audit-util.js"></script> | |
8 <script src="resources/audio-testing.js"></script> | |
9 </head> | |
10 | |
11 <body> | |
12 <script> | |
13 description("Test Custom Oscillator at Very Low Frequency"); | |
14 window.jsTestIsAsync = true; | |
15 | |
16 // Create a custom oscillator and verify that the parts of a periodic wave
that should be | |
17 // ignored really are ignored. | |
18 | |
19 var sampleRate = 48000; | |
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 // coefficients used for the FFT. In the current implementation, the max n
umber of | |
25 // coefficients is 2048 (for a sample rate of 48 kHz) so the lowest freque
ncy is 24000/2048 = | |
26 // 11.78 Hz. | |
27 var desiredFrequencyHz = 1; | |
28 | |
29 // Minimum allowed SNR between the actual oscillator and the expected resu
lt. Experimentally | |
30 // determined. | |
31 var snrThreshold = 130; | |
32 | |
33 var context; | |
34 var osc; | |
35 var actual; | |
36 | |
37 var audit = Audit.createTaskRunner(); | |
38 | |
39 // Compute the SNR between the actual result and expected cosine wave | |
40 function checkCosineResult(result, freq, sampleRate) { | |
41 var signal = 0; | |
42 var noise = 0; | |
43 var omega = 2 * Math.PI * freq / sampleRate; | |
44 | |
45 actual = result.getChannelData(0); | |
46 | |
47 for (var k = 0; k < actual.length; ++k) { | |
48 var x = Math.cos(omega * k); | |
49 var diff = x - actual[k]; | |
50 signal += x * x; | |
51 noise += diff * diff; | |
52 } | |
53 | |
54 var snr = 10 * Math.log10(signal / noise); | |
55 | |
56 Should("SNR of " + desiredFrequencyHz + " Hz sine wave", snr, { | |
57 brief: true | |
58 }).beGreaterThanOrEqualTo(snrThreshold); | |
59 testPassed("PeriodicWave coefficients that must be ignored were correctl
y ignored."); | |
60 } | |
61 | |
62 function runTest() { | |
63 context = new OfflineAudioContext(1, sampleRate, sampleRate); | |
64 osc = context.createOscillator(); | |
65 | |
66 // Create the custom oscillator. For simplicity of testing, we use just
a cosine wave, but | |
67 // the initial elements of the real and imaginary parts are explicitly s
et to non-zero to | |
68 // test that they are ignored. | |
69 var r = new Float32Array(2); | |
70 var i = new Float32Array(2); | |
71 r[0] = 1; // DC component to be ignored | |
72 r[1] = 1; // Fundamental | |
73 i[0] = 1; // Sine term that doesn't actually exist in a Fourier series | |
74 i[1] = 0; | |
75 var wave = context.createPeriodicWave(r, i); | |
76 | |
77 osc.setPeriodicWave(wave); | |
78 osc.frequency.value = desiredFrequencyHz; | |
79 osc.connect(context.destination); | |
80 osc.start(); | |
81 context.startRendering().then(function (buffer) { | |
82 checkCosineResult(buffer, desiredFrequencyHz, sampleRate); | |
83 }).then(finishJSTest); | |
84 }; | |
85 | |
86 runTest(); | |
87 successfullyParsed = true; | |
88 </script> | |
89 </body> | |
90 </html> | |
OLD | NEW |