Skip to content

Commit

Permalink
migrated ex/polysynth_old synths to ex/polyphonic_synth; added a velo…
Browse files Browse the repository at this point in the history
…city max range to polysynth
  • Loading branch information
jvntf committed Aug 23, 2017
1 parent 05178da commit 6e3f153
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 182 deletions.
177 changes: 126 additions & 51 deletions examples/polyphonic_synth/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ var real_wave = [];
var imag_wave = []

function preload(){
ptable_real = loadStrings('../polyphonic_synth/TwelveStringGuitar_real.txt');
ptable_imag = loadStrings('../polyphonic_synth/TwelveStringGuitar_imag.txt' );
ptable_real = loadStrings('Wurlitzer_2_imag.txt');
ptable_imag = loadStrings('Wurlitzer_2_real.txt' );
}


Expand All @@ -29,6 +29,10 @@ function setup() {
imag_wave[i] = float(ptable_imag[i]);
}

//Uncomment for PeriodicWave
// psynth.audiovoices.forEach(function(voice){
// voice.setParams({real: real_wave , imag: imag_wave});
// });

}

Expand All @@ -40,66 +44,79 @@ function draw() {
}

function mousePressed(){

var note = int(map(mouseX,0,width,200,440));
var length = map(mouseY,0,300,0,5);



psynth.play(note,1,0,length);
psynth.noteADSR(note, 0.021,0.025,length,0.025);

var index = psynth.notes[note].getValueAtTime();
psynth.audiovoices[index].setParams({real: real_wave , imag: imag_wave});

// uncomment for SquareVoice detune parameters
//var d = int(random(1,12));
//psynth.setParams({detune: d });

// uncomment for PeriodicWave
//psynth.setParams({real: real_wave , imag: imag_wave});


psynth.setADSR(0.021,0.025,length,0.025);
psynth.play(note,1,0,1);
}


