Skip to content

Commit

Permalink
basic draft of improved polysynth
Browse files Browse the repository at this point in the history
  • Loading branch information
jvntf committed Aug 19, 2017
1 parent 9a431bd commit 25630c2
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 63 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ module.exports = function(grunt) {
'metro': 'src/metro',
'peakdetect': 'src/peakDetect',
'gain': 'src/gain',
'audioVoice': 'src/audioVoice',
'monosynth': 'src/monosynth',
'polysynth': 'src/polysynth'
},
Expand Down
27 changes: 19 additions & 8 deletions examples/synthpresets/sketch.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
var monoSynth;
var note;
var octave = 0;

function setup() {
monoSynth = new p5.MonoSynth();
monoSynth.loadPreset('punchyBass');
}

function keyPressed() {
// pick a random midi note
var midiVal = round( random(50,72) );
monoSynth.triggerAttack(keyToMidi(), 1 );
console.log(key);

// monoSynth.triggerAttack(keyToMidi() + octave, 1 )



//OCTAVEf
if (keyCode === UP_ARROW) {
octave +=12;
} else if (keyCode === DOWN_ARROW) {
octave -=12;
}
else {
monoSynth.play(keyToMidi() + octave, 1);

}
}

function keyReleased() {
monoSynth.triggerRelease();
}
// function keyReleased() {
// monoSynth.triggerRelease();
// }


