In [1]:
import ctcsound
cs = ctcsound.Csound()
Doing it the classical way, we compile an orchestra and a score file, passing also some flags. Note that the first argument, indicating the program name, is mandatory. This is due to the C origin of Csound. You can give as first argument to cs.compile_ any string such as "dummy" or "myProgram", etc. The usage, calling C programs, is to pass the program name.
If the compilation is successful, the Csound instance is ready to run a performance. Then orchestra and score are performed and the Csound instance is reset, to be ready for another performance:
In [2]:
ret = cs.compile_("csound", "-o", "dac", "examples/02-a.orc", "examples/02-a.sco")
if ret == ctcsound.CSOUND_SUCCESS:
cs.perform()
cs.reset()
We can do the same with a single input file (.csd file):
In [3]:
ret = cs.compile_("csound", "examples/02-a.csd")
if ret == ctcsound.CSOUND_SUCCESS:
cs.perform()
cs.reset()
Csound offers also the possibility to compile directly a csd file. As this can be done during a performance to replace or add new instruments and events (more on this later), cs.start() has to be called here, because Csound is not actually running a performance (due to the preceding cs.reset():
In [4]:
ret = cs.compileCsd("examples/02-a.csd")
if ret == ctcsound.CSOUND_SUCCESS:
cs.start()
cs.perform()
cs.reset()
Finally, instead of compiling a csd from an input file, we can do it from a string:
In [5]:
csd = '''
<CsoundSynthesizer>
<CsOptions>
-d -o dac -m0
</CsOptions>
<CsInstruments>
sr = 48000
ksmps = 100
nchnls = 2
0dbfs = 1
instr 1
idur = p3
iamp = p4
icps = cpspch(p5)
irise = p6
idec = p7
ipan = p8
kenv linen iamp, irise, idur, idec
kenv = kenv*kenv
asig poscil kenv, icps
a1, a2 pan2 asig, ipan
outs a1, a2
endin
</CsInstruments>
<CsScore>
i 1 0 1 0.5 8.06 0.05 0.3 0.5
e 1.5
</CsScore>
</CsoundSynthesizer>
'''
ret = cs.compileCsdText(csd)
if ret == ctcsound.CSOUND_SUCCESS:
cs.start()
cs.perform()
cs.reset()
Instead of performing an entire score with a single cs.perform() command, it is possible to run a loop, performing a buffer (ksmps samples long with cs.performKsmps(), or -b frames long with cs.performBuffer()) during each pass. Note that cs.performKsmps() and cs.performBuffer() return False while the score is not finished. Here, a dot is displayed for each pass in the loop:
In [6]:
ret = cs.compile_("csound", "examples/02-a.csd")
if ret == ctcsound.CSOUND_SUCCESS:
while not cs.performKsmps():
print('.', end='')
print()
cs.reset()
In [7]:
ret = cs.compile_("csound", "examples/02-a.csd")
if ret == ctcsound.CSOUND_SUCCESS:
while not cs.performBuffer():
print('.', end='')
print()
cs.reset()