Midi file input can be combined with other Csound inputs from the score or from live midi and also bear in mind that a midi file doesn't need to contain midi note events, it could instead contain, for example, a sequence of controller data used to automate parameters of effects during a live performance.
Rather than to directly play back a midi file using Csound instruments it might be useful to import midi note events as a standard Csound score. This way events could be edited within the Csound editor or several scores could be combined. The following example takes a midi file as input and outputs standard Csound .sco files of the events contained therein. For convenience each midi channel is output to a separate .sco file, therefore up to 16 .sco files will be created. Multiple .sco files can be later recombined by using #include... statements or simply by using copy and paste.
The only tricky aspect of this example is that note-ons followed by note-offs need to be sensed and calculated as p3 duration values. This is implemented by sensing the note-off by using the release opcode and at that moment triggering a note in another instrument with the required score data. It is this second instrument that is responsible for writing this data to a score file. Midi channels are rendered as p1 values, midi note numbers as p4 and velocity values as p5.
EXAMPLE 07D02_MidiToScore.csd
; enter name of input midi file -F InputMidiFile.mid ; Example by Iain McCurdy ;ksmps needs to be 10 to ensure accurate rendering of timings ksmps = 10 massign 0,1 instr 1 iChan midichn iCps cpsmidi ; read pitch in frequency from midi notes iVel veloc 0, 127 ; read in velocity from midi notes kDur timeinsts ; running total of duration of this note kRelease release ; sense when note is ending if kRelease=1 then ; if note is about to end ; p1 p2 p3 p4 p5 p6 event "i", 2, 0, kDur, iChan, iCps, iVel ; send full note data to instr 2 endif endin instr 2 iDur = p3 iChan = p4 iCps = p5 iVel = p6 iStartTime times ; read current time since the start of performance ; form file name for this channel (1-16) as a string variable SFileName sprintf "Channel%d.sco",iChan ; write a line to the score for this channel's .sco file fprints SFileName, "i%d\\t%f\\t%f\\t%f\\t%d\\n",\ iChan,iStartTime-iDur,iDur,iCps,iVel endin f 0 480 ; ensure this duration is as long or longer that duration of midi file e