diff options
author | Felix Palmer <felix.palmer@metaswitch.com> | 2011-12-02 23:46:31 -0800 |
---|---|---|
committer | Felix Palmer <felix.palmer@metaswitch.com> | 2011-12-02 23:46:31 -0800 |
commit | 8d8499861a5eb6f238616b11cf5f7da0175efa90 (patch) | |
tree | 9dac096c4e7c8b634c5db10a8f7c013da7430c3a | |
parent | 6fa6928b1e07f0004b05a5aed1634b0e43096054 (diff) | |
download | android_external_android-visualizer-8d8499861a5eb6f238616b11cf5f7da0175efa90.tar.gz android_external_android-visualizer-8d8499861a5eb6f238616b11cf5f7da0175efa90.tar.bz2 android_external_android-visualizer-8d8499861a5eb6f238616b11cf5f7da0175efa90.zip |
Created circle renderer
-rw-r--r-- | src/com/pheelicks/visualizer/CircleRenderer.java | 72 | ||||
-rw-r--r-- | src/com/pheelicks/visualizer/VisualizerView.java | 49 |
2 files changed, 80 insertions, 41 deletions
diff --git a/src/com/pheelicks/visualizer/CircleRenderer.java b/src/com/pheelicks/visualizer/CircleRenderer.java new file mode 100644 index 0000000..2f73524 --- /dev/null +++ b/src/com/pheelicks/visualizer/CircleRenderer.java @@ -0,0 +1,72 @@ +package com.pheelicks.visualizer; + +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Rect; + +public class CircleRenderer extends Renderer +{ + private Paint mPaint; + + /** + * Renders the audio data onto a pulsing circle + * @param canvas + * @param paint - Paint to draw lines with + */ + public CircleRenderer(Canvas canvas, + Paint paint) + { + super(canvas); + mPaint = paint; + } + + @Override + public void onRender(AudioData data, Rect rect) + { + for (int i = 0; i < data.bytes.length - 1; i++) { + float[] cartPoint = { + (float)i / (data.bytes.length - 1), + rect.height() / 2 + ((byte) (data.bytes[i] + 128)) * (rect.height() / 2) / 128 + }; + + float[] polarPoint = toPolar(cartPoint, rect); + mPoints[i * 4] = polarPoint[0]; + mPoints[i * 4 + 1] = polarPoint[1]; + + float[] cartPoint2 = { + (float)(i + 1) / (data.bytes.length - 1), + rect.height() / 2 + ((byte) (data.bytes[i + 1] + 128)) * (rect.height() / 2) / 128 + }; + + float[] polarPoint2 = toPolar(cartPoint2, rect); + mPoints[i * 4 + 2] = polarPoint2[0]; + mPoints[i * 4 + 3] = polarPoint2[1]; + } + + mCanvas.drawLines(mPoints, mPaint); + + // Controls the pulsing rate + modulation += 0.04; + } + + @Override + public void onRender(FFTData data, Rect rect) + { + // Do nothing, we only display audio data + } + + float modulation = 0; + float aggresive = 0.33f; + 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.2 + Math.sin(modulation))/2.2; + float[] out = { + (float)(cX + radius * Math.sin(angle)), + (float)(cY + radius * Math.cos(angle)) + }; + return out; + } +} diff --git a/src/com/pheelicks/visualizer/VisualizerView.java b/src/com/pheelicks/visualizer/VisualizerView.java index 82d7100..82cba62 100644 --- a/src/com/pheelicks/visualizer/VisualizerView.java +++ b/src/com/pheelicks/visualizer/VisualizerView.java @@ -21,7 +21,7 @@ import android.util.AttributeSet; import android.view.View; /** - * A class that draws waveform data received from a + * A class that draws visualizations of data received from a * {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture } */ class VisualizerView extends View { @@ -30,7 +30,6 @@ class VisualizerView extends View { private byte[] mBytes; private byte[] mFFTBytes; private float[] mPoints; - private float[] mFFTPoints; private Rect mRect = new Rect(); private Paint mCirclePaint = new Paint(); @@ -125,6 +124,7 @@ class VisualizerView extends View { BarGraphRenderer mBarGraphRendererTop; BarGraphRenderer mBarGraphRendererBottom; + CircleRenderer mCircleRenderer; @Override protected void onDraw(Canvas canvas) { @@ -140,25 +140,6 @@ class VisualizerView extends View { mRect.set(0, 0, getWidth(), getHeight()); - for (int i = 0; i < mBytes.length - 1; i++) { - float[] cartPoint = { - (float)i / (mBytes.length - 1), - mRect.height() / 2 + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128 - }; - - float[] polarPoint = toPolar(cartPoint); - mPoints[i * 4] = polarPoint[0]; - mPoints[i * 4 + 1] = polarPoint[1]; - - float[] cartPoint2 = { - (float)(i + 1) / (mBytes.length - 1), - mRect.height() / 2 + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128 - }; - - float[] polarPoint2 = toPolar(cartPoint2); - mPoints[i * 4 + 2] = polarPoint2[0]; - mPoints[i * 4 + 3] = polarPoint2[1]; - } if(mCanvasBitmap == null) { @@ -178,10 +159,9 @@ class VisualizerView extends View { paint2.setAntiAlias(true); paint2.setColor(Color.argb(200, 11, 111, 233)); mBarGraphRendererTop = new BarGraphRenderer(mCanvas, 4, paint2, true); - } - - mCanvas.drawLines(mPoints, mCirclePaint); + mCircleRenderer = new CircleRenderer(mCanvas, paint2); + } // Draw normal line - offset by amplitude for (int i = 0; i < mBytes.length - 1; i++) { @@ -212,6 +192,10 @@ class VisualizerView extends View { mCanvas.drawLines(mPoints, mLinePaint); } + + AudioData audioData = new AudioData(mBytes); + mCircleRenderer.render(audioData, mRect); + // FFT time!!!! if (mFFTBytes == null) { return; @@ -237,23 +221,6 @@ class VisualizerView extends View { } canvas.drawBitmap(mCanvasBitmap, new Matrix(), null); - modulation += 0.04; } - float modulation = 0; - float aggresive = 0.33f; - private float[] toPolar(float[] cartesian) - { - double cX = mRect.width()/2; - double cY = mRect.height()/2; - double angle = (cartesian[0]) * 2 * Math.PI; - double radius = ((mRect.width()/2) * (1 - aggresive) + aggresive * cartesian[1]/2) * (1.2 + Math.sin(modulation))/2.2; - float[] out = { - (float)(cX + radius * Math.sin(angle)), - (float)(cY + radius * Math.cos(angle)) - }; - return out; - } - - }
\ No newline at end of file |