Switch to threaded mode by default, run engine at 120FPS
[SugarCubes.git] / TobySegaran.pde
1 class GlitchPlasma extends SCPattern {
2 private int pos = 0;
3 private float satu = 100;
4 private float speed = 1;
5 private float glitch = 0;
6 BasicParameter saturationParameter = new BasicParameter("SATU", 1.0);
7 BasicParameter speedParameter = new BasicParameter("SPEED", 0.1);
8 BasicParameter glitchParameter = new BasicParameter("GLITCH", 0.0);
9
10 public GlitchPlasma(LX lx) {
11 super(lx);
12 addParameter(saturationParameter);
13 addParameter(speedParameter);
14 addParameter(glitchParameter);
15 }
16 public void onParameterChanged(LXParameter parameter) {
17 if (parameter == saturationParameter) {
18 satu = 100*parameter.getValuef();
19 } else if (parameter == speedParameter) {
20 speed = 8*parameter.getValuef();
21 } else if (parameter == glitchParameter) {
22 glitch = parameter.getValuef();
23 }
24 }
25
26 public void run(double deltaMs) {
27 for (LXPoint p : model.points) {
28 float hv = sin(dist(p.x + pos, p.y, 128.0, 128.0) / 8.0)
29 + sin(dist(p.x, p.y, 64.0, 64.0) / 8.0)
30 + sin(dist(p.x, p.y + pos / 7, 192.0, 64.0) / 7.0)
31 + sin(dist(p.x, p.z + pos, 192.0, 100.0) / 8.0);
32 float bv = 100;
33 colors[p.index] = lx.hsb((hv+2)*50, satu, bv);
34 }
35 if (random(1.0)<glitch/20) {
36 pos=pos-int(random(10,30));
37 }
38 pos+=speed;
39 if (pos >= MAX_INT-1) pos=0;
40 }
41 }
42
43 // This is very much a work in progress. Trying to get a flame effect.
44 class FireEffect extends SCPattern {
45 private float[][] intensity;
46 private float hotspot;
47 private float decay = 0.3;
48 private int xm;
49 private int ym;
50 BasicParameter decayParameter = new BasicParameter("DECAY", 0.3);
51
52 public FireEffect(LX lx) {
53 super(lx);
54 xm = int(model.xMax);
55 ym = int(model.yMax);
56
57 intensity = new float[xm][ym];
58 addParameter(decayParameter);
59 }
60 public void onParameterChanged(LXParameter parameter) {
61 if (parameter == decayParameter) {
62 decay = parameter.getValuef();
63 }
64 }
65 private color flameColor(float level) {
66 if (level<=0) return lx.hsb(0,0,0);
67 float br=min(100,sqrt(level)*15);
68 return lx.hsb(level/1.7,100,br);
69 }
70 public void run(double deltaMs) {
71 for (int x=10;x<xm-10;x++) {
72 if (x%50>45 || x%50<5) {
73 intensity[x][ym-1] = random(30,100);
74 } else {
75 intensity[x][ym-1] = random(0,50);
76 }
77 }
78 for (int x=1;x<xm-1;x++) {
79 for (int y=0;y<ym-1;y++) {
80 intensity[x][y] = (intensity[x-1][y+1]+intensity[x][y+1]+intensity[x+1][y+1])/3-decay;
81 }
82 }
83
84 for (LXPoint p : model.points) {
85 int x = max(0,(int(p.x)+int(p.z))%xm);
86 int y = constrain(ym-int(p.y),0,ym-1);
87 colors[p.index] = flameColor(intensity[x][y]);
88 }
89 }
90 }
91
92 class StripBounce extends SCPattern {
93 private final int numOsc = 30;
94 SinLFO[] fX = new SinLFO[numOsc]; //new SinLFO(0, model.xMax, 5000);
95 SinLFO[] fY = new SinLFO[numOsc]; //new SinLFO(0, model.yMax, 4000);
96 SinLFO[] fZ = new SinLFO[numOsc]; //new SinLFO(0, model.yMax, 3000);
97 SinLFO[] sat = new SinLFO[numOsc];
98 float[] colorOffset = new float[numOsc];
99
100 public StripBounce(LX lx) {
101 super(lx);
102 for (int i=0;i<numOsc;i++) {
103 fX[i] = new SinLFO(0, model.xMax, random(2000,20000));
104 fY[i] = new SinLFO(0, model.yMax, random(2000,20000));
105 fZ[i] = new SinLFO(0, model.zMax, random(2000,20000));
106 sat[i] = new SinLFO(60, 100, random(2000,50000));
107 addModulator(fX[i]).trigger();
108 addModulator(fY[i]).trigger();
109 addModulator(fZ[i]).trigger();
110 colorOffset[i]=random(0,256);
111 }
112 }
113
114 public void run(double deltaMs) {
115 float[] bright = new float[model.points.size()];
116 for (Strip strip : model.strips) {
117 for (int i=0;i<numOsc;i++) {
118 float avgdist=0.0;
119 avgdist = dist(strip.points.get(8).x,strip.points.get(8).y,strip.points.get(8).z,fX[i].getValuef(),fY[i].getValuef(),fZ[i].getValuef());
120 boolean on = avgdist<30;
121 float hv = (lx.getBaseHuef()+colorOffset[i])%360;
122 float br = max(0,100-avgdist*4);
123 for (LXPoint p : strip.points) {
124 if (on && br>bright[p.index]) {
125 colors[p.index] = lx.hsb(hv,sat[i].getValuef(),br);
126 bright[p.index] = br;
127 }
128 }
129 }
130 }
131 }
132 }
133
134 class SoundRain extends SCPattern {
135
136 private FFT fft = null;
137 private LinearEnvelope[] bandVals = null;
138 private float[] lightVals = null;
139 private int avgSize;
140 private float gain = 25;
141 SawLFO pos = new SawLFO(0, 9, 8000);
142 SinLFO col1 = new SinLFO(0, model.xMax, 5000);
143 BasicParameter gainParameter = new BasicParameter("GAIN", 0.5);
144
145 public SoundRain(LX lx) {
146 super(lx);
147 addModulator(pos).trigger();
148 addModulator(col1).trigger();
149 addParameter(gainParameter);
150 }
151
152 public void onParameterChanged(LXParameter parameter) {
153 if (parameter == gainParameter) {
154 gain = 50*parameter.getValuef();
155 }
156 }
157 void onActive() {
158 if (this.fft == null) {
159 this.fft = new FFT(lx.audioInput().bufferSize(), lx.audioInput().sampleRate());
160 this.fft.window(FFT.HAMMING);
161 this.fft.logAverages(40, 1);
162 this.avgSize = this.fft.avgSize();
163 this.bandVals = new LinearEnvelope[this.avgSize];
164 for (int i = 0; i < this.bandVals.length; ++i) {
165 this.addModulator(this.bandVals[i] = (new LinearEnvelope(0, 0, 700+i*4))).trigger();
166 }
167 lightVals = new float[avgSize];
168 }
169 }
170
171 public void run(double deltaMs) {
172 this.fft.forward(this.lx.audioInput().mix);
173 for (int i = 0; i < avgSize; ++i) {
174 float value = this.fft.getAvg(i);
175 this.bandVals[i].setEndVal(value,40).trigger();
176 float lv = min(value*gain,100);
177 if (lv>lightVals[i]) {
178 lightVals[i]=min(lightVals[i]+15,lv,100);
179 } else {
180 lightVals[i]=max(lv,lightVals[i]-5,0);
181 }
182 }
183 for (Cube c : model.cubes) {
184 for (int j=0; j<c.strips.size(); j++) {
185 Strip s = c.strips.get(j);
186 if (j%4!=0 && j%4!=2) {
187 for (LXPoint p : s.points) {
188 int seq = int(p.y*avgSize/model.yMax+pos.getValuef()+sin(p.x+p.z)*2)%avgSize;
189 seq=min(abs(seq-(avgSize/2)),avgSize-1);
190 colors[p.index] = lx.hsb(200,max(0,100-abs(p.x-col1.getValuef())/2),lightVals[seq]);
191 }
192 }
193 }
194 }
195 }
196 }
197
198 class FaceSync extends SCPattern {
199 SinLFO xosc = new SinLFO(-10, 10, 3000);
200 SinLFO zosc = new SinLFO(-10, 10, 3000);
201 SinLFO col1 = new SinLFO(0, model.xMax, 5000);
202 SinLFO col2 = new SinLFO(0, model.xMax, 4000);
203
204 public FaceSync(LX lx) {
205 super(lx);
206 addModulator(xosc).trigger();
207 addModulator(zosc).trigger();
208 zosc.setValue(0);
209 addModulator(col1).trigger();
210 addModulator(col2).trigger();
211 col2.setValue(model.xMax);
212 }
213
214 public void run(double deltaMs) {
215 int i=0;
216 for (Strip s : model.strips) {
217 i++;
218 for (LXPoint p : s.points) {
219 float dx, dz;
220 if (i%32 < 16) {
221 dx = p.x - (s.cx+xosc.getValuef());
222 dz = p.z - (s.cz+zosc.getValuef());
223 } else {
224 dx = p.x - (s.cx+zosc.getValuef());
225 dz = p.z - (s.cz+xosc.getValuef());
226 }
227 //println(dx);
228 float a1=max(0,100-abs(p.x-col1.getValuef()));
229 float a2=max(0,100-abs(p.x-col2.getValuef()));
230 float sat = max(a1,a2);
231 float h = (359*a1+200*a2) / (a1+a2);
232 colors[p.index] = lx.hsb(h,sat,100-abs(dx*5)-abs(dz*5));
233 }
234 }
235 }
236 }
237
238 class SoundSpikes extends SCPattern {
239 private FFT fft = null;
240 private LinearEnvelope[] bandVals = null;
241 private float[] lightVals = null;
242 private int avgSize;
243 private float gain = 25;
244 BasicParameter gainParameter = new BasicParameter("GAIN", 0.5);
245 SawLFO pos = new SawLFO(0, model.xMax, 8000);
246
247 public SoundSpikes(LX lx) {
248 super(lx);
249 addParameter(gainParameter);
250 addModulator(pos).trigger();
251 }
252
253 public void onParameterChanged(LXParameter parameter) {
254 if (parameter == gainParameter) {
255 gain = 50*parameter.getValuef();
256 }
257 }
258 void onActive() {
259 if (this.fft == null) {
260 this.fft = new FFT(lx.audioInput().bufferSize(), lx.audioInput().sampleRate());
261 this.fft.window(FFT.HAMMING);
262 this.fft.logAverages(40, 1);
263 this.avgSize = this.fft.avgSize();
264 this.bandVals = new LinearEnvelope[this.avgSize];
265 for (int i = 0; i < this.bandVals.length; ++i) {
266 this.addModulator(this.bandVals[i] = (new LinearEnvelope(0, 0, 700+i*4))).trigger();
267 }
268 lightVals = new float[avgSize];
269 }
270 }
271
272 public void run(double deltaMs) {
273 this.fft.forward(this.lx.audioInput().mix);
274 for (int i = 0; i < avgSize; ++i) {
275 float value = this.fft.getAvg(i);
276 this.bandVals[i].setEndVal(value,40).trigger();
277 float lv = min(value*gain,model.yMax+10);
278 if (lv>lightVals[i]) {
279 lightVals[i]=min(lightVals[i]+30,lv,model.yMax+10);
280 } else {
281 lightVals[i]=max(lv,lightVals[i]-10,0);
282 }
283 }
284 int i = 0;
285 for (Cube c : model.cubes) {
286 for (int j=0; j<c.strips.size(); j++) {
287 Strip s = c.strips.get(j);
288 if (j%4!=0 && j%4!=2) {
289 for (LXPoint p : s.points) {
290 float dis = (abs(p.x-model.xMax/2)+pos.getValuef())%model.xMax/2;
291 int seq = int((dis*avgSize*2)/model.xMax);
292 if (seq>avgSize) seq=avgSize-seq;
293 seq=constrain(seq,0,avgSize-1);
294 float br=max(0, lightVals[seq]-p.y);
295 colors[p.index] = lx.hsb((dis*avgSize*65)/model.xMax,90,br);
296 }
297 }
298 }
299 }
300 }
301 }
302