summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_AudioBuffer.html
blob: 82bfdd420a9f7c0024b55b7a0ac6d8e72e9e1d6b (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test whether we can create an AudioContext interface</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script src="webaudio.js" type="text/javascript"></script>
<script class="testbody" type="text/javascript">

SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
  var context = new AudioContext();
  var buffer = context.createBuffer(2, 2048, context.sampleRate);
  SpecialPowers.gc(); // Make sure that our channels are accessible after GC
  ok(buffer, "Buffer was allocated successfully");
  is(buffer.sampleRate, context.sampleRate, "Correct sample rate");
  is(buffer.length, 2048, "Correct length");
  ok(Math.abs(buffer.duration - 2048 / context.sampleRate) < 0.0001, "Correct duration");
  is(buffer.numberOfChannels, 2, "Correct number of channels");
  for (var i = 0; i < buffer.numberOfChannels; ++i) {
    var buf = buffer.getChannelData(i);
    ok(buf, "Buffer index " + i + " exists");
    ok(buf instanceof Float32Array, "Result is a typed array");
    is(buf.length, buffer.length, "Correct length");
    var foundNonZero = false;
    for (var j = 0; j < buf.length; ++j) {
      if (buf[j] != 0) {
        foundNonZero = true;
        break;
      }
      buf[j] = j;
    }
    ok(!foundNonZero, "Buffer " + i + " should be initialized to 0");
  }

  // Now test copying the channel data out of a normal buffer
  var copy = new Float32Array(100);
  buffer.copyFromChannel(copy, 0, 1024);
  for (var i = 0; i < copy.length; ++i) {
    is(copy[i], 1024 + i, "Correct sample");
  }

  // Test copying the channel data out of a playing buffer
  var srcNode = context.createBufferSource();
  srcNode.buffer = buffer;
  srcNode.start(0);
  copy = new Float32Array(100);
  buffer.copyFromChannel(copy, 0, 1024);
  for (var i = 0; i < copy.length; ++i) {
    is(copy[i], 1024 + i, "Correct sample");
  }

  // Test copying to the channel data
  var newData = new Float32Array(200);
  buffer.copyToChannel(newData, 0, 100);
  var changedData = buffer.getChannelData(0);
  for (var i = 0; i < changedData.length; ++i) {
    if (i < 100 || i >= 300) {
      is(changedData[i], i, "Untouched sample");
    } else {
      is(changedData[i], 0, "Correct sample");
    }
  }

  // Now, detach the array buffer
  var worker = new Worker("audioBufferSourceNodeDetached_worker.js");
  var data = buffer.getChannelData(0).buffer;
  worker.postMessage(data, [data]);
  SpecialPowers.gc();

  expectException(function() {
    buffer.copyFromChannel(copy, 0, 1024);
  }, DOMException.INDEX_SIZE_ERR);

  expectException(function() {
    buffer.copyToChannel(newData, 0, 100);
  }, DOMException.INDEX_SIZE_ERR);

  expectException(function() {
    context.createBuffer(2, 2048, 7999);
  }, DOMException.INDEX_SIZE_ERR);
  expectException(function() {
    context.createBuffer(2, 2048, 192001);
  }, DOMException.INDEX_SIZE_ERR);
  context.createBuffer(2, 2048, 8000);  // no exception
  context.createBuffer(2, 2048, 192000); // no exception
  context.createBuffer(32, 2048, 48000); // no exception
  // Null length
  expectException(function() {
    context.createBuffer(2, 0, 48000);
  }, DOMException.INDEX_SIZE_ERR);
  // Null number of channels
  expectException(function() {
    context.createBuffer(0, 2048, 48000);
  }, DOMException.INDEX_SIZE_ERR);
  SimpleTest.finish();
});

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