Hey there
I have the following code:
MIDIClient.init;
MIDIIn.connectAll;
(
SynthDef(\saw_1, {
arg freq=440,gate=1,out=0,pitchBend=0,amp=0.7;
var snd, env, feedb, lfo, noise, rev;
env = EnvGen.ar(Env.adsr(0, 0, 0.6, 0.01), gate: gate, doneAction:2);
feedb = LocalIn.ar(5);
lfo = Latch.ar(SinOsc.ar(100,mul:3),Impulse.ar(40));
noise = LFDNoise3.ar(3,1).tanh;
snd = LFSaw.ar(freq+(pitchBend/10)+lfo+noise,mul:1)
+LFSaw.ar(freq*0.985+(pitchBend/10)+lfo+noise,mul:0.3)
+LFSaw.ar(freq*1.015+(pitchBend/10)+lfo+noise,mul:0.3);
snd = snd+feedb;
snd = snd/3;
rev = Greyhole.ar(snd,2,0.3,10,0.6);
LocalOut.ar(snd);
snd = [snd,rev]*amp*(freq.cpsmidi/58)*env;
Out.ar(out,snd);
}).add
)
MIDIFunc.free
MIDIIn.bend
(
var notes, onsaw_1, off, amp;
notes = Array.newClear(128);
onsaw_1 = MIDIFunc.noteOn({ arg veloc, num, chan, src;
notes[num] = Synth(\saw_1, [\freq, num.midicps]);
});
off = MIDIFunc.noteOff({ arg veloc, num, chan, src;
notes[num].release;
});
amp = MIDIFunc.cc({ arg val;
"amp : ".post; val.postln;
notes.do({arg synth;
if( synth != nil , { synth.set(\amp, val * (1.0/128.0)) });
});
},31);
)
I am a bit confused about how SynthDefs are initialised by the last section of my code (var notes, onsaw_1, off, amp;…)
If I evaluate this part once I can see one Synth appearing in the Node Tree when I hit a key on the MIDI keyboard. If I evaluate it twice there appear two Synths in the Node Tree for playing one key on the keyboard.
Well, I can understand the logic behind this, but somehow I would have expected that the previous evaluation is overwritten when I evaluate the code once again.
What can I do to clear the previous evaluation so that synths aren’t added?
The only idea I could figure out so far was to reboot the interpreter…
But as a matter of fact I really like the sound when two of these SynthDefs are added. How can I create this sound with one evaluation of the code?
I tried something like:
onsaw_1 = MIDIFunc.noteOn({ arg veloc, num, chan, src;
notes[num] = Synth(\saw_1, [\freq, num.midicps]);
});
onsaw_2 = MIDIFunc.noteOn({ arg veloc, num, chan, src;
notes[num] = Synth(\saw_1, [\freq, num.midicps]);
});
But then the keys aren’t released.
I couldn’t quite figure out how to create a well-working pitchbend.
So if someone could lend me his/her code I would really appreciate that.
I would like to be able to control the overall amplitude through my MIDI keyboard. You can see this in the very last part of my code
(amp = MIDIFunc.cc({ arg val;…).
It’s actually possible to change the volume of a note I have played. But that way I can’t change the volume before a note is played.
What could I do about that?
Thanks a lot.