Magic Commands and Syntax highlighting for Csound

Once the csoundmagic extension is installed (see the 05-installingCsoundmagics notebook), one can use the Csound magics and functions by loading the extension:


In [1]:
%load_ext csoundmagics

The first magic allows the user to enter a csd in the user namespace, giving it a name:


In [2]:
%%csd crossfm
<CsoundSynthesizer>

<CsOptions>
  -d -o dac
</CsOptions>

<CsInstruments>
sr     = 48000
ksmps  = 100
nchnls = 2
0dbfs  = 1

          instr 1
idur       =          p3 
iamp       =          p4 
ifrq1      =          p5 
ifrq2      =          p6 
indx1      =          p7 
indx2      =          p8 
kenv       linen      iamp, 0.05, idur, 0.2 
a1, a2     crossfm    ifrq1, ifrq2, indx1, indx2, 1, 1, 1 
           outs       a1*kenv, a2*kenv 
          endin

          instr 2
kx         init       0.0 
kdx        init       0.1 
kdur       init       p4 
kamp       init       p5 
kndx       init       0 
kmax       init       p6 
kfq1       init       440.0 
kfq2       init       557.0 
knx1       init       3.5 
knx2       init       4.8 
           event      "i", 1, kx, kdur, kamp, kfq1, kfq2, knx1, knx2 
kx         =          kx + kdx 
knx1       =          knx1 - 0.025 
knx2       =          knx2 - 0.075 
kndx       =          kndx + 1 
if (kndx <= kmax) kgoto nextone
           turnoff 
nextone: 
          endin
</CsInstruments>

<CsScore>
f 1 0 16384 10 1 0

;f 0 3600

i 2 0 2 0.67 0.03 150
i 1 15.1 1.5 0.025 440 557 -0.25 -6.45 0.05 1.2
e
</CsScore>
</CsoundSynthesizer>

The csd can then be used in the Python code:


In [3]:
print(__csd['crossfm'])


<CsoundSynthesizer>

<CsOptions>
  -d -o dac
</CsOptions>

<CsInstruments>
sr     = 48000
ksmps  = 100
nchnls = 2
0dbfs  = 1

          instr 1
idur       =          p3 
iamp       =          p4 
ifrq1      =          p5 
ifrq2      =          p6 
indx1      =          p7 
indx2      =          p8 
kenv       linen      iamp, 0.05, idur, 0.2 
a1, a2     crossfm    ifrq1, ifrq2, indx1, indx2, 1, 1, 1 
           outs       a1*kenv, a2*kenv 
          endin

          instr 2
kx         init       0.0 
kdx        init       0.1 
kdur       init       p4 
kamp       init       p5 
kndx       init       0 
kmax       init       p6 
kfq1       init       440.0 
kfq2       init       557.0 
knx1       init       3.5 
knx2       init       4.8 
           event      "i", 1, kx, kdur, kamp, kfq1, kfq2, knx1, knx2 
kx         =          kx + kdx 
knx1       =          knx1 - 0.025 
knx2       =          knx2 - 0.075 
kndx       =          kndx + 1 
if (kndx <= kmax) kgoto nextone
           turnoff 
nextone: 
          endin
</CsInstruments>

<CsScore>
f 1 0 16384 10 1 0

;f 0 3600

i 2 0 2 0.67 0.03 150
i 1 15.1 1.5 0.025 440 557 -0.25 -6.45 0.05 1.2
e
</CsScore>
</CsoundSynthesizer>

The runCsd function plays the csd:


In [4]:
runCsd('crossfm')


Out[4]:
'OK'

The csound engine used by runCsd and runOrcSco is bound to slot#0. It can be released with the following command:


In [5]:
%csound 0


Erasing slot#: 0

The %%orc and the %%sco magics allow the user to enter respectively a named orchestra and a named score in the user namespace:


In [6]:
%%orc 02_43_1
; ************************************************************************
; ACCCI:      02_43_1.ORC
; timbre:     tibetan chant
; synthesis:  additive same units(02)
;             basic instrument with minimal differences in frequency(43)
;             arpeggio instrument by Risset
; source:     Phase6, Lorrain(1980); Boulanger(1990): risset1.orc
; coded:      jpg 9/93

sr = 44100
kr  =  441
ksmps= 100
nchnls = 2

