midisender examples

Main ChucK page

Basic MIDI sending. While this is pretty primitive, it's actually the example from the ChucK manual. This is where you can really start to hate magic numbers, particularly if some sadist has written everything out in decimal. ChucK does support hex, too.

MidiOut port;
MidiMsg message;

port.open(2);

144 => msg.data1;
52 => msg.data2;
100 => msg.data3;
send( msg ); 

Here's a simple example of what we can with the midisender object. Here we show off being able to find synths by name, default channel specification, and remembering to turn the notes off afterwards.

MIDIsender midi;
midi.open("JD990");     // Huzzah!
midi.set_channel(1);    // the default.

midi.noteon(60, 40);
1::second => now;
midi.noteoff(60);

A more complex example, who can remember all the outstanding notes anyway. Once we've finished sending notes, we can simply tell the object to send noteoffs for all outstanding notes. With the -1 option, it would even stop things on other channels (for a multitimbral synth, for example)

MIDIsender snd;
snd.open("JD990"); 

snd.noteon(50, 64);
50::ms => now;
snd.noteon(53, 70);
50::ms => now;
snd.noteon(57, 85);
1::second => now;

snd.stop_hanging_notes(-1);

And here's something that would have been an utter utter pain in the backside to do the old way. Fine pitchbends (like several other controllers) are actually transmitted as a number varying from -8192 to 8191, 0 in the middle, with the resulting 14-bit number split into two 7-bit chunks and fed into MIDI bytes.

I suspect most people would prefer to do it this way.

MIDIsender m;
m.open("JD990");

m.noteon(55, 60);
1::second => now;
m.noteoff(55);

1::second => now;

m.noteon(55, 60);
1::second => now;

for (0 => int ii; ii <= 20; ii++) {
	m.pitchbend_fine(0.05 * ii);
	0.25::second => now;
}
m.stop_hanging_notes();
1::second => now; // wait for the notes to fade.
m.pitchbend(0);