Update web-platform-tests to revision 68a256f49be380ca4add535ce8ece9de28820e6b

This commit is contained in:
WPT Sync Bot 2018-02-04 20:08:48 -05:00
parent e54935c25a
commit cd5bf022bd
178 changed files with 6082 additions and 795 deletions

View file

@ -0,0 +1,203 @@
let StereoPannerTest = (function() {
// Constants
let PI_OVER_TWO = Math.PI * 0.5;
let gSampleRate = 44100;
// Time step when each panner node starts.
let gTimeStep = 0.001;
// How many panner nodes to create for the test
let gNodesToCreate = 100;
// Total render length for all of our nodes.
let gRenderLength = gTimeStep * (gNodesToCreate + 1) + gSampleRate;
// Calculates channel gains based on equal power panning model.
// See: http://webaudio.github.io/web-audio-api/#panning-algorithm
function getChannelGain(pan, numberOfChannels) {
// The internal panning clips the pan value between -1, 1.
pan = Math.min(Math.max(pan, -1), 1);
let gainL, gainR;
// Consider number of channels and pan value's polarity.
if (numberOfChannels == 1) {
let panRadian = (pan * 0.5 + 0.5) * PI_OVER_TWO;
gainL = Math.cos(panRadian);
gainR = Math.sin(panRadian);
} else {
let panRadian = (pan <= 0 ? pan + 1 : pan) * PI_OVER_TWO;
if (pan <= 0) {
gainL = 1 + Math.cos(panRadian);
gainR = Math.sin(panRadian);
} else {
gainL = Math.cos(panRadian);
gainR = 1 + Math.sin(panRadian);
}
}
return {gainL: gainL, gainR: gainR};
}
/**
* Test implementation class.
* @param {Object} options Test options
* @param {Object} options.description Test description
* @param {Object} options.numberOfInputChannels Number of input channels
*/
function Test(should, options) {
// Primary test flag.
this.success = true;
this.should = should;
this.context = null;
this.prefix = options.prefix;
this.numberOfInputChannels = (options.numberOfInputChannels || 1);
switch (this.numberOfInputChannels) {
case 1:
this.description = 'Test for mono input';
break;
case 2:
this.description = 'Test for stereo input';
break;
}
// Onset time position of each impulse.
this.onsets = [];
// Pan position value of each impulse.
this.panPositions = [];
// Locations of where the impulses aren't at the expected locations.
this.errors = [];
// The index of the current impulse being verified.
this.impulseIndex = 0;
// The max error we allow between the rendered impulse and the
// expected value. This value is experimentally determined. Set
// to 0 to make the test fail to see what the actual error is.
this.maxAllowedError = 1.3e-6;
// Max (absolute) error and the index of the maxima for the left
// and right channels.
this.maxErrorL = 0;
this.maxErrorR = 0;
this.maxErrorIndexL = 0;
this.maxErrorIndexR = 0;
// The maximum value to use for panner pan value. The value will range from
// -panLimit to +panLimit.
this.panLimit = 1.0625;
}
Test.prototype.init = function() {
this.context = new OfflineAudioContext(2, gRenderLength, gSampleRate);
};
// Prepare an audio graph for testing. Create multiple impulse generators and
// panner nodes, then play them sequentially while varying the pan position.
Test.prototype.prepare = function() {
let impulse;
let impulseLength = Math.round(gTimeStep * gSampleRate);
let sources = [];
let panners = [];
// Moves the pan value for each panner by pan step unit from -2 to 2.
// This is to check if the internal panning value is clipped properly.
let panStep = (2 * this.panLimit) / (gNodesToCreate - 1);
if (this.numberOfInputChannels === 1) {
impulse = createImpulseBuffer(this.context, impulseLength);
} else {
impulse = createStereoImpulseBuffer(this.context, impulseLength);
}
for (let i = 0; i < gNodesToCreate; i++) {
sources[i] = this.context.createBufferSource();
panners[i] = this.context.createStereoPanner();
sources[i].connect(panners[i]);
panners[i].connect(this.context.destination);
sources[i].buffer = impulse;
panners[i].pan.value = this.panPositions[i] = panStep * i - this.panLimit;
// Store the onset time position of impulse.
this.onsets[i] = gTimeStep * i;
sources[i].start(this.onsets[i]);
}
};
Test.prototype.verify = function() {
let chanL = this.renderedBufferL;
let chanR = this.renderedBufferR;
for (let i = 0; i < chanL.length; i++) {
// Left and right channels must start at the same instant.
if (chanL[i] !== 0 || chanR[i] !== 0) {
// Get amount of error between actual and expected gain.
let expected = getChannelGain(
this.panPositions[this.impulseIndex], this.numberOfInputChannels);
let errorL = Math.abs(chanL[i] - expected.gainL);
let errorR = Math.abs(chanR[i] - expected.gainR);
if (errorL > this.maxErrorL) {
this.maxErrorL = errorL;
this.maxErrorIndexL = this.impulseIndex;
}
if (errorR > this.maxErrorR) {
this.maxErrorR = errorR;
this.maxErrorIndexR = this.impulseIndex;
}
// Keep track of the impulses that didn't show up where we expected
// them to be.
let expectedOffset =
timeToSampleFrame(this.onsets[this.impulseIndex], gSampleRate);
if (i != expectedOffset) {
this.errors.push({actual: i, expected: expectedOffset});
}
this.impulseIndex++;
}
}
};
Test.prototype.showResult = function() {
this.should(this.impulseIndex, this.prefix + 'Number of impulses found')
.beEqualTo(gNodesToCreate);
this.should(
this.errors.length,
this.prefix + 'Number of impulse at the wrong offset')
.beEqualTo(0);
this.should(this.maxErrorL, this.prefix + 'Left channel error magnitude')
.beLessThanOrEqualTo(this.maxAllowedError);
this.should(this.maxErrorR, this.prefix + 'Right channel error magnitude')
.beLessThanOrEqualTo(this.maxAllowedError);
};
Test.prototype.run = function() {
this.init();
this.prepare();
return this.context.startRendering().then(renderedBuffer => {
this.renderedBufferL = renderedBuffer.getChannelData(0);
this.renderedBufferR = renderedBuffer.getChannelData(1);
this.verify();
this.showResult();
});
};
return {
create: function(should, options) {
return new Test(should, options);
}
};
})();

