1
+ import Effect from './effect.js' ;
2
+
1
3
import p5sound from './main' ;
2
4
var ac = p5sound . audiocontext ;
3
5
var panner ;
4
6
// Stereo panner
5
7
// if there is a stereo panner node use it
6
8
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
+ /**
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
+ */
22
+ class Panner extends Effect {
23
+ constructor ( ) {
24
+ super ( ) ;
25
+ this . stereoPanner = this . ac . createStereoPanner ( ) ;
26
+
27
+ this . input . connect ( this . stereoPanner ) ;
28
+ this . stereoPanner . connect ( this . wet ) ;
12
29
}
13
30
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
+ */
14
41
pan ( val , tFromNow ) {
15
- var time = tFromNow || 0 ;
16
- var t = ac . currentTime + time ;
17
-
18
- this . stereoPanner . pan . linearRampToValueAtTime ( val , t ) ;
42
+ if ( typeof val === 'number' ) {
43
+ let time = tFromNow || 0 ;
44
+ this . stereoPanner . pan . linearRampToValueAtTime (
45
+ val ,
46
+ this . ac . currentTime + time
47
+ ) ;
48
+ } else if ( typeof val !== 'undefined' ) {
49
+ val . connect ( this . stereoPanner . pan ) ;
50
+ }
19
51
}
20
52
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 ) ;
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
+ */
60
+ getPan ( ) {
61
+ return this . stereoPanner . pan . value ;
29
62
}
30
63
31
- disconnect ( ) {
64
+ /**
65
+ * Get rid of the Panner and free up its resources / memory.
66
+ *
67
+ * @method dispose
68
+ * @for p5.Panner
69
+ */
70
+ dispose ( ) {
71
+ super . dispose ( ) ;
32
72
if ( this . stereoPanner ) {
33
73
this . stereoPanner . disconnect ( ) ;
74
+ delete this . stereoPanner ;
34
75
}
35
76
}
77
+
78
+ //not implemented because stereopanner
79
+ //node does not require this and will automatically
80
+ //convert single channel or multichannel to stereo.
81
+ //tested with single and stereo, not with (>2) multichannel
82
+ inputChannels ( ) { }
36
83
}
37
84
38
85
panner = Panner ;
@@ -45,6 +92,7 @@ if (typeof ac.createStereoPanner !== 'undefined') {
45
92
this . input = ac . createGain ( ) ;
46
93
input . connect ( this . input ) ;
47
94
95
+ this . panValue = 0 ;
48
96
this . left = ac . createGain ( ) ;
49
97
this . right = ac . createGain ( ) ;
50
98
this . left . channelInterpretation = 'discrete' ;
@@ -70,6 +118,7 @@ if (typeof ac.createStereoPanner !== 'undefined') {
70
118
71
119
// -1 is left, +1 is right
72
120
pan ( val , tFromNow ) {
121
+ this . panValue = val ;
73
122
var time = tFromNow || 0 ;
74
123
var t = ac . currentTime + time ;
75
124
var v = ( val + 1 ) / 2 ;
@@ -79,6 +128,10 @@ if (typeof ac.createStereoPanner !== 'undefined') {
79
128
this . right . gain . linearRampToValueAtTime ( rightVal , t ) ;
80
129
}
81
130
131
+ getPan ( ) {
132
+ return this . panValue ;
133
+ }
134
+
82
135
inputChannels ( numChannels ) {
83
136
if ( numChannels === 1 ) {
84
137
this . input . disconnect ( ) ;
@@ -104,6 +157,29 @@ if (typeof ac.createStereoPanner !== 'undefined') {
104
157
this . output . disconnect ( ) ;
105
158
}
106
159
}
160
+
161
+ dispose ( ) {
162
+ if ( this . input ) {
163
+ this . input . disconnect ( ) ;
164
+ delete this . input ;
165
+ }
166
+ if ( this . output ) {
167
+ this . output . disconnect ( ) ;
168
+ delete this . output ;
169
+ }
170
+ if ( this . left ) {
171
+ this . left . disconnect ( ) ;
172
+ delete this . left ;
173
+ }
174
+ if ( this . right ) {
175
+ this . right . disconnect ( ) ;
176
+ delete this . right ;
177
+ }
178
+ if ( this . splitter ) {
179
+ this . splitter . disconnect ( ) ;
180
+ delete this . splitter ;
181
+ }
182
+ }
107
183
}
108
184
panner = Panner ;
109
185
}
0 commit comments