Skip to content

midifilepos

Gets/Sets the playback position of a MIDI file.

This opcode can be called from anywhere, the setting function is also a non-op if used in an instrument triggered by a MIDI event.

Requires the -F flag to operate. If a filename is passed to -F, it is open with id 0 and playback starts immediately.

Syntax

 pos:i = midifilepos([id:i])
 pos:k = midifilepos([id:i])
 midifilepos(pos:i[,id:i])
 midifilepos(pos:k[,id:i])
ipos midifilepos [id]
kpos midifilepos [id]
midifilepos ipos[,id]
midifilepos kpos[,id]

Initialization

pos -- playback position in seconds

id (optional, default=0) -- MIDI file id, defaults to 0, the id of any file passed to the -F flag.

Examples

Here is an example of the miditempo opcode. It uses the files midifilepos.csd.

Example of the midifilepos opcode.
<CsoundSynthesizer>
<CsOptions>
--midifile -odac
</CsOptions>
<CsInstruments>
0dbfs=1

mf@global:i = midifileopen("catherine.mid")

instr Midisynth
 midifileplay(mf)
 amp:i = ampmidi(0.2)
 cps:i = cpsmidi()
 sig:a  = vco2(amp,cps)
 env:a  = madsr(0.001, 0.5, 0.1, 0.1)
   out(sig*env)
endin

instr SetPos
 midifilepos(p4,mf)
endin

instr GetPos
printk 1, midifilepos:k(mf)
endin

schedule(Midisynth,0,0)
schedule(GetPos,0,-1)
schedule(SetPos,5,0,10)
schedule(SetPos,10,0,5)
event_i("e", 0, midifilelen(mf))

</CsInstruments>
<CsScore>
</CsScore>
</CsoundSynthesizer>

See also

Sensing and Control: Tempo and Sequencing

Credits

Author: Victor Lazzarini
May 2025
New in Csound 7