summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_delayNodeTailWithReconnect.html
blob: 6c1cda58051ce64117bf6e0188fc32a1b45b4f98 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE HTML>
<html>
<head>
  <title>Test tail time lifetime of DelayNode after input finishes and new input added</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();

// The buffer source will start on a block boundary, so keeping the signal
// within one block ensures that it will not cross AudioProcessingEvent buffer
// boundaries.
const signalLength = 128;
const bufferSize = 1024;
// Delay should be long enough to allow CC to run
var delayBufferCount = 50;
var delayBufferOffset;
const delayLength = delayBufferCount * bufferSize;

var phase = "initial";
var sourceCount = 0;
var delayCount = 0;
var oscillator;
var delay;
var source;

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

function bufferIsSilent(buffer, out) {
  for (var i = 0; i < buffer.length; ++i) {
    if (buffer.getChannelData(0)[i] != 0) {
      if (out) {
        out.soundOffset = i;
      }
      return false;
    }
  }
  return true;
}

function onDelayOutput(e) {
  switch(phase) {

  case "initial":
    // Wait for oscillator sound to exit delay
    if (bufferIsSilent(e.inputBuffer))
      break;

    phase = "played oscillator";
    break;

  case "played oscillator":
    // First tail time has expired.  Start second source and remove references
    // to the delay and connected second source.
    oscillator.disconnect();
    source.connect(delay);
    source.start();
    source = null;
    delay = null;
    phase = "started second source";
    break;

  case "second tail time":
    if (delayCount == delayBufferCount) {
      var ctx = e.target.context;
      var expected = ctx.createBuffer(1, bufferSize, ctx.sampleRate);
      applySignal(expected, delayBufferOffset);
      compareBuffers(e.inputBuffer, expected);
      e.target.onaudioprocess = null;
      SimpleTest.finish();
    }
  }

  delayCount++;
}

function onSourceOutput(e) {
  switch(phase) {
  case "started second source":
    var out = {};
    if (!bufferIsSilent(e.inputBuffer, out)) {
      delayBufferCount += sourceCount;
      delayBufferOffset = out.soundOffset;
      phase = "played second source";
    }
    break;
  case "played second source":
    SpecialPowers.forceGC();
    SpecialPowers.forceCC();
    phase = "second tail time";
    e.target.onaudioprocess = null;
  }

  sourceCount++;
}

function startTest() {
  var ctx = new AudioContext();
  var delayDuration = delayLength / ctx.sampleRate;
  delay = ctx.createDelay(delayDuration);
  delay.delayTime.value = delayDuration;
  var processor1 = ctx.createScriptProcessor(bufferSize, 1, 0);
  delay.connect(processor1);
  processor1.onaudioprocess = onDelayOutput;

  // Signal to trigger initial tail time reference
  oscillator = ctx.createOscillator();
  oscillator.start(0);
  oscillator.stop(100/ctx.sampleRate);
  oscillator.connect(delay);

  // Short signal, not started yet, with a ScriptProcessor to detect when it
  // starts.  It should finish before garbage collection.
  var buffer = ctx.createBuffer(1, signalLength, ctx.sampleRate);
  applySignal(buffer, 0);
  source = ctx.createBufferSource();
  source.buffer = buffer;
  var processor2 = ctx.createScriptProcessor(bufferSize, 1, 0);
  source.connect(processor2);
  processor2.onaudioprocess = onSourceOutput;
};

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