5 <script src="../fast/js/resources/js-test-pre.js"></script>
6 <script type="text/javascript" src="resources/audio-testing.js"></script>
7 <script type="text/javascript" src="resources/buffer-loader.js"></script>
12 <div id="description"></div>
13 <div id="console"></div>
16 description("Tests that WaveShaperNode applies proper non-linear distortion.");
18 var sampleRate = 44100;
19 var lengthInSeconds = 4;
20 var numberOfRenderFrames = sampleRate * lengthInSeconds;
21 var numberOfCurveFrames = 65536;
27 function generateInputBuffer() {
28 // Create mono input buffer.
29 var buffer = context.createBuffer(1, numberOfRenderFrames, context.sampleRate);
30 var data = buffer.getChannelData(0);
32 // Generate an input vector with values from -1 -> +1 over a duration of lengthInSeconds.
33 // This exercises the full nominal input range and will touch every point of the shaping curve.
34 for (var i = 0; i < numberOfRenderFrames; ++i) {
35 var x = i / numberOfRenderFrames; // 0 -> 1
36 x = 2 * x - 1; // -1 -> +1
43 // Generates a symmetric curve: Math.atan(5 * x) / (0.5 * Math.PI)
44 // (with x == 0 corresponding to the center of the array)
45 // This curve is arbitrary, but would be useful in the real-world.
46 // To some extent, the actual curve we choose is not important in this test,
47 // since the input vector walks through all possible curve values.
48 function generateWaveShapingCurve() {
49 var curve = new Float32Array(numberOfCurveFrames);
51 var n = numberOfCurveFrames;
54 for (var i = 0; i < n; ++i) {
55 var x = (i - n2) / n2;
56 var y = Math.atan(5 * x) / (0.5 * Math.PI);
62 function checkShapedCurve(event) {
63 var buffer = event.renderedBuffer;
65 var inputData = inputBuffer.getChannelData(0);
66 var outputData = buffer.getChannelData(0);
70 // Go through every sample and make sure it has been shaped exactly according to the shaping curve we gave it.
71 for (var i = 0; i < buffer.length; ++i) {
72 var input = inputData[i];
74 // Calculate an index based on input -1 -> +1 with 0 being at the center of the curve data.
75 var index = Math.floor(numberOfCurveFrames * 0.5 * (input + 1));
77 // Clip index to the input range of the curve.
78 // This takes care of input outside of nominal range -1 -> +1
79 index = index < 0 ? 0 : index;
80 index = index > numberOfCurveFrames - 1 ? numberOfCurveFrames - 1 : index;
82 var expectedOutput = waveShapingCurve[index];
84 var output = outputData[i];
86 if (output != expectedOutput) {
93 testPassed("WaveShaperNode properly applied non-linear distortion.");
95 testFailed("WaveShaperNode did not properly apply non-linear distortion.");
102 if (window.testRunner) {
103 testRunner.dumpAsText();
104 testRunner.waitUntilDone();
107 window.jsTestIsAsync = true;
109 // Create offline audio context.
110 context = new webkitOfflineAudioContext(1, numberOfRenderFrames, sampleRate);
112 // source -> waveshaper -> destination
113 var source = context.createBufferSource();
114 var waveshaper = context.createWaveShaper();
115 source.connect(waveshaper);
116 waveshaper.connect(context.destination);
118 // Create an input test vector.
119 inputBuffer = generateInputBuffer();
120 source.buffer = inputBuffer;
122 // We'll apply non-linear distortion according to this shaping curve.
123 waveShapingCurve = generateWaveShapingCurve();
124 waveshaper.curve = waveShapingCurve;
128 context.oncomplete = checkShapedCurve;
129 context.startRendering();
136 <script src="../fast/js/resources/js-test-post.js"></script>