summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_audioParamChaining.html
blob: 85b8099e2e127919d7f593a794071b4bb347b3bf (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test whether we can create an AudioContext interface</title>
  <script src="/tests/SimpleTest/SimpleTest.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()

function frameToTime(frame, rate)
{
  return frame / rate;
}

const RATE = 44100;

var oc = new OfflineAudioContext(1, 44100, RATE);
// This allows us to have a source that is simply a DC offset.
var source = oc.createBufferSource();
var buf = oc.createBuffer(1, 1, RATE);
buf.getChannelData(0)[0] = 1;
source.loop = true;
source.buffer = buf;

source.start(0);

var gain = oc.createGain();

source.connect(gain).connect(oc.destination);

var gain2 = oc.createGain();
var rv2 = gain2.gain.linearRampToValueAtTime(0.1, 0.5);
ok(rv2 instanceof AudioParam, "linearRampToValueAtTime returns an AudioParam.");
ok(rv2 == gain2.gain, "linearRampToValueAtTime returns the right AudioParam.");

rv2 = gain2.gain.exponentialRampToValueAtTime(0.01, 1.0);
ok(rv2 instanceof AudioParam,
    "exponentialRampToValueAtTime returns an AudioParam.");
ok(rv2 == gain2.gain,
    "exponentialRampToValueAtTime returns the right AudioParam.");

rv2 = gain2.gain.setTargetAtTime(1.0, 2.0, 0.1);
ok(rv2 instanceof AudioParam, "setTargetAtTime returns an AudioParam.");
ok(rv2 == gain2.gain, "setTargetAtTime returns the right AudioParam.");

var array = new Float32Array(10);
rv2 = gain2.gain.setValueCurveAtTime(array, 10, 11);
ok(rv2 instanceof AudioParam, "setValueCurveAtTime returns an AudioParam.");
ok(rv2 == gain2.gain, "setValueCurveAtTime returns the right AudioParam.");

// We chain three automation methods, making a gain step.
var rv = gain.gain.setValueAtTime(0, frameToTime(0, RATE))
                  .setValueAtTime(0.5, frameToTime(22000, RATE))
                  .setValueAtTime(1, frameToTime(44000, RATE));

ok(rv instanceof AudioParam, "setValueAtTime returns an AudioParam.");
ok(rv == gain.gain, "setValueAtTime returns the right AudioParam.");

oc.startRendering().then(function(rendered) {
    console.log(rendered.getChannelData(0));
    is(rendered.getChannelData(0)[0], 0,
      "The value of the first step is correct.");
    is(rendered.getChannelData(0)[22050], 0.5,
      "The value of the second step is correct");
    is(rendered.getChannelData(0)[44099], 1,
      "The value of the third step is correct.");
    SimpleTest.finish();
});

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