1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
4 <script src=
"../resources/js-test.js"></script>
5 <script src=
"resources/compatibility.js"></script>
9 <div id=
"description"></div>
10 <div id=
"console"></div>
13 description("Test WaveShaperNode including values outside the range of [-1,1]");
20 var sampleRate
= 48000;
21 // Must be odd so we have an exact middle point.
23 var scale
= 1 / ((testFrames
- 1) / 2 - 1);
24 // Number of decimal digits to print
27 var diffThreshold
= Math
.pow(10, -decimals
);
29 // Generate reference data
30 function generateReference() {
31 // The curve data is 0, 1, 0, and the input data is a ramp from -1+eps to 1+eps. Then the
32 // output is a ramp from 0 to 1 back to 0.
33 var ref
= new Float32Array(testFrames
);
34 var midPoint
= (testFrames
- 1) / 2;
35 // First sample is below -1 at -1-scale.
37 // Generate ramp up to the mid-point
38 for (var k
= 0; k
< midPoint
; ++k
) {
39 ref
[k
+ 1] = k
* scale
;
41 // The value at the mid-point must be 1, from the curve
43 // Generate a ramp from 1 down to 0
44 for (var k
= midPoint
; k
< testFrames
- 1; ++k
) {
45 ref
[k
+ 1] = 2 - k
* scale
;
47 // The last sample is out of range at 1+scale
48 ref
[testFrames
- 1] = 0;
52 function checkResult (event
) {
53 outputData
= event
.renderedBuffer
.getChannelData(0);
54 reference
= generateReference();
56 // Verify that every output value matches our expected reference value.
57 for (var k
= 0; k
< outputData
.length
; ++k
) {
58 var diff
= outputData
[k
] - reference
[k
];
59 if (Math
.abs(diff
) <= diffThreshold
) {
60 testPassed(bufferData
[k
].toFixed(decimals
) + " -> " + outputData
[k
].toFixed(decimals
) + ".");
62 testFailed(bufferData
[k
].toFixed(decimals
) + " -> " + outputData
[k
].toFixed(decimals
) + ", but expected " + reference
[k
].toFixed(decimals
) + ".");
68 testPassed("All outputs matched expected results.");
70 testFailed("Some outputs did not match expected results.");
76 if (window
.testRunner
) {
77 testRunner
.dumpAsText();
78 testRunner
.waitUntilDone();
81 window
.jsTestIsAsync
= true;
83 context
= new OfflineAudioContext(1, testFrames
, sampleRate
);
84 // Create input values between -1.1 and 1.1
85 var buffer
= context
.createBuffer(1, testFrames
, context
.sampleRate
);
86 bufferData
= buffer
.getChannelData(0);
87 var start
= -1 - scale
;
88 for (var k
= 0; k
< testFrames
; ++k
) {
89 bufferData
[k
] = k
* scale
+ start
;
92 var source
= context
.createBufferSource();
93 source
.buffer
= buffer
;
95 // Create simple waveshaper. It should map -1 to 0, 0 to 1, and +1 to 0 and interpolate
96 // all points in between using a simple linear interpolator.
97 var shaper
= context
.createWaveShaper();
98 var curve
= new Float32Array(3);
102 shaper
.curve
= curve
;
103 source
.connect(shaper
);
104 shaper
.connect(context
.destination
);
107 context
.oncomplete
= checkResult
;
108 context
.startRendering();
112 successfullyParsed
= true;