View file

@ -0,0 +1,261 @@
<!DOCTYPE html>
<html>
<head>
<title>
Test StereoPannerNode Has No Dezippering
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../resources/audit-util.js"></script>
<script src="../../resources/audit.js"></script>
</head>
<body>
<script id="layout-test-code">
// Arbitrary sample rate except that it should be a power of two to
// eliminate any round-off in computing frame boundaries.
let sampleRate = 16384;
let audit = Audit.createTaskRunner();
audit.define(
{
label: 'test mono input',
description: 'Test StereoPanner with mono input has no dezippering'
},
(task, should) => {
let context = new OfflineAudioContext(2, sampleRate, sampleRate);
let src = new ConstantSourceNode(context, {offset: 1});
let p = new StereoPannerNode(context, {pan: -1});
src.connect(p).connect(context.destination);
src.start();
// Frame at which to change pan value.
let panFrame = 256;
context.suspend(panFrame / context.sampleRate)
.then(() => p.pan.value = 1)
.then(() => context.resume());
context.startRendering()
.then(renderedBuffer => {
let c0 = renderedBuffer.getChannelData(0);
let c1 = renderedBuffer.getChannelData(1);
// The first part should be full left.
should(
c0.slice(0, panFrame), 'Mono: Left channel, pan = -1: ')
.beConstantValueOf(1);
should(
c1.slice(0, panFrame), 'Mono: Right channel, pan = -1:')
.beConstantValueOf(0);
// The second part should be full right, but due to roundoff,
// the left channel won't be exactly zero. Compare the left
// channel against zero with a threshold instead.
let tail = c0.slice(panFrame);
let zero = new Float32Array(tail.length);
should(c0.slice(panFrame), 'Mono: Left channel, pan = 1: ')
.beCloseToArray(zero, {absoluteThreshold: 6.1233e-17});
should(c1.slice(panFrame), 'Mono: Right channel, pan = 1:')
.beConstantValueOf(1);
})
.then(() => task.done());
});
audit.define(
{
label: 'test stereo input',
description:
'Test StereoPanner with stereo input has no dezippering'
},
(task, should) => {
let context = new OfflineAudioContext(2, sampleRate, sampleRate);
// Create stereo source from two constant source nodes.
let s0 = new ConstantSourceNode(context, {offset: 1});
let s1 = new ConstantSourceNode(context, {offset: 2});
let merger = new ChannelMergerNode(context, {numberOfInputs: 2});
s0.connect(merger, 0, 0);
s1.connect(merger, 0, 1);
let p = new StereoPannerNode(context, {pan: -1});
merger.connect(p).connect(context.destination);
s0.start();
s1.start();
// Frame at which to change pan value.
let panFrame = 256;
context.suspend(panFrame / context.sampleRate)
.then(() => p.pan.value = 1)
.then(() => context.resume());
context.startRendering()
.then(renderedBuffer => {
let c0 = renderedBuffer.getChannelData(0);
let c1 = renderedBuffer.getChannelData(1);
// The first part should be full left.
should(
c0.slice(0, panFrame), 'Stereo: Left channel, pan = -1: ')
.beConstantValueOf(3);
should(
c1.slice(0, panFrame), 'Stereo: Right channel, pan = -1:')
.beConstantValueOf(0);
// The second part should be full right, but due to roundoff,
// the left channel won't be exactly zero. Compare the left
// channel against zero with a threshold instead.
let tail = c0.slice(panFrame);
let zero = new Float32Array(tail.length);
should(c0.slice(panFrame), 'Stereo: Left channel, pan = 1: ')
.beCloseToArray(zero, {absoluteThreshold: 6.1233e-17});
should(c1.slice(panFrame), 'Stereo: Right channel, pan = 1:')
.beConstantValueOf(3);
})
.then(() => task.done());
});
audit.define(
{
label: 'test mono input setValue',
description: 'Test StereoPanner with mono input value setter ' +
'vs setValueAtTime'
},
(task, should) => {
let context = new OfflineAudioContext(4, sampleRate, sampleRate);
let src = new OscillatorNode(context);
src.start();
testWithSetValue(context, src, should, {
prefix: 'Mono'
}).then(() => task.done());
});
audit.define(
{
label: 'test stereo input setValue',
description: 'Test StereoPanner with mono input value setter ' +
' vs setValueAtTime'
},
(task, should) => {
let context = new OfflineAudioContext(4, sampleRate, sampleRate);
let src0 = new OscillatorNode(context, {frequency: 800});
let src1 = new OscillatorNode(context, {frequency: 250});
let merger = new ChannelMergerNode(context, {numberOfChannels: 2});
src0.connect(merger, 0, 0);
src1.connect(merger, 0, 1);
src0.start();
src1.start();
testWithSetValue(context, merger, should, {
prefix: 'Stereo'
}).then(() => task.done());
});
audit.define(
{
label: 'test mono input automation',
description: 'Test StereoPanner with mono input and automation'
},
(task, should) => {
let context = new OfflineAudioContext(4, sampleRate, sampleRate);
let src0 = new OscillatorNode(context, {frequency: 800});
let src1 = new OscillatorNode(context, {frequency: 250});
let merger = new ChannelMergerNode(context, {numberOfChannels: 2});
src0.connect(merger, 0, 0);
src1.connect(merger, 0, 1);
src0.start();
src1.start();
let mod = new OscillatorNode(context, {frequency: 100});
mod.start();
testWithSetValue(context, merger, should, {
prefix: 'Modulated Stereo',
modulator: (testNode, refNode) => {
mod.connect(testNode.pan);
mod.connect(refNode.pan);
}
}).then(() => task.done());
});
function testWithSetValue(context, src, should, options) {
let merger = new ChannelMergerNode(
context, {numberOfInputs: context.destination.channelCount});
merger.connect(context.destination);
let pannerRef = new StereoPannerNode(context, {pan: -0.3});
let pannerTest =
new StereoPannerNode(context, {pan: pannerRef.pan.value});
let refSplitter =
new ChannelSplitterNode(context, {numberOfOutputs: 2});
let testSplitter =
new ChannelSplitterNode(context, {numberOfOutputs: 2});
pannerRef.connect(refSplitter);
pannerTest.connect(testSplitter);
testSplitter.connect(merger, 0, 0);
testSplitter.connect(merger, 1, 1);
refSplitter.connect(merger, 0, 2);
refSplitter.connect(merger, 1, 3);
src.connect(pannerRef);
src.connect(pannerTest);
let changeTime = 3 * RENDER_QUANTUM_FRAMES / context.sampleRate;
// An arbitrary position, different from the default pan value.
let newPanPosition = .71;
pannerRef.pan.setValueAtTime(newPanPosition, changeTime);
context.suspend(changeTime)
.then(() => pannerTest.pan.value = newPanPosition)
.then(() => context.resume());
if (options.modulator) {
options.modulator(pannerTest, pannerRef);
}
return context.startRendering().then(renderedBuffer => {
let actual = new Array(2);
let expected = new Array(2);
actual[0] = renderedBuffer.getChannelData(0);
actual[1] = renderedBuffer.getChannelData(1);
expected[0] = renderedBuffer.getChannelData(2);
expected[1] = renderedBuffer.getChannelData(3);
let label = ['Left', 'Right'];
for (let k = 0; k < 2; ++k) {
let match =
should(
actual[k],
options.prefix + ' ' + label[k] + ' .value setter output')
.beEqualToArray(expected[k]);
should(
match,
options.prefix + ' ' + label[k] +
' .value setter output matches setValueAtTime output')
.beTrue();
}
});
}
audit.run();
</script>
</body>
</html>

