Skip to content

midipgm

Reads the last program number received on a MIDI channel.

Syntax

iprogram = midipgm([ichannel])
iprogram midipgm [ichannel]

Initialization

iprogram is the program number, from 1 to 128. MIDI messages carry values from 0 to 127, so midipgm adds 1. It returns 0 if the channel has not received a Program Change message or if no channel is available.

ichannel is optional and defaults to 0. With 0, the opcode uses the MIDI channel that started the current note. A score note has no such channel, so it returns 0 unless you give an explicit channel number.

Use a positive integer to read a specific channel. A single MIDI port has channels 1 to 16. Csound also accepts channel numbers up to 1024 for input mapped from multiple ports. A number above 1024 causes an initialization error.

midipgm reads the stored program at initialization. A later Program Change message does not update this note's result. The next note reads the new value. The result does not include the bank number.

Reading a program does not change MIDI instrument assignments. Use pgmassign to control those assignments. With pgmassign 0, 0, Program Change messages still update the number that midipgm reads, but they do not select a different Csound instrument.

Examples

Use a MIDI keyboard's program selector to choose a sound for each new note. Program 2 selects a brighter tone. Other programs, including the initial value 0, select a sine tone. A held note keeps its sound when the program changes.

The example listens to MIDI device 0 for one minute. Change -M0 to suit your MIDI device. If your keyboard labels programs from 0 to 127, select program 1 to hear the brighter tone.

It uses midipgm.csd.

Choose a tone with MIDI Program Change
<CsoundSynthesizer>
<CsOptions>
-d -odac -M0
</CsOptions>
<CsInstruments>
sr = 48000
ksmps = 32
nchnls = 1
0dbfs = 1

; Send notes on every MIDI channel to instrument 1.
massign 0, 1
; Keep that assignment when a Program Change arrives.
pgmassign 0, 0

giSine = ftgen(0, 0, 4096, 10, 1)
giBright = ftgen(0, 0, 4096, 10, 1, 0.5, 0.25)

instr 1
  ; Read the program on the channel that started this note.
  iProgram = midipgm()
  iWave = giSine
  if iProgram == 2 then
    iWave = giBright
  endif
  prints "Program %d\n", iProgram

  iFrequency = cpsmidi()
  iAmplitude = ampmidi(0.2)
  aEnvelope = madsr(0.01, 0.1, 0.7, 0.15)
  aTone = oscili(iAmplitude, iFrequency, iWave)
  out(aTone * aEnvelope)
endin
</CsInstruments>
<CsScore>
f 0 60
</CsScore>
</CsoundSynthesizer>

To read channel 3 from a score note, use iprogram = midipgm(3).

See also

pgmassign, massign, midichn, midiin, MIDI input

Credits

Author Istvan Varga, 2006.