Playing Csound instruments from a MIDI keyboard (draft version 2)

OK, this mini-tutorial still isn’t “complete,” but I’ll keep improving it little by little and maybe someday I’ll  look at it and be amazed that it’s “done.”

“I don’t want to do any programming AT ALL!”

OK, if you insist…here I’m going to show a couple ways I connected my MIDI keyboard such that I could play a Csound instrument.

Again with the apologies to Mac and Linux users: this is all Windows (2000/XP) stuff. Sorry about that…

To work this example, I recommend you use the CsoundVST tool. csound5gui will work, but the latency [i.e., the time lag or delay between when you press a key and when you hear the sound] when you set it up for MIDI input is huge (at least on my system).

Remember this will require that you install the “python” package as referred to elsewhere. It’s not that bad, you just run the .msi installer as with other conventional Windows software. You don’t really need to know anything about it at first. But at this writing, you should have the latest version of the Csound release (5.06) and 2.5.x of Python. If not you may be setting yourself up for more grief than you really want!

The “Classic Csound command line” box in CsoundVST reads:

csound --midi-device=9 --midi-key=4 --midi-velocity=5 temp.orc temp.sco

Starting with the tutorial2.csd file in Program Files/Csound/tutorials… (and with help from Michael Gogins excellent tutorial)

1. First make sure you can get some sounds out of tutorial2.csd. Bring up csound5gui, use the browser (…) button on the left of the Orchestra/CSD box to select that file. (The Edit button on the right should open a Notepad window with the .csd file in it in case you want to make tweaks.)

For output file use dac — that should work no matter what even if you have a better audio card on your system. You can experiment with dac1, dac2, etc. if you have other sound output hardward available on your system.

2. Press Play/Pause (the right pointing triangle under the time display). You should see time advance and hear a series of simple tones playing the 6 instruments in the .orc (orchestra) part of the .csd file, about 30 seconds worth.

3. If you have a MIDI interface on your system with a keyboard hooked up, make the following changes to the .csd file.

In the <CsOptions> section it reads

--midi-key=4 --midi-velocity=5

To this line, add the following string: “–midi-device=9999” (without the quote marks, of course.)

and run it again. It should figure that the MIDI device number is out of range a print a list of the legal ones in the messages window. On my system I get

The available MIDI in devices are: 
0: MIDI Yoke NT: 1 (MMSystem) 
1: MIDI Yoke NT: 2 (MMSystem) 
2: MIDI Yoke NT: 3 (MMSystem) 
3: MIDI Yoke NT: 4 (MMSystem) 
4: MIDI Yoke NT: 5 (MMSystem) 
5: MIDI Yoke NT: 6 (MMSystem) 
6: MIDI Yoke NT: 7 (MMSystem) 
7: MIDI Yoke NT: 8 (MMSystem) 
8: micro lite: Port 1 (MMSystem) 
9: micro lite: Port 2 (MMSystem) 
10: micro lite: Port 3 (MMSystem) 
11: micro lite: Port 4 (MMSystem) 
12: micro lite: Port 5 (MMSystem)   

*** PortMIDI: error: device number is out of range   

*** error opening MIDI in device: -1 (Unknown MIDI error)

OK: this tells me that my Oxygen 8 MIDI keyboard is hooked up to the micro lite interface port 2, so I change –midi-device=9999 to -midi-device=9

4. You should also remove the i statements in the <CsScore> section or you’ll hear those play at the same time. Change that section to this:

f 0 3600

5. Under


I also changed the sample rate frrom 88200

sr = 44100

Since there’s no benefit on my system to using anything less.

6. Now play notes on your MIDI keyboard. When I do this I see in the messages window for instance

rtevent: T 2.136 TT 2.136 M: 0.0 0.0             

new alloc for instr 6:             

rtevent: T 2.879 TT 2.879 M: 729.3 846.7 
rtevent: T 4.551 TT 4.551 M: 129.6 129.5 
rtevent: T 5.759 TT 5.759 M: 729.3 846.7 
rtevent: T 6.409 TT 6.409 M: 168.4 152.9 
rtevent: T 6.594 TT 6.594 M: 427.9 496.9

Where each key press and release results in a message.

Well, it’s a start. The only problem, and it’s a pretty nasty one, is that more than 2 seconds elapses between a key press on the Oxygen keyboard and a note sounding. This is not so great.
One trick to reduce CPU usage is to increase the control rate (kr) which is currently = 1. Setting that to even 100 doesn’t help the latency.

7. Well, let’s try a couple other routes. We can use CsoundVST as a standalone program.

You can launch CsoundVST from the Start>Programs>Csound>CsoundVST menu. Once the GUI displays, use the Import… button to get out tutorial2-MIDI.csd file. I know you don’t want to know about code (!) but You can inspect the Orchestra and Score code using the tabs in the GUI. (You can resize the GUI in the usual way to see the code better)

8. In the “Classic Csound Command Line” box you should see:

csound --midi-key=4 --midi-velocity=5 --midi-device=9 temp.orc temp.sco

Now we are getting used to the meanings of these parameters and arguments. I don’t know if it’s a default in Csound, but you may need to add “-o dac” after the csound token in the above. (I think if you don’t put anything for an output destination, Csound VST will attempt to play realtime.)
Ideally now, when you click the “Perform” tab at the top of the GUI, the program should wait for MIDI notes and play them. In my case this works, and the latency is MUCH improved.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: