Skip to content

Commit e1a9d07

Browse files
committed
add docs and tests to panner
1 parent 1c303fb commit e1a9d07

File tree

5 files changed

+58
-20
lines changed

5 files changed

+58
-20
lines changed

src/effect.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import CrossFade from 'Tone/component/CrossFade.js';
1313
* <a href="/reference/#/p5.Filter">p5.Filter</a>,
1414
* <a href="/reference/#/p5.Reverb">p5.Reverb</a>,
1515
* <a href="/reference/#/p5.EQ">p5.EQ</a>,
16+
* <a href="/reference/#/p5.Panner">p5.Panner</a>.
1617
* <a href="/reference/#/p5.Panner3D">p5.Panner3D</a>.
1718
*
1819
* @class p5.Effect

src/panner.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ var panner;
66
// Stereo panner
77
// if there is a stereo panner node use it
88
if (typeof ac.createStereoPanner !== 'undefined') {
9+
/**
10+
* The Panner class allows you to control the stereo
11+
* panning of a sound source. It uses the [StereoPannerNode](https://developer.mozilla.org/en-US/docs/Web/API/StereoPannerNode),
12+
* which allows you to adjust the balance between the left and right channels of a sound source.
13+
*
14+
* This class extends <a href = "/reference/#/p5.Effect">p5.Effect</a>.
15+
* Methods <a href = "/reference/#/p5.Effect/amp">amp()</a>, <a href = "/reference/#/p5.Effect/chain">chain()</a>,
16+
* <a href = "/reference/#/p5.Effect/drywet">drywet()</a>, <a href = "/reference/#/p5.Effect/connect">connect()</a>, and
17+
* <a href = "/reference/#/p5.Effect/disconnect">disconnect()</a> are available.
18+
*
19+
* @class p5.Panner
20+
* @extends p5.Effect
21+
*/
922
class Panner extends Effect {
1023
constructor() {
1124
super();
@@ -15,6 +28,16 @@ if (typeof ac.createStereoPanner !== 'undefined') {
1528
this.stereoPanner.connect(this.wet);
1629
}
1730

31+
/**
32+
* Set the stereo pan position, a value of -1 means the sound will be fully panned
33+
* to the left, a value of 0 means the sound will be centered, and a value of 1 means
34+
* the sound will be fully panned to the right.
35+
* @method pan
36+
* @for p5.Panner
37+
* @param {Number} value A value between -1 and 1 that sets the pan position.
38+
*
39+
* @param {Number} [time] time in seconds that it will take for the panning to change to the specified value.
40+
*/
1841
pan(val, tFromNow) {
1942
if (typeof val === 'number') {
2043
let time = tFromNow || 0;
@@ -27,10 +50,23 @@ if (typeof ac.createStereoPanner !== 'undefined') {
2750
}
2851
}
2952

53+
/**
54+
* Return the current panning value.
55+
*
56+
* @method getPan
57+
* @for p5.Panner
58+
* @return {Number} current panning value, number between -1 (left) and 1 (right).
59+
*/
3060
getPan() {
3161
return this.stereoPanner.pan.value;
3262
}
3363

64+
/**
65+
* Get rid of the Panner and free up its resources / memory.
66+
*
67+
* @method dispose
68+
* @for p5.Panner
69+
*/
3470
dispose() {
3571
super.dispose();
3672
if (this.stereoPanner) {

test/tests/p5.Oscillator.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,8 @@ describe('p5.Oscillator', function () {
216216
it('can be panned without any delay', function (done) {
217217
let osc = new p5.Oscillator();
218218
let panner = osc.panner;
219-
expect(osc.panPosition).to.equal(0); //default value
220219
expect(osc.getPan()).to.equal(0);
221220
osc.pan(-0.3);
222-
expect(osc.panPosition).to.equal(-0.3);
223-
expect(osc.getPan()).to.equal(-0.3);
224221
if (typeof p5.soundOut.audiocontext.createStereoPanner !== 'undefined') {
225222
setTimeout(() => {
226223
expect(panner.stereoPanner.pan.value).to.be.approximately(-0.3, 0.01);
@@ -238,8 +235,6 @@ describe('p5.Oscillator', function () {
238235
let osc = new p5.Oscillator();
239236
osc.pan(0.7, 0.1);
240237
let panner = osc.panner;
241-
expect(osc.panPosition).to.equal(0.7);
242-
expect(osc.getPan()).to.equal(0.7);
243238
if (typeof p5.soundOut.audiocontext.createStereoPanner !== 'undefined') {
244239
setTimeout(() => {
245240
expect(panner.stereoPanner.pan.value).to.not.be.approximately(
@@ -252,7 +247,7 @@ describe('p5.Oscillator', function () {
252247
0.01
253248
);
254249
done();
255-
}, 50);
250+
}, 60);
256251
}, 50);
257252
} else {
258253
setTimeout(() => {
@@ -268,7 +263,7 @@ describe('p5.Oscillator', function () {
268263
expect(panner.left.gain.value).to.be.approximately(0.972, 0.001);
269264
expect(panner.right.gain.value).to.be.approximately(0.233, 0.001);
270265
done();
271-
}, 100);
266+
}, 60);
272267
}, 50);
273268
}
274269
});

test/tests/p5.Panner.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// const expect = chai.expect;
1+
const expect = chai.expect;
22

33
describe('p5.Panner', function () {
44
let ac, output, input;
@@ -8,22 +8,28 @@ describe('p5.Panner', function () {
88
input = ac.createGain();
99
});
1010
it('can be created', function () {
11-
new p5.Panner(input, output);
11+
new p5.Panner();
1212
});
1313
it('can be connected and disconnected', function () {
14-
let panner = new p5.Panner(input, output);
14+
let panner = new p5.Panner();
1515
panner.connect(input);
1616
panner.disconnect();
1717
});
18-
it('can be panned without a delay', function () {
19-
let panner = new p5.Panner(input, output);
18+
it('can be panned without a delay', function (done) {
19+
let panner = new p5.Panner();
2020
panner.pan(0.4);
21-
panner.pan(0.3, 0);
22-
//TODO: to check the value of left gain/ right gain (or) the stereoPanner.pan
21+
setTimeout(() => {
22+
expect(panner.getPan()).to.be.approximately(0.4, 0.01);
23+
done();
24+
}, 25);
2325
});
24-
it('can be panned with a delay', function () {
26+
it('can be panned with a delay', function (done) {
2527
let panner = new p5.Panner(input, output);
26-
panner.pan(0.4, 10);
28+
panner.pan(-0.7, 0.1);
29+
setTimeout(() => {
30+
expect(panner.getPan()).to.be.approximately(-0.7, 0.01);
31+
done();
32+
}, 125);
2733
});
2834
it('can take inputChannels as 1 or 2', function () {
2935
let panner = new p5.Panner(input, output);

test/tests/p5.SoundFile.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('p5.SoundFile', function () {
3030
expect(sf.pauseTime).to.equal(0);
3131
expect(sf.mode).to.equal('sustain');
3232
expect(sf.startMillis).to.be.null;
33-
expect(sf.panPosition).to.equal(0);
33+
expect(sf.getPan()).to.equal(0);
3434
expect(sf.panner).to.have.property('stereoPanner');
3535
expect(p5.soundOut.soundArray).to.include(sf);
3636

@@ -507,13 +507,13 @@ describe('p5.SoundFile', function () {
507507
let sf = new p5.SoundFile();
508508
expect(sf.getPan()).to.equal(0);
509509
sf.pan(0.32);
510-
expect(sf.panPosition).to.equal(0.32);
511-
expect(sf.getPan()).to.equal(0.32);
510+
setTimeout(() => {
511+
expect(sf.getPan()).to.equal(0.32);
512+
}, 5);
512513
//with delay
513514
let sf2 = new p5.SoundFile();
514515
sf2.pan(-0.89, 0.1);
515516
setTimeout(() => {
516-
expect(sf2.panPosition).to.equal(-0.89);
517517
expect(sf2.getPan()).to.equal(-0.89);
518518
}, 100);
519519
});

0 commit comments

Comments
 (0)