Debian Codegraph @

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

raw
1 <CsoundSynthesizer>
2 <CsOptions>
3 -odac -m0
4 </CsOptions>
5 <CsInstruments>
6 sr = 44100
7 ksmps = 32
8 nchnls = 2
9 0dbfs = 1
10 seed 0
11
12 ;****DEFINE OPCODES FOR LINEAR DISTRIBUTION****
13
14 opcode linrnd_low, i, ii
15 ;linear random with precedence of lower values
16 iMin, iMax xin
17 ;generate two random values with the random opcode
18 iOne random iMin, iMax
19 iTwo random iMin, iMax
20 ;compare and get the lower one
21 iRnd = iOne < iTwo ? iOne : iTwo
22 xout iRnd
23 endop
24
25 opcode linrnd_high, i, ii
26 ;linear random with precedence of higher values
27 iMin, iMax xin
28 ;generate two random values with the random opcode
29 iOne random iMin, iMax
30 iTwo random iMin, iMax
31 ;compare and get the higher one
32 iRnd = iOne > iTwo ? iOne : iTwo
33 xout iRnd
34 endop
35
36
37 ;****INSTRUMENTS FOR THE DIFFERENT DISTRIBUTIONS****
38
39 instr notes_uniform
40 prints "... instr notes_uniform playing:\n"
41 prints "EQUAL LIKELINESS OF ALL PITCHES AND DURATIONS\n"
42 ;how many notes to be played
43 iHowMany = p4
44 ;trigger as many instances of instr play as needed
45 iThisNote = 0
46 iStart = 0
47 until iThisNote == iHowMany do
48 iMidiPch random 36, 84 ;midi note
49 iDur random .5, 1 ;duration
50 event_i "i", "play", iStart, iDur, int(iMidiPch)
51 iStart += iDur ;increase start
52 iThisNote += 1 ;increase counter
53 enduntil
54 ;reset the duration of this instr to make all events happen
55 p3 = iStart + 2
56 ;trigger next instrument two seconds after the last note
57 event_i "i", "notes_linrnd_low", p3, 1, iHowMany
58 endin
59
60 instr notes_linrnd_low
61 prints "... instr notes_linrnd_low playing:\n"
62 prints "LOWER NOTES AND LONGER DURATIONS PREFERRED\n"
63 iHowMany = p4
64 iThisNote = 0
65 iStart = 0
66 until iThisNote == iHowMany do
67 iMidiPch linrnd_low 36, 84 ;lower pitches preferred
68 iDur linrnd_high .5, 1 ;longer durations preferred
69 event_i "i", "play", iStart, iDur, int(iMidiPch)
70 iStart += iDur
71 iThisNote += 1
72 enduntil
73 ;reset the duration of this instr to make all events happen
74 p3 = iStart + 2
75 ;trigger next instrument two seconds after the last note
76 event_i "i", "notes_linrnd_high", p3, 1, iHowMany
77 endin
78
79 instr notes_linrnd_high
80 prints "... instr notes_linrnd_high playing:\n"
81 prints "HIGHER NOTES AND SHORTER DURATIONS PREFERRED\n"
82 iHowMany = p4
83 iThisNote = 0
84 iStart = 0
85 until iThisNote == iHowMany do
86 iMidiPch linrnd_high 36, 84 ;higher pitches preferred
87 iDur linrnd_low .3, 1.2 ;shorter durations preferred
88 event_i "i", "play", iStart, iDur, int(iMidiPch)
89 iStart += iDur
90 iThisNote += 1
91 enduntil
92 ;reset the duration of this instr to make all events happen
93 p3 = iStart + 2
94 ;call instr to exit csound
95 event_i "i", "exit", p3+1, 1
96 endin
97
98
99 ;****INSTRUMENTS TO PLAY THE SOUNDS AND TO EXIT CSOUND****
100
101 instr play
102 ;increase duration in random range
103 iDur random p3, p3*1.5
104 p3 = iDur
105 ;get midi note and convert to frequency
106 iMidiNote = p4
107 iFreq cpsmidinn iMidiNote
108 ;generate note with karplus-strong algorithm
109 aPluck pluck .2, iFreq, iFreq, 0, 1
110 aPluck linen aPluck, 0, p3, p3
111 ;filter
112 aFilter mode aPluck, iFreq, .1
113 ;mix aPluck and aFilter according to MidiNote
114 ;(high notes will be filtered more)
115 aMix ntrpol aPluck, aFilter, iMidiNote, 36, 84
116 ;panning also according to MidiNote
117 ;(low = left, high = right)
118 iPan = (iMidiNote-36) / 48
119 aL, aR pan2 aMix, iPan
120 outs aL, aR
121 endin
122
123 instr exit
124 exitnow
125 endin
126
127 </CsInstruments>
128 <CsScore>
129 i "notes_uniform" 0 1 23 ;set number of notes per instr here
130 ;instruments linrnd_low and linrnd_high are triggered automatically
131 e 99999 ;make possible to perform long (exit will be automatically)
132 </CsScore>
133 </CsoundSynthesizer>
134 ;example by joachim heintz