From dedb5bc5e976e8a677c70b6af3e03b5b7b8e43d4 Mon Sep 17 00:00:00 2001 From: Alberto Venturini Date: Wed, 14 Mar 2018 22:01:02 +0200 Subject: [PATCH] Add midi functions --- synthbass.scd | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/synthbass.scd b/synthbass.scd index 6a02d74..0ed8e1c 100644 --- a/synthbass.scd +++ b/synthbass.scd @@ -39,6 +39,8 @@ p = Pdef(\pattern, p.play; ) +FreqScope.new; + // PBinds generate events. It's like a stream of events. We can look at each Event object // by polling the stream. // Events are objects containing key-value pairs, e.g.: @@ -54,3 +56,38 @@ p.asStream.next(Event.new); p.play; +// Midi input +( +var keys; +keys = Array.newClear(128); + +~noteOnFunc = {arg src, chan, num, vel; + var node; + node = keys.at(num); + if (node.notNil, { + node.release; + keys.put(num, nil); + }); + node = Synth.new(\synthbass, [\freq, num.midicps, \amp, vel/1500]); + keys.put(num, node); + [chan,num,vel/1500].postln; +}; +MIDIIn.addFuncTo(\noteOn, ~noteOnFunc); + +~noteOffFunc = {arg src, chan, num, vel; + var node; + node = keys.at(num); + if (node.notNil, { + node.release; + keys.put(num, nil); + }); +}; +MIDIIn.addFuncTo(\noteOff, ~noteOffFunc); + +) + +// cleanup +( +MIDIIn.removeFuncFrom(\noteOn, ~noteOnFunc); +MIDIIn.removeFuncFrom(\noteOff, ~noteOffFunc); +) \ No newline at end of file