<!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;
      let renderQuantumFrames = 128;

      // For a notch filter:
      //   b0 = 1
      //   b1 = -2*cos(w0)
      //   b2 = 1
      //   a0 = 1 + alpha
      //   a1 = -2*cos(w0)
      //   a2 = 1 - alpha
      //
      // where alpha = sin(w0)/(2*Q) 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
      //
      //   (2*Q*cos(w0) +/- sqrt(1-4*Q^2)*sin(w0))/(2*Q + sin(w0))
      //
      // Thus, if 1-4*Q^2 < 0, the poles are complex.  For 1-4*Q^2 > 0, the
      // poles are real and distinct.  For 1-4*Q^2 = 0, there are two identical
      // real poles.

      // Array of tests to run.  |descripton| is the task description for
      // audit.define.  |parameters| is option for |testTailTime|.
      let tests = [
        {
          descripton:
              {label: 'notch-complex-roots', description: 'complex roots'},
          parameters: {
            prefix: 'Notch complex roots',
            filterOptions: {type: 'notch', Q: 200, frequency: sampleRate / 4},
            // Node computed tail frame is 2039.5 frames, which matches the
            // actual tail, so tail output should be exactly zero.
            threshold: 0
          },
        },
        {
          descripton: {
            label: 'notch-real-distinct-roots',
            description: 'real distinct roots'
          },
          parameters: {
            prefix: 'Notch real distinct roots',
            filterOptions: {type: 'notch', Q: 0.001, frequency: sampleRate / 4},
            // Node computed tail frame is 2437 frames, which matches the actual
            // tail, so tail output should be exactly zero.
            threshold: 0
          },
        },
        {
          descripton: {
            label: 'notch-repeated-roots',
            description: 'repeated real root'
          },
          parameters: {
            prefix: 'Notch repeated roots',
            // Note that while the roots are mathematically repeated,
            // numerical roundoff in compute the filter coefficients causes
            // the resulting filter to have roots at 0 and 6.123e-17.
            filterOptions: {type: 'notch', Q: 0.5, frequency: sampleRate / 4},
            // 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>
