Debian Codegraph @

src/Examples/FLOSS Manual Examples/01 Basics/01D05_more_lin_tri_units.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 ;****SEQUENCE OF UNITS AS ARRAY****/
13 giSequence[] array 0, 1.2, 1.4, 2.2, 2.4, 3.2, 3.6
14 giSeqIndx = 0 ;startindex
15
16 ;****UDO DEFINITIONS****
17 opcode linrnd_low, i, iii
18 ;linear random with precedence of lower values
19 iMin, iMax, iMaxCount xin
20 ;set counter and initial (absurd) result
21 iCount = 0
22 iRnd = iMax
23 ;loop and reset iRnd
24 until iCount == iMaxCount do
25 iUniRnd random iMin, iMax
26 iRnd = iUniRnd < iRnd ? iUniRnd : iRnd
27 iCount += 1
28 enduntil
29 xout iRnd
30 endop
31
32 opcode linrnd_high, i, iii
33 ;linear random with precedence of higher values
34 iMin, iMax, iMaxCount xin
35 ;set counter and initial (absurd) result
36 iCount = 0
37 iRnd = iMin
38 ;loop and reset iRnd
39 until iCount == iMaxCount do
40 iUniRnd random iMin, iMax
41 iRnd = iUniRnd > iRnd ? iUniRnd : iRnd
42 iCount += 1
43 enduntil
44 xout iRnd
45 endop
46
47 opcode trirnd, i, iii
48 iMin, iMax, iMaxCount xin
49 ;set a counter and accumulator
50 iCount = 0
51 iAccum = 0
52 ;perform loop and accumulate
53 until iCount == iMaxCount do
54 iUniRnd random iMin, iMax
55 iAccum += iUniRnd
56 iCount += 1
57 enduntil
58 ;get the mean and output
59 iRnd = iAccum / iMaxCount
60 xout iRnd
61 endop
62
63 ;****ONE INSTRUMENT TO PERFORM ALL DISTRIBUTIONS****
64 ;0 = uniform, 1 = linrnd_low, 2 = linrnd_high, 3 = trirnd
65 ;the fractional part denotes the number of units, e.g.
66 ;3.4 = triangular distribution with four sub-units
67
68 instr notes
69 ;how many notes to be played
70 iHowMany = p4
71 ;by which distribution with how many units
72 iWhich = giSequence[giSeqIndx]
73 iDistrib = int(iWhich)
74 iUnits = round(frac(iWhich) * 10)
75 ;set min and max duration
76 iMinDur = .1
77 iMaxDur = 2
78 ;set min and max pitch
79 iMinPch = 36
80 iMaxPch = 84
81
82 ;trigger as many instances of instr play as needed
83 iThisNote = 0
84 iStart = 0
85 iPrint = 1
86
87 ;for each note to be played
88 until iThisNote == iHowMany do
89
90 ;calculate iMidiPch and iDur depending on type
91 if iDistrib == 0 then
92 printf_i "%s", iPrint, "... uniform distribution:\n"
93 printf_i "%s", iPrint, "EQUAL LIKELIHOOD OF ALL PITCHES AND DURATIONS\n"
94 iMidiPch random iMinPch, iMaxPch ;midi note
95 iDur random iMinDur, iMaxDur ;duration
96 elseif iDistrib == 1 then
97 printf_i "... linear low distribution with %d units:\n", iPrint, iUnits
98 printf_i "%s", iPrint, "LOWER NOTES AND LONGER DURATIONS PREFERRED\n"
99 iMidiPch linrnd_low iMinPch, iMaxPch, iUnits
100 iDur linrnd_high iMinDur, iMaxDur, iUnits
101 elseif iDistrib == 2 then
102 printf_i "... linear high distribution with %d units:\n", iPrint, iUnits
103 printf_i "%s", iPrint, "HIGHER NOTES AND SHORTER DURATIONS PREFERRED\n"
104 iMidiPch linrnd_high iMinPch, iMaxPch, iUnits
105 iDur linrnd_low iMinDur, iMaxDur, iUnits
106 else
107 printf_i "... triangular distribution with %d units:\n", iPrint, iUnits
108 printf_i "%s", iPrint, "MEDIUM NOTES AND DURATIONS PREFERRED\n"
109 iMidiPch trirnd iMinPch, iMaxPch, iUnits
110 iDur trirnd iMinDur, iMaxDur, iUnits
111 endif
112
113 ;call subinstrument to play note
114 event_i "i", "play", iStart, iDur, int(iMidiPch)
115
116 ;increase start tim and counter
117 iStart += iDur
118 iThisNote += 1
119 ;avoid continuous printing
120 iPrint = 0
121 enduntil
122
123 ;reset the duration of this instr to make all events happen
124 p3 = iStart + 2
125
126 ;increase index for sequence
127 giSeqIndx += 1
128 ;call instr again if sequence has not been ended
129 if giSeqIndx < lenarray(giSequence) then
130 event_i "i", "notes", p3, 1, iHowMany
131 ;or exit
132 else
133 event_i "i", "exit", p3, 1
134 endif
135 endin
136
137
138 ;****INSTRUMENTS TO PLAY THE SOUNDS AND EXIT CSOUND****
139 instr play
140 ;increase duration in random range
141 iDur random p3, p3*1.5
142 p3 = iDur
143 ;get midi note and convert to frequency
144 iMidiNote = p4
145 iFreq cpsmidinn iMidiNote
146 ;generate note with karplus-strong algorithm
147 aPluck pluck .2, iFreq, iFreq, 0, 1
148 aPluck linen aPluck, 0, p3, p3
149 ;filter
150 aFilter mode aPluck, iFreq, .1
151 ;mix aPluck and aFilter according to MidiNote
152 ;(high notes will be filtered more)
153 aMix ntrpol aPluck, aFilter, iMidiNote, 36, 84
154 ;panning also according to MidiNote
155 ;(low = left, high = right)
156 iPan = (iMidiNote-36) / 48
157 aL, aR pan2 aMix, iPan
158 outs aL, aR
159 endin
160
161 instr exit
162 exitnow
163 endin
164
165 </CsInstruments>
166 <CsScore>
167 i "notes" 0 1 23 ;set number of notes per instr here
168 e 99999 ;make possible to perform long (exit will be automatically)
169 </CsScore>
170 </CsoundSynthesizer>
171 ;example by joachim heintz