Debian Codegraph @

src/Examples/FLOSS Manual Examples/01 Basics/01D08_markov_music.csd

raw
1 <CsoundSynthesizer>
2 <CsOptions>
3 -m128 -odac
4 </CsOptions>
5 <CsInstruments>
6 sr = 44100
7 ksmps = 32
8 0dbfs = 1
9 nchnls = 2
10 seed 0
11
12 ;****USER DEFINED OPCODES FOR MARKOV CHAINS****
13 opcode Markov, i, i[][]i
14 iMarkovTable[][], iPrevEl xin
15 iRandom random 0, 1
16 iNextEl = 0
17 iAccum = iMarkovTable[iPrevEl][iNextEl]
18 until iAccum >= iRandom do
19 iNextEl += 1
20 iAccum += iMarkovTable[iPrevEl][iNextEl]
21 enduntil
22 xout iNextEl
23 endop
24 opcode Markovk, k, k[][]k
25 kMarkovTable[][], kPrevEl xin
26 kRandom random 0, 1
27 kNextEl = 0
28 kAccum = kMarkovTable[kPrevEl][kNextEl]
29 until kAccum >= kRandom do
30 kNextEl += 1
31 kAccum += kMarkovTable[kPrevEl][kNextEl]
32 enduntil
33 xout kNextEl
34 endop
35
36 ;****DEFINITIONS FOR NOTES****
37 ;notes as proportions and a base frequency
38 giNotes[] array 1, 9/8, 6/5, 5/4, 4/3, 3/2, 5/3
39 giBasFreq = 330
40 ;probability of notes as markov matrix:
41 ;first -> only to third and fourth
42 ;second -> anywhere without self
43 ;third -> strong probability for repetitions
44 ;fourth -> idem
45 ;fifth -> anywhere without third and fourth
46 ;sixth -> mostly to seventh
47 ;seventh -> mostly to sixth
48 giProbNotes[][] init 7, 7
49 giProbNotes fillarray 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0,
50 0.2, 0.0, 0.2, 0.2, 0.2, 0.1, 0.1,
51 0.1, 0.1, 0.5, 0.1, 0.1, 0.1, 0.0,
52 0.0, 0.1, 0.1, 0.5, 0.1, 0.1, 0.1,
53 0.2, 0.2, 0.0, 0.0, 0.2, 0.2, 0.2,
54 0.1, 0.1, 0.0, 0.0, 0.1, 0.1, 0.6,
55 0.1, 0.1, 0.0, 0.0, 0.1, 0.6, 0.1
56
57 ;****DEFINITIONS FOR DURATIONS****
58 ;possible durations
59 gkDurs[] array 1, 1/2, 1/3
60 ;probability of durations as markov matrix:
61 ;first -> anything
62 ;second -> mostly self
63 ;third -> mostly second
64 gkProbDurs[][] init 3, 3
65 gkProbDurs array 1/3, 1/3, 1/3,
66 0.2, 0.6, 0.3,
67 0.1, 0.5, 0.4
68
69 ;****SET FIRST NOTE AND DURATION FOR MARKOV PROCESS****
70 giPrevNote init 1
71 gkPrevDur init 1
72
73 ;****INSTRUMENT FOR DURATIONS****
74 instr trigger_note
75 kTrig metro 1/gkDurs[gkPrevDur]
76 if kTrig == 1 then
77 event "i", "select_note", 0, 1
78 gkPrevDur Markovk gkProbDurs, gkPrevDur
79 endif
80 endin
81
82 ;****INSTRUMENT FOR PITCHES****
83 instr select_note
84 ;choose next note according to markov matrix and previous note
85 ;and write it to the global variable for (next) previous note
86 giPrevNote Markov giProbNotes, giPrevNote
87 ;call instr to play this note
88 event_i "i", "play_note", 0, 2, giPrevNote
89 ;turn off this instrument
90 turnoff
91 endin
92
93 ;****INSTRUMENT TO PERFORM ONE NOTE****
94 instr play_note
95 ;get note as index in ginotes array and calculate frequency
96 iNote = p4
97 iFreq = giBasFreq * giNotes[iNote]
98 ;random choice for mode filter quality and panning
99 iQ random 10, 200
100 iPan random 0.1, .9
101 ;generate tone and put out
102 aImp mpulse 1, p3
103 aOut mode aImp, iFreq, iQ
104 aL, aR pan2 aOut, iPan
105 outs aL, aR
106 endin
107
108 </CsInstruments>
109 <CsScore>
110 i "trigger_note" 0 100
111 </CsScore>
112 </CsoundSynthesizer>
113 ;example by joachim heintz