View file

@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<title>
stereopannernode-basic.html
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../resources/audit-util.js"></script>
<script src="../../resources/audit.js"></script>
</head>
<body>
<script id="layout-test-code">
let audit = Audit.createTaskRunner();
audit.define(
{
label: 'test',
description:
'Attributes and basic functionality of StereoPannerNode'
},
(task, should) => {
let context = new AudioContext();
let panner = context.createStereoPanner();
should(panner.numberOfInputs, 'panner.numberOfInputs').beEqualTo(1);
should(panner.numberOfOutputs, 'panner.numberOfOutputs')
.beEqualTo(1);
should(panner.pan.defaultValue, 'panner.pan.defaultValue')
.beEqualTo(0.0);
should(() => panner.pan.value = 1.0, 'panner.pan.value = 1.0')
.notThrow();
should(panner.pan.value, 'panner.pan.value').beEqualTo(1.0);
should(() => panner.channelCount = 1, 'panner.channelCount = 1')
.notThrow();
should(() => panner.channelCount = 3, 'panner.channelCount = 3')
.throw();
should(
() => panner.channelCountMode = 'explicit',
'panner.channelCountMode = "explicit"')
.notThrow();
should(
() => panner.channelCountMode = 'max',
'panner.channelCountMode = "max"')
.throw();
task.done();
});
audit.run();
</script>
</body>
</html>

