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