Ok, I see, I didn’t read that from your first post, so I suggested the two variants. Anyway I’m posting these different approaches below, indeed examples of this kind are not contained in the VarGui help. Nothing complicated but different things have to be put together.
It can generate a Stream from a given Pattern (or take over a passed Stream) but there’s no need to do so.
// 1) Generating new synths with each MIDI trigger
// the newly generated synths can take over values from buses
(
~bus = Bus.control(s, 2);
// helper synth, playing to buses
SynthDef(\setBus, { |width = 0.5, pan = 0|
Out.kr(~bus, [width, pan])
}).add;
// sound synth, needs envelope
SynthDef(\synth_with_env, {|out = 0, freq = 440, width = 0.5, amp = 0.1, pan = 0,
att = 0.01, rel = 1, curveAtt = -5, curveRel = 5|
Out.ar(out, Pan2.ar(Pulse.ar(freq, width.lag(0.1), amp), pan)
* EnvGen.ar(Env.perc(), doneAction: 2)
)
}).add;
)
// generate gui and start helper synth playing to buses by pushing green button
(
v = VarGui(
synthCtr: [
\width, [0.01, 0.99, \lin, 0, 0.5],
\pan, [-1, 1, \lin, 0, 0]
],
synth: \setBus
).gui
)
// connect MIDI and define MIDIFunc
(
MIDIIn.connectAll;
m = MIDIFunc.noteOn({ |...args|
var midi = args[0];
var amp = args[1] / 127;
// with every MIDI trigger one Event is played
(
instrument: \synth_with_env,
midinote: midi,
amp: amp,
// mapping mechanism used in Event/Pbind
width: ~bus.subBus(0).asMap,
pan: ~bus.subBus(1).asMap,
latency: 0
).play;
}); // match any noteOn
)
// spoof a note on, or play with your MIDI device
// check changing params with gui (also applies during Event as mapped)
MIDIIn.doNoteOnAction(1, 1, 64, 63);
MIDIIn.doNoteOnAction(1, 1, 20, 70);
MIDIIn.doNoteOnAction(1, 1, 40, 74);
// free resources: stop synth in gui and free MIDIFunc (also happens with Cmd-.)
m.free
// 2) Monophonic approach, only one synth
// sound synth
(
SynthDef(\synth_without_env, { |out = 0, freq = 440, width = 0.5, amp = 0.1, pan = 0,
att = 0.01, rel = 1, curveAtt = -5, curveRel = 5|
Out.ar(out, Pan2.ar(Pulse.ar(freq, width.lag(0.1), amp), pan))
}).add;
)
// start synth silently
x = Synth(\synth_without_env, args: [amp: 0]).register;
// start gui taking over above already running Synth
(
v = VarGui(
synthCtr: [
\width, [0.01, 0.99, \lin, 0, 0.5],
\pan, [-1, 1, \lin, 0, 0]
],
synth: x
).gui
)
// connect MIDI and define MIDIFunc that sets the running Synth
(
MIDIIn.connectAll;
n = MIDIFunc.noteOn({ |...args|
var midi = args[0];
var amp = args[1] / 127;
x.set(\freq, midi.midicps, \amp, amp)
}); // match any noteOn
)
// spoof a note on, or play with your MIDI device
// check changing params with gui
MIDIIn.doNoteOnAction(1, 1, 64, 63);
MIDIIn.doNoteOnAction(1, 1, 20, 70);
MIDIIn.doNoteOnAction(1, 1, 40, 74);
// free resources
x.free;
n.free;