Skip to content

Commit 650eea7

Browse files
author
Felix Palmer
committedDec 5, 2011
Created LineRenderer
1 parent f605419 commit 650eea7

File tree

2 files changed

+116
-56
lines changed

2 files changed

+116
-56
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.pheelicks.visualizer;
2+
3+
import android.graphics.Canvas;
4+
import android.graphics.Color;
5+
import android.graphics.Paint;
6+
import android.graphics.Rect;
7+
8+
public class LineRenderer extends Renderer
9+
{
10+
private Paint mPaint;
11+
private Paint mFlashPaint;
12+
private boolean mCycleColor;
13+
private float amplitude = 0;
14+
15+
16+
/**
17+
* Renders the audio data onto a line. The line flashes on prominent beats
18+
* @param canvas
19+
* @param paint - Paint to draw lines with
20+
* @param paint - Paint to draw flash with
21+
*/
22+
public LineRenderer(Canvas canvas,
23+
Paint paint,
24+
Paint flashPaint)
25+
{
26+
this(canvas,
27+
paint,
28+
flashPaint,
29+
false);
30+
}
31+
32+
/**
33+
* Renders the audio data onto a line. The line flashes on prominent beats
34+
* @param canvas
35+
* @param paint - Paint to draw lines with
36+
* @param paint - Paint to draw flash with
37+
* @param cycleColor - If true the color will change on each frame
38+
*/
39+
public LineRenderer(Canvas canvas,
40+
Paint paint,
41+
Paint flashPaint,
42+
boolean cycleColor)
43+
{
44+
super(canvas);
45+
mPaint = paint;
46+
mFlashPaint = flashPaint;
47+
mCycleColor = cycleColor;
48+
}
49+
50+
@Override
51+
public void onRender(AudioData data, Rect rect)
52+
{
53+
if(mCycleColor)
54+
{
55+
cycleColor();
56+
}
57+
58+
// Calculate points for line
59+
for (int i = 0; i < data.bytes.length - 1; i++) {
60+
mPoints[i * 4] = rect.width() * i / (data.bytes.length - 1);
61+
mPoints[i * 4 + 1] = rect.height() / 2
62+
+ ((byte) (data.bytes[i] + 128)) * (rect.height() / 3) / 128;
63+
mPoints[i * 4 + 2] = rect.width() * (i + 1) / (data.bytes.length - 1);
64+
mPoints[i * 4 + 3] = rect.height() / 2
65+
+ ((byte) (data.bytes[i + 1] + 128)) * (rect.height() / 3) / 128;
66+
}
67+
68+
// Calc amplitude for this waveform
69+
float accumulator = 0;
70+
for (int i = 0; i < data.bytes.length - 1; i++) {
71+
accumulator += Math.abs(data.bytes[i]);
72+
}
73+
74+
float amp = accumulator/(128 * data.bytes.length);
75+
if(amp > amplitude)
76+
{
77+
// Amplitude is bigger than normal, make a prominent line
78+
amplitude = amp;
79+
mCanvas.drawLines(mPoints, mFlashPaint);
80+
}
81+
else
82+
{
83+
// Amplitude is nothing special, reduce the amplitude
84+
amplitude *= 0.99;
85+
mCanvas.drawLines(mPoints, mPaint);
86+
}
87+
}
88+
89+
@Override
90+
public void onRender(FFTData data, Rect rect)
91+
{
92+
// Do nothing, we only display audio data
93+
}
94+
95+
private float colorCounter = 0;
96+
private void cycleColor()
97+
{
98+
int r = (int)Math.floor(128*(Math.sin(colorCounter) + 3));
99+
int g = (int)Math.floor(128*(Math.sin(colorCounter + 1) + 1));
100+
int b = (int)Math.floor(128*(Math.sin(colorCounter + 7) + 1));
101+
mPaint.setColor(Color.argb(128, r, g, b));
102+
colorCounter += 0.03;
103+
}
104+
}

‎src/com/pheelicks/visualizer/VisualizerView.java

+12-56
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// WARNING!!! This file has more magic numbers in it than you could shake a
44
// stick at
55