View file

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<title>
stereopannernode-panning.html
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../resources/audit-util.js"></script>
<script src="../../resources/audit.js"></script>
<script src="../../resources/stereopanner-testing.js"></script>
</head>
<body>
<script id="layout-test-code">
let audit = Audit.createTaskRunner();
audit.define('mono-test', (task, should) => {
StereoPannerTest
.create(should, {numberOfInputChannels: 1, prefix: 'Mono: '})
.run()
.then(() => task.done());
});
audit.define('stereo-test', (task, should) => {
StereoPannerTest
.create(should, {numberOfInputChannels: 2, prefix: 'Stereo: '})
.run()
.then(() => task.done());
});
audit.run();
</script>
</body>
</html>

View file

@ -0,0 +1,100 @@
<!DOCTYPE html>
<html>
<head>
<title>
Test WaveShaper Copies Curve Data
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../resources/audit-util.js"></script>
<script src="../../resources/audit.js"></script>
</head>
<body>
<script id="layout-test-code">
// Sample rate and number of frames are fairly arbitrary. We need to
// render, however, at least 384 frames. 1024 is a nice small value.
let sampleRate = 16000;
let renderFrames = 1024;
let audit = Audit.createTaskRunner();
audit.define(
{
label: 'test copying',
description: 'Modifying curve should not modify WaveShaper'
},
(task, should) => {
// Two-channel context; channel 0 contains the test data and channel
// 1 contains the expected result. Channel 1 has the normal
// WaveShaper output and channel 0 has the WaveShaper output with a
// modified curve.
let context = new OfflineAudioContext(2, renderFrames, sampleRate);
// Just use a default oscillator as the source. Doesn't really
// matter what we use.
let src = context.createOscillator();
src.type = 'sawtooth';
// Create the wave shapers: ws0 is the test shaper, and ws1 is the
// reference wave shaper.
let ws0 = context.createWaveShaper();
let ws1 = context.createWaveShaper();
// Wave shaper curves. Doesn't really matter what we use as long as
// it modifies the input in some way. Thus, keep it simple and just
// invert the input.
let desiredCurve = [1, 0, -1];
let curve0 = Float32Array.from(desiredCurve);
let curve1 = Float32Array.from(desiredCurve);
ws0.curve = curve0;
ws1.curve = curve1;
let merger = context.createChannelMerger(2);
// Connect the graph
src.connect(ws0);
src.connect(ws1);
ws0.connect(merger, 0, 0);
ws1.connect(merger, 0, 1);
merger.connect(context.destination);
// Let the context run for a bit and then modify the curve for ws0.
// Doesn't really matter what we modify the curve to as long as it's
// different.
context.suspend(256 / context.sampleRate)
.then(() => {
should(
() => {
curve0[0] = -0.5;
curve0[1] = 0.125;
curve0[2] = 0.75;
},
`Modifying curve array at time ${context.currentTime}`)
.notThrow();
})
.then(context.resume.bind(context));
src.start();
context.startRendering()
.then(function(renderedBuffer) {
let actual = renderedBuffer.getChannelData(0);
let expected = renderedBuffer.getChannelData(1);
// Modifying the wave shaper curve should not modify the
// output so the outputs from the two wave shaper nodes should
// be exactly identical.
should(actual, 'Output of WaveShaper with modified curve')
.beEqualToArray(expected);
})
.then(() => task.done());
});
audit.run();
</script>
</body>
</html>

