<!DOCTYPE html>
<html>
  <head>
    <title>
      Test setValueCurveAtTime Copies the Curve Data
    </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/panner-formulas.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let sampleRate = 48000;
      let renderFrames = 1024;
      let renderDuration = renderFrames / sampleRate;

      let audit = Audit.createTaskRunner();

      // Test that changing the curve array to setValueCurveAtTime doesn't
      // change the automation output.
      audit.define('test-copy', (task, should) => {
        // Two-channel context; channel 0 is the test result, and channel 1 is
        // the expected result.
        let context = new OfflineAudioContext(2, renderFrames, sampleRate);

        let source = context.createBufferSource();
        source.buffer = createConstantBuffer(context, 1, 1);
        source.loop = true;

        // Create two gain nodes.  gainRef is the reference with the expected
        // automation results.  gainTest is the test which will have the curve
        // modified.
        let gainTest = context.createGain();
        let gainRef = context.createGain();

        let merger = context.createChannelMerger(2);

        // The actual curve data can be anything, but we use this for
        // simplicity.
        let curveData = [1, 0];
        let curveRef = Float32Array.from(curveData);
        let curveTest = Float32Array.from(curveData);

        // Create the graph.
        source.connect(gainTest);
        source.connect(gainRef);
        gainTest.connect(merger, 0, 0);
        gainRef.connect(merger, 0, 1);
        merger.connect(context.destination);

        // Initialize the gains.
        gainTest.gain.setValueAtTime(0, 0);
        gainRef.gain.setValueAtTime(0, 0);

        // Set up the value curve.  At this point curveTest and curveRef are the
        // same.
        gainTest.gain.setValueCurveAtTime(curveTest, 0, renderDuration);
        gainRef.gain.setValueCurveAtTime(curveRef, 0, renderDuration);

        // After rendering has started, modify curveTest.
        context.suspend(128 / sampleRate)
            .then(function() {
              // Change the values of curve now.  Any transformation is ok as
              // long as curveTest changes.  We do this to make it really
              // obvious.
              for (let k = 0; k < curveTest.length; ++k)
                curveTest[k] = 100 * curveTest[k] + 1;
            })
            .then(context.resume.bind(context));

        // Start the test!
        source.start();

        context.startRendering()
            .then(function(resultBuffer) {
              let testData = resultBuffer.getChannelData(0);
              let refData = resultBuffer.getChannelData(1);

              // The test result and the reference should be identical since
              // changing the curve data should not affect the automation.
              should(testData, 'setValueCurve output').beEqualToArray(refData);
            })
            .then(() => task.done());
      });

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