Skip to content

exitnowk

Stops the whole Csound performance at k-rate with an exit status.

Syntax

exitnowk(kStatus)
exitnowk kStatus

Performance

kStatus is the requested exit status. This argument is required. The opcode has no output.

Every call stops performance, including a call with a status of 0. To wait for a condition, put exitnowk inside a k-rate if statement. An unconditional call stops Csound on the first control cycle that reaches it.

The exit affects all instruments and pending events. It does not wait for release envelopes or add a fade. Fade the sound before calling it if you need a smooth ending. To stop one note while the rest of the piece continues, use turnoff.

Use exitnow for an exit at initialization. exitnowk runs during performance, so it can respond directly to a control signal or a check made at k-rate.

Exit status

Use a whole number from 0 to 255. Zero requests a successful exit. A nonzero value lets a calling script detect a failure.

Csound uses the magnitude of the status and keeps its lowest eight bits. For example, exitnowk(-1) and exitnowk(1) both give command-line status 1. Values outside the range wrap, so 256 requests the same status as 0. A request for status 0 does not clear errors that Csound has already recorded.

When Csound runs inside another program, this opcode returns control through Csound's performance API. The host decides whether its own process exits.

Examples

A live patch can use exitnowk to stop when a control signal reaches a limit. This example uses elapsed time for that signal and fades a tone to silence before exiting after two seconds, even though the score requests ten seconds. It uses exitnowk.csd.

Stop performance after a fade
<CsoundSynthesizer>
<CsOptions>
-odac -d -m0
</CsOptions>
<CsInstruments>
sr = 48000
ksmps = 32
nchnls = 1
0dbfs = 1

instr 1
  ; Fade the tone to silence before stopping the whole performance.
  aEnvelope linseg 0, 0.01, 0.1, 1.92, 0.1, 0.02, 0
  aTone poscil aEnvelope, 440
  out aTone

  kElapsed timeinsts
  if kElapsed >= 2 then
    printks "Stopping after two seconds\n", 0
    ; p4 selects the exit status. Zero requests a successful exit.
    exitnowk(p4)
  endif
endin
</CsInstruments>
<CsScore>
; The score asks for ten seconds, but exitnowk stops it after two.
i 1 0 10 0
e
</CsScore>
</CsoundSynthesizer>

The last score value, p4, selects the status. Leave it at 0 for a successful exit, or change it to 1 to report failure to a calling script. The example prints Stopping after two seconds in either case.

See also

exitnow, turnoff, Real-time Performance Control

Credits

Original exitnow by Gabriel Maldonado. Steven Yi added the performance-time exitnowk form in March 2026.

New in Csound 7.