View file

@ -0,0 +1,110 @@
<!DOCTYPE html>
<html>
<head>
<title>
waveshaper-limits.html
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../resources/audit-util.js"></script>
<script src="../../resources/audit.js"></script>
</head>
<body>
<script id="layout-test-code">
let audit = Audit.createTaskRunner();
let context;
let bufferData;
let outputData;
let reference;
let sampleRate = 48000;
// Must be odd so we have an exact middle point.
let testFrames = 23;
let scale = 1 / ((testFrames - 1) / 2 - 1);
// Number of decimal digits to print
let decimals = 6;
// Required accuracy
let diffThreshold = Math.pow(10, -decimals);
// Generate reference data
function generateReference() {
// The curve data is 0, 1, 0, and the input data is a ramp from -1+eps
// to 1+eps. Then the output is a ramp from 0 to 1 back to 0.
let ref = new Float32Array(testFrames);
let midPoint = (testFrames - 1) / 2;
// First sample is below -1 at -1-scale.
ref[0] = 0;
// Generate ramp up to the mid-point
for (let k = 0; k < midPoint; ++k) {
ref[k + 1] = k * scale;
}
// The value at the mid-point must be 1, from the curve
ref[midPoint] = 1;
// Generate a ramp from 1 down to 0
for (let k = midPoint; k < testFrames - 1; ++k) {
ref[k + 1] = 2 - k * scale;
}
// The last sample is out of range at 1+scale
ref[testFrames - 1] = 0;
return ref;
}
function checkResult(renderedBuffer, should) {
outputData = renderedBuffer.getChannelData(0);
reference = generateReference();
let success = true;
// Verify that every output value matches our expected reference value.
for (let k = 0; k < outputData.length; ++k) {
let diff = outputData[k] - reference[k];
should(
Math.abs(diff),
'Max error mapping ' + bufferData[k].toFixed(decimals) + ' to ' +
outputData[k].toFixed(decimals))
.beLessThanOrEqualTo(diffThreshold);
}
}
audit.define(
{
label: 'test',
description:
'WaveShaperNode including values outside the range of [-1,1]'
},
function(task, should) {
context = new OfflineAudioContext(1, testFrames, sampleRate);
// Create input values between -1.1 and 1.1
let buffer =
context.createBuffer(1, testFrames, context.sampleRate);
bufferData = new Float32Array(testFrames);
let start = -1 - scale;
for (let k = 0; k < testFrames; ++k) {
bufferData[k] = k * scale + start;
}
buffer.copyToChannel(bufferData, 0);
let source = context.createBufferSource();
source.buffer = buffer;
// Create simple waveshaper. It should map -1 to 0, 0 to 1, and +1
// to 0 and interpolate all points in between using a simple linear
// interpolator.
let shaper = context.createWaveShaper();
let curve = new Float32Array(3);
curve[0] = 0;
curve[1] = 1;
curve[2] = 0;
shaper.curve = curve;
source.connect(shaper);
shaper.connect(context.destination);
source.start();
context.startRendering()
.then(buffer => checkResult(buffer, should))
.then(() => task.done());
});
audit.run();
</script>
</body>
</html>