instr 1; *****************************************************************
idur  = p3
iamp  = p4/9
ifq   = p5
ioff1 = p6
ioff2 = 2*p6
ioff3 = 3*p6
ioff4 = 4*p6
irise = p7
idec  = p8

   ae  linen   iamp,irise,idur,idec   
   a1  oscili  ae, ifq, 1
   a2  oscili  ae, ifq+ioff1, 1  ; nine oscillators with the same ae
   a3  oscili  ae, ifq+ioff2, 1  ; and waveform, but slightly different
   a4  oscili  ae, ifq+ioff3, 1  ; frequencies create harmonic arpeggio 
   a5  oscili  ae, ifq+ioff4, 1
   a6  oscili  ae, ifq-ioff1, 1
   a7  oscili  ae, ifq-ioff2, 1
   a8  oscili  ae, ifq-ioff3, 1
   a9  oscili  ae, ifq-ioff4, 1
       outs1   a1+a2+a3+a4+a5+a6+a7+a8+a9

endin


instr 2; *****************************************************************
idur  = p3
iamp  = p4/9
ifq   = p5
ioff1 = p6
ioff2 = 2*p6
ioff3 = 3*p6
ioff4 = 4*p6
irise = p7
idec  = p8

   ae  linen   iamp,irise,idur,idec   
   a1  oscili  ae, ifq, 1
   a2  oscili  ae, ifq+ioff1, 1  ; nine oscillators with the same ae
   a3  oscili  ae, ifq+ioff2, 1  ; and waveform, but slightly different
   a4  oscili  ae, ifq+ioff3, 1  ; frequencies create harmonic arpeggio
   a5  oscili  ae, ifq+ioff4, 1
   a6  oscili  ae, ifq-ioff1, 1
   a7  oscili  ae, ifq-ioff2, 1
   a8  oscili  ae, ifq-ioff3, 1
   a9  oscili  ae, ifq-ioff4, 1
       outs2   a1+a2+a3+a4+a5+a6+a7+a8+a9
endin

In [7]:
%%sco 02_43_1
; ************************************************************************
; ACCCI:      02_43_1.SCO
; coded:      jpg 9/93


; GEN functions **********************************************************

; carrier
f1 0 1024 10 .3 0 0 0 .1 .1 .1 .1 .1 .1


; score ******************************************************************

;    start   idur  iamp   ifq     ioff   irise   idec
i1     0      35   8000   110     .03     .07     21     ; 1st envelope
i2     5      20   9600    55     .02     .04     12     ;
i2    20      15   8000   220     .05      1.5    3      ;
i1    20      20   9600   110     .04      2      4      ;
i1    28      30   8000   220     .04      3      6      ;  2nd env.
i2    32      26   9600   110     .025     2.6    5.2    ;
i1    32.1    23   8000   110     .03      2.3    4.6    ;
i2    36      22   8000    55     .01     .04     13     ; 1st envelope
e

These orchestra and score can be used in python with the following code:

__orc['02_43_1']
__sco['02_43_1']

The runOrcSco function plays a given orchestra and score which are registered in the user namespace. If the csound engine in slot#0 had been released, it will be automatically recreated bu the runOrcSco command. Here we explicitly get that instance to force real-time audio output:


In [8]:
cs = getCsound()
cs.setOption('-odac')
runOrcSco('02_43_1', '02_43_1')


Out[8]:
'OK'

Csound Code Syntax Highlighting in Markdown Cells

The csd, orchestra, and score entered with the magics %%csd, %%orc, and %%sco in the code cells above were syntax highlighted, thanks to the csound.js installed in the codemirror editor of Jupyter. This can be used in markdown cells as well. To activate syntax highlighting of Csound code in a markdown cell, one have to insert this line above the csound code block:

```csound
and then add that line below the code block:
```

This works with csd's:

<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-odac     ;;;realtime audio out
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o oscil.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

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

instr 1

kamp = .6
kcps = 440
ifn  = p4

asig oscil kamp, kcps, ifn
     outs asig,asig

endin
</CsInstruments>
<CsScore>
f1 0 16384 10 1                                          ; Sine
f2 0 16384 10 1 0.5 0.3 0.25 0.2 0.167 0.14 0.125 .111   ; Sawtooth
f3 0 16384 10 1 0   0.3 0    0.2 0     0.14 0     .111   ; Square
f4 0 16384 10 1 1   1   1    0.7 0.5   0.3  0.1          ; Pulse

i 1 0 2 1
i 1 3 2 2
i 1 6 2 3
i 1 9 2 4

e
</CsScore>
</CsoundSynthesizer>

with orchestra:

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

instr 1

kamp = .6
kcps = 440
ifn  = p4

asig oscil kamp, kcps, ifn
     outs asig,asig

endin

and with scores:

f1 0 16384 10 1                                          ; Sine
f2 0 16384 10 1 0.5 0.3 0.25 0.2 0.167 0.14 0.125 .111   ; Sawtooth
f3 0 16384 10 1 0   0.3 0    0.2 0     0.14 0     .111   ; Square
f4 0 16384 10 1 1   1   1    0.7 0.5   0.3  0.1          ; Pulse

i 1 0 2 1
i 1 3 2 2
i 1 6 2 3
i 1 9 2 4

e