Debian Codegraph @

Resources/modules/Synthesis/StochGrains2.c5

raw
1 class GrainFilter:
2 def __init__(self, input, freq, q, type):
3 self.filter = Biquad(input, freq, q, self.clipTypeToFilter(type))
4 self.output = Interp(input, self.filter, interp=self.clipTypeToInterp(type))
5
6 def stop(self):
7 self.filter.stop()
8 self.output.stop()
9
10 def play(self):
11 self.filter.play()
12 self.output.play()
13
14 def setType(self, x):
15 self.filter.type = self.clipTypeToFilter(x)
16 self.output.interp = self.clipTypeToInterp(x)
17
18 def clipTypeToFilter(self, x):
19 x = x - 1
20 if x < 0:
21 return 0
22 return x
23
24 def clipTypeToInterp(self, x):
25 if x > 0:
26 return 1
27 return 0
28
29 class GrainSnd:
30 def __init__(self, order, count, table, freq, start, dur, pan, ftype, ffr, fq, mul, env, nchnls, table_dur):
31 self.trig = Select(count, order)
32 self.freq = SampHold(freq, self.trig, 1.0)
33 self.start_rng = SampHold(start, self.trig, 1.0)
34 self.start = TrigXnoise(self.trig, mul=self.start_rng[1]-self.start_rng[0], add=self.start_rng[0])
35 self.dur_rng = SampHold(dur, self.trig, 1.0)
36 self.dur = TrigXnoise(self.trig, mul=self.dur_rng[1]-self.dur_rng[0], add=self.dur_rng[0])
37 self.pan = TrigRand(self.trig, pan[0], pan[1])
38 self.ffr = TrigRand(self.trig, ffr[0], ffr[1])
39 self.fq = TrigRand(self.trig, fq[0], fq[1])
40 self.mul = TrigXnoise(self.trig, mul=mul[1]-mul[0], add=mul[0])
41 self.amp = TrigEnv(self.trig, env, self.dur, mul=self.mul*0.25)
42 self.pointer = TrigEnv(self.trig, LinTable(), self.dur, mul=self.dur*self.freq/table_dur, add=self.start)
43 self.s1 = Pointer(table, self.pointer, mul=self.amp)
44 self.filter = GrainFilter(self.s1, freq=self.ffr, q=self.fq, type=ftype)
45 self.out = SPan(self.filter.output, outs=nchnls, pan=self.pan)
46
47 class Module(BaseModule):
48 """
49 "Stochastic granular synthesis based on a soundfile"
50
51 Description
52
53 This module implements a stochastic granular synthesis where grains
54 coe from a given soundfile. The user has control over the range of
55 every generation parameters and envelopes.
56
57 Sliders
58
59 # Pitch Offset :
60 Base transposition, in semitones, applied to every grain
61 # Pitch Range :
62 Range, in semitone, over which grain transpositions are chosen randomly
63 # Speed Range :
64 Range, in second, over which grain start times are chosen randomly
65 # Duration Range :
66 Range, in second, over which grain durations are chosen randomly
67 # Start Range :
68 Range, in seconds, over which grain starting poistions (in the file)
69 are chosen randomly
70 # Intensity Range :
71 Range, in dB, over which grain amplitudes are chosen randomly
72 # Filter Freq Range :
73 Range over which filter cutoff or center frequencies are chosen randomly
74 # Filter Q Range :
75 Range over which filter Qsare chosen
76 # Pan Range :
77 Range over which grain spatial positions are chosen randomly
78 # Density :
79 Density of active grains, expressed as percent of the total generated grains
80 # Global Seed :
81 Root of stochatic generators. If 0, a new value is chosen randomly each
82 time the performance starts. Otherwise, the same root is used every
83 performance, making the generated sequences the same every time.
84
85 Graph Only
86
87 # Overall Amplitude :
88 The amplitude curve applied on the total duration of the performance
89 # Grain Envelope :
90 Amplitude envelope of each grain
91
92 Popups & Toggles
93
94 # Pitch Scaling :
95 Controls the possible values (as chords or scales) of the pitch generation
96 # Pitch Algorithm :
97 Noise distribution used by the pitch generator
98 # Speed Algorithm :
99 Noise distribution used by the speed generator
100 # Duration Algorithm :
101 Noise distribution used by the duration generator
102 # Intensity Algorithm :
103 Noise distribution used by the intensity generator
104 # Filter Type :
105 Type of the grain's filter (None, Lowpass, Highpass, Bandpass, Bandstop)
106 # Max Num of Grains :
107 Regardless the speed generation and the duration of each grain, there will
108 never be more overlapped grains than this value. The more CPU power you have,
109 higher this value can be.
110
111 """
112 def __init__(self):
113 BaseModule.__init__(self)
114 self.table = self.addFilein("snd")
115 self.table_dur = self.table.getDur()
116 self.setGlobalSeed(int(self.seed.get()))
117 self.num = int(self.numofvoices_value)
118 self.scaledict = {'Major':[0,4,7], 'Minor':[0,3,7], 'Seventh':[0,4,7,10], 'Minor 7':[0,3,7,10], 'Major 7':[0,4,7,11],
119 'Minor 7 b5':[0,3,6,10], 'Diminished':[0,3,6], 'Diminished 7':[0,3,6,9], 'Minor 9':[0,3,7,10,14],
120 'Major 9':[0,4,7,11,14], 'Ninth':[0,4,7,10,14], 'Minor 11':[0,3,7,10,14,17], 'Major 11':[0,4,7,11,14,18],
121 'Eleventh':[0,4,7,10,14,18], 'Major 13':[0,4,7,11,14,18,21], 'Thirteenth':[0,4,7,10,14,18,21],
122 'Serial':[0,1,2,3,4,5,6,7,8,9,10,11], 'Whole-tone': [0,2,4,6,8,10]}
123
124 self.speedgen = XnoiseDur(min=self.speed_rng[0], max=self.speed_rng[1])
125 self.new = Change(self.speedgen)
126 self.newpass = Percent(self.new, self.density)
127 self.count = VoiceManager(self.newpass)
128
129 self.pitfloat = TrigXnoise(self.newpass, mul=self.pitch[1]-self.pitch[0], add=self.pitch_off+self.pitch[0])
130 self.freq = MToT(self.pitfloat)
131 self.pitint = TrigXnoiseMidi(self.newpass, mrange=(0, 120), mul=0.007874015748031496)
132 self.pitch_range = self.pitch[1]-self.pitch[0]
133 self.scl = Snap(self.pitint*self.pitch_range+self.pitch[0]+self.pitch_off, choice=self.scaledict["Serial"], scale=2)
134 self.frtostack = Sig(self.freq)
135
136 self.mul_rng = DBToA(self.dbamp_rng)
137
138 self.stack = [GrainSnd(i, self.count, self.table, self.frtostack, self.start_rng, self.dur_rng, self.pan_rng,
139 self.ftype_index, self.ffreq_rng, self.fq_rng, self.mul_rng, self.grainenv, self.nchnls, self.table_dur) for i in range(self.num)]
140 self.stack_mix = Mix([gr.out for gr in self.stack], voices=self.nchnls)
141 self.out = Sig(self.stack_mix, mul=self.env)
142
143 self.count.setTriggers([obj.amp["trig"] for obj in self.stack])
144
145 self.speedalgo(self.speedalgo_index, self.speedalgo_value)
146 self.mulalgo(self.mulalgo_index, self.mulalgo_value)
147 self.duralgo(self.duralgo_index, self.duralgo_value)
148 self.pitalgo(self.pitalgo_index, self.pitalgo_value)
149 self.genmethod(self.genmethod_index, self.genmethod_value)
150
151 def assignX1X2(self, index, *args):
152 for arg in args:
153 arg.dist = index
154 if index in [4,5,6]:
155 arg.x1 = 8
156 elif index == 7:
157 arg.x1 = 2
158 elif index == 8:
159 arg.x1 = 0.5
160 arg.x2 = 3.2
161 elif index == 9:
162 arg.x1 = 0.5
163 arg.x2 = 1
164 elif index == 10:
165 arg.x1 = 3
166 arg.x2 = 2
167 elif index in [11,12]:
168 arg.x1 = 1
169 arg.x2 = .25
170
171 def speedalgo(self, index, value):
172 self.assignX1X2(index, self.speedgen)
173
174 def pitalgo(self, index, value):
175 self.assignX1X2(index, self.pitfloat, self.pitint)
176
177 def duralgo(self, index, value):
178 self.assignX1X2(index, *[obj.dur for obj in self.stack])
179
180 def mulalgo(self, index, value):
181 self.assignX1X2(index, *[obj.mul for obj in self.stack])
182
183 def genmethod(self, index, value):
184 if value == "All-over":
185 self.pitfloat.play()
186 self.freq.play()
187 self.frtostack.value = self.freq
188 self.pitint.stop()
189 self.scl.stop()
190 else:
191 self.scl.choice = self.scaledict[value]
192 self.pitint.play()
193 self.scl.play()
194 self.frtostack.value = self.scl
195 self.pitfloat.stop()
196 self.freq.stop()
197
198 def seed_up(self, value):
199 self.setGlobalSeed(int(value))
200
201 def ftype(self, index, value):
202 [obj.filter.setType(index) for obj in self.stack]
203
204 Interface = [ cfilein(name="snd", label="Audio"),
205 cgraph(name="env", label="Overall Amplitude", func=[(0,1),(1,1)], col="blue1"),
206 cgraph(name="grainenv", label="Grain Envelope", func=[(0,0),(.1,1),(.4,.8),(.7,.3),(1,0)], table=True, col="orange1"),
207 cslider(name="pitch_off", label="Pitch Offset", min=-12, max=12, init=0, rel="lin", res="int", unit="midi", col="red1"),
208 crange(name="pitch", label="Pitch Range", min=12, max=115, init=[48,72], rel="lin", unit="midi", col="red2"),
209 crange(name="speed_rng", label="Speed Range", min=0.0005, max=5, init=[.05, .25], rel="log", unit="sec", col="orange1"),
210 crange(name="dur_rng", label="Duration Range", min=0.001, max=10, init=[.25,2], rel="log", unit="sec", col="orange2"),
211 crange(name="start_rng", label="Start Range", min=0, max=1, init=[.1,.5], rel="lin", unit="%", col="purple1"),
212 crange(name="dbamp_rng", label="Intensity Range", min=-90, max=0, init=[-18,-6], rel="lin", unit="dB", col="blue2"),
213 crange(name="pan_rng", label="Pan Range", min=0, max=1, init=[0,1], rel="lin", unit="x", col="orange1"),
214 cslider(name="density", label="Density", min=0, max=100, init=100, rel="lin", unit="%", col="orange3"),
215 crange(name="ffreq_rng", label="Filter Freq Range", min=50, max=18000, init=[500, 5000], rel="log", unit="Hz", col="green1"),
216 crange(name="fq_rng", label="Filter Q Range", min=0.5, max=50, init=[1, 5], rel="log", unit="Q", col="green2"),
217 cslider(name="seed", label="Global seed", min=0, max=5000, init=0, rel="lin", res="int", unit="x", up=True),
218 cpopup(name="genmethod", label="Pitch Scaling", value=['All-over', 'Serial', 'Major', 'Minor', 'Seventh', 'Minor 7',
219 'Major 7', 'Minor 7 b5', 'Diminished', 'Diminished 7', 'Ninth', 'Major 9', 'Minor 9', 'Eleventh', 'Major 11',
220 'Minor 11', 'Thirteenth', 'Major 13', 'Whole-tone'], init="Major 11", col="red1"),
221 cpopup(name="pitalgo", label="Pitch Algorithm", value=['Uniform', 'Linear min', 'Linear max', 'Triangular',
222 'Expon min', 'Expon max', 'Bi-exponential', 'Cauchy', 'Weibull', 'Gaussian', 'Poisson', 'Walker', 'Loopseg'],
223 init="Uniform", col="red2"),
224 cpopup(name="speedalgo", label="Speed Algorithm", value=['Uniform', 'Linear min', 'Linear max', 'Triangular',
225 'Expon min', 'Expon max', 'Bi-exponential', 'Cauchy', 'Weibull', 'Gaussian', 'Poisson', 'Walker', 'Loopseg'],
226 init="Uniform", col="orange1"),
227 cpopup(name="duralgo", label="Duration Algorithm", value=['Uniform', 'Linear min', 'Linear max', 'Triangular',
228 'Expon min', 'Expon max', 'Bi-exponential', 'Cauchy', 'Weibull', 'Gaussian', 'Poisson', 'Walker', 'Loopseg'],
229 init="Uniform", col="orange2"),
230 cpopup(name="mulalgo", label="Intensity Algorithm", value=['Uniform', 'Linear min', 'Linear max', 'Triangular',
231 'Expon min', 'Expon max', 'Bi-exponential', 'Cauchy', 'Weibull', 'Gaussian', 'Poisson', 'Walker', 'Loopseg'],
232 init="Uniform", col="blue2"),
233 cpopup(name="ftype", label="Filter Type", value=['None', 'Lowpass', 'Highpass', 'Bandpass', 'Bandstop'], init="None", col="green1"),
234 cpopup(name="numofvoices", label="Max Num of Grains", value=['5','10','15','20','25','30','40','50','60'], init='10', rate="i")
235 ]
236