<!doctype html>
<html>
  <head>
    <title>Test Biquad Tail-Time</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/biquad-filters.js"></script>
    <script src="test-tail-time.js"></script>
  </head>

  <body>
    <script>
      let audit = Audit.createTaskRunner();

      let sampleRate = 16384;
      let renderSeconds = 1;
      let renderFrames = renderSeconds * sampleRate;

      // For a highshelf filter:
      //   b0 = A*[(A+1)+(A−1)*cos(w0)+2*as*sqrt(A)]
      //   b1 = 2*A*[(A-1)+(A+1)*cos(w0)]
      //   b2 = A*[(A+1)+(A−1)*cos(w0)-2*as*sqrt(A)]
      //   a0 = (A+1)-(A-1)*cos(w0)+2*as*sqrt(A)
      //   a1 = -2*[(A-1)-(A+1)*cos(w0)]
      //   a2 = (A+1)-(A-1)*cos(w0)-2*as*sqrt(A)
      //
      // where as = sin(w0)/sqrt(2), w0 = 2*%pi*f0/Fs, and A = 10^(G/40)
      //
      // The poles of this filter are
      //
      //   -a2/(2*a0) +/- sqrt(a1^2-4*a0*a2)/(2*a0).
      //
      // Thus, the poles depend on the sign of d = a1^2-4*a0*a2 =
      // 16*A*(as^2-sin(w0)^2) = -8*A*sin(w0)^2.  Thus, the poles are always
      // complex except if w0 = 0, in which case there is a repeated pole at 0.

      // Array of tests to run.  |descripton| is the task description for
      // audit.define.  |parameters| is option for |testTailTime|.
      let tests = [
        {
          descripton:
              {label: 'highshelf-complex-roots', description: 'complex roots'},
          parameters: {
            prefix: 'Highshelf complex roots',
            filterOptions:
                {type: 'highshelf', gain: 40, frequency: sampleRate / 8},
            // Node computed tail frame is 18.6 frames, which matches the actual
            // tail, so tail output should be exactly zero.
            threshold: 0
          }
        },
        {
          descripton: {
            label: 'highshelf-repeated-roots',
            description: 'repeated real root'
          },
          parameters: {
            prefix: 'Highshelf repeated roots',
            filterOptions: {type: 'highshelf', gain: 40, frequency: 0},
            // Node computed tail frame is 2 frames, which matches the actual
            // tail, so tail output should be exactly zero.
            threshold: 0
          }
        },
      ];

      // Define an appropriate task for each test.
      tests.forEach(entry => {
        audit.define(entry.descripton, (task, should) => {
          let context = new OfflineAudioContext(1, renderFrames, sampleRate);
          testTailTime(should, context, entry.parameters)
              .then(() => task.done());
        });
      });

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