6-
import java.util.Random;
7-
86
import android.content.Context;
97
import android.graphics.Bitmap;
108
import android.graphics.Bitmap.Config;
@@ -58,14 +56,6 @@ public VisualizerView(Context context)
5856
private void init() {
5957
mBytes = null;
6058

61-
mLinePaint.setStrokeWidth(1f);
62-
mLinePaint.setAntiAlias(true);
63-
mLinePaint.setColor(Color.argb(88, 0, 128, 255));
64-
65-
mSpecialLinePaint.setStrokeWidth(5f);
66-
mSpecialLinePaint.setAntiAlias(true);
67-
mSpecialLinePaint.setColor(Color.argb(188, 255, 255, 255));
68-
6959
mProgressLinePaint.setStrokeWidth(4f);
7060
mProgressLinePaint.setAntiAlias(true);
7161
mProgressLinePaint.setColor(Color.argb(255, 22, 131, 255));
@@ -79,7 +69,6 @@ private void init() {
7969

8070
public void updateVisualizer(byte[] bytes) {
8171
mBytes = bytes;
82-
rotateColours();
8372
invalidate();
8473
}
8574

@@ -100,25 +89,13 @@ public void updateVisualizerFFT(byte[] bytes) {
10089
invalidate();
10190
}
10291

103-
104-
float colorCounter = 0;
105-
private void rotateColours()
106-
{
107-
int r = (int)Math.floor(128*(Math.sin(colorCounter) + 1));
108-
int g = (int)Math.floor(128*(Math.sin(colorCounter + 2) + 1));
109-
int b = (int)Math.floor(128*(Math.sin(colorCounter + 4) + 1));
110-
mLinePaint.setColor(Color.argb(128, r, g, b));
111-
colorCounter += 0.03;
112-
}
113-
11492
Bitmap mCanvasBitmap;
11593
Canvas mCanvas;
116-
Random mRandom = new Random();
117-
float amplitude = 0;
11894

11995
BarGraphRenderer mBarGraphRendererTop;
12096
BarGraphRenderer mBarGraphRendererBottom;
12197
CircleRenderer mCircleRenderer;
98+
LineRenderer mLineRenderer;
12299

123100
@Override
124101
protected void onDraw(Canvas canvas) {
@@ -158,42 +135,26 @@ protected void onDraw(Canvas canvas) {
158135
paint3.setStrokeWidth(3f);
159136
paint3.setAntiAlias(true);
160137
paint3.setColor(Color.argb(255, 222, 92, 143));
161-
162138
mCircleRenderer = new CircleRenderer(mCanvas, paint3, true);
163-
}
164139

165-
// Draw normal line - offset by amplitude
166-
for (int i = 0; i < mBytes.length - 1; i++) {
167-
mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
168-
mPoints[i * 4 + 1] = mRect.height() / 2
169-
+ ((byte) (mBytes[i] + 128)) * (mRect.height() / 3) / 128;
170-
mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
171-
mPoints[i * 4 + 3] = mRect.height() / 2
172-
+ ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 3) / 128;
173-
}
140+
Paint linePaint = new Paint();
141+
linePaint.setStrokeWidth(1f);
142+
linePaint.setAntiAlias(true);
143+
linePaint.setColor(Color.argb(88, 0, 128, 255));
174144

175-
// Calc amplitude for this waveform
176-
float accumulator = 0;
177-
for (int i = 0; i < mBytes.length - 1; i++) {
178-
accumulator += Math.abs(mBytes[i]);
145+
Paint lineFlashPaint = new Paint();
146+
lineFlashPaint.setStrokeWidth(5f);
147+
lineFlashPaint.setAntiAlias(true);
148+
lineFlashPaint.setColor(Color.argb(188, 255, 255, 255));
149+
mLineRenderer = new LineRenderer(mCanvas, linePaint, lineFlashPaint, true);
179150
}
180151

181-
float amp = accumulator/(128 * mBytes.length);
182-
if(amp > amplitude)
183-
{
184-
amplitude = amp;
185-
// Occassionally, make a prominent line
186-
mCanvas.drawLines(mPoints, mSpecialLinePaint);
187-
}
188-
else
189-
{
190-
amplitude *= 0.99;
191-
mCanvas.drawLines(mPoints, mLinePaint);
192-
}
152+
193153

194154

195155
AudioData audioData = new AudioData(mBytes);
196156
mCircleRenderer.render(audioData, mRect);
157+
mLineRenderer.render(audioData, mRect);
197158

198159
// FFT time!!!!
199160
if (mFFTBytes == null) {
@@ -205,11 +166,6 @@ protected void onDraw(Canvas canvas) {
205166
mBarGraphRendererTop.render(fftData, mRect);
206167
mBarGraphRendererBottom.render(fftData, mRect);
207168

208-
// We totally need a thing moving along the bottom
209-
float cX = mRect.width()*(SystemClock.currentThreadTimeMillis() - mFlashTime)/mFlashPeriod;
210-
211-
mCanvas.drawLine(cX - 35, mRect.height(), cX, mRect.height(), mProgressLinePaint);
212-
213169
// Fade out old contents
214170
mCanvas.drawPaint(mFadePaint);
215171

0 commit comments

Comments
 (0)
Please sign in to comment.