summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_stereoPannerNode.html
blob: ffc735364ab18405232d25fcdf0981095ea358cc (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!DOCTYPE HTML>
<html>
<head>
  <title>Test StereoPannerNode</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">

var SR = 44100;
var BUF_SIZE = 128;
var PANNING = 0.1;
var GAIN = 0.5;

// Cheap reimplementation of some bits of the spec
function gainForPanningMonoToStereo(panning) {
  panning += 1;
  panning /= 2;
  return [ Math.cos(0.5 * Math.PI * panning),
           Math.sin(0.5 * Math.PI * panning) ];
}

function gainForPanningStereoToStereo(panning) {
  if (panning <= 0) {
    panning += 1.;
  }
  return [ Math.cos(0.5 * Math.PI * panning),
           Math.sin(0.5 * Math.PI * panning) ];
}

function applyStereoToStereoPanning(l, r, panningValues, panning) {
  var outL, outR;
  if (panning <= 0) {
    outL = l + r * panningValues[0];
    outR = r * panningValues[1];
  } else {
    outL = l * panningValues[0];
    outR = r + l * panningValues[1];
  }
  return [outL,outR];
}

function applyMonoToStereoPanning(c, panning) {
  return [c * panning[0], c * panning[1]];
}

// Test the DOM interface
var context = new OfflineAudioContext(1, 1, SR);
var stereoPanner = context.createStereoPanner();
ok(stereoPanner.pan, "The AudioParam member must exist");
is(stereoPanner.pan.value, 0.0, "Correct initial value");
is(stereoPanner.pan.defaultValue, 0.0, "Correct default value");
is(stereoPanner.channelCount, 2, "StereoPannerNode node has 2 input channels by default");
is(stereoPanner.channelCountMode, "clamped-max", "Correct channelCountMode for the StereoPannerNode");
is(stereoPanner.channelInterpretation, "speakers", "Correct channelCountInterpretation for the StereoPannerNode");
expectException(function() {
  stereoPanner.channelCount = 3;
}, DOMException.NOT_SUPPORTED_ERR);
expectException(function() {
  stereoPanner.channelCountMode = "max";
}, DOMException.NOT_SUPPORTED_ERR);

// A sine to be used to fill the buffers
function sine(t) {
  return Math.sin(440 * 2 * Math.PI * t / context.sampleRate);
}

// A couple mono and stereo buffers: the StereoPannerNode equation is different
// if the input is mono or stereo
var stereoBuffer = context.createBuffer(2, BUF_SIZE, context.sampleRate);
var monoBuffer = context.createBuffer(1, BUF_SIZE, context.sampleRate);
for (var i = 0; i < BUF_SIZE; ++i) {
  monoBuffer.getChannelData(0)[i]   =
  stereoBuffer.getChannelData(0)[i] =
  stereoBuffer.getChannelData(1)[i] = sine(i);
}

// Expected test vectors
function expectedBufferNoop(gain) {
  gain = gain || 1.0;
  var expectedBuffer = context.createBuffer(2, BUF_SIZE, SR);
  for (var i = 0; i < BUF_SIZE; i++) {
    expectedBuffer.getChannelData(0)[i] = gain * sine(i);
    expectedBuffer.getChannelData(1)[i] = gain * sine(i);
  }
  return expectedBuffer;
}

function expectedBufferForStereo(gain) {
  gain = gain || 1.0;
  var expectedBuffer = context.createBuffer(2, BUF_SIZE, SR);
  var gainPanning = gainForPanningStereoToStereo(PANNING);
  gainPanning[0] *= gain;
  gainPanning[1] *= gain;
  for (var i = 0; i < BUF_SIZE; i++) {
    var values = [ sine(i), sine(i) ];
    var processed = applyStereoToStereoPanning(values[0], values[1], gainPanning, PANNING);
    expectedBuffer.getChannelData(0)[i] = processed[0];
    expectedBuffer.getChannelData(1)[i] = processed[1];
  }
  return expectedBuffer;
}

function expectedBufferForMono(gain) {
  gain = gain || 1.0;
  var expectedBuffer = context.createBuffer(2, BUF_SIZE, SR);
  var gainPanning = gainForPanningMonoToStereo(PANNING);
  gainPanning[0] *= gain;
  gainPanning[1] *= gain;
  for (var i = 0; i < BUF_SIZE; i++) {
    var value = sine(i);
    var processed = applyMonoToStereoPanning(value, gainPanning);
    expectedBuffer.getChannelData(0)[i] = processed[0];
    expectedBuffer.getChannelData(1)[i] = processed[1];
  }
  return expectedBuffer;
}

// Actual test cases
var tests = [
  function monoPanningNoop(ctx, panner) {
    var monoSource = ctx.createBufferSource();
    monoSource.connect(panner);
    monoSource.buffer = monoBuffer;
    monoSource.start(0);
    return expectedBufferNoop();
  },
  function stereoPanningNoop(ctx, panner) {
    var stereoSource = ctx.createBufferSource();
    stereoSource.connect(panner);
    stereoSource.buffer = stereoBuffer;
    stereoSource.start(0);
    return expectedBufferNoop();
  },
  function monoPanningNoopWithGain(ctx, panner) {
    var monoSource = ctx.createBufferSource();
    var gain = ctx.createGain();
    gain.gain.value = GAIN;
    monoSource.connect(gain);
    gain.connect(panner);
    monoSource.buffer = monoBuffer;
    monoSource.start(0);
    return expectedBufferNoop(GAIN);
  },
  function stereoPanningNoopWithGain(ctx, panner) {
    var stereoSource = ctx.createBufferSource();
    var gain = ctx.createGain();
    gain.gain.value = GAIN;
    stereoSource.connect(gain);
    gain.connect(panner);
    stereoSource.buffer = stereoBuffer;
    stereoSource.start(0);
    return expectedBufferNoop(GAIN);
  },
  function stereoPanningAutomation(ctx, panner) {
    var stereoSource = ctx.createBufferSource();
    stereoSource.connect(panner);
    stereoSource.buffer = stereoBuffer;
    panner.pan.setValueAtTime(0.1, 0.0);
    stereoSource.start(0);
    return expectedBufferForStereo();
  },
  function stereoPanning(ctx, panner) {
    var stereoSource = ctx.createBufferSource();
    stereoSource.buffer = stereoBuffer;
    stereoSource.connect(panner);
    panner.pan.value = 0.1;
    stereoSource.start(0);
    return expectedBufferForStereo();
  },
  function monoPanningAutomation(ctx, panner) {
    var monoSource = ctx.createBufferSource();
    monoSource.connect(panner);
    monoSource.buffer = monoBuffer;
    panner.pan.setValueAtTime(PANNING, 0.0);
    monoSource.start(0);
    return expectedBufferForMono();
  },
  function monoPanning(ctx, panner) {
    var monoSource = ctx.createBufferSource();
    monoSource.connect(panner);
    monoSource.buffer = monoBuffer;
    panner.pan.value = 0.1;
    monoSource.start(0);
    return expectedBufferForMono();
  },
  function monoPanningWithGain(ctx, panner) {
    var monoSource = ctx.createBufferSource();
    var gain = ctx.createGain();
    gain.gain.value = GAIN;
    monoSource.connect(gain);
    gain.connect(panner);
    monoSource.buffer = monoBuffer;
    panner.pan.value = 0.1;
    monoSource.start(0);
    return expectedBufferForMono(GAIN);
  },
  function stereoPanningWithGain(ctx, panner) {
    var stereoSource = ctx.createBufferSource();
    var gain = ctx.createGain();
    gain.gain.value = GAIN;
    stereoSource.connect(gain);
    gain.connect(panner);
    stereoSource.buffer = stereoBuffer;
    panner.pan.value = 0.1;
    stereoSource.start(0);
    return expectedBufferForStereo(GAIN);
  },
  function monoPanningWithGainAndAutomation(ctx, panner) {
    var monoSource = ctx.createBufferSource();
    var gain = ctx.createGain();
    gain.gain.value = GAIN;
    monoSource.connect(gain);
    gain.connect(panner);
    monoSource.buffer = monoBuffer;
    panner.pan.setValueAtTime(PANNING, 0);
    monoSource.start(0);
    return expectedBufferForMono(GAIN);
  },
  function stereoPanningWithGainAndAutomation(ctx, panner) {
    var stereoSource = ctx.createBufferSource();
    var gain = ctx.createGain();
    gain.gain.value = GAIN;
    stereoSource.connect(gain);
    gain.connect(panner);
    stereoSource.buffer = stereoBuffer;
    panner.pan.setValueAtTime(PANNING, 0);
    stereoSource.start(0);
    return expectedBufferForStereo(GAIN);
  }
];

var finished = 0;
function finish() {
  if (++finished == tests.length) {
    SimpleTest.finish();
  }
}

tests.forEach(function(f) {
  var ac = new OfflineAudioContext(2, BUF_SIZE, SR);
  var panner = ac.createStereoPanner();
  panner.connect(ac.destination);
  var expected = f(ac, panner);
  ac.oncomplete = function(e) {
    info(f.name);
    compareBuffers(e.renderedBuffer, expected);
    finish();
  };
  ac.startRendering()
});

SimpleTest.waitForExplicitFinish();

</script>
</pre>
<pre id=dump>
</pre>
</body>
</html>