Skip to content

Commit 6e3f153

Browse files
committed
migrated ex/polysynth_old synths to ex/polyphonic_synth; added a velocity max range to polysynth
1 parent 05178da commit 6e3f153

File tree

6 files changed

+133
-182
lines changed

6 files changed

+133
-182
lines changed

examples/polyphonic_synth/sketch.js

Lines changed: 126 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ var real_wave = [];
55
var imag_wave = []
66

77
function preload(){
8-
ptable_real = loadStrings('../polyphonic_synth/TwelveStringGuitar_real.txt');
9-
ptable_imag = loadStrings('../polyphonic_synth/TwelveStringGuitar_imag.txt' );
8+
ptable_real = loadStrings('Wurlitzer_2_imag.txt');
9+
ptable_imag = loadStrings('Wurlitzer_2_real.txt' );
1010
}
1111

1212

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

32+
//Uncomment for PeriodicWave
33+
// psynth.audiovoices.forEach(function(voice){
34+
// voice.setParams({real: real_wave , imag: imag_wave});
35+
// });
3236

3337
}
3438

@@ -40,66 +44,79 @@ function draw() {
4044
}
4145

4246
function mousePressed(){
43-
4447
var note = int(map(mouseX,0,width,200,440));
4548
var length = map(mouseY,0,300,0,5);
4649

47-
48-
49-
psynth.play(note,1,0,length);
50-
psynth.noteADSR(note, 0.021,0.025,length,0.025);
51-
52-
var index = psynth.notes[note].getValueAtTime();
53-
psynth.audiovoices[index].setParams({real: real_wave , imag: imag_wave});
54-
55-
// uncomment for SquareVoice detune parameters
56-
//var d = int(random(1,12));
57-
//psynth.setParams({detune: d });
58-
59-
// uncomment for PeriodicWave
60-
//psynth.setParams({real: real_wave , imag: imag_wave});
61-
62-
50+
psynth.setADSR(0.021,0.025,length,0.025);
51+
psynth.play(note,1,0,1);
6352
}
6453

6554

