Skip to content

Commit 726cbdc

Browse files
committed
update panner.js to extend effect.js
1 parent 9d7458f commit 726cbdc

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

src/oscillator.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,14 @@ class Oscillator {
133133

134134
this.oscillator.connect(this.output);
135135
// stereo panning
136-
this.panPosition = 0.0;
137136
this.connection = p5sound.input; // connect to p5sound by default
138-
this.panner = new Panner(this.output, this.connection, 1);
139137

138+
if (typeof p5sound.audiocontext.createStereoPanner !== 'undefined') {
139+
this.panner = new Panner();
140+
this.output.connect(this.panner);
141+
} else {
142+
this.panner = new Panner(this.output, this.connection, 1);
143+
}
140144
//array of math operation signal chaining
141145
this.mathOps = [this.output];
142146

@@ -415,21 +419,20 @@ class Oscillator {
415419
* seconds from now
416420
*/
417421
pan(pval, tFromNow) {
418-
this.panPosition = pval;
419422
this.panner.pan(pval, tFromNow);
420423
}
421424

422425
/**
423-
* Returns the current value of panPosition , between Left (-1) and Right (1)
426+
* Returns the current value of pan position , between Left (-1) and Right (1)
424427
*
425428
* @method getPan
426429
* @for p5.Oscillator
427430
*
428-
* @returns {number} panPosition of oscillator , between Left (-1) and Right (1)
431+
* @returns {number} pan position of oscillator , between Left (-1) and Right (1)
429432
*/
430433

431434
getPan() {
432-
return this.panPosition;
435+
return this.panner.getPan();
433436
}
434437

435438
// get rid of the oscillator
@@ -442,7 +445,7 @@ class Oscillator {
442445
var now = p5sound.audiocontext.currentTime;
443446
this.stop(now);
444447
this.disconnect();
445-
this.panner = null;
448+
this.panner.dispose();
446449
this.oscillator = null;
447450
}
448451
// if it is a Pulse

src/panner.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
1+
import Effect from './effect.js';
2+
13
import p5sound from './main';
24
var ac = p5sound.audiocontext;
35
var panner;
46
// Stereo panner
57
// if there is a stereo panner node use it
68
if (typeof ac.createStereoPanner !== 'undefined') {
7-
class Panner {
8-
constructor(input, output) {
9-
this.stereoPanner = this.input = ac.createStereoPanner();
10-
input.connect(this.stereoPanner);
11-
this.stereoPanner.connect(output);
9+
class Panner extends Effect {
10+
constructor() {
11+
super();
12+
this.stereoPanner = this.ac.createStereoPanner();
13+
14+
this.input.connect(this.stereoPanner);
15+
this.stereoPanner.connect(this.wet);
1216
}
1317

1418
pan(val, tFromNow) {
15-
var time = tFromNow || 0;
16-
var t = ac.currentTime + time;
17-
18-
this.stereoPanner.pan.linearRampToValueAtTime(val, t);
19+
if (typeof val === 'number') {
20+
let time = tFromNow || 0;
21+
this.stereoPanner.pan.linearRampToValueAtTime(
22+
val,
23+
this.ac.currentTime + 0.02 + time
24+
);
25+
} else if (typeof val !== 'undefined') {
26+
val.connect(this.stereoPanner.pan);
27+
}
1928
}
2029

21-
//not implemented because stereopanner
22-
//node does not require this and will automatically
23-
//convert single channel or multichannel to stereo.
24-
//tested with single and stereo, not with (>2) multichannel
25-
inputChannels() {}
26-
27-
connect(obj) {
28-
this.stereoPanner.connect(obj);
30+
getPan() {
31+
return this.stereoPanner.pan.value;
2932
}
3033

31-
disconnect() {
34+
dispose() {
35+
super.dispose();
3236
if (this.stereoPanner) {
3337
this.stereoPanner.disconnect();
38+
delete this.stereoPanner;
3439
}
3540
}
41+
42+
//not implemented because stereopanner
43+
//node does not require this and will automatically
44+
//convert single channel or multichannel to stereo.
45+
//tested with single and stereo, not with (>2) multichannel
46+
inputChannels() {}
3647
}
3748

3849
panner = Panner;

src/soundfile.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ class SoundFile {
182182
this.startMillis = null;
183183

184184
// stereo panning
185-
this.panPosition = 0.0;
186-
this.panner = new Panner(this.output, p5sound.input, 2);
185+
if (typeof p5sound.audiocontext.createStereoPanner !== 'undefined') {
186+
this.panner = new Panner();
187+
this.output.connect(this.panner);
188+
} else {
189+
this.panner = new Panner(this.output, p5sound.input, 2);
190+
}
187191

188192
// it is possible to instantiate a soundfile with no path
189193
if (this.url || this.file) {
@@ -795,7 +799,6 @@ class SoundFile {
795799
* </div></code>
796800
*/
797801
pan(pval, tFromNow) {
798-
this.panPosition = pval;
799802
this.panner.pan(pval, tFromNow);
800803
}
801804

@@ -809,7 +812,7 @@ class SoundFile {
809812
* 0.0 is center and default.
810813
*/
811814
getPan() {
812-
return this.panPosition;
815+
return this.panner.getPan();
813816
}
814817

815818
/**
@@ -1264,7 +1267,7 @@ class SoundFile {
12641267
this.output = null;
12651268
}
12661269
if (this.panner) {
1267-
this.panner.disconnect();
1270+
this.panner.dispose();
12681271
this.panner = null;
12691272
}
12701273
}

0 commit comments

Comments
 (0)