midichn
Prev | Orchestra Opcodes and Operators | Next

## midichn

midichn — Returns the MIDI channel number from which the note was activated.

## Description

midichn returns the MIDI channel number (1 - 16) from which the note was activated. In the case of score notes, it returns 0.

## Syntax

    ichn midichn

## Initialization

ichn -- channel number. If the current note was activated from score, it is set to zero.

## Examples

Here is a simple example of the midichn opcode. It uses the file midichn.csd.

Example 591. Example of the midichn 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
    -odac -M0 -+rtmidi=virtual ;;; midi file input
    </CsOptions>
    <CsInstruments>

    sr = 44100
    ksmps = 32
    nchnls = 2
    0dbfs  = 1

    instr 1
    ;change channel on virtual midi keyboard
    i1 midichn
       print i1

    endin
    </CsInstruments>
    <CsScore>
    f 0 20	;runs for 20 seconds

    e
    </CsScore>
    </CsoundSynthesizer>

Here is an advanced example of the midichn opcode. It uses the file midichn_advanced.csd.

Do not forget that you must include the -F flag when using an external MIDI file like “midichn_advanced.mid”.

Example 592. An advanced example of the midichn opcode.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    -odac -F midichn_advanced.mid ;;;realtime audio out with MIDI file input
    ; For Non-realtime ouput leave only the line below:
    ; -o midichn_advanced.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    sr = 44100
    ksmps = 32
    nchnls = 2
    0dbfs  = 1

    	massign  1, 1		; all channels use instr 1
    	massign  2, 1
    	massign  3, 1
    	massign  4, 1
    	massign  5, 1
    	massign  6, 1
    	massign  7, 1
    	massign  8, 1
    	massign  9, 1
    	massign 10, 1
    	massign 11, 1
    	massign 12, 1
    	massign 13, 1
    	massign 14, 1
    	massign 15, 1
    	massign 16, 1

    gicnt	=  0		; note counter

    	instr 1

    gicnt	=  gicnt + 1	; update note counter
    kcnt	init gicnt	; copy to local variable
    ichn	midichn		; get channel number
    istime	times		; note-on time

    	if (ichn > 0.5) goto l2		; MIDI note
    	printks "note %.0f (time = %.2f) was activated from the score\\n", \
    		3600, kcnt, istime
    	goto l1
    l2:
    	printks "note %.0f (time = %.2f) was activated from channel %.0f\\n", \
    		3600, kcnt, istime, ichn
    l1:

    icps	cpsmidi		; convert midi note to pitch
    kenv	madsr	0.1, 0, 0.8, 0.9
    asig	pluck	kenv, icps, icps, 1, 1
    	outs	asig, asig

    endin
    </CsInstruments>
    <CsScore>
    t 0 60		;beats per minute

    f 0 8		;stay active for 8 seconds
    f 1 0 4096 10 1	;sine

    e
    </CsScore>
    </CsoundSynthesizer>

Its output should include lines like:

    note 1 (time = 0.00) was activated from channel 1
    note 2 (time = 2.00) was activated from channel 4
    note 3 (time = 3.00) was activated from channel 2
    note 4 (time = 5.00) was activated from channel 3

## See Also

pgmassign

## Credits

Author: Istvan Varga
May 2002

New in version 4.20

Prev | Up | Next
midichannelaftertouch | Home | midicontrolchange