Tutorial 1

MIDI triggered notes

This series of tutorials for Csound will explore the use of MIDI to control aspects of synthesis and processing within Csound. I've tried to use canonical opcodes, so they should be cross platform. Please let me know if you find one which isn't.

The tutorials assume basic knowledge of Csound, though I try to explain everything in detail so I hope even a beginner will understand the programs. If you are such a beginner, you might want to check out the tutorials in the CsoundAV Video Tutorials first. They describe csound variable types and syntax starting from absolute scratch. You will also find editor suggestions and how to download the program for Windows.

OK, here we go with the first tutorial for midi. It is a very simple instrument that just makes a sine wave sound with the appropriate pitch according to the midi note. You can copy-paste all the code (including the comments) into your editor and compile.

For MacCsound, you can paste the <CsInstruments> section into the orc tab, and the <CsScore> into the sco tab. You then choose realtime audio output on the upper right corner, and you should be set. Let me know if you have any problems.

You will need a MIDI controller keyboard which transmits on channel one, connected to your system.

<CsoundSynthesizer>

<CsOptions>
-+P -+K

;The -+P flag enables realtime audio output
;The -+K flag enables MIDI input.
;After you know what values your system uses
;You can place them after the flag to avoid having to enter
;them every time you run the program.
;These flags are for use with CsoundAV only.
;To use this on Linux, use something like:
;-odac:plughw:2 -M/dev/midi00 -dm6
;You need to adjust the devaudio number and the midi device name
;If you're using MacCsound, just choosing Real-Time output should do it.
</CsOptions>
<CsInstruments>
sr=44100
kr=441
ksmps=100
nchnls=2
;These are usual Csound headers which indicate Sample Rate, Control Rate (kr),
;Control Samples per second (ksmps) and Number of Channels.
massign 1,1
;This MIDI instruction assigns a MIDI channel to trigger a certain instrument.
;In this case MIDI channel 1 will trigger instrument 1.
;This is actually the default value, so it isn't really necessary,
;but you should know this opcode for MIDI use.
instr 1
;We define our first instrument which will be triggered by any note
;played on the controller keyboard. In other words,
;when a note is played on the controller keyboard
;a note with this instrument is generated
icps cpsmidi
;The opcode cpsmidi assigns the variable icps the Herz frequency
;corresponding to the MIDI note that triggered the instrument
;i.e. If I play an A2 the variable icps will hold a value of 440.
print icps
;This instruction will display the value of the variables icps.
;The print opcode works at init time, which means the value will be displayed
;every time a new note is played, and only once per note.
;Notice that the instrument we are creating is polyphonic, and icps is a
;local variables, each instance of the instrument has its own separate icps value.
aout oscil 1, icps, 1
;This opcode is an oscillator. The audio-rate variable aout will hold the result of this oscillator
;The parameters for the oscillator are first amplitude, here a 1
;(there is no attack and no decay, the note is held while the MIDI note remains pressed),
;Second frequency, which will be icps, or the frequency for the MIDI note
;and lastly the f-table according to which the oscillator 'oscillates'.
;Here we use f-table 1, which is defined further down, in the score section
;and corresponds to a simple sine wave.
outs (aout*10000) , (aout*10000)
;The outs opcode sends an a-rate signal to the audio outputs.
;outs is used when the number of channels is 2.
;Notice we have a mono instrument sounding equally left and right.
;We must multiply the oscillator's value by 10000 to make it audible.
;The maximum amplitude is 32768. (215)
endin
;The end of the instrument.
</CsInstruments>

<CsScore>
f 1 0 4096 10 1
;A sine wave made up of 4096 points is stored as f-table 1
;Notice that the parameters correspond exactly to the parameters used
;in the video tutorials with the opcode ftgen, except for the number of points
;(which instead of 1024 are 4096). The f instruction in the score
;is the older way to define f-tables, but may be a little confusing for
;beginners because even though it is further down, it can be processed first
;and called from an instrument above.
f 0 120
;A dummy f-table 0 must be created so the csound program runs for a certain
;time. Since we are not creating any notes in the score a table whose creation is
;deferred to second 120 will make Csound run until that time.
</CsScore>
</CsoundSynthesizer>

This very simple synthesizer introduces the use of MIDI in Csound. Please let me know how you do, especially if you're running on Linux or Mac.

Thanks to Matt Ingalls for showing me a better way to comment a tutorial, and to Art Hunkins for the linsegr suggestion.



Back to the MIDI tutorials page