From: Toby Segaran Date: Sat, 17 Aug 2013 06:00:23 +0000 (-0700) Subject: Added SoundSpikes X-Git-Url: https://git.piment-noir.org/?p=SugarCubes.git;a=commitdiff_plain;h=f5d9de39b45481476cfa2a34d30aa8c6578f7d9c Added SoundSpikes --- diff --git a/SugarCubes.pde b/SugarCubes.pde index 8a8b46b..0f102e3 100644 --- a/SugarCubes.pde +++ b/SugarCubes.pde @@ -41,6 +41,7 @@ LXPattern[] patterns(GLucose glucose) { new FireEffect(glucose), new StripBounce(glucose), new SoundRain(glucose), + new SoundSpikes(glucose), new FaceSync(glucose), // Basic test patterns for reference, not art diff --git a/TobySegaran.pde b/TobySegaran.pde index 0358668..c5e19d2 100644 --- a/TobySegaran.pde +++ b/TobySegaran.pde @@ -175,7 +175,7 @@ class SoundRain extends SCPattern { this.bandVals[i].setEndVal(value,40).trigger(); float lv = min(value*gain,100); if (lv>lightVals[i]) { - lightVals[i]=min(lightVals[i]+10,lv,100); + lightVals[i]=min(lightVals[i]+15,lv,100); } else { lightVals[i]=max(lv,lightVals[i]-5,0); } @@ -234,3 +234,69 @@ class FaceSync extends SCPattern { } } } + +class SoundSpikes extends SCPattern { + private FFT fft = null; + private LinearEnvelope[] bandVals = null; + private float[] lightVals = null; + private int avgSize; + private float gain = 25; + BasicParameter gainParameter = new BasicParameter("GAIN", 0.5); + SawLFO pos = new SawLFO(0, model.xMax, 8000); + + public SoundSpikes(GLucose glucose) { + super(glucose); + addParameter(gainParameter); + addModulator(pos).trigger(); + } + + public void onParameterChanged(LXParameter parameter) { + if (parameter == gainParameter) { + gain = 50*parameter.getValuef(); + } + } + protected void onActive() { + if (this.fft == null) { + this.fft = new FFT(lx.audioInput().bufferSize(), lx.audioInput().sampleRate()); + this.fft.window(FFT.HAMMING); + this.fft.logAverages(40, 1); + this.avgSize = this.fft.avgSize(); + this.bandVals = new LinearEnvelope[this.avgSize]; + for (int i = 0; i < this.bandVals.length; ++i) { + this.addModulator(this.bandVals[i] = (new LinearEnvelope(0, 0, 700+i*4))).trigger(); + } + lightVals = new float[avgSize]; + } + } + + public void run(int deltaMs) { + this.fft.forward(this.lx.audioInput().mix); + for (int i = 0; i < avgSize; ++i) { + float value = this.fft.getAvg(i); + this.bandVals[i].setEndVal(value,40).trigger(); + float lv = min(value*gain,model.yMax+10); + if (lv>lightVals[i]) { + lightVals[i]=min(lightVals[i]+30,lv,model.yMax+10); + } else { + lightVals[i]=max(lv,lightVals[i]-10,0); + } + } + int i = 0; + for (Cube c : model.cubes) { + for (int j=0; javgSize) seq=avgSize-seq; + seq=constrain(seq,0,avgSize-1); + float br=max(0, lightVals[seq]-p.fy); + colors[p.index] = color((dis*avgSize*65)/model.xMax,90,br); + } + } + } + } + } +} +