summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_delayNodeTailIncrease.html
blob: 7516028245d859e497d114f5b4387dcf31d06bac (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test increasing delay of DelayNode after input finishes</title>
  <script type="text/javascript" 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">

SimpleTest.waitForExplicitFinish();

const signalLength = 100;
const bufferSize = 1024;
// Delay should be long enough to allow CC to run
const delayBufferCount = 50;
const delayLength = delayBufferCount * bufferSize + 700;

var count = 0;

function applySignal(buffer, offset) {
  for (var i = 0; i < signalLength; ++i) {
    buffer.getChannelData(0)[offset + i] = Math.cos(Math.PI * i / signalLength);
  }
}

function onAudioProcess(e) {
  switch(count) {
  case 5:
    SpecialPowers.forceGC();
    SpecialPowers.forceCC();
    break;
  case delayBufferCount:
    var offset = delayLength - count * bufferSize;
    var ctx = e.target.context;
    var expected = ctx.createBuffer(1, bufferSize, ctx.sampleRate);
    applySignal(expected, offset);
    compareBuffers(e.inputBuffer, expected);
    SimpleTest.finish();
  }
  count++;
}

function startTest() {
  var ctx = new AudioContext();
  var processor = ctx.createScriptProcessor(bufferSize, 1, 0);
  processor.onaudioprocess = onAudioProcess;

  // Switch on delay at a time in the future.
  var delayDuration = delayLength / ctx.sampleRate;
  var delayStartTime = (delayLength - bufferSize) / ctx.sampleRate;
  var delay = ctx.createDelay(delayDuration);
  delay.delayTime.setValueAtTime(delayDuration, delayStartTime);
  delay.connect(processor);

  // Short signal that finishes before switching to long delay
  var buffer = ctx.createBuffer(1, signalLength, ctx.sampleRate);
  applySignal(buffer, 0);
  var source = ctx.createBufferSource();
  source.buffer = buffer;
  source.start();
  source.connect(delay);
};

startTest();
</script>
</pre>
</body>
</html>