Connect MIDIdef and VSTPlugin

Hi!

I’m struggling to figure out how to go about connecting MIDIdef.noteOn/Off objects to VSTPlugin UGens. I’ve managed to figure out how to play VSTPlugins with Pbinds but I would like to use incoming MIDI data instead.

The following code (ever so slightly adjusted from a tutorial of Eli’s) works fine for me when playing SynthDefs made up of regular UGens;

~notes = Array.newClear(128);

(
SynthDef.new(\tone, { |freq=440, amp=0.3, out=0|
    var env = EnvGen.kr(Env.perc, doneAction:2);
    var sig = LFTri.ar(freq);
    sig = sig * env * amp;
    Out.ar(out, sig!2);
}).add;
)

(
MIDIdef.noteOn(
  \noteOnTest,
  { |vel, nn, chan, src|
	[vel, nn, chan, src].postln;
	~notes[nn] = Synth.new(
		\tone,
		[
			\freq, nn.midicps,
			\amp, vel.linexp(1,127,0.01,0.3),
			\gate, 1,
		]
	);
  },
  chan: 0,
  srcID: 1092319143
);

MIDIdef.noteOff(
	\noteOffTest,
	{ |vel, nn, chan, src|
	[vel, nn, chan, src].postln;
		~notes[nn].set(\gate, 0);
		~notes[nn] = nil;
  },
  chan: 0,
  srcID: 1092319143
	);
)

And the following VSTPlugin code works totally fine when using a Pbind;

SynthDef(\vst, {
	arg in=0, out=0, amp=1;
	var sig;
	sig = VSTPlugin.ar(nil, 2) * amp;
	Out.ar(out, sig);
}).add;

~surge = VSTPluginController(Synth(\vst, [\in: 0, \out: 0]));
~surge.open("Surge XT.vst3", editor: true, verbose: true);

Pbind(
			\type, \vst_midi,
			\vst, ~element,
			\midicmd, \noteOn,
			\dur, Pwhite(0.05, 0.5, inf),
			\midinote, Pseq([35], inf).trace,
			\harmonic, Pexprand(1, 80, inf).round.trace,
			\amp, Pkey(\harmonic).reciprocal * 0.3,
).play;

So I know that everything is working on either side, but how can I go about playing VSTPlugins with MIDIdefs?

Thanks!

I just worked this out the other day. Here is some sample code for how to connect MIDIdefs to VSTPlugin

//Start MIDI
MIDIClient.init;
MIDIIn.connectAll;


(
SynthDef(\aalto, { arg bus;
	ReplaceOut.ar(bus, VSTPlugin.ar(nil, 2, id: \aalto));
}).add;
)

~aalto = VSTPluginController(Synth(\aalto, [\bus, 0]));
~aalto.open("Aalto", editor: true, verbose: true);
~aalto.editor;
//~aalto.get(0, {|f|f.postln;})

//MIDI Controls
MIDIdef.noteOn(\noteOn, {|vel,pitch,chan|
	~aalto.midi.noteOn(chan+2, pitch, vel);
});

MIDIdef.noteOff(\noteOff, {|vel,pitch,chan|
	~aalto.midi.noteOff(chan+2, pitch, vel);
});

MIDIdef.polytouch(\touch, {|val, num, chan, src|
	~aalto.midi.polyTouch(chan+2, num, val);
});

MIDIdef.cc(\y_axis, {|val,num,chan,src|
	~aalto.midi.control(chan+2, num, val);
}, 74);

MIDIdef.bend(\x_axis, {|val,chan,src|
	~aalto.midi.control(chan+2, val);
});

Basically, VSTPluginController has a MIDI method which is the VSTPluginMIDIProxy class and you can send it messages received from your MIDI devices directly.

2 Likes

That’s perfect! Thanks heaps!

1 Like