diff options
-rw-r--r-- | src/com/pheelicks/visualizer/LineRenderer.java | 104 | ||||
-rw-r--r-- | src/com/pheelicks/visualizer/VisualizerView.java | 68 |
2 files changed, 116 insertions, 56 deletions
diff --git a/src/com/pheelicks/visualizer/LineRenderer.java b/src/com/pheelicks/visualizer/LineRenderer.java new file mode 100644 index 0000000..201a0a5 --- /dev/null +++ b/src/com/pheelicks/visualizer/LineRenderer.java @@ -0,0 +1,104 @@ +package com.pheelicks.visualizer; + +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Rect; + +public class LineRenderer extends Renderer +{ + private Paint mPaint; + private Paint mFlashPaint; + private boolean mCycleColor; + private float amplitude = 0; + + + /** + * Renders the audio data onto a line. The line flashes on prominent beats + * @param canvas + * @param paint - Paint to draw lines with + * @param paint - Paint to draw flash with + */ + public LineRenderer(Canvas canvas, + Paint paint, + Paint flashPaint) + { + this(canvas, + paint, + flashPaint, + false); + } + + /** + * Renders the audio data onto a line. The line flashes on prominent beats + * @param canvas + * @param paint - Paint to draw lines with + * @param paint - Paint to draw flash with + * @param cycleColor - If true the color will change on each frame + */ + public LineRenderer(Canvas canvas, + Paint paint, + Paint flashPaint, + boolean cycleColor) + { + super(canvas); + mPaint = paint; + mFlashPaint = flashPaint; + mCycleColor = cycleColor; + } + + @Override + public void onRender(AudioData data, Rect rect) + { + if(mCycleColor) + { + cycleColor(); + } + + // Calculate points for line + for (int i = 0; i < data.bytes.length - 1; i++) { + mPoints[i * 4] = rect.width() * i / (data.bytes.length - 1); + mPoints[i * 4 + 1] = rect.height() / 2 + + ((byte) (data.bytes[i] + 128)) * (rect.height() / 3) / 128; + mPoints[i * 4 + 2] = rect.width() * (i + 1) / (data.bytes.length - 1); + mPoints[i * 4 + 3] = rect.height() / 2 + + ((byte) (data.bytes[i + 1] + 128)) * (rect.height() / 3) / 128; + } + + // Calc amplitude for this waveform + float accumulator = 0; + for (int i = 0; i < data.bytes.length - 1; i++) { + accumulator += Math.abs(data.bytes[i]); + } + + float amp = accumulator/(128 * data.bytes.length); + if(amp > amplitude) + { + // Amplitude is bigger than normal, make a prominent line + amplitude = amp; + mCanvas.drawLines(mPoints, mFlashPaint); + } + else + { + // Amplitude is nothing special, reduce the amplitude + amplitude *= 0.99; + mCanvas.drawLines(mPoints, mPaint); + } + } + + @Override + public void onRender(FFTData data, Rect rect) + { + // Do nothing, we only display audio data + } + + private float colorCounter = 0; + private void cycleColor() + { + int r = (int)Math.floor(128*(Math.sin(colorCounter) + 3)); + int g = (int)Math.floor(128*(Math.sin(colorCounter + 1) + 1)); + int b = (int)Math.floor(128*(Math.sin(colorCounter + 7) + 1)); + mPaint.setColor(Color.argb(128, r, g, b)); + colorCounter += 0.03; + } +} diff --git a/src/com/pheelicks/visualizer/VisualizerView.java b/src/com/pheelicks/visualizer/VisualizerView.java index 0947fa4..5a80797 100644 --- a/src/com/pheelicks/visualizer/VisualizerView.java +++ b/src/com/pheelicks/visualizer/VisualizerView.java @@ -3,8 +3,6 @@ package com.pheelicks.visualizer; // WARNING!!! This file has more magic numbers in it than you could shake a // stick at -import java.util.Random; - import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; @@ -58,14 +56,6 @@ public class VisualizerView extends View { private void init() { mBytes = null; - mLinePaint.setStrokeWidth(1f); - mLinePaint.setAntiAlias(true); - mLinePaint.setColor(Color.argb(88, 0, 128, 255)); - - mSpecialLinePaint.setStrokeWidth(5f); - mSpecialLinePaint.setAntiAlias(true); - mSpecialLinePaint.setColor(Color.argb(188, 255, 255, 255)); - mProgressLinePaint.setStrokeWidth(4f); mProgressLinePaint.setAntiAlias(true); mProgressLinePaint.setColor(Color.argb(255, 22, 131, 255)); @@ -79,7 +69,6 @@ public class VisualizerView extends View { public void updateVisualizer(byte[] bytes) { mBytes = bytes; - rotateColours(); invalidate(); } @@ -100,25 +89,13 @@ public class VisualizerView extends View { invalidate(); } - - float colorCounter = 0; - private void rotateColours() - { - 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)); - mLinePaint.setColor(Color.argb(128, r, g, b)); - colorCounter += 0.03; - } - Bitmap mCanvasBitmap; Canvas mCanvas; - Random mRandom = new Random(); - float amplitude = 0; BarGraphRenderer mBarGraphRendererTop; BarGraphRenderer mBarGraphRendererBottom; CircleRenderer mCircleRenderer; + LineRenderer mLineRenderer; @Override protected void onDraw(Canvas canvas) { @@ -158,42 +135,26 @@ public class VisualizerView extends View { paint3.setStrokeWidth(3f); paint3.setAntiAlias(true); paint3.setColor(Color.argb(255, 222, 92, 143)); - mCircleRenderer = new CircleRenderer(mCanvas, paint3, true); - } - // Draw normal line - offset by amplitude - for (int i = 0; i < mBytes.length - 1; i++) { - mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1); - mPoints[i * 4 + 1] = mRect.height() / 2 - + ((byte) (mBytes[i] + 128)) * (mRect.height() / 3) / 128; - mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1); - mPoints[i * 4 + 3] = mRect.height() / 2 - + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 3) / 128; - } + Paint linePaint = new Paint(); + linePaint.setStrokeWidth(1f); + linePaint.setAntiAlias(true); + linePaint.setColor(Color.argb(88, 0, 128, 255)); - // Calc amplitude for this waveform - float accumulator = 0; - for (int i = 0; i < mBytes.length - 1; i++) { - accumulator += Math.abs(mBytes[i]); + Paint lineFlashPaint = new Paint(); + lineFlashPaint.setStrokeWidth(5f); + lineFlashPaint.setAntiAlias(true); + lineFlashPaint.setColor(Color.argb(188, 255, 255, 255)); + mLineRenderer = new LineRenderer(mCanvas, linePaint, lineFlashPaint, true); } - float amp = accumulator/(128 * mBytes.length); - if(amp > amplitude) - { - amplitude = amp; - // Occassionally, make a prominent line - mCanvas.drawLines(mPoints, mSpecialLinePaint); - } - else - { - amplitude *= 0.99; - mCanvas.drawLines(mPoints, mLinePaint); - } + AudioData audioData = new AudioData(mBytes); mCircleRenderer.render(audioData, mRect); + mLineRenderer.render(audioData, mRect); // FFT time!!!! if (mFFTBytes == null) { @@ -205,11 +166,6 @@ public class VisualizerView extends View { mBarGraphRendererTop.render(fftData, mRect); mBarGraphRendererBottom.render(fftData, mRect); - // We totally need a thing moving along the bottom - float cX = mRect.width()*(SystemClock.currentThreadTimeMillis() - mFlashTime)/mFlashPeriod; - - mCanvas.drawLine(cX - 35, mRect.height(), cX, mRect.height(), mProgressLinePaint); - // Fade out old contents mCanvas.drawPaint(mFadePaint); |