summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_channelSplitterNodeWithVolume.html
blob: c03f6deeaf7d9df8c39e89d78ca2976452423d2a (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test ChannelSplitterNode</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">

// We do not use our generic graph test framework here because
// the splitter node is special in that it creates multiple
// output ports.

SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
  var context = new AudioContext();
  var buffer = context.createBuffer(4, 2048, context.sampleRate);
  var expectedBuffer = context.createBuffer(4, 2048, context.sampleRate);
  for (var j = 0; j < 4; ++j) {
    for (var i = 0; i < 2048; ++i) {
      buffer.getChannelData(j)[i] = Math.sin(440 * 2 * (j + 1) * Math.PI * i / context.sampleRate);
      expectedBuffer.getChannelData(j)[i] = buffer.getChannelData(j)[i] / 2;
    }
  }
  var emptyBuffer = context.createBuffer(1, 2048, context.sampleRate);

  var destination = context.destination;

  var source = context.createBufferSource();

  var splitter = context.createChannelSplitter();
  is(splitter.channelCount, 6, "splitter node has 2 input channels by default");
  is(splitter.channelCountMode, "explicit", "Correct channelCountMode for the splitter node");
  is(splitter.channelInterpretation, "discrete", "Correct channelCountInterpretation for the splitter node");

  source.buffer = buffer;
  var gain = context.createGain();
  gain.gain.value = 0.5;
  source.connect(gain);
  gain.connect(splitter);

  var channelsSeen = 0;
  function createHandler(i) {
    return function(e) {
      is(e.inputBuffer.numberOfChannels, 1, "Correct input channel count");
      if (i < 4) {
        compareBuffers(e.inputBuffer.getChannelData(0), expectedBuffer.getChannelData(i));
      } else {
        compareBuffers(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
      }
      e.target.onaudioprocess = null;
      ++channelsSeen;

      if (channelsSeen == 6) {
        SimpleTest.finish();
      }
    };
  }

  for (var i = 0; i < 6; ++i) {
    var sp = context.createScriptProcessor(2048, 1);
    splitter.connect(sp, i);
    sp.onaudioprocess = createHandler(i);
    sp.connect(destination);
  }

  source.start(0);
});

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