Skip to content

Commit 2896d6e

Browse files
authored
replacement of es5 functions to es6 classes feat p5.Pulse (#534)
1 parent 838f3ac commit 2896d6e

File tree

2 files changed

+143
-132
lines changed

2 files changed

+143
-132
lines changed

src/app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ p5.FFT = FFT;
3030
import './signal';
3131
import './oscillator';
3232
import './envelope';
33-
import './pulse';
33+
import Pulse from './pulse';
34+
p5.Pulse = Pulse;
3435
import './noise';
3536

3637
import AudioIn from './audioin';
@@ -71,9 +72,11 @@ import Compressor from './compressor';
7172
p5.Compressor = Compressor;
7273

7374

75+
7476
import peakDetect from './peakDetect';
7577
p5.peakDetect = peakDetect;
7678

79+
7780
import SoundRecorder from './soundRecorder';
7881
p5.SoundRecorder = SoundRecorder;
7982

src/pulse.js

Lines changed: 139 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import p5sound from './master';
2-
import './oscillator';
2+
import Oscillator, { SawOsc } from './oscillator';
33

44
/**
55
* Creates a Pulse object, an oscillator that implements
@@ -45,150 +45,156 @@ import './oscillator';
4545
* }
4646
* </code></div>
4747
*/
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');
8351

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;
10254

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
13562
this.dcOffset = createDCOffset();
63+
this.dcGain = p5sound.audiocontext.createGain();
13664
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);
13878

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);
143110
}
144-
this.started = true;
145-
this.osc2.started = true;
146111
}
147-
};
148112

149-
p5.Pulse.prototype.stop = function (time) {
150-
if (this.started) {
151-
var t = time || 0;
113+
start(f, time) {
152114
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;
156148
}
157-
this.dcOffset.stop(t + now);
158-
this.started = false;
159-
this.osc2.started = false;
160149
}
161-
};
162150

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;
184162
}
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;
190163
}
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+
}
192198

193199
// inspiration: http://webaudiodemos.appspot.com/oscilloscope/
194200
function createDCOffset() {
@@ -201,3 +207,5 @@ function createDCOffset() {
201207
bufferSource.loop = true;
202208
return bufferSource;
203209
}
210+
211+
export default Pulse;

0 commit comments

Comments
 (0)