From: Toby Segaran Date: Mon, 12 Aug 2013 00:35:15 +0000 (-0700) Subject: Efficiency X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=10692893d2872a618a79ebc58d0d83e3cf7ae420;p=SugarCubes.git Efficiency --- diff --git a/SugarCubes.pde b/SugarCubes.pde index aa794b4..ca6a1c8 100644 --- a/SugarCubes.pde +++ b/SugarCubes.pde @@ -37,6 +37,7 @@ LXPattern[] patterns(GLucose glucose) { new GlitchPlasma(glucose), new FireEffect(glucose), new StripBounce(glucose), + new SoundCubes(glucose), //new SineSphere(glucose), // Basic test patterns for reference, not art diff --git a/TobySegaran.pde b/TobySegaran.pde index 722f4d4..2eba4cf 100644 --- a/TobySegaran.pde +++ b/TobySegaran.pde @@ -128,10 +128,41 @@ class StripBounce extends SCPattern { } } } - for (Point p : model.points) { - if (bright[p.index]==0) { - colors[p.index]=color(0,0,0); + } +} + +class SoundCubes extends SCPattern { + + private FFT fft = null; + private LinearEnvelope[] bandVals = null; + private int avgSize; + + public SoundCubes(GLucose glucose) { + super(glucose); + } + + 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(); } } } + + 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(); + } + for (Point p : model.points) { + colors[p.index] = color(100,0,bandVals[1].getValuef()*25 ); + } + } }