<!DOCTYPE html>
<html>
  <head>
    <title>
      Test FLAC-encoded HRTF databse
    </title>
    <script src="../../imported/w3c/web-platform-tests/resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
    <script src="../resources/audit.js"></script>
    <script src="../resources/buffer-loader.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      // This MUST be the sample rate used by the HTRF database!
      let sampleRate = 44100;

      let context;
      let wavBuffer;
      let flacBuffer;

      let audit = Audit.createTaskRunner();

      audit.define(
          {label: 'loadfiles', description: 'Load HRTF database files'},
          function(task, should) {

            // Any valid context with the right sample rate will do.
            context = new OfflineAudioContext(1, 1, sampleRate);

            let bufferLoader = new BufferLoader(
                context,
                [
                  '../resources/hrtf/Composite.wav',
                  '../resources/hrtf/Composite.flac',
                ],
                function(bufferList) {
                  should(bufferList.length, 'Number of buffers loaded')
                      .beEqualTo(2);
                  wavBuffer = bufferList[0];
                  flacBuffer = bufferList[1];
                  task.done();
                });

            bufferLoader.load();
          });

      audit.define(
          {
            label: 'verify-flac',
            description: 'Verify FLAC-encoded HRTF database matches original'
          },
          function(task, should) {

            should(flacBuffer.numberOfChannels, 'Number of FLAC channels')
                .beEqualTo(wavBuffer.numberOfChannels);

            for (let k = 0; k < wavBuffer.numberOfChannels; ++k) {
              should(
                  flacBuffer.getChannelData(k),
                  'FLAC-encoded HRTF database channel ' + k)
                  .beEqualToArray(wavBuffer.getChannelData(k));
            }

            task.done();
          });

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