| 1 |
<CsoundSynthesizer> |
| 2 |
<CsOptions> |
| 3 |
-odac -dm0 |
| 4 |
</CsOptions> |
| 5 |
<CsInstruments> |
| 6 |
|
| 7 |
sr = 44100 |
| 8 |
ksmps = 32 |
| 9 |
nchnls = 2 |
| 10 |
0dbfs = 1 |
| 11 |
|
| 12 |
pyinit |
| 13 |
|
| 14 |
; Python script to define probabilities for each note as lists within a list |
| 15 |
; Definition of the get_new_note function which randomly generates a new |
| 16 |
; note based on the probabilities of each note occuring. |
| 17 |
; Each note list must total 1, or there will be problems! |
| 18 |
|
| 19 |
pyruni {{ |
| 20 |
c = [0.1, 0.2, 0.05, 0.4, 0.25] |
| 21 |
d = [0.4, 0.1, 0.1, 0.2, 0.2] |
| 22 |
e = [0.2, 0.35, 0.05, 0.4, 0] |
| 23 |
g = [0.7, 0.1, 0.2, 0, 0] |
| 24 |
a = [0.1, 0.2, 0.05, 0.4, 0.25] |
| 25 |
|
| 26 |
markov = [c, d, e, g, a] |
| 27 |
|
| 28 |
from random import random, seed |
| 29 |
|
| 30 |
seed() |
| 31 |
|
| 32 |
def get_new_note(previous_note): |
| 33 |
number = random() |
| 34 |
accum = 0 |
| 35 |
i = 0 |
| 36 |
while accum < number: |
| 37 |
accum = accum + markov[int(previous_note)] [int(i)] |
| 38 |
i = i + 1 |
| 39 |
return i - 1.0 |
| 40 |
}} |
| 41 |
|
| 42 |
giSine ftgen 0, 0, 2048, 10, 1 ;sine wave |
| 43 |
giPenta ftgen 0, 0, -6, -2, 0, 2, 4, 7, 9 ;Pitch classes for pentatonic scale |
| 44 |
|
| 45 |
|
| 46 |
instr 1 ;Markov chain reader and note spawner |
| 47 |
;p4 = frequency of note generation |
| 48 |
;p5 = octave |
| 49 |
ioct init p5 |
| 50 |
klastnote init 0 ;Used to remember last note played |
| 51 |
ktrig metro p4 ;generate a trigger with frequency p4 |
| 52 |
knewnote pycall1t ktrig, "get_new_note", klastnote ;get new note from chain |
| 53 |
schedkwhen ktrig, 0, 10, 2, 0, 0.2, knewnote, ioct ;launch note on instr 2 |
| 54 |
klastnote = knewnote ;New note is now the old note |
| 55 |
endin |
| 56 |
|
| 57 |
instr 2 ;A simple sine wave instrument |
| 58 |
;p4 = note to be played |
| 59 |
;p5 = octave |
| 60 |
ioct init p5 |
| 61 |
ipclass table p4, giPenta |
| 62 |
ipclass = ioct + (ipclass / 100) ; Pitch class of the note |
| 63 |
ifreq = cpspch(ipclass) ;Note frequency in Hertz |
| 64 |
aenv linen .2, 0.05, p3, 0.1 ;Amplitude envelope |
| 65 |
aout poscil aenv, ifreq , giSine ;Simple oscillator |
| 66 |
outs aout, aout |
| 67 |
endin |
| 68 |
|
| 69 |
</CsInstruments> |
| 70 |
<CsScore> |
| 71 |
; frequency of Octave of |
| 72 |
; note generation melody |
| 73 |
i 1 0 30 3 7 |
| 74 |
i 1 5 25 6 9 |
| 75 |
i 1 10 20 7.5 10 |
| 76 |
i 1 15 15 1 8 |
| 77 |
</CsScore> |
| 78 |
</CsoundSynthesizer> |
| 79 |
;Example by Andrés Cabrera |