2 summary:: receive MIDI messages
3 related:: Classes/MIDIOut, Classes/MIDIFunc, Classes/MIDIdef, Guides/MIDI, Guides/UsingMIDI
4 categories:: External Control>MIDI
7 This document explains technical details of the MIDI hardware interface class, MIDIIn.
9 MIDIIn is a simple and direct interface. When MIDI events come into Super Collider, MIDIIn evaluates simple handler functions.
12 For general programming, strong::users should not use the MIDIIn class directly::. See the link::Classes/MIDIFunc:: and link::Classes/MIDIdef:: classes for higher level event matching and more flexible handling of event handlers.
15 Certain MIDI messages are supported only through MIDIIn. These are: sysex, sysrt, smpte.
17 See the link::Guides/UsingMIDI:: helpfile for practical considerations and techniques for using MIDI in SC.
19 subsection::The MIDIIn class
21 MIDIIn links MIDI input received from the operating system to a set of user defined functions. Only one set of MIDI input handling functions can be active at a time, they are stored in the following class variables:
23 noteOff, noteOn, polytouch, control, program, touch, bend, sysex, sysrt, smpte
25 The first argument these functions receive is an unique identifier that specifies the source of the data.
30 private::prDispatchEvent, connectByUID, disconnectByUID
33 searches for a connected link::Classes/MIDIEndPoint:: by name.
41 Add a link::Classes/Function:: or similar object to be evaluated whenever a particular MIDI message is received. This method is preferable to the setters below, since it will not overwrite any existing functions.
44 A link::Classes/Symbol:: indicating the message type to wait for, one of code::'noteOn':: code::'noteOff'::, code::'polytouch'::, code::'control'::, code::'program'::, code::'touch'::, code::'bend'::, code::'sysex'::, code::'sysrt'::, code::'smpte'::, or code::'invalid':: (for invalid sysex messages).
47 A link::Classes/Function:: or similar object to be evaluated when a message of the specified type is received. See the setters below for the arguments which will be passed at evaluation time.
49 method::removeFuncFrom
50 Remove a link::Classes/Function:: or similar object from the list of those to be evaluated whenever a particular MIDI message is received. This method is preferable to the setters below, since it will leave any existing functions in place.
53 A link::Classes/Symbol:: indicating the message type, one of code::'noteOn':: code::'noteOff'::, code::'polytouch'::, code::'control'::, code::'program'::, code::'touch'::, code::'bend'::, code::'sysex'::, code::'sysrt'::, code::'smpte'::, or code::'invalid':: (for invalid sysex messages).
56 The link::Classes/Function:: or similar object to be removed.
59 Replace a link::Classes/Function:: or similar object in the list to be evaluated whenever a particular MIDI message is received with another one. This method is preferable to the setters below, since it will not overwrite any existing functions.
62 A link::Classes/Symbol:: indicating the message type to wait for, one of code::'noteOn':: code::'noteOff'::, code::'polytouch'::, code::'control'::, code::'program'::, code::'touch'::, code::'bend'::, code::'sysex'::, code::'sysrt'::, code::'smpte'::, or code::'invalid':: (for invalid sysex messages).
65 The link::Classes/Function:: or similar object to be replaced.
68 A link::Classes/Function:: or similar object to be evaluated when a message of the specified type is received. See the setters below for the arguments which will be passed at evaluation time.
70 subsection:: Getter/Setters for Specific Message Types
72 The methods below allow you to register a function to respond to a particular message type.
74 note::It is preferable to use the link::#*addFuncTo::, link::#*removeFuncFrom:: and link::#*replaceFuncTo:: methods above instead of these methods, as they will not overwrite any functions added by system objects.::
78 a link::Classes/Function:: evaluated whenever a MIDI noteOn message is received. It is passed the following arguments:
80 ## uid || unique identifier of the MIDI port
81 ## MIDIchannel || ranges from 0 to 15
82 ## keyNumber || 0 - 127
83 ## velocity || 0 - 127
88 a link::Classes/Function:: evaluated whenever a MIDI noteOff message is received. It is passed the following arguments:
90 ## uid || unique identifier of the MIDI port
91 ## MIDIchannel || ranges from 0 to 15
92 ## keyNumber || 0 - 127
93 ## velocity || 0 - 127 (typically 64 unless noteOff velocity is supported)
98 a link::Classes/Function:: evaluated whenever a MIDI polytouch message is received. It is passed the following arguments:
100 ## uid || unique identifier of the MIDI port
101 ## MIDIchannel || ranges from 0 to 15
102 ## keyNumber || 0 - 127
103 ## pressure || 0 - 127
108 a link::Classes/Function:: evaluated whenever a MIDI control change message (CC) is received. It is passed the following arguments:
110 ## uid || unique identifier of the MIDI port
111 ## MIDIchannel || ranges from 0 to 15
112 ## controllerNumber || 0 - 127
118 a link::Classes/Function:: evaluated whenever a MIDI program change message is received. It is passed the following arguments:
120 ## uid || unique identifier of the MIDI port
121 ## MIDIchannel || ranges from 0 to 15
122 ## programNumber || 0 - 127
127 a link::Classes/Function:: evaluated whenever a MIDI after-touch message is received. It is passed the following arguments:
129 ## uid || unique identifier of the MIDI port
130 ## MIDIchannel || ranges from 0 to 15
131 ## pressure || 0 - 127
136 a link::Classes/Function:: evaluated whenever a MIDI pitch wheel change message is received. It is passed the following arguments:
138 ## uid || unique identifier of the MIDI port
139 ## MIDIchannel || ranges from 0 to 15
140 ## bend || 0 - 16383 (14bits, the midpoint is 8192)
145 The current implementation assembles a complete system exclusive packet before evaluating the function.
148 a link::Classes/Function:: evaluated whenever a MIDI System Exclusive message is received. It is passed the following arguments:
150 ## uid || unique identifier of the MIDI port
151 ## data || an link::Classes/Int8Array:: (includes f0 and f7). See manufacturer references for details.
156 ## strong::index:: || strong::data:: || strong::message::
157 ## 2 || 14bits || song pointer
158 ## 3 || 7bits || song select
165 a link::Classes/Function:: evaluated whenever a MIDI System Real-Time message is received. It is passed the following arguments:
167 ## uid || unique identifier of the MIDI port
168 ## index || ranges from 0 to 15
173 Over MIDI, SMPTE is transmitted at 1/4 frame intervals four times faster than the frame rate.
175 ## strong::index:: || strong::data::
176 ## 0 || frames low nibble
177 ## 1 || frames hi nibble
178 ## 2 || seconds low nibble
179 ## 3 || seconds hi nibble
180 ## 4 || minutes low nibble
181 ## 5 || minutes hi nibble
182 ## 6 || hours low nibble
183 ## 7 || hours hi emphasis::bit:: OR'ed with frameRate
187 ## 4 -> 30 fps drop frame
191 Nibbles are sent in ascending order.
193 a link::Classes/Function:: evaluated whenever a MIDI smpte message is received. It is passed the following arguments:
195 ## uid || unique identifier of the MIDI port
196 ## index || ranges from 0 to 7
197 ## data || 0 - 15 (4bits)
202 subsection::Quick start for 1 port
205 MIDIIn.connect; // init for one port midi interface
206 // register functions:
207 ~noteOff = { arg src, chan, num, vel; [chan,num,vel / 127].postln; };
208 ~noteOn = { arg src, chan, num, vel; [chan,num,vel / 127].postln; };
209 ~polytouch = { arg src, chan, num, vel; [chan,num,vel / 127].postln; };
210 ~control = { arg src, chan, num, val; [chan,num,val].postln; };
211 ~program = { arg src, chan, prog; [chan,prog].postln; };
212 ~touch = { arg src, chan, pressure; [chan,pressure].postln; };
213 ~bend = { arg src, chan, bend; [chan,bend - 8192].postln; };
214 ~sysex = { arg src, sysex; sysex.postln; };
215 ~sysrt = { arg src, chan, val; [chan,val].postln; };
216 ~smpte = { arg src, chan, val; [chan,val].postln; };
217 MIDIIn.addFuncTo(\noteOn, ~noteOn);
218 MIDIIn.addFuncTo(\noteOff, ~noteOff);
219 MIDIIn.addFuncTo(\polytouch, ~polytouch);
220 MIDIIn.addFuncTo(\control, ~control);
221 MIDIIn.addFuncTo(\program, ~program);
222 MIDIIn.addFuncTo(\touch, ~touch);
223 MIDIIn.addFuncTo(\bend, ~bend);
224 MIDIIn.addFuncTo(\sysex, ~sysex);
225 MIDIIn.addFuncTo(\sysrt, ~sysrt);
226 MIDIIn.addFuncTo(\smpte, ~smpte);
231 MIDIIn.removeFuncFrom(\noteOn, ~noteOn);
232 MIDIIn.removeFuncFrom(\noteOff, ~noteOff);
233 MIDIIn.removeFuncFrom(\polytouch, ~polytouch);
234 MIDIIn.removeFuncFrom(\control, ~control);
235 MIDIIn.removeFuncFrom(\program, ~program);
236 MIDIIn.removeFuncFrom(\touch, ~touch);
237 MIDIIn.removeFuncFrom(\bend, ~bend);
238 MIDIIn.removeFuncFrom(\sysex, ~sysex);
239 MIDIIn.removeFuncFrom(\sysrt, ~sysrt);
240 MIDIIn.removeFuncFrom(\smpte, ~smpte);
244 subsection::Quick start for 2 or more ports
249 MIDIClient.init(inPorts,outPorts); // explicitly intialize the client
251 MIDIIn.connect(i, MIDIClient.sources.at(i));
256 subsection::example with sound
263 SynthDef("sik-goo", { arg freq=440,formfreq=100,gate=0.0,bwfreq=800;
266 SinOsc.kr(0.02, 0, 10, freq),
270 x = EnvGen.kr(Env.adsr, gate,Latch.kr(gate,gate)) * x;
275 x = Synth("sik-goo");
279 ~noteOn = {arg src, chan, num, vel;
280 x.set(\freq, num.midicps / 4.0);
281 x.set(\gate, vel / 200 );
282 x.set(\formfreq, vel / 127 * 1000);
284 MIDIIn.addFuncTo(\noteOn, ~noteOn);
286 ~noteOff = { arg src,chan,num,vel;
289 MIDIIn.addFuncTo(\noteOff, ~noteOff);
291 ~bend = { arg src,chan,val;
292 //(val * 0.048828125).postln;
293 x.set(\bwfreq, val * 0.048828125 );
295 MIDIIn.addFuncTo(\bend, ~bend);
299 MIDIIn.removeFuncFrom(\noteOn, ~noteOn);
300 MIDIIn.removeFuncFrom(\noteOff, ~noteOff);
301 MIDIIn.removeFuncFrom(\bend, ~bend);
304 subsection::writing to the bus rather than directly to the synth
306 //i used this and got acceptable latency for triggering synths live.
307 //The latency might actually be less than sc2, but i haven't used it enough
308 //to tell for sure yet.
309 //Powerbook G4, 512mb ram.
310 //- matrix6k@somahq.com
316 SynthDef("moto-rev", { arg ffreq=100;
318 x = RLPF.ar(LFPulse.ar(SinOsc.kr(0.2, 0, 10, 21), [0,0.1], 0.1),
327 x = Synth("moto-rev");
329 // map the synth's first input (ffreq) to read
330 // from the bus' output index
337 ~noteOn = {arg src, chan, num, vel;
338 b.value = num.midicps.postln;
340 MIDIIn.addFuncTo(\noteOn, ~noteOn);
342 ~control = {arg src, chan, num, val;
343 [chan,num,val].postln;
345 MIDIIn.addFuncTo(\control, ~control);
347 ~bend = {arg src, chan, val;
350 MIDIIn.addFuncTo(\bend, ~bend);
356 MIDIIn.removeFuncFrom(\noteOn, ~noteOn);
357 MIDIIn.removeFuncFrom(\control, ~control);
358 MIDIIn.removeFuncFrom(\bend, ~bend);
361 subsection::Keyboard Split for two voices
363 //pbend to cutoff, mod to rez, 7 to amp
364 //- matrix6k@somahq.com
368 SynthDef("funk",{ arg freq = 700, amp = 0.2, gate = 1, cutoff = 20000, rez = 1, lfospeed=0;
369 var e,x,env,range,filterfreq;
370 e = Env.new([0, 0.1, 0.1, 0], [0, 0.1, 0.1], 'linear', 2);
371 env=Env.adsr(0.3,1,1,1);
373 filterfreq = SinOsc.kr(lfospeed,0, range, cutoff).abs;
375 Mix.arFill(2, {Saw.ar(freq *2 + 0.2.rand2, amp)}),
376 Mix.arFill(2, {Saw.ar(freq *4+ 0.2.rand2, amp)})
378 EnvGen.kr(env,gate)*filterfreq,
380 Out.ar([0,1],x * EnvGen.kr(e, gate, doneAction: 2))
383 SynthDef("strings",{ arg freq = 700, amp = 0.2, gate = 1;
385 enve = Env.new([0, 0.1, 0.1, 0], [2, 0.1, 1], 'linear', 2);
387 Mix.arFill(2, {Saw.ar(freq +2.rand2,0.6)}),
388 Mix.arFill(2, {Saw.ar(freq *0.5 + 2.rand2,0.6)})
391 Out.ar([0,1],x * EnvGen.kr(enve, gate, doneAction: 2))
396 var keys, cutspec, cutbus, rezspec, rezbus, lfospec, lfobus;
397 keys = Array.newClear(128);
400 MIDIIn.connect(0, MIDIClient.sources.at(0));
404 cutspec = ControlSpec(100,10000,\linear,0.001);
405 cutbus = Bus.new(\control,1,1,s);
406 cutbus.value = 10000;
408 rezspec = ControlSpec(1,0,\linear,0.001);
409 rezbus = Bus.new(\control,2,1,s);
412 lfospec = ControlSpec(0,50,\linear,0.001);
413 lfobus = Bus.new(\control,3,1,s);
415 ~control = {arg src, chan, num, val;
417 rezbus.value = rezspec.map(val/127.0);
420 lfobus.value = lfospec.map(val/127.0).postln;
423 MIDIIn.addFuncTo(\control, ~control);
425 ~bend = {arg src, chan, val;
426 cutbus.value = cutspec.map(val/16383.0);
428 MIDIIn.addFuncTo(\bend, ~bend);
430 ~noteOn = {arg src, chan, num, vel;
433 node = Synth.tail(g, "funk", [\freq, num.midicps, \amp, vel/255]);
434 node.map("cutoff",1,"rez",2,"lfospeed",3);
435 // node = Synth.basicNew("funk",s);
437 // node.addToTailMsg(g,[\freq, num.midicps, \amp, vel/255]),
438 // node.mapMsg("cutoff",1,"rez",2,"lfospeed",3)
442 node = Synth.tail(g, "strings", [\freq, num.midicps, \amp, vel/255]);
446 MIDIIn.addFuncTo(\noteOn, ~noteOn);
448 ~noteOff = {arg src, chan, num, vel;
453 s.sendMsg("/n_set", node.nodeID, "gate", 0);
455 // then free it ... or get the NodeWatcher to do it
458 MIDIIn.addFuncTo(\noteOff, ~noteOff);
462 MIDIIn.removeFuncFrom(\noteOn, ~noteOn);
463 MIDIIn.removeFuncFrom(\control, ~control);
464 MIDIIn.removeFuncFrom(\bend, ~bend);