Skip to content

Commit b6b1562

Browse files
authored
Replacement of es5 functions to es6 classes feat p5.panner (#535)
1 parent 7b24c41 commit b6b1562

File tree

2 files changed

+102
-87
lines changed

2 files changed

+102
-87
lines changed

src/app.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ p5.prototype.saveSound = saveSound;
1313

1414
import './errorHandler';
1515
import './audioWorklet';
16-
import './panner';
16+
17+
import Panner from './panner';
18+
p5.Panner = Panner;
1719

1820
import SoundFile, { loadSound } from './soundfile';
1921
p5.SoundFile = SoundFile;
2022
p5.prototype.loadSound = loadSound;
2123
// register preload handling of loadSound
2224
p5.prototype.registerPreloadMethod('loadSound', p5.prototype);
2325

26+
2427
import Amplitude from './amplitude';
2528
p5.Amplitude = Amplitude;
2629

@@ -72,13 +75,15 @@ import Delay from './delay';
7275
p5.Delay = Delay;
7376

7477

78+
7579
import { Reverb, Convolver, createConvolver } from './reverb';
7680
p5.Reverb = Reverb;
7781
p5.Convolver = Convolver;
7882
p5.prototype.createConvolver = createConvolver;
7983
p5.prototype.registerPreloadMethod('createConvolver', p5.prototype);
8084

8185

86+
8287
import Metro from './metro';
8388
p5.Metro = Metro;
8489

@@ -97,14 +102,15 @@ import Compressor from './compressor';
97102
p5.Compressor = Compressor;
98103

99104

100-
101105
import peakDetect from './peakDetect';
102106
p5.peakDetect = peakDetect;
103107

104108

109+
105110
import SoundRecorder from './soundRecorder';
106111
p5.SoundRecorder = SoundRecorder;
107112

113+
108114
import Distortion from './distortion';
109115
p5.Distortion = Distortion;
110116

src/panner.js

Lines changed: 94 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,113 @@
11
import p5sound from './master';
22
var ac = p5sound.audiocontext;
3-
3+
var panner;
44
// Stereo panner
55
// if there is a stereo panner node use it
66
if (typeof ac.createStereoPanner !== 'undefined') {
7-
p5.Panner = function (input, output) {
8-
this.stereoPanner = this.input = ac.createStereoPanner();
9-
input.connect(this.stereoPanner);
10-
this.stereoPanner.connect(output);
11-
};
12-
13-
p5.Panner.prototype.pan = function (val, tFromNow) {
14-
var time = tFromNow || 0;
15-
var t = ac.currentTime + time;
16-
17-
this.stereoPanner.pan.linearRampToValueAtTime(val, t);
18-
};
19-
20-
//not implemented because stereopanner
21-
//node does not require this and will automatically
22-
//convert single channel or multichannel to stereo.
23-
//tested with single and stereo, not with (>2) multichannel
24-
p5.Panner.prototype.inputChannels = function () {};
25-
26-
p5.Panner.prototype.connect = function (obj) {
27-
this.stereoPanner.connect(obj);
28-
};
29-
30-
p5.Panner.prototype.disconnect = function () {
31-
if (this.stereoPanner) {
32-
this.stereoPanner.disconnect();
7+
class Panner {
8+
constructor(input, output) {
9+
this.stereoPanner = this.input = ac.createStereoPanner();
10+
input.connect(this.stereoPanner);
11+
this.stereoPanner.connect(output);
12+
}
13+
14+
pan(val, tFromNow) {
15+
var time = tFromNow || 0;
16+
var t = ac.currentTime + time;
17+
18+
this.stereoPanner.pan.linearRampToValueAtTime(val, t);
19+
}
20+
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);
29+
}
30+
31+
disconnect() {
32+
if (this.stereoPanner) {
33+
this.stereoPanner.disconnect();
34+
}
3335
}
34-
};
36+
}
37+
38+
panner = Panner;
3539
} else {
3640
// if there is no createStereoPanner object
3741
// such as in safari 7.1.7 at the time of writing this
3842
// use this method to create the effect
39-
p5.Panner = function (input, output, numInputChannels) {
40-
this.input = ac.createGain();
41-
input.connect(this.input);
42-
43-
this.left = ac.createGain();
44-
this.right = ac.createGain();
45-
this.left.channelInterpretation = 'discrete';
46-
this.right.channelInterpretation = 'discrete';
47-
48-
// if input is stereo
49-
if (numInputChannels > 1) {
50-
this.splitter = ac.createChannelSplitter(2);
51-
this.input.connect(this.splitter);
52-
53-
this.splitter.connect(this.left, 1);
54-
this.splitter.connect(this.right, 0);
55-
} else {
56-
this.input.connect(this.left);
57-
this.input.connect(this.right);
58-
}
43+
class Panner {
44+
constructor(input, output, numInputChannels) {
45+
this.input = ac.createGain();
46+
input.connect(this.input);
5947

60-
this.output = ac.createChannelMerger(2);
61-
this.left.connect(this.output, 0, 1);
62-
this.right.connect(this.output, 0, 0);
63-
this.output.connect(output);
64-
};
65-
66-
// -1 is left, +1 is right
67-
p5.Panner.prototype.pan = function (val, tFromNow) {
68-
var time = tFromNow || 0;
69-
var t = ac.currentTime + time;
70-
var v = (val + 1) / 2;
71-
var rightVal = Math.cos((v * Math.PI) / 2);
72-
var leftVal = Math.sin((v * Math.PI) / 2);
73-
this.left.gain.linearRampToValueAtTime(leftVal, t);
74-
this.right.gain.linearRampToValueAtTime(rightVal, t);
75-
};
76-
77-
p5.Panner.prototype.inputChannels = function (numChannels) {
78-
if (numChannels === 1) {
79-
this.input.disconnect();
80-
this.input.connect(this.left);
81-
this.input.connect(this.right);
82-
} else if (numChannels === 2) {
83-
if (typeof this.splitter === 'undefined') {
48+
this.left = ac.createGain();
49+
this.right = ac.createGain();
50+
this.left.channelInterpretation = 'discrete';
51+
this.right.channelInterpretation = 'discrete';
52+
53+
// if input is stereo
54+
if (numInputChannels > 1) {
8455
this.splitter = ac.createChannelSplitter(2);
56+
this.input.connect(this.splitter);
57+
58+
this.splitter.connect(this.left, 1);
59+
this.splitter.connect(this.right, 0);
60+
} else {
61+
this.input.connect(this.left);
62+
this.input.connect(this.right);
63+
}
64+
65+
this.output = ac.createChannelMerger(2);
66+
this.left.connect(this.output, 0, 1);
67+
this.right.connect(this.output, 0, 0);
68+
this.output.connect(output);
69+
}
70+
71+
// -1 is left, +1 is right
72+
pan(val, tFromNow) {
73+
var time = tFromNow || 0;
74+
var t = ac.currentTime + time;
75+
var v = (val + 1) / 2;
76+
var rightVal = Math.cos((v * Math.PI) / 2);
77+
var leftVal = Math.sin((v * Math.PI) / 2);
78+
this.left.gain.linearRampToValueAtTime(leftVal, t);
79+
this.right.gain.linearRampToValueAtTime(rightVal, t);
80+
}
81+
82+
inputChannels(numChannels) {
83+
if (numChannels === 1) {
84+
this.input.disconnect();
85+
this.input.connect(this.left);
86+
this.input.connect(this.right);
87+
} else if (numChannels === 2) {
88+
if (typeof this.splitter === 'undefined') {
89+
this.splitter = ac.createChannelSplitter(2);
90+
}
91+
this.input.disconnect();
92+
this.input.connect(this.splitter);
93+
this.splitter.connect(this.left, 1);
94+
this.splitter.connect(this.right, 0);
8595
}
86-
this.input.disconnect();
87-
this.input.connect(this.splitter);
88-
this.splitter.connect(this.left, 1);
89-
this.splitter.connect(this.right, 0);
9096
}
91-
};
9297

93-
p5.Panner.prototype.connect = function (obj) {
94-
this.output.connect(obj);
95-
};
98+
connect(obj) {
99+
this.output.connect(obj);
100+
}
96101

97-
p5.Panner.prototype.disconnect = function () {
98-
if (this.output) {
99-
this.output.disconnect();
102+
disconnect() {
103+
if (this.output) {
104+
this.output.disconnect();
105+
}
100106
}
101-
};
107+
}
108+
panner = Panner;
102109
}
103110

104-
export default p5.Panner;
111+
112+
export default panner;
113+

0 commit comments

Comments
 (0)