<!DOCTYPE html>
<html>
  <head>
    <title>
      audiobuffersource-playbackrate-modulation.html
    </title>
    <script src="../../imported/w3c/web-platform-tests/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/audiobuffersource-testing.js"></script>
    <script src="../resources/buffer-loader.js"></script>
    <script src="../resources/audio-file-utils.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let sampleRate = 44100;
      let duration = 0.25;

      let context =
          new OfflineAudioContext(1, sampleRate * duration, sampleRate);
      let referenceBuffer;

      let audit = Audit.createTaskRunner();

      // Task: Load the reference file asynchronously. In order to create a new
      // reference file, use the task 'generate-reference' below.
      audit.define('load-reference', (task, should) => {
        let loader = new BufferLoader(
            context, ['resources/audiobuffersource-playbackrate-modulation-expected.wav'],
            function(bufferList) {
              referenceBuffer = bufferList[0];
              should(true, 'Loaded reference file').beTrue();
              task.done();
            });

        loader.load();
      });


      // Task: Render the actual buffer and compare with the reference.
      audit.define('generate-verify', (task, should) => {

        // With this setting, the playback rate will be changing continuously
        // and repeatedly within the range of [0, 200] around 100Hz, based on
        // the input from the oscillator.
        createSawtoothWithModulation(context, 'playbackRate', 100, 100);

        context.startRendering()
            .then(function(renderedBuffer) {
              let actual = renderedBuffer.getChannelData(0);
              let expected = referenceBuffer.getChannelData(0);

              // Compare two buffers with arbitrary (yet reasonable)
              // constraints. There parameters are determined by try bot
              // experiments.
              compareBuffersWithConstraints(should, actual, expected, {
                prefix: 'Verify',
                thresholdSNR: 93.274,
                thresholdDiffULP: 1.0519,
                thresholdDiffCount: 0,
                bitDepth: 16
              });

              let filename =
                  'resources/audiobuffersource-playbackrate-modulation-actual.wav';
              if (downloadAudioBuffer(renderedBuffer, filename))
                should(true, 'Saved reference file').message(filename, '');
            })
            .then(() => task.done());
      });

      audit.run();
    </script>
  </body>
</html>
