<!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 highpass filter:
      //   b0 = (1+cos(w0))/2
      //   b1 = -(1+cos(w0))
      //   b2 = (1+cos(w0))/2
      //   a0 = 1 + alpha
      //   a1 = -2*cos(w0)
      //   a2 = 1 - alpha
      //
      // where alpha = sin(w0)/(2*10^(Q/20)) and w0 = 2*%pi*f0/Fs.
      //
      // Equivalently a1 = -2*cos(w0)/(1+alpha), a2 = (1-alpha)/(1+alpha).  The
      // poles of this filter are at
      //
      //   cos(w0)/(1+alpha) +/- sqrt(alpha^2-sin(w0)^2)/(1+alpha)
      //
      // But alpha^2-sin(w0)^2 = sin(w0)^2*(1/4/10^(Q/10) - 1).  Thus the poles
      // are complex if 1/4/10^(Q/10) < 1; real distinct if 1/4/10^(Q/10) > 1;
      // and repeated if 1/4/10^(Q/10) = 1.

      // Array of tests to run.  |descripton| is the task description for
      // audit.define.  |parameters| is option for |testTailTime|.
      let tests = [
        {
          descripton:
              {label: 'hpf-complex-roots', description: 'complex roots'},
          parameters: {
            prefix: 'HPF complex roots',
            filterOptions: {type: 'highpass', Q: 40, frequency: sampleRate / 4},
            // Node computed tail frame is 2079.4, which matches the actual tail
            // frome so output should be exactly 0.
            threshold: 0
          }
        },
        {
          descripton: {
            label: 'hpf-real-distinct-roots',
            description: 'real distinct roots'
          },
          parameters: {
            prefix: 'HPF real distinct roots',
            filterOptions:
                {type: 'highpass', Q: -50, frequency: sampleRate / 8},
            // With these filter parameters, the real tail time is 408, but
            // the node overestimates it to be 2367.  Thus, the actual tail
            // frames won't be exactly zero.
            threshold: 1 / 32768
          }
        },
        {
          descripton:
              {label: 'hpf-repeated-root', description: 'repeated real root'},
          parameters: {
            prefix: 'HPF repeated roots (approximately)',
            // For a repeated root, we need 1/4/10^(Q/10) = 1, or Q =
            // -10*log(4)/log(10). This isn't exactly representable as a float,
            // so the roots might not actually be repeated.  In fact the roots
            // are complex at 6.40239e-5*exp(i*1.570596).
            filterOptions: {
              type: 'highpass',
              Q: -10 * Math.log10(4),
              frequency: sampleRate / 4
            },
            // Node computed tail frame is 2.9, which matches the actual tail
            // frome so output should be exactly 0.
            threshold: 0
          }
        },
        {
          descripton: {label: 'hpf-real-roots-2', description: 'complex roots'},
          parameters: {
            prefix: 'HPF repeated roots 2',
            // This tests an extreme case where approximate impulse response is
            // h(n) = C*r^(n-1) and C < 1/32768.  Thus, the impulse response is
            // always less than the response threshold of 1/32768.
            filterOptions:
                {type: 'highpass', Q: -100, frequency: sampleRate / 4},
            // Node computed tail frame is 0, which matches the actual tail
            // frame so output should be exactly 0.
            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>
