1+ import Effect from './effect.js' ;
2+
13import p5sound from './main' ;
24var ac = p5sound . audiocontext ;
35var panner ;
46// Stereo panner
57// if there is a stereo panner node use it
68if ( 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 ) ;
1229 }
1330
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+ */
1441 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+ }
1951 }
2052
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 ;
2962 }
3063
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 ( ) ;
3272 if ( this . stereoPanner ) {
3373 this . stereoPanner . disconnect ( ) ;
74+ delete this . stereoPanner ;
3475 }
3576 }
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 ( ) { }
3683 }
3784
3885 panner = Panner ;
@@ -45,6 +92,7 @@ if (typeof ac.createStereoPanner !== 'undefined') {
4592 this . input = ac . createGain ( ) ;
4693 input . connect ( this . input ) ;
4794
95+ this . panValue = 0 ;
4896 this . left = ac . createGain ( ) ;
4997 this . right = ac . createGain ( ) ;
5098 this . left . channelInterpretation = 'discrete' ;
@@ -70,6 +118,7 @@ if (typeof ac.createStereoPanner !== 'undefined') {
70118
71119 // -1 is left, +1 is right
72120 pan ( val , tFromNow ) {
121+ this . panValue = val ;
73122 var time = tFromNow || 0 ;
74123 var t = ac . currentTime + time ;
75124 var v = ( val + 1 ) / 2 ;
@@ -79,6 +128,10 @@ if (typeof ac.createStereoPanner !== 'undefined') {
79128 this . right . gain . linearRampToValueAtTime ( rightVal , t ) ;
80129 }
81130
131+ getPan ( ) {
132+ return this . panValue ;
133+ }
134+
82135 inputChannels ( numChannels ) {
83136 if ( numChannels === 1 ) {
84137 this . input . disconnect ( ) ;
@@ -104,6 +157,29 @@ if (typeof ac.createStereoPanner !== 'undefined') {
104157 this . output . disconnect ( ) ;
105158 }
106159 }
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+ }
107183 }
108184 panner = Panner ;
109185}
0 commit comments