function keyToMidi() {
Expand Down
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ define(function (require) {
require('monosynth');
require('polysynth');
require('distortion');
require('audioVoice');
require('monosynth');
require('polysynth');

Expand Down
70 changes: 70 additions & 0 deletions src/audioVoice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';
define(function() {
var p5sound = require('master');

/**
* Base class for synthesizers and instruments
* handles note triggering and what not
*/
p5.AudioVoice = function () {


//this.osctype = 'sine';
//this.volume= 0.33;
//this.note = 60;
this.note;

// this.attack = 0.25;
// this.decay=0.25;
// this.sustain=0.95;
// this.release=0.25;
//this.env = new p5.Env(this.attack,this.volume, this.decay,this.volume, this.sustain, this.volume,this.release);

//this.filter.set(22050, 5);

//this.env.connect(this.filter);
//
this.ac = p5sound.audiocontext;
this.output = this.ac.createGain();
this.connect();

p5sound.soundArray.push(this);
};

p5.AudioVoice.prototype._setNote = function() {
};

p5.AudioVoice.prototype.play = function () {
};

p5.AudioVoice.prototype.triggerAttack = function (note, velocity, secondsFromNow) {
};

p5.AudioVoice.prototype.triggerRelease = function () {
};

p5.AudioVoice.prototype.amp = function(vol, rampTime) {
};

p5.AudioVoice.prototype.setParams = function(params) {
};

p5.AudioVoice.prototype.loadPreset = function(preset) {
};

p5.AudioVoice.prototype.connect = function(unit) {
var u = unit || p5sound.input;
this.output.connect(u.input ? u.input : u);
};

p5.AudioVoice.prototype.disconect = function() {
this.output.disconnect();
};

p5.AudioVoice.prototype.dispose = function() {
this.output.disconnect();
delete this.output;
};

return p5.AudioVoice;
});
117 changes: 90 additions & 27 deletions src/monosynth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
define(function (require) {

var p5sound = require('master');
var AudioVoice = require('audioVoice');
require('sndcore');
require('oscillator');
require('env');
Expand All @@ -19,10 +20,11 @@ define(function (require) {
**/

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

this.ac = p5sound.audiocontext;
// this.ac = p5sound.audiocontext;

this.output = this.ac.createGain();
// this.output = this.ac.createGain();

// this.attack = 0.02;
// this.decay=0.25;
Expand Down Expand Up @@ -54,6 +56,8 @@ define(function (require) {

this.oscillator.start();

this._isOn = false;

p5sound.soundArray.push(this);
};

Expand Down Expand Up @@ -112,7 +116,7 @@ define(function (require) {
*/
p5.MonoSynth.prototype.triggerAttack = function (note, velocity, secondsFromNow) {
this._setNote(note, secondsFromNow);

this._isOn = true;
this.env.ramp(this.output, secondsFromNow , velocity);
// this.env.triggerAttack(this.output, secondsFromNow);
};
Expand All @@ -128,6 +132,7 @@ define(function (require) {

p5.MonoSynth.prototype.triggerRelease = function (secondsFromNow) {
this.env.ramp(this.output, secondsFromNow, 0);
this._isOn = false;
// this.env.triggerRelease(this.output, secondsFromNow);
};

Expand All @@ -150,14 +155,17 @@ define(function (require) {
};




p5.MonoSynth.prototype.loadPreset = function(preset) {
var options = this[preset];
this.oscillator.setType(options.oscillator.type);

};


this.env.setADSR(options.env.attack, options.env.decay,
options.env.sustain, options.env.release);

this.filter.setType(options.filter.type);
this.filter.set(options.filter.freq, options.filter.res);
return this;
};

//PRESETS
//
Expand All @@ -166,10 +174,13 @@ define(function (require) {
'type' : 'sine'
},
'env' : {
'setADSR': [0.02, 0.25, 0.05, 0.35]
'attack' : 0.02,
'decay' : 0.25,
'sustain': 0.05,
'release': 0.35
},
'filter': {
'setType' : 'highpass',
'type' : 'highpass',
'freq' : 5,
'res' : 1
}
Expand All @@ -180,14 +191,34 @@ define(function (require) {
'type' : 'square'
},
'env' : {
'setADSR': [0.02, 0.25, 0.05, 0.35]
'attack': 0,
'decay': .60,
'sustain': 0.1,
'release': .28
},
'filter' : {
'type' : 'lowpass',
'freq' : 15000,
'res' : 1
}
};

p5.MonoSynth.prototype.electricPiano = {
'oscillator' : {
'type' : 'sine'
},
'env' : {
'attack': 0.029,
'decay': .16,
'sustain': 0.1,
'release': .1
},
'filter': {
'setType' : 'highpass',
'freq' : 5,
'type' : 'lowpass',
'freq' : 15000,
'res' : 1
}
}
};
/**
* Set values like a traditional
* <a href="https://en.wikipedia.org/wiki/Synthesizer#/media/File:ADSR_parameter.svg">
Expand All @@ -210,14 +241,50 @@ define(function (require) {
* @param {Number} [releaseTime] Time in seconds from now (defaults to 0)
**/

p5.MonoSynth.prototype.setADSR = function (a,d,s,r) {
this.attack = a;
this.decay=d;
this.sustain=s;
this.release=r;
this.env.setADSR(this.attack, this.decay, this.sustain, this.release);
p5.MonoSynth.prototype.setADSR = function (attack,decay,sustain,release) {
this.env.setADSR(attack, decay, sustain, release);
};


Object.defineProperties(p5.MonoSynth, {
'attack': {
get : function() {
return this.env.aTime;
},
set : function(attack) {
this.env.setADSR(attack, this.env.dTime,
this.env.sPercent, this.env.rTime);
}
},
'decay': {
get : function() {
return this.env.dTime;
},
set : function(decay) {
this.env.setADSR(this.env.aTime, decay,
this.env.sPercent, this.env.rTime);
}
},
'sustain': {
get : function() {
return this.env.sPercent;
},
set : function(sustain) {
this.env.setADSR(this.env.aTime, this.env.dTime,
sustain, this.env.rTime);
}
},
'release': {
get : function() {
return this.env.rTime;
},
set : function(release) {
this.env.setADSR(this.env.aTime, this.env.dTime,
this.env.sPercent, release);
}
},
});

p5.MonoSynth.prototype.amp = function(vol, rampTime) {
var t = rampTime || 0;
if (typeof vol !== 'undefined') {
Expand All @@ -233,7 +300,7 @@ define(function (require) {
* @method connect
* @param {Object} unit A p5.sound or Web Audio object
*/

p5.MonoSynth.prototype.connect = function(unit) {
var u = unit || p5sound.input;
this.output.connect(u.input ? u.input : u);
Expand All @@ -255,19 +322,15 @@ define(function (require) {
* @method dispose
*/
p5.MonoSynth.prototype.dispose = function() {
AudioVoice.prototype.disposed.apply(this);

this.filter.dispose();
this.env.dispose();

try {
this.oscillator.dispose();
} catch(e) {
console.error('mono synth default oscillator already disposed');
}

this.output.disconnect();
this.output = undefined;
};



});
Loading

0 comments on commit 25630c2

Please sign in to comment.