summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_waveShaperGain.html
blob: 45411eca02237a25083ba566b7b5e13bfd92f5a5 (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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
  <title>Test that WaveShaperNode doesn't corrupt its inputs when the gain is !=
    1.0 (bug 1203616)</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre>
</pre>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var samplerate = 44100;
var context = new OfflineAudioContext(1, 44100, samplerate);

var dc = context.createBufferSource();

var buffer = context.createBuffer(1, 1, samplerate);
buffer.getChannelData(0)[0] = 1.0;
dc.buffer = buffer;

var gain = context.createGain();
var ws2 = context.createWaveShaper();
var ws = [];

// No-op waveshaper curves.
for (var i = 0; i < 2; i++) {
  ws[i] = context.createWaveShaper();
  var curve = new Float32Array(2);
  curve[0] = -1.0;
  curve[1] = 1.0;
  ws[i].curve = curve;
  ws[i].connect(context.destination);
  gain.connect(ws[i]);
}

dc.connect(gain);
dc.start();

gain.gain.value = 0.5;

context.startRendering().then(buffer => {
  document.querySelector("pre").innerHTML = buffer.getChannelData(0)[0];
  ok(buffer.getChannelData(0)[0] == 1.0, "Volume was handled properly");

  context = new OfflineAudioContext(1, 100, samplerate);
  var oscillator = context.createOscillator();
  var gain = context.createGain();
  var waveShaper = context.createWaveShaper();

  oscillator.start(0);
  oscillator.connect(gain);

  // to silence
  gain.gain.value = 0;
  gain.connect(waveShaper);

  // convert all signal into 1.0. The non unity values are to detect the use
  // of uninitialized buffers (see Bug 1283910).
  waveShaper.curve = new Float32Array([ 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5 ]);
  waveShaper.connect(context.destination);

  context.startRendering().then((buffer) => {
    var result = buffer.getChannelData(0);
    ok(result.every(x => x === 1), "WaveShaper handles zero gain properly");
    SimpleTest.finish();
  });
});
</script>
</body>