summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_delayNodeTailWithDisconnect.html
blob: fa431d61b2fc45a3461a7bb0ae4d36f8488564b8 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test tail time lifetime of DelayNode after input is disconnected</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">

// Web Audio doesn't provide a means to precisely time disconnect()s but we
// can test that the output of delay nodes matches the output from their
// sources before they are disconnected.

SimpleTest.waitForExplicitFinish();

const signalLength = 128;
const bufferSize = 4096;
const sourceCount = bufferSize / signalLength;
// Delay should be long enough to allow CC to run
var delayBufferCount = 20;
const delayLength = delayBufferCount * bufferSize;

var sourceOutput = new Float32Array(bufferSize);
var delayOutputCount = 0;
var sources = [];

function onDelayOutput(e) {
  if (delayOutputCount < delayBufferCount) {
    delayOutputCount++;
    return;
  }

  compareChannels(e.inputBuffer.getChannelData(0), sourceOutput);
  e.target.onaudioprocess = null;
  SimpleTest.finish();
}

function onSourceOutput(e) {
  // Record the first buffer
  e.inputBuffer.copyFromChannel(sourceOutput, 0);
  e.target.onaudioprocess = null;
}

function disconnectSources() {
  for (var i = 0; i < sourceCount; ++i) {
    sources[i].disconnect();
  }

  SpecialPowers.forceGC();
  SpecialPowers.forceCC();
}

function startTest() {
  var ctx = new AudioContext();

  var sourceProcessor = ctx.createScriptProcessor(bufferSize, 1, 0);
  sourceProcessor.onaudioprocess = onSourceOutput;
  // Keep audioprocess events going after source disconnect.
  sourceProcessor.connect(ctx.destination);

  var delayProcessor = ctx.createScriptProcessor(bufferSize, 1, 0);
  delayProcessor.onaudioprocess = onDelayOutput;

  var delayDuration = delayLength / ctx.sampleRate;
  for (var i = 0; i < sourceCount; ++i) {
    var delay = ctx.createDelay(delayDuration);
    delay.delayTime.value = delayDuration;
    delay.connect(delayProcessor);

    var source = ctx.createOscillator();
    source.frequency.value = 440 + 10 * i
    source.start(i * signalLength / ctx.sampleRate);
    source.stop((i + 1) * signalLength / ctx.sampleRate);
    source.connect(delay);
    source.connect(sourceProcessor);

    sources[i] = source;
  }

  // Assuming the above Web Audio operations have already scheduled an event
  // to run in stable state and start the graph thread, schedule a subsequent
  // event to disconnect the sources, which will remove main thread connection
  // references before it knows the graph thread has started using the source
  // streams.
  SimpleTest.executeSoon(disconnectSources);
};

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