csound / manual

Csound Reference Manual (English)
Other
45 stars 29 forks source link

Python opcode plugin examples #573

Open tjingboem opened 3 years ago

tjingboem commented 3 years ago

This one is from the Floss Manuals

--env:SSDIR+=../SourceMaterials -odac -dm0 sr = 44100 ksmps = 32 nchnls = 2 0dbfs = 1 pyinit ; Python script to define probabilities for each note as lists within a list ; Definition of the get_new_note function which randomly generates a new ; note based on the probabilities of each note occuring. ; Each note list must total 1, or there will be problems! pyruni {{ c = [0.1, 0.2, 0.05, 0.4, 0.25] d = [0.4, 0.1, 0.1, 0.2, 0.2] e = [0.2, 0.35, 0.05, 0.4, 0] g = [0.7, 0.1, 0.2, 0, 0] a = [0.1, 0.2, 0.05, 0.4, 0.25] markov = [c, d, e, g, a] from random import random, seed seed() def get_new_note(previous_note): number = random() accum = 0 i = 0 while accum < number: accum = accum + markov[int(previous_note)] [int(i)] i = i + 1 return i - 1.0 }} giSine ftgen 0, 0, 2048, 10, 1 ;sine wave giPenta ftgen 0, 0, -6, -2, 0, 2, 4, 7, 9 ;Pitch classes for pentatonic scale instr 1 ;Markov chain reader and note spawner ;p4 = frequency of note generation ;p5 = octave ioct init p5 klastnote init 0 ;Used to remember last note played (start at first note of scale) ktrig metro p4 ;generate a trigger with frequency p4 knewnote pycall1t ktrig, "get_new_note", klastnote ;get new note from chain schedkwhen ktrig, 0, 10, 2, 0, 0.2, knewnote, ioct ;launch note on instrument 2 klastnote = knewnote ;New note is now the old note endin instr 2 ;A simple sine wave instrument ;p4 = note to be played ;p5 = octave ioct init p5 ipclass table p4, giPenta ipclass = ioct + (ipclass / 100) ; Pitch class of the note ifreq = cpspch(ipclass) ;Note frequency in Hertz aenv linen .2, 0.05, p3, 0.1 ;Amplitude envelope aout poscil aenv, ifreq , giSine ;Simple oscillator outs aout, aout endin ; frequency of Octave of ; note generation melody i 1 0 30 3 7 i 1 5 25 6 9 i 1 10 20 7.5 10 i 1 15 15 1 8

;Example by Andrés Cabrera

tjingboem commented 3 years ago

Also: complete existing examples for every python opcode already on manaul page

Screenshot from 2021-08-26 16-59-57

tjingboem commented 3 years ago

first attempt for pyruni

<CsoundSynthesizer>
<CsOptions>
--nosound
</CsOptions>
<CsInstruments>

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

;If you are not running CsoundAC you need the following line
;to initialize the python interpreter
pyinit

pyruni "import random"

instr 1
        ; This message is stored in the main namespace
        ; and is the same for every instance
        pyruni  "message = 'a global random number: %f' % random.random()"
        pyrun   "print(message)"

        ; This message is stored in the private namespace
        ; and is different for different instances
        pylruni "message = 'a private random number: %f' % random.random()"
        pylrun  "print(message)"
endin
</CsInstruments>
<CsScore>
i1 0 .1
e
</CsScore>
</CsoundSynthesizer>

produces lots of

a global random number: 0.869267
a private random number: 0.870727
a global random number: 0.869267
a private random number: 0.870727
...........