summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_OfflineAudioContext.html
blob: 6d8a9075421549a22e251b31a5d7ad50d0422696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE HTML>
<html>
<head>
  <title>Test OfflineAudioContext</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="webaudio.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">

var renderedBuffer = null;
var finished = 0;

function finish() {
  finished++;
  if (finished == 2) {
    SimpleTest.finish();
  }
}

function setOrCompareRenderedBuffer(aRenderedBuffer) {
  if (renderedBuffer) {
    is(renderedBuffer, aRenderedBuffer, "Rendered buffers from the event and the promise should be the same");
    finish();
  } else {
    renderedBuffer = aRenderedBuffer;
  }
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
  let ctxs = [
    new OfflineAudioContext(2, 100, 22050),
    new OfflineAudioContext({length: 100, sampleRate: 22050}),
    new OfflineAudioContext({channels: 2, length: 100, sampleRate: 22050}),
  ];

  for (let ctx of ctxs) {
    ok(ctx instanceof EventTarget, "OfflineAudioContexts must be EventTargets");
    is(ctx.length, 100, "OfflineAudioContext.length is equal to the value passed to the ctor.");

    var buf = ctx.createBuffer(2, 100, ctx.sampleRate);
    for (var i = 0; i < 2; ++i) {
      for (var j = 0; j < 100; ++j) {
        buf.getChannelData(i)[j] = Math.sin(2 * Math.PI * 200 * j / ctx.sampleRate);
      }
    }
  }

  is(ctxs[1].destination.channelCount, 1, "OfflineAudioContext defaults to to correct channelCount.");

  let ctx = ctxs[0];

  expectException(function() {
    new OfflineAudioContext(2, 100, 0);
  }, DOMException.NOT_SUPPORTED_ERR);
  expectException(function() {
    new OfflineAudioContext(2, 100, -1);
  }, DOMException.NOT_SUPPORTED_ERR);
  expectException(function() {
    new OfflineAudioContext(0, 100, 44100);
  }, DOMException.NOT_SUPPORTED_ERR);
  new OfflineAudioContext(32, 100, 44100);
  expectException(function() {
    new OfflineAudioContext(33, 100, 44100);
  }, DOMException.NOT_SUPPORTED_ERR);
  expectException(function() {
    new OfflineAudioContext(2, 0, 44100);
  }, DOMException.NOT_SUPPORTED_ERR);
  expectTypeError(function() {
    new OfflineAudioContext({});
  });
  expectTypeError(function() {
    new OfflineAudioContext({sampleRate: 44100});
  });
  expectTypeError(function() {
    new OfflineAudioContext({length: 44100*40});
  });

  var src = ctx.createBufferSource();
  src.buffer = buf;
  src.start(0);
  src.connect(ctx.destination);

  ctx.addEventListener("complete", function(e) {
    ok(e instanceof OfflineAudioCompletionEvent, "Correct event received");
    is(e.renderedBuffer.numberOfChannels, 2, "Correct expected number of buffers");
    ok(renderedBuffer != null, "The event should be fired after the promise callback.");
    expectNoException(function() {
      ctx.startRendering().then(function() {
        ok(false, "Promise should not resolve when startRendering is called a second time on an OfflineAudioContext")
        finish();
      }).catch(function() {
        ok(true, "Promise should reject when startRendering is called a second time on an OfflineAudioContext")
        finish();
      });
    });
    compareBuffers(e.renderedBuffer, buf);
    setOrCompareRenderedBuffer(e.renderedBuffer);

  });

  expectNoException(function() {
    ctx.startRendering().then(function(b) {
      is(renderedBuffer, null, "The promise callback should be called first.");
      setOrCompareRenderedBuffer(b);
    }).catch(function () {
       ok(false, "The promise from OfflineAudioContext.startRendering should never be rejected");
    });
  });
});

</script>
</pre>
</body>
</html>