diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/webaudio/resources/delay-testing.js | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/webaudio/resources/delay-testing.js')
-rw-r--r-- | testing/web-platform/tests/webaudio/resources/delay-testing.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/testing/web-platform/tests/webaudio/resources/delay-testing.js b/testing/web-platform/tests/webaudio/resources/delay-testing.js new file mode 100644 index 0000000000..9033da6730 --- /dev/null +++ b/testing/web-platform/tests/webaudio/resources/delay-testing.js @@ -0,0 +1,66 @@ +let sampleRate = 44100.0; + +let renderLengthSeconds = 4; +let delayTimeSeconds = 0.5; +let toneLengthSeconds = 2; + +function createToneBuffer(context, frequency, numberOfCycles, sampleRate) { + let duration = numberOfCycles / frequency; + let sampleFrameLength = duration * sampleRate; + + let audioBuffer = context.createBuffer(1, sampleFrameLength, sampleRate); + + let n = audioBuffer.length; + let data = audioBuffer.getChannelData(0); + + for (let i = 0; i < n; ++i) + data[i] = Math.sin(frequency * 2.0 * Math.PI * i / sampleRate); + + return audioBuffer; +} + +function checkDelayedResult(renderedBuffer, toneBuffer, should) { + let sourceData = toneBuffer.getChannelData(0); + let renderedData = renderedBuffer.getChannelData(0); + + let delayTimeFrames = delayTimeSeconds * sampleRate; + let toneLengthFrames = toneLengthSeconds * sampleRate; + + let success = true; + + let n = renderedBuffer.length; + + for (let i = 0; i < n; ++i) { + if (i < delayTimeFrames) { + // Check that initial portion is 0 (since signal is delayed). + if (renderedData[i] != 0) { + should( + renderedData[i], 'Initial portion expected to be 0 at frame ' + i) + .beEqualTo(0); + success = false; + break; + } + } else if (i >= delayTimeFrames && i < delayTimeFrames + toneLengthFrames) { + // Make sure that the tone data is delayed by exactly the expected number + // of frames. + let j = i - delayTimeFrames; + if (renderedData[i] != sourceData[j]) { + should(renderedData[i], 'Actual data at frame ' + i) + .beEqualTo(sourceData[j]); + success = false; + break; + } + } else { + // Make sure we have silence after the delayed tone. + if (renderedData[i] != 0) { + should(renderedData[j], 'Final portion at frame ' + i).beEqualTo(0); + success = false; + break; + } + } + } + + should( + success, 'Delaying test signal by ' + delayTimeSeconds + ' sec was done') + .message('correctly', 'incorrectly') +} |