diff options
author | Felix Palmer <felix.palmer@metaswitch.com> | 2011-12-10 12:13:19 -0800 |
---|---|---|
committer | Felix Palmer <felix.palmer@metaswitch.com> | 2011-12-10 12:13:19 -0800 |
commit | d472d6db20e741c71f2f2be8cac2b16af5c96e7a (patch) | |
tree | 96a983ea154aef5fdd9ec463e73eae46f9602fc6 | |
parent | de21ddda49cf726af8aba8a645e2d677fb95e3e3 (diff) | |
download | android_external_android-visualizer-d472d6db20e741c71f2f2be8cac2b16af5c96e7a.tar.gz android_external_android-visualizer-d472d6db20e741c71f2f2be8cac2b16af5c96e7a.tar.bz2 android_external_android-visualizer-d472d6db20e741c71f2f2be8cac2b16af5c96e7a.zip |
Added CircleBarRenderer & changed All to Clear
-rw-r--r-- | res/layout/main.xml | 13 | ||||
-rw-r--r-- | src/com/pheelicks/app/MainActivity.java | 37 | ||||
-rw-r--r-- | src/com/pheelicks/visualizer/renderer/CircleBarRenderer.java | 121 |
3 files changed, 157 insertions, 14 deletions
diff --git a/res/layout/main.xml b/res/layout/main.xml index d8f2f72..bc323a8 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -47,6 +47,15 @@ android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="0.25" + android:onClick="circleBarPressed" + android:text="Circle Bar" > + </Button> + + <Button + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_margin="10dp" + android:layout_weight="0.25" android:onClick="linePressed" android:text="Line" > </Button> @@ -56,8 +65,8 @@ android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="0.25" - android:onClick="allPressed" - android:text="All" > + android:onClick="clearPressed" + android:text="Clear" > </Button> </LinearLayout> diff --git a/src/com/pheelicks/app/MainActivity.java b/src/com/pheelicks/app/MainActivity.java index 7d0f755..1b38f44 100644 --- a/src/com/pheelicks/app/MainActivity.java +++ b/src/com/pheelicks/app/MainActivity.java @@ -11,6 +11,8 @@ import java.io.IOException; import android.app.Activity; import android.graphics.Color; import android.graphics.Paint; +import android.graphics.PorterDuff.Mode; +import android.graphics.PorterDuffXfermode; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; @@ -18,6 +20,7 @@ import android.view.View; import com.pheelicks.visualizer.R; import com.pheelicks.visualizer.VisualizerView; import com.pheelicks.visualizer.renderer.BarGraphRenderer; +import com.pheelicks.visualizer.renderer.CircleBarRenderer; import com.pheelicks.visualizer.renderer.CircleRenderer; import com.pheelicks.visualizer.renderer.LineRenderer; @@ -99,13 +102,24 @@ public class MainActivity extends Activity { mVisualizerView.addRenderer(barGraphRendererTop); } + private void addCircleBarRenderer() + { + Paint paint = new Paint(); + paint.setStrokeWidth(8f); + paint.setAntiAlias(true); + paint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN)); + paint.setColor(Color.argb(255, 222, 92, 143)); + CircleBarRenderer circleBarRenderer = new CircleBarRenderer(paint, 32, true); + mVisualizerView.addRenderer(circleBarRenderer); + } + private void addCircleRenderer() { - Paint paint3 = new Paint(); - paint3.setStrokeWidth(3f); - paint3.setAntiAlias(true); - paint3.setColor(Color.argb(255, 222, 92, 143)); - CircleRenderer circleRenderer = new CircleRenderer(paint3, true); + Paint paint = new Paint(); + paint.setStrokeWidth(3f); + paint.setAntiAlias(true); + paint.setColor(Color.argb(255, 222, 92, 143)); + CircleRenderer circleRenderer = new CircleRenderer(paint, true); mVisualizerView.addRenderer(circleRenderer); } @@ -142,27 +156,26 @@ public class MainActivity extends Activity { public void barPressed(View view) { - mVisualizerView.clearRenderers(); addBarGraphRenderers(); } public void circlePressed(View view) { - mVisualizerView.clearRenderers(); addCircleRenderer(); } + public void circleBarPressed(View view) + { + addCircleBarRenderer(); + } + public void linePressed(View view) { - mVisualizerView.clearRenderers(); addLineRenderer(); } - public void allPressed(View view) + public void clearPressed(View view) { mVisualizerView.clearRenderers(); - addBarGraphRenderers(); - addCircleRenderer(); - addLineRenderer(); } }
\ No newline at end of file diff --git a/src/com/pheelicks/visualizer/renderer/CircleBarRenderer.java b/src/com/pheelicks/visualizer/renderer/CircleBarRenderer.java new file mode 100644 index 0000000..ff1e7d5 --- /dev/null +++ b/src/com/pheelicks/visualizer/renderer/CircleBarRenderer.java @@ -0,0 +1,121 @@ +/** + * Copyright 2011, Felix Palmer + * + * Licensed under the MIT license: + * http://creativecommons.org/licenses/MIT/ + */ +package com.pheelicks.visualizer.renderer; + +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Rect; + +import com.pheelicks.visualizer.AudioData; +import com.pheelicks.visualizer.FFTData; + +public class CircleBarRenderer extends Renderer +{ + private int mDivisions; + private Paint mPaint; + private boolean mCycleColor; + + /** + * Renders the FFT data onto a pulsing, rotating circle + * @param canvas + * @param paint - Paint to draw lines with + */ + public CircleBarRenderer(Paint paint, int divisions) + { + this(paint, divisions, false); + } + + /** + * Renders the audio data onto a pulsing circle + * @param canvas + * @param paint - Paint to draw lines with + * @param divisions - must be a power of 2. Controls how many lines to draw + * @param cycleColor - If true the color will change on each frame + */ + public CircleBarRenderer(Paint paint, int divisions, boolean cycleColor) + { + super(); + mPaint = paint; + mDivisions = divisions; + mCycleColor = cycleColor; + } + + @Override + public void onRender(Canvas canvas, AudioData data, Rect rect) + { + // Do nothing, we only display FFT data + } + + @Override + public void onRender(Canvas canvas, FFTData data, Rect rect) + { + if(mCycleColor) + { + cycleColor(); + } + + for (int i = 0; i < data.bytes.length / mDivisions; i++) { + // Calculate dbValue + byte rfk = data.bytes[mDivisions * i]; + byte ifk = data.bytes[mDivisions * i + 1]; + float magnitude = (rfk * rfk + ifk * ifk); + float dbValue = 75 * (float)Math.log10(magnitude); + + float[] cartPoint = { + (float)(i * mDivisions) / (data.bytes.length - 1), + rect.height() / 2 - dbValue / 4 + }; + + float[] polarPoint = toPolar(cartPoint, rect); + mFFTPoints[i * 4] = polarPoint[0]; + mFFTPoints[i * 4 + 1] = polarPoint[1]; + + float[] cartPoint2 = { + (float)(i * mDivisions) / (data.bytes.length - 1), + rect.height() / 2 + dbValue + }; + + float[] polarPoint2 = toPolar(cartPoint2, rect); + mFFTPoints[i * 4 + 2] = polarPoint2[0]; + mFFTPoints[i * 4 + 3] = polarPoint2[1]; + } + + canvas.drawLines(mFFTPoints, mPaint); + + // Controls the pulsing rate + modulation += 0.13; + angleModulation += 0.28; + } + + float modulation = 0; + float modulationStrength = 0.4f; // 0-1 + float angleModulation = 0; + float aggresive = 0.4f; + private float[] toPolar(float[] cartesian, Rect rect) + { + double cX = rect.width()/2; + double cY = rect.height()/2; + double angle = (cartesian[0]) * 2 * Math.PI; + double radius = ((rect.width()/2) * (1 - aggresive) + aggresive * cartesian[1]/2) * ((1 - modulationStrength) + modulationStrength * (1 + Math.sin(modulation)) / 2); + float[] out = { + (float)(cX + radius * Math.sin(angle + angleModulation)), + (float)(cY + radius * Math.cos(angle + angleModulation)) + }; + return out; + } + + private float colorCounter = 0; + private void cycleColor() + { + int r = (int)Math.floor(128*(Math.sin(colorCounter) + 1)); + int g = (int)Math.floor(128*(Math.sin(colorCounter + 2) + 1)); + int b = (int)Math.floor(128*(Math.sin(colorCounter + 4) + 1)); + mPaint.setColor(Color.argb(128, r, g, b)); + colorCounter += 0.03; + } +} |