midiin
Prev | Orchestra Opcodes and Operators | Next

## midiin

midiin — Returns a generic MIDI message received by the MIDI IN port.

## Description

Returns a generic MIDI message received by the MIDI IN port

## Syntax

    kstatus, kchan, kdata1, kdata2 midiin

## Performance

kstatus -- the type of MIDI message. Can be:

- 128 (note off)

- 144 (note on)

- 160 (polyphonic aftertouch)

- 176 (control change)

- 192 (program change)

- 208 (channel aftertouch)

- 224 (pitch bend

- 0 if no MIDI message are pending in the MIDI IN buffer

kchan -- MIDI channel (1-16)

kdata1, kdata2 -- message-dependent data values

midiin has no input arguments, because it reads at the MIDI in port implicitly. It works at k-rate. Normally (i.e., when no messages are pending) kstatus is zero, only when MIDI data are present in the MIDI IN buffer, is kstatus set to the type of the relevant messages.

| Note
Be careful when using midiin in low numbered instruments, since a MIDI note will launch additional instances of the instrument, resulting in duplicate events and weird behaviour. Use massign to direct MIDI note on messages to a different instrument or to disable triggering of instruments from MIDI.

## Examples

Here is an example of the midiin opcode. It uses the file midiin.csd.

Example 264. Example of the midiin opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    ; Audio out   Audio in    No messages
    -odac           -iadc     -d         -M0  -+rtmidi=virtual ;;;RT audio I/O with MIDI in
    ; For Non-realtime ouput leave only the line below:
    ; -o midiin.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    sr        =  44100
    ksmps     =  10
    nchnls    =  1

    ; Example by schwaahed 2006

          massign         0, 130  ; make sure that all channels
          pgmassign       0, 130  ; and programs are assigned to test instr

      instr   130

    knotelength    init    0
    knoteontime    init    0

    kstatus, kchan, kdata1, kdata2                  midiin

    if (kstatus == 128) then
    knoteofftime    times
    knotelength    =    knoteofftime - knoteontime
    printks "kstatus= %d, kchan = %d, \\tnote#  = %d, velocity = %d \\tNote OFF\\t%f %f\\n", 0, kstatus, kchan, kdata1,kdata2, knoteofftime, knotelength

    elseif (kstatus == 144) then
    knoteontime    times
    printks "kstatus= %d, kchan = %d, \\tnote#  = %d, velocity = %d \\tNote ON\\t%f\\n", 0, kstatus, kchan, kdata1, kdata2, knoteontime

    elseif (kstatus == 160) then
    printks "kstatus= %d, kchan = %d, \\tkdata1 = %d, kdata2 = %d \\tPolyphonic Aftertouch\\n", 0, kstatus, kchan, kdata1, kdata2

    elseif (kstatus == 176) then
    printks "kstatus= %d, kchan = %d, \\t CC = %d, value = %d \\tControl Change\\n", 0, kstatus, kchan, kdata1, kdata2

    elseif (kstatus == 192) then
    printks "kstatus= %d, kchan = %d, \\tkdata1 = %d, kdata2 = %d \\tProgram Change\\n", 0, kstatus, kchan, kdata1, kdata2

    elseif (kstatus == 208) then
    printks  "kstatus= %d, kchan = %d, \\tkdata1 = %d, kdata2 = %d \\tChannel Aftertouch\\n", 0, kstatus, kchan, kdata1, kdata2

    elseif (kstatus == 224) then
    printks "kstatus= %d, kchan = %d, \\t ( data1 , kdata2 ) = ( %d, %d )\\tPitch Bend\\n", 0, kstatus, kchan, kdata1, kdata2

    endif

      endin

    </CsInstruments>
    <CsScore>
    i130 0 3600
    e
    </CsScore>
    </CsoundSynthesizer>

## Credits

Author: Gabriel Maldonado
Italy
1998

New in Csound version 3.492

Prev | Up | Next
mididefault | Home | midinoteoff