Skip to content

Commit ef5fc02

Browse files
committed
fixed an scheduling issue in oscillator
1 parent fec77b0 commit ef5fc02

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/audioVoice.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ define(function() {
1010
this.ac = p5sound.audiocontext;
1111
this.output = this.ac.createGain();
1212
this.connect();
13-
1413
p5sound.soundArray.push(this);
1514
};
1615

src/monosynth.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ define(function (require) {
1717
* @class p5.MonoSynth
1818
* @constructor
1919
*
20-
*
2120
**/
2221

2322
p5.MonoSynth = function () {
2423
AudioVoice.call(this);
2524

2625
this.oscillator = new p5.Oscillator();
26+
this.oscillator.disconnect();
27+
2728

2829
this.env = new p5.Env();
2930
this.env.setRange(1, 0);
@@ -37,15 +38,16 @@ define(function (require) {
3738
this.filter.set(5, 1);
3839

3940
// oscillator --> env --> filter --> this.output (gain) --> p5.soundOut
41+
42+
this.oscillator.connect(this.filter);
4043
this.env.setInput(this.oscillator);
4144
this.env.connect(this.filter);
4245
this.filter.connect(this.output);
4346

44-
// connect to master output by default
45-
this.connect();
46-
4747
this.oscillator.start();
4848

49+
//Audiovoices are connected to soundout by default
50+
4951
this._isOn = false;
5052

5153
p5sound.soundArray.push(this);
@@ -69,8 +71,7 @@ define(function (require) {
6971
*/
7072
p5.MonoSynth.prototype._setNote = function(note, secondsFromNow) {
7173
var freqVal = p5.prototype.midiToFreq(note);
72-
var t = secondsFromNow || 0;
73-
this.oscillator.freq(freqVal, 0, t);
74+
this.oscillator.freq(freqVal, 0, secondsFromNow);
7475
};
7576

7677
/**
@@ -85,13 +86,8 @@ define(function (require) {
8586
*/
8687

8788
p5.MonoSynth.prototype.play = function (note, velocity, secondsFromNow, susTime) {
88-
this._setNote(note, secondsFromNow);
89-
9089
// set range of env (TO DO: allow this to be scheduled in advance)
9190
var vel = velocity || 1;
92-
// this.env.setRange(vel, 0);
93-
94-
// this.env.play(this.output, secondsFromNow, susTime);
9591

9692
this.triggerAttack(note,velocity,secondsFromNow);
9793
this.triggerRelease(secondsFromNow + susTime);
@@ -108,10 +104,15 @@ define(function (require) {
108104
* @method triggerAttack
109105
*/
110106
p5.MonoSynth.prototype.triggerAttack = function (note, velocity, secondsFromNow) {
111-
this._setNote(note, secondsFromNow);
107+
108+
var now = p5sound.audiocontext.currentTime;
109+
var tFromNow = secondsFromNow || 0;
110+
var t = now + tFromNow;
111+
var n = p5.prototype.midiToFreq(note);
112+
112113
this._isOn = true;
113-
this.env.ramp(this.output, secondsFromNow , velocity);
114-
// this.env.triggerAttack(this.output, secondsFromNow);
114+
this.oscillator.freq(n, 0, t);
115+
this.env.ramp(this.output, t, velocity);
115116
};
116117

117118
/**
@@ -163,7 +164,6 @@ define(function (require) {
163164
};
164165

165166
//PRESETS
166-
//
167167
p5.MonoSynth.prototype.default = {
168168
'oscillator' : {
169169
'type' : 'sine'

src/oscillator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,12 @@ define(function (require) {
236236
var now = p5sound.audiocontext.currentTime;
237237
var rampTime = rampTime || 0;
238238
var tFromNow = tFromNow || 0;
239+
var t = now + tFromNow + rampTime;
239240
// var currentFreq = this.oscillator.frequency.value;
240241
// this.oscillator.frequency.cancelScheduledValues(now);
241242

242243
if (rampTime === 0) {
243-
this.oscillator.frequency.cancelScheduledValues(now);
244+
// this.oscillator.frequency.cancelScheduledValues(now);
244245
this.oscillator.frequency.setValueAtTime(val, tFromNow + now);
245246
} else {
246247
if (val > 0 ) {

0 commit comments

Comments
 (0)