OLD | NEW |
| (Empty) |
1 <!doctype html> | |
2 <html> | |
3 <head> | |
4 <script src="../resources/js-test.js"></script> | |
5 <script src="resources/compatibility.js"></script> | |
6 <script src="resources/audit-util.js"></script> | |
7 <script src="resources/audio-testing.js"></script> | |
8 <script src="resources/fft.js"></script> | |
9 <script src="resources/realtimeanalyser-testing.js"></script> | |
10 <title>Test AnalyserNode Downmixing</title> | |
11 </head> | |
12 | |
13 <body> | |
14 <script> | |
15 description("Test AnalyserNode Downmixing"); | |
16 window.jsTestIsAsync = true; | |
17 | |
18 var sampleRate = 44100; | |
19 var renderFrames = 2048; | |
20 | |
21 var audit = Audit.createTaskRunner(); | |
22 | |
23 var testConfigs = [{ | |
24 channelCount: 1, | |
25 message: "mono", | |
26 floatRelError: 6.3283e-8 | |
27 }, { | |
28 channelCount: 2, | |
29 message: "stereo", | |
30 floatRelError: 1.1681e-7 | |
31 }, { | |
32 channelCount: 4, | |
33 message: "quad", | |
34 floatRelError: 4.9793e-7 | |
35 }, { | |
36 channelCount: 6, | |
37 message: "5.1", | |
38 floatRelError: 2.0215e-7 | |
39 }, { | |
40 channelCount: 3, | |
41 message: "3-channel", | |
42 floatRelError: 6.3283e-8 | |
43 }]; | |
44 | |
45 // Create tasks for each entry in testConfigs | |
46 for (k in testConfigs) { | |
47 audit.defineTask(testConfigs[k].message, (function (config) { | |
48 return function(done) { | |
49 runTest(config).then(done); | |
50 }; | |
51 })(testConfigs[k])); | |
52 } | |
53 | |
54 audit.defineTask("finish", function (done) { | |
55 finishJSTest(); | |
56 done(); | |
57 }); | |
58 | |
59 audit.runTasks(); | |
60 | |
61 // Test downmixing of the AnalyserNode time data. We use the downmixing t
hat automatically | |
62 // happens in the destination node to generate the reference data which is
compared to the | |
63 // data that the Analyser node has captured. | |
64 function runTest(options) { | |
65 // Context MUST have exactly one channel so that we downmix the source t
o mono to generate | |
66 // the reference. | |
67 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | |
68 | |
69 var channels = options.channelCount || 1; | |
70 var source = context.createBufferSource(); | |
71 | |
72 // The signals in each channel. Doesn't matter much what is in here, but
it's best if the | |
73 // values aren't linearly increasing so that the average of the values i
sn't one of the | |
74 // values (in case the implementation does something silly). Only need
to support up to 6 | |
75 // channels. | |
76 var bufferValues = [1, 2, 3, 4, 5, 6].map(function (x) { | |
77 return x * x | |
78 });; | |
79 source.buffer = createConstantBuffer(context, renderFrames, bufferValues
.slice(0, channels)); | |
80 | |
81 var analyser = context.createAnalyser(); | |
82 analyser.smoothingTimeConstant = 0; | |
83 analyser.fftSize = 256; | |
84 | |
85 // Run analyser as an automatic pull node. Do NOT connect to the destina
tion. We don't want | |
86 // the output of the analyser to mix in with the source that is also dir
ectly connected to | |
87 // the destination. | |
88 source.connect(analyser); | |
89 source.connect(context.destination); | |
90 | |
91 var timeData = new Float32Array(analyser.fftSize); | |
92 var freqData = new Float32Array(analyser.frequencyBinCount); | |
93 | |
94 var suspendFrame = analyser.fftSize; | |
95 context.suspend(suspendFrame / context.sampleRate).then(function () { | |
96 analyser.getFloatTimeDomainData(timeData); | |
97 analyser.getFloatFrequencyData(freqData); | |
98 }).then(context.resume.bind(context)); | |
99 | |
100 source.start(); | |
101 return context.startRendering().then(function (renderedBuffer) { | |
102 var success = true; | |
103 | |
104 // Verify the time domain data is correct. | |
105 var prefix = "Analyser downmix " + options.message + " to mono" | |
106 success = Should(prefix + " time data", timeData) | |
107 .beEqualToArray(renderedBuffer.getChannelData(0).subarray(0, analyse
r.fftSize)); | |
108 | |
109 var expectedTimeData = renderedBuffer.getChannelData(0).subarray(0, an
alyser.fftSize); | |
110 var fftOrder = Math.floor(Math.log2(analyser.fftSize)); | |
111 var expectedFreqData = computeFFTMagnitude(expectedTimeData, fftOrder)
.map(linearToDb); | |
112 | |
113 success = compareFloatFreq(prefix + " freq data", freqData, expectedFr
eqData, { | |
114 precision: 6, | |
115 floatRelError: options.floatRelError, | |
116 }) && success; | |
117 | |
118 if (success) | |
119 testPassed(prefix + " downmixed correctly.\n"); | |
120 else | |
121 testFailed(prefix + " not downmixed correctly.\n"); | |
122 }); | |
123 } | |
124 </script> | |
125 </body> | |
126 </html> | |
OLD | NEW |