Debian Codegraph @

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