66-
//////////////////////////////////////////////////////////////////////////////
6755
function PeriodicWave(params){
68-
p5.MonoSynth.call(this);
69-
56+
p5.AudioVoice.call(this);
57+
this.osc = new p5.Oscillator('sine');
58+
7059
this.real = new Float32Array([0,0]) ;
7160
this.imag = new Float32Array([0,1]);
72-
7361
this.wt = this.ac.createPeriodicWave(this.real,this.imag);
7462

75-
// this.oscillator = this.context.createOscillator();
76-
this.oscillator.oscillator.setPeriodicWave(this.wt);
77-
this.filter.setType('lowpass');
63+
this.osc.disconnect();
64+
this.osc.start();
65+
66+
this.env = new p5.Env(0.021,0.025,0.025,0.025,0.95,0.33,0.25);
67+
this.env.disconnect();
68+
69+
this.filter = new p5.LowPass();
7870
this.filter.set(22050,5);
7971

72+
8073
this.env.connect(this.filter);
74+
this.osc.connect(this.filter);
75+
76+
this.connect();
77+
78+
this.filter.set(22050,5);
79+
8180

8281
this.setParams = function(params){
8382
this.real = new Float32Array(params.real);
8483
this.imag = new Float32Array(params.imag);
8584
this.wt = this.ac.createPeriodicWave(this.real, this.imag);
86-
this.oscillator.oscillator.setPeriodicWave(this. wt);
85+
this.osc.oscillator.setPeriodicWave(this. wt);
86+
}
87+
88+
this.setADSR = function(aTime,aLevel,dTime,dLevel) {
89+
this.env.set(aTime, aLevel, dTime, dLevel)
90+
}
91+
92+
this.play = function(){
93+
this.env.play(this.filter);
94+
8795
}
88-
this.setADSR = function(a,d,s,r) {
89-
this.attack = a;
90-
this.decay=d;
91-
this.sustain=s;
92-
this.release=r;
93-
this.volume = 1
94-
this.env.set(this.attack, this.volume, this.decay, this.volume, this.release, this.volume);
95-
// this.env.set(this.attack, this.decay, this.sustain, this.release);
96-
this.env.play(this.filter);
9796

97+
this.triggerAttack = function(note, velocity, secondsFromNow) {
98+
var secondsFromNow = secondsFromNow || 0;
99+
100+
//triggerAttack uses ._setNote to convert a midi string to a frequency if necessary
101+
var freq = typeof note === 'string' ? this._setNote(note)
102+
: typeof note === 'number' ? note : 440;
103+
var vel = velocity || 1;
104+
// this.env.setRange(this.env.aLevel / velocity,0);
105+
this._isOn = true;
106+
this.osc.freq(freq, 0, secondsFromNow);
107+
this.env.triggerAttack(this.filter, secondsFromNow);
108+
109+
}
110+
111+
this.triggerRelease = function(secondsFromNow) {
112+
var secondsFromNow = secondsFromNow || 0;
113+
this.env.triggerRelease(this.filter,secondsFromNow);
114+
this._isOn = false;
98115
}
99116

100117
}
101118

102-
PeriodicWave.prototype = Object.create(p5.MonoSynth.prototype);
119+
PeriodicWave.prototype = Object.create(p5.AudioVoice.prototype);
103120
PeriodicWave.prototype.constructor = PeriodicWave;
104121

105122

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

117134
//////////////////////////////////////////////////////////////////////////////////////////////
118-
// A second one
135+
// A Detuned synth
119136
function DetunedOsc(){
120137

121-
AudioVoice.call(this);
138+
p5.MonoSynth.call(this);
122139

123140
this.osctype = 'sine';
124-
this.detune = 5;
141+
this.detune = -5;
142+
143+
this.oscOne = this.oscillator;
144+
this.oscTwo = new p5.Oscillator();
145+
146+
this.filter.setType('lowpass');
147+
this.filter.set(22050,5);
125148

126-
this.oscOne = new p5.Oscillator(midiToFreq(this.note),this.osctype);
127-
this.oscTwo = new p5.Oscillator(midiToFreq(this.note)-this.detune,this.osctype);
128149
this.oscOne.disconnect();
129150
this.oscTwo.disconnect();
130-
this.oscOne.start();
131-
this.oscTwo.start();
151+
132152

133153
this.oscOne.connect(this.filter);
134154
this.oscTwo.connect(this.filter);
135155

136-
this.setNote = function(note){
137-
this.oscOne.freq(midiToFreq(note));
138-
this.oscTwo.freq(midiToFreq(note)-this.detune);
139-
}
140156

141-
this.setParams = function(params){
142-
this.detune = params.detune;
157+
this.env.setInput(this.oscOne,this.oscTwo);
158+
159+
this.oscOne.start();
160+
this.oscTwo.start();
161+
162+
163+
this.triggerAttack = function(note, velocity, secondsFromNow) {
164+
this.oscTwo.oscillator.detune.value
165+
var secondsFromNow = secondsFromNow || 0;
166+
var freq = typeof note === 'string' ? this._setNote(note)
167+
: typeof note === 'number' ? note : 440;
168+
var vel = velocity || 1;
169+
170+
this._isOn = true;
171+
172+
this.oscOne.freq(freq, 0, secondsFromNow);
173+
this.oscTwo.freq(freq + this.detune, 0, secondsFromNow);
174+
this.env.ramp(this.output, secondsFromNow, this.env.aLevel);
143175
}
176+
144177
}
145178

146-
DetunedOsc.prototype = Object.create(p5.AudioVoice.prototype);
179+
DetunedOsc.prototype = Object.create(p5.MonoSynth.prototype);
147180
DetunedOsc.prototype.constructor = DetunedOsc;
181+
182+
183+
184+
function AdditiveSynth(){
185+
p5.MonoSynth.call(this);
186+
187+
this.osctype = 'triangle';
188+
this.harmonics = [1,2,4,6,8];
189+
this.note = 60;
190+
191+
this.oscbank =[];
192+
this.oscillator.dispose();
193+
delete this.oscillator;
194+
this.env.disconnect();
195+
196+
for (var i = 0 ; i < this.harmonics.length; i++){
197+
this.oscbank.push(new p5.Oscillator());
198+
this.oscbank[i].setType(this.osctype);
199+
this.oscbank[i].disconnect();
200+
this.oscbank[i].connect(this.filter);
201+
this.env.connect(this.oscbank[i]);
202+
this.oscbank[i].start();
203+
}
204+
205+
this.triggerAttack = function(note, velocity, secondsFromNow) {
206+
var secondsFromNow = secondsFromNow || 0;
207+
var freq = typeof note === 'string' ? this._setNote(note)
208+
: typeof note === 'number' ? note : 440;
209+
var vel = velocity || 1;
210+
211+
this._isOn = true;
212+
213+
for (var i = 0 ; i < this.harmonics.length; i++){
214+
this.oscbank[i].freq(freq + midiToFreq(this.harmonics[i]*12),0,secondsFromNow);
215+
}
216+
217+
this.env.ramp(this.output, secondsFromNow, this.env.aLevel);
218+
}
219+
220+
}
221+
AdditiveSynth.prototype = Object.create(p5.MonoSynth.prototype);
222+
AdditiveSynth.prototype.constructor = AdditiveSynth;

examples/polysynth_old/index.html

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/polysynth_old/sketch.js

Lines changed: 0 additions & 110 deletions
This file was deleted.

src/audioVoice.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ define(function() {
6060
* Disconnect from soundOut
6161
* @method disconnect
6262
*/
63-
p5.AudioVoice.prototype.disconect = function() {
63+
p5.AudioVoice.prototype.disconnect = function() {
6464
this.output.disconnect();
6565
};
6666

src/monosynth.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ define(function (require) {
4949
// oscillator --> env --> filter --> this.output (gain) --> p5.soundOut
5050
this.oscillator.disconnect();
5151
this.oscillator.connect(this.filter);
52+
this.env.disconnect();
5253
this.env.setInput(this.oscillator);
54+
// this.env.connect(this.filter);
5355
this.filter.connect(this.output);
5456

5557
this.oscillator.start();

0 commit comments

Comments
 (0)