Run an opcode at the end of current event


defer runs an opcode as the last action of a given event, during the process of being deallocated. This can be used together with opcodes which release resources back which are being used during the event. At the moment defer works only with builtin opcodes. For complex release actions, it is nonetheless possible to bundle the actions in a separate instrument and use schedule together with defer (or simpler, use atstop instead).

Any arguments passed after the name of the opcode (which must be within quotations) are passed directly to the opcode itself. Notice that only opcodes without output make sense in this context. Useful opcodes are print, prints, pool_push, dict_free, etc

Release time vs deinit time

The event is not scheduled at release time (see below "Release time vs Deinit time") but at the moment the note is freed.

Release time is when the note is within its release phase, which will only happen if the note has an envelope with a release segment (like linsegr), or if it has setup extra time with xtratim. The opcode release can be used to query if the current note is being released. The instrument keeps running in release phase as long as the release part of the envelope is finished or the extra time allocated via xtratim is through.

Deinit time is the moment the note is actually being freed, so the instrument is not running anymore at this point.


defer Sopcode, [arg1, arg2, ...]


defer evaluates at init time but acts at deacllocation time. The arguments passed are evaluated at deallocation time


  • Sopcode: the name of the opcode to defer
  • args: any args (i, k, S) are passed to the opcodes. They are evaluated at deallocation time.

Execution Time

  • Init
  • Deallocation




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

;; Example 1
instr 1
  printk2 timeinsts()
  ;; this will be called at the end of this instrument
  defer "event_i", "i", 2, 0, 1, 93

instr 2
  print p4

instr example1
  schedule 1, 0, 0.1

;; -------------------------------------------
;; Example 2

instr 10
  idict dict_new "*sf", "foo", 100, "bar", 200
  ;; delete this key at the end of the note
  defer "dict_set", idict, "foo"
  dict_print idict
  schedule 11, 0.5, 1, idict

instr 11
printf "Instr 11 \n", 1
  idict = p4
  ;; by now the dict should have deleted the key "foo"
  dict_print idict

instr example2
  schedule 10, 0, 1

schedule "example2", 0, 0.1


e 3
; f0 3600


See also


Eduardo Moguillansky, 2019