View file

@ -0,0 +1,61 @@
<!DOCTYPE html>
<html>
<head>
<title>
Simple Tests of WaveShaperNode
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../resources/audit-util.js"></script>
<script src="../../resources/audit.js"></script>
</head>
<body>
<script id="layout-test-code">
let audit = Audit.createTaskRunner();
audit.define('simple', (task, should) => {
let context = new OfflineAudioContext(1, 1, 48000);
let shaper = context.createWaveShaper();
// Verify default values are correct.
should(shaper.curve, 'Initial WaveShaper.curve').beEqualTo(null);
should(shaper.oversample, 'Initial WaveShaper.oversample')
.beEqualTo('none');
// Set oversample and verify that it is set correctly.
should(() => shaper.oversample = '2x', 'Setting oversample to "2x"')
.notThrow();
should(shaper.oversample, 'Waveshaper.oversample = "2x"')
.beEqualTo('2x');
should(() => shaper.oversample = '4x', 'Setting oversample to "4x"')
.notThrow();
should(shaper.oversample, 'Waveshaper.oversample = "4x"')
.beEqualTo('4x');
should(
() => shaper.oversample = 'invalid',
'Setting oversample to "invalid"')
.notThrow();
should(shaper.oversample, 'Waveshaper.oversample = "invalid"')
.beEqualTo('4x');
// Set the curve and verify that the returned curve is the same as what
// it was set to.
let curve = Float32Array.from([-1, 0.25, .75]);
should(() => shaper.curve = curve, 'Setting curve to [' + curve + ']')
.notThrow();
should(shaper.curve, 'WaveShaper.curve').beEqualToArray(curve);
// Verify setting the curve to null works.
should(() => shaper.curve = null, 'Setting curve back to null')
.notThrow();
should(shaper.curve, 'Waveshaper.curve = null').beEqualTo(null);
task.done();
});
audit.run();
</script>
</body>
</html>

