1
1
import p5sound from './master' ;
2
- import './oscillator' ;
2
+ import Oscillator , { SawOsc } from './oscillator' ;
3
3
4
4
/**
5
5
* Creates a Pulse object, an oscillator that implements
@@ -45,150 +45,156 @@ import './oscillator';
45
45
* }
46
46
* </code></div>
47
47
*/
48
- p5 . Pulse = function ( freq , w ) {
49
- p5 . Oscillator . call ( this , freq , 'sawtooth' ) ;
50
-
51
- // width of PWM, should be betw 0 to 1.0
52
- this . w = w || 0 ;
53
-
54
- // create a second oscillator with inverse frequency
55
- this . osc2 = new p5 . SawOsc ( freq ) ;
56
-
57
- // create a delay node
58
- this . dNode = p5sound . audiocontext . createDelay ( ) ;
59
-
60
- // dc offset
61
- this . dcOffset = createDCOffset ( ) ;
62
- this . dcGain = p5sound . audiocontext . createGain ( ) ;
63
- this . dcOffset . connect ( this . dcGain ) ;
64
- this . dcGain . connect ( this . output ) ;
65
- // set delay time based on PWM width
66
- this . f = freq || 440 ;
67
- var mW = this . w / this . oscillator . frequency . value ;
68
- this . dNode . delayTime . value = mW ;
69
- this . dcGain . gain . value = 1.7 * ( 0.5 - this . w ) ;
70
-
71
- // disconnect osc2 and connect it to delay, which is connected to output
72
- this . osc2 . disconnect ( ) ;
73
- this . osc2 . panner . disconnect ( ) ;
74
- this . osc2 . amp ( - 1 ) ; // inverted amplitude
75
- this . osc2 . output . connect ( this . dNode ) ;
76
- this . dNode . connect ( this . output ) ;
77
-
78
- this . output . gain . value = 1 ;
79
- this . output . connect ( this . panner ) ;
80
- } ;
81
-
82
- p5 . Pulse . prototype = Object . create ( p5 . Oscillator . prototype ) ;
48
+ class Pulse extends Oscillator {
49
+ constructor ( freq , w ) {
50
+ super ( freq , 'sawtooth' ) ;
83
51
84
- /**
85
- * Set the width of a Pulse object (an oscillator that implements
86
- * Pulse Width Modulation).
87
- *
88
- * @method width
89
- * @param {Number } [width] Width between the pulses (0 to 1.0,
90
- * defaults to 0)
91
- */
92
- p5 . Pulse . prototype . width = function ( w ) {
93
- if ( typeof w === 'number' ) {
94
- if ( w <= 1.0 && w >= 0.0 ) {
95
- this . w = w ;
96
- // set delay time based on PWM width
97
-
98
- // var mW = map(this.w, 0, 1.0, 0, 1/this.f);
99
- var mW = this . w / this . oscillator . frequency . value ;
100
- this . dNode . delayTime . value = mW ;
101
- }
52
+ // width of PWM, should be betw 0 to 1.0
53
+ this . w = w || 0 ;
102
54
103
- this . dcGain . gain . value = 1.7 * ( 0.5 - this . w ) ;
104
- } else {
105
- w . connect ( this . dNode . delayTime ) ;
106
- var sig = new p5 . SignalAdd ( - 0.5 ) ;
107
- sig . setInput ( w ) ;
108
- sig = sig . mult ( - 1 ) ;
109
- sig = sig . mult ( 1.7 ) ;
110
- sig . connect ( this . dcGain . gain ) ;
111
- }
112
- } ;
113
-
114
- p5 . Pulse . prototype . start = function ( f , time ) {
115
- var now = p5sound . audiocontext . currentTime ;
116
- var t = time || 0 ;
117
- if ( ! this . started ) {
118
- var freq = f || this . f ;
119
- var type = this . oscillator . type ;
120
- this . oscillator = p5sound . audiocontext . createOscillator ( ) ;
121
- this . oscillator . frequency . setValueAtTime ( freq , now ) ;
122
- this . oscillator . type = type ;
123
- this . oscillator . connect ( this . output ) ;
124
- this . oscillator . start ( t + now ) ;
125
-
126
- // set up osc2
127
- this . osc2 . oscillator = p5sound . audiocontext . createOscillator ( ) ;
128
- this . osc2 . oscillator . frequency . setValueAtTime ( freq , t + now ) ;
129
- this . osc2 . oscillator . type = type ;
130
- this . osc2 . oscillator . connect ( this . osc2 . output ) ;
131
- this . osc2 . start ( t + now ) ;
132
- this . freqNode = [ this . oscillator . frequency , this . osc2 . oscillator . frequency ] ;
133
-
134
- // start dcOffset, too
55
+ // create a second oscillator with inverse frequency
56
+ this . osc2 = new SawOsc ( freq ) ;
57
+
58
+ // create a delay node
59
+ this . dNode = p5sound . audiocontext . createDelay ( ) ;
60
+
61
+ // dc offset
135
62
this . dcOffset = createDCOffset ( ) ;
63
+ this . dcGain = p5sound . audiocontext . createGain ( ) ;
136
64
this . dcOffset . connect ( this . dcGain ) ;
137
- this . dcOffset . start ( t + now ) ;
65
+ this . dcGain . connect ( this . output ) ;
66
+ // set delay time based on PWM width
67
+ this . f = freq || 440 ;
68
+ var mW = this . w / this . oscillator . frequency . value ;
69
+ this . dNode . delayTime . value = mW ;
70
+ this . dcGain . gain . value = 1.7 * ( 0.5 - this . w ) ;
71
+
72
+ // disconnect osc2 and connect it to delay, which is connected to output
73
+ this . osc2 . disconnect ( ) ;
74
+ this . osc2 . panner . disconnect ( ) ;
75
+ this . osc2 . amp ( - 1 ) ; // inverted amplitude
76
+ this . osc2 . output . connect ( this . dNode ) ;
77
+ this . dNode . connect ( this . output ) ;
138
78
139
- // if LFO connections depend on these oscillators
140
- if ( this . mods !== undefined && this . mods . frequency !== undefined ) {
141
- this . mods . frequency . connect ( this . freqNode [ 0 ] ) ;
142
- this . mods . frequency . connect ( this . freqNode [ 1 ] ) ;
79
+ this . output . gain . value = 1 ;
80
+ this . output . connect ( this . panner ) ;
81
+ }
82
+
83
+ /**
84
+ * Set the width of a Pulse object (an oscillator that implements
85
+ * Pulse Width Modulation).
86
+ *
87
+ * @method width
88
+ * @param {Number } [width] Width between the pulses (0 to 1.0,
89
+ * defaults to 0)
90
+ */
91
+ width ( w ) {
92
+ if ( typeof w === 'number' ) {
93
+ if ( w <= 1.0 && w >= 0.0 ) {
94
+ this . w = w ;
95
+ // set delay time based on PWM width
96
+
97
+ // var mW = map(this.w, 0, 1.0, 0, 1/this.f);
98
+ var mW = this . w / this . oscillator . frequency . value ;
99
+ this . dNode . delayTime . value = mW ;
100
+ }
101
+
102
+ this . dcGain . gain . value = 1.7 * ( 0.5 - this . w ) ;
103
+ } else {
104
+ w . connect ( this . dNode . delayTime ) ;
105
+ var sig = new p5 . SignalAdd ( - 0.5 ) ; //repalce it with tones Signals Method
106
+ sig . setInput ( w ) ;
107
+ sig = sig . mult ( - 1 ) ;
108
+ sig = sig . mult ( 1.7 ) ;
109
+ sig . connect ( this . dcGain . gain ) ;
143
110
}
144
- this . started = true ;
145
- this . osc2 . started = true ;
146
111
}
147
- } ;
148
112
149
- p5 . Pulse . prototype . stop = function ( time ) {
150
- if ( this . started ) {
151
- var t = time || 0 ;
113
+ start ( f , time ) {
152
114
var now = p5sound . audiocontext . currentTime ;
153
- this . oscillator . stop ( t + now ) ;
154
- if ( this . osc2 . oscillator ) {
155
- this . osc2 . oscillator . stop ( t + now ) ;
115
+ var t = time || 0 ;
116
+ if ( ! this . started ) {
117
+ var freq = f || this . f ;
118
+ var type = this . oscillator . type ;
119
+ this . oscillator = p5sound . audiocontext . createOscillator ( ) ;
120
+ this . oscillator . frequency . setValueAtTime ( freq , now ) ;
121
+ this . oscillator . type = type ;
122
+ this . oscillator . connect ( this . output ) ;
123
+ this . oscillator . start ( t + now ) ;
124
+
125
+ // set up osc2
126
+ this . osc2 . oscillator = p5sound . audiocontext . createOscillator ( ) ;
127
+ this . osc2 . oscillator . frequency . setValueAtTime ( freq , t + now ) ;
128
+ this . osc2 . oscillator . type = type ;
129
+ this . osc2 . oscillator . connect ( this . osc2 . output ) ;
130
+ this . osc2 . start ( t + now ) ;
131
+ this . freqNode = [
132
+ this . oscillator . frequency ,
133
+ this . osc2 . oscillator . frequency ,
134
+ ] ;
135
+
136
+ // start dcOffset, too
137
+ this . dcOffset = createDCOffset ( ) ;
138
+ this . dcOffset . connect ( this . dcGain ) ;
139
+ this . dcOffset . start ( t + now ) ;
140
+
141
+ // if LFO connections depend on these oscillators
142
+ if ( this . mods !== undefined && this . mods . frequency !== undefined ) {
143
+ this . mods . frequency . connect ( this . freqNode [ 0 ] ) ;
144
+ this . mods . frequency . connect ( this . freqNode [ 1 ] ) ;
145
+ }
146
+ this . started = true ;
147
+ this . osc2 . started = true ;
156
148
}
157
- this . dcOffset . stop ( t + now ) ;
158
- this . started = false ;
159
- this . osc2 . started = false ;
160
149
}
161
- } ;
162
150
163
- p5 . Pulse . prototype . freq = function ( val , rampTime = 0 , tFromNow = 0 ) {
164
- if ( typeof val === 'number' ) {
165
- this . f = val ;
166
- var now = p5sound . audiocontext . currentTime ;
167
- var currentFreq = this . oscillator . frequency . value ;
168
- this . oscillator . frequency . cancelScheduledValues ( now ) ;
169
- this . oscillator . frequency . setValueAtTime ( currentFreq , now + tFromNow ) ;
170
- this . oscillator . frequency . exponentialRampToValueAtTime (
171
- val ,
172
- tFromNow + rampTime + now
173
- ) ;
174
- this . osc2 . oscillator . frequency . cancelScheduledValues ( now ) ;
175
- this . osc2 . oscillator . frequency . setValueAtTime ( currentFreq , now + tFromNow ) ;
176
- this . osc2 . oscillator . frequency . exponentialRampToValueAtTime (
177
- val ,
178
- tFromNow + rampTime + now
179
- ) ;
180
-
181
- if ( this . freqMod ) {
182
- this . freqMod . output . disconnect ( ) ;
183
- this . freqMod = null ;
151
+ stop ( time ) {
152
+ if ( this . started ) {
153
+ var t = time || 0 ;
154
+ var now = p5sound . audiocontext . currentTime ;
155
+ this . oscillator . stop ( t + now ) ;
156
+ if ( this . osc2 . oscillator ) {
157
+ this . osc2 . oscillator . stop ( t + now ) ;
158
+ }
159
+ this . dcOffset . stop ( t + now ) ;
160
+ this . started = false ;
161
+ this . osc2 . started = false ;
184
162
}
185
- } else if ( val . output ) {
186
- val . output . disconnect ( ) ;
187
- val . output . connect ( this . oscillator . frequency ) ;
188
- val . output . connect ( this . osc2 . oscillator . frequency ) ;
189
- this . freqMod = val ;
190
163
}
191
- } ;
164
+
165
+ freq ( val , rampTime = 0 , tFromNow = 0 ) {
166
+ if ( typeof val === 'number' ) {
167
+ this . f = val ;
168
+ var now = p5sound . audiocontext . currentTime ;
169
+ var currentFreq = this . oscillator . frequency . value ;
170
+ this . oscillator . frequency . cancelScheduledValues ( now ) ;
171
+ this . oscillator . frequency . setValueAtTime ( currentFreq , now + tFromNow ) ;
172
+ this . oscillator . frequency . exponentialRampToValueAtTime (
173
+ val ,
174
+ tFromNow + rampTime + now
175
+ ) ;
176
+ this . osc2 . oscillator . frequency . cancelScheduledValues ( now ) ;
177
+ this . osc2 . oscillator . frequency . setValueAtTime (
178
+ currentFreq ,
179
+ now + tFromNow
180
+ ) ;
181
+ this . osc2 . oscillator . frequency . exponentialRampToValueAtTime (
182
+ val ,
183
+ tFromNow + rampTime + now
184
+ ) ;
185
+
186
+ if ( this . freqMod ) {
187
+ this . freqMod . output . disconnect ( ) ;
188
+ this . freqMod = null ;
189
+ }
190
+ } else if ( val . output ) {
191
+ val . output . disconnect ( ) ;
192
+ val . output . connect ( this . oscillator . frequency ) ;
193
+ val . output . connect ( this . osc2 . oscillator . frequency ) ;
194
+ this . freqMod = val ;
195
+ }
196
+ }
197
+ }
192
198
193
199
// inspiration: http://webaudiodemos.appspot.com/oscilloscope/
194
200
function createDCOffset ( ) {
@@ -201,3 +207,5 @@ function createDCOffset() {
201
207
bufferSource . loop = true ;
202
208
return bufferSource ;
203
209
}
210
+
211
+ export default Pulse ;
0 commit comments