//////////////////////////////////////////////////////////////////////////////
function PeriodicWave(params){
p5.MonoSynth.call(this);

p5.AudioVoice.call(this);
this.osc = new p5.Oscillator('sine');

this.real = new Float32Array([0,0]) ;
this.imag = new Float32Array([0,1]);

this.wt = this.ac.createPeriodicWave(this.real,this.imag);

// this.oscillator = this.context.createOscillator();
this.oscillator.oscillator.setPeriodicWave(this.wt);
this.filter.setType('lowpass');
this.osc.disconnect();
this.osc.start();

this.env = new p5.Env(0.021,0.025,0.025,0.025,0.95,0.33,0.25);
this.env.disconnect();

this.filter = new p5.LowPass();
this.filter.set(22050,5);


this.env.connect(this.filter);
this.osc.connect(this.filter);

this.connect();

this.filter.set(22050,5);


this.setParams = function(params){
this.real = new Float32Array(params.real);
this.imag = new Float32Array(params.imag);
this.wt = this.ac.createPeriodicWave(this.real, this.imag);
this.oscillator.oscillator.setPeriodicWave(this. wt);
this.osc.oscillator.setPeriodicWave(this. wt);
}

this.setADSR = function(aTime,aLevel,dTime,dLevel) {
this.env.set(aTime, aLevel, dTime, dLevel)
}

this.play = function(){
this.env.play(this.filter);

}
this.setADSR = function(a,d,s,r) {
this.attack = a;
this.decay=d;
this.sustain=s;
this.release=r;
this.volume = 1
this.env.set(this.attack, this.volume, this.decay, this.volume, this.release, this.volume);
// this.env.set(this.attack, this.decay, this.sustain, this.release);
this.env.play(this.filter);

this.triggerAttack = function(note, velocity, secondsFromNow) {
var secondsFromNow = secondsFromNow || 0;

//triggerAttack uses ._setNote to convert a midi string to a frequency if necessary
var freq = typeof note === 'string' ? this._setNote(note)
: typeof note === 'number' ? note : 440;
var vel = velocity || 1;
// this.env.setRange(this.env.aLevel / velocity,0);
this._isOn = true;
this.osc.freq(freq, 0, secondsFromNow);
this.env.triggerAttack(this.filter, secondsFromNow);

}

this.triggerRelease = function(secondsFromNow) {
var secondsFromNow = secondsFromNow || 0;
this.env.triggerRelease(this.filter,secondsFromNow);
this._isOn = false;
}

}

PeriodicWave.prototype = Object.create(p5.MonoSynth.prototype);
PeriodicWave.prototype = Object.create(p5.AudioVoice.prototype);
PeriodicWave.prototype.constructor = PeriodicWave;


Expand All @@ -115,33 +132,91 @@ SquareVoice.prototype = Object.create(p5.MonoSynth.prototype); // browsers supp
SquareVoice.prototype.constructor = SquareVoice;

//////////////////////////////////////////////////////////////////////////////////////////////
// A second one
// A Detuned synth
function DetunedOsc(){

AudioVoice.call(this);
p5.MonoSynth.call(this);

this.osctype = 'sine';
this.detune = 5;
this.detune = -5;

this.oscOne = this.oscillator;
this.oscTwo = new p5.Oscillator();

this.filter.setType('lowpass');
this.filter.set(22050,5);

this.oscOne = new p5.Oscillator(midiToFreq(this.note),this.osctype);
this.oscTwo = new p5.Oscillator(midiToFreq(this.note)-this.detune,this.osctype);
this.oscOne.disconnect();
this.oscTwo.disconnect();
this.oscOne.start();
this.oscTwo.start();


this.oscOne.connect(this.filter);
this.oscTwo.connect(this.filter);

this.setNote = function(note){
this.oscOne.freq(midiToFreq(note));
this.oscTwo.freq(midiToFreq(note)-this.detune);
}

this.setParams = function(params){
this.detune = params.detune;
this.env.setInput(this.oscOne,this.oscTwo);

this.oscOne.start();
this.oscTwo.start();


this.triggerAttack = function(note, velocity, secondsFromNow) {
this.oscTwo.oscillator.detune.value
var secondsFromNow = secondsFromNow || 0;
var freq = typeof note === 'string' ? this._setNote(note)
: typeof note === 'number' ? note : 440;
var vel = velocity || 1;

this._isOn = true;

this.oscOne.freq(freq, 0, secondsFromNow);
this.oscTwo.freq(freq + this.detune, 0, secondsFromNow);
this.env.ramp(this.output, secondsFromNow, this.env.aLevel);
}

}

DetunedOsc.prototype = Object.create(p5.AudioVoice.prototype);
DetunedOsc.prototype = Object.create(p5.MonoSynth.prototype);
DetunedOsc.prototype.constructor = DetunedOsc;



function AdditiveSynth(){
p5.MonoSynth.call(this);

this.osctype = 'triangle';
this.harmonics = [1,2,4,6,8];
this.note = 60;

this.oscbank =[];
this.oscillator.dispose();
delete this.oscillator;
this.env.disconnect();

for (var i = 0 ; i < this.harmonics.length; i++){
this.oscbank.push(new p5.Oscillator());
this.oscbank[i].setType(this.osctype);
this.oscbank[i].disconnect();
this.oscbank[i].connect(this.filter);
this.env.connect(this.oscbank[i]);
this.oscbank[i].start();
}

this.triggerAttack = function(note, velocity, secondsFromNow) {
var secondsFromNow = secondsFromNow || 0;
var freq = typeof note === 'string' ? this._setNote(note)
: typeof note === 'number' ? note : 440;
var vel = velocity || 1;

this._isOn = true;

for (var i = 0 ; i < this.harmonics.length; i++){
this.oscbank[i].freq(freq + midiToFreq(this.harmonics[i]*12),0,secondsFromNow);
}

this.env.ramp(this.output, secondsFromNow, this.env.aLevel);
}

}
AdditiveSynth.prototype = Object.create(p5.MonoSynth.prototype);
AdditiveSynth.prototype.constructor = AdditiveSynth;
20 changes: 0 additions & 20 deletions examples/polysynth_old/index.html

This file was deleted.

110 changes: 0 additions & 110 deletions examples/polysynth_old/sketch.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/audioVoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ define(function() {
* Disconnect from soundOut
* @method disconnect
*/
p5.AudioVoice.prototype.disconect = function() {
p5.AudioVoice.prototype.disconnect = function() {
this.output.disconnect();
};

Expand Down
2 changes: 2 additions & 0 deletions src/monosynth.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ define(function (require) {
// oscillator --> env --> filter --> this.output (gain) --> p5.soundOut
this.oscillator.disconnect();
this.oscillator.connect(this.filter);
this.env.disconnect();
this.env.setInput(this.oscillator);
// this.env.connect(this.filter);
this.filter.connect(this.output);

this.oscillator.start();
Expand Down
Loading

0 comments on commit 6e3f153

Please sign in to comment.