View file

@ -0,0 +1,127 @@
<!DOCTYPE html>
<html>
<head>
<title>
waveshaper.html
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../resources/audit-util.js"></script>
<script src="../../resources/audit.js"></script>
<script src="../../resources/buffer-loader.js"></script>
</head>
<body>
<script id="layout-test-code">
let audit = Audit.createTaskRunner();
let sampleRate = 44100;
let lengthInSeconds = 4;
let numberOfRenderFrames = sampleRate * lengthInSeconds;
let numberOfCurveFrames = 65536;
let inputBuffer;
let waveShapingCurve;
let context;
function generateInputBuffer() {
// Create mono input buffer.
let buffer =
context.createBuffer(1, numberOfRenderFrames, context.sampleRate);
let data = buffer.getChannelData(0);
// Generate an input vector with values from -1 -> +1 over a duration of
// lengthInSeconds. This exercises the full nominal input range and will
// touch every point of the shaping curve.
for (let i = 0; i < numberOfRenderFrames; ++i) {
let x = i / numberOfRenderFrames; // 0 -> 1
x = 2 * x - 1; // -1 -> +1
data[i] = x;
}
return buffer;
}
// Generates a symmetric curve: Math.atan(5 * x) / (0.5 * Math.PI)
// (with x == 0 corresponding to the center of the array)
// This curve is arbitrary, but would be useful in the real-world.
// To some extent, the actual curve we choose is not important in this
// test, since the input vector walks through all possible curve values.
function generateWaveShapingCurve() {
let curve = new Float32Array(numberOfCurveFrames);
let n = numberOfCurveFrames;
let n2 = n / 2;
for (let i = 0; i < n; ++i) {
let x = (i - n2) / n2;
let y = Math.atan(5 * x) / (0.5 * Math.PI);
}
return curve;
}
function checkShapedCurve(buffer, should) {
let inputData = inputBuffer.getChannelData(0);
let outputData = buffer.getChannelData(0);
let success = true;
// Go through every sample and make sure it has been shaped exactly
// according to the shaping curve we gave it.
for (let i = 0; i < buffer.length; ++i) {
let input = inputData[i];
// Calculate an index based on input -1 -> +1 with 0 being at the
// center of the curve data.
let index = Math.floor(numberOfCurveFrames * 0.5 * (input + 1));
// Clip index to the input range of the curve.
// This takes care of input outside of nominal range -1 -> +1
index = index < 0 ? 0 : index;
index =
index > numberOfCurveFrames - 1 ? numberOfCurveFrames - 1 : index;
let expectedOutput = waveShapingCurve[index];
let output = outputData[i];
if (output != expectedOutput) {
success = false;
break;
}
}
should(
success, 'WaveShaperNode applied non-linear distortion correctly')
.beTrue();
}
audit.define('test', function(task, should) {
// Create offline audio context.
context = new OfflineAudioContext(1, numberOfRenderFrames, sampleRate);
// source -> waveshaper -> destination
let source = context.createBufferSource();
let waveshaper = context.createWaveShaper();
source.connect(waveshaper);
waveshaper.connect(context.destination);
// Create an input test vector.
inputBuffer = generateInputBuffer();
source.buffer = inputBuffer;
// We'll apply non-linear distortion according to this shaping curve.
waveShapingCurve = generateWaveShapingCurve();
waveshaper.curve = waveShapingCurve;
source.start(0);
context.startRendering()
.then(buffer => checkShapedCurve(buffer, should))
.then(task.done.bind(task));
});
audit.run();
</script>
</body>
</html>