Skip to content

Commit 8d84998

Browse files
author
Felix Palmer
committed
Created circle renderer
1 parent 6fa6928 commit 8d84998

File tree

2 files changed

+80
-41
lines changed

2 files changed

+80
-41
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.pheelicks.visualizer;
2+
3+
import android.graphics.Canvas;
4+
import android.graphics.Paint;
5+
import android.graphics.Rect;
6+
7+
public class CircleRenderer extends Renderer
8+
{
9+
private Paint mPaint;
10+
11+
/**
12+
* Renders the audio data onto a pulsing circle
13+
* @param canvas
14+
* @param paint - Paint to draw lines with
15+
*/
16+
public CircleRenderer(Canvas canvas,
17+
Paint paint)
18+
{
19+
super(canvas);
20+
mPaint = paint;
21+
}
22+
23+
@Override
24+
public void onRender(AudioData data, Rect rect)
25+
{
26+
for (int i = 0; i < data.bytes.length - 1; i++) {
27+
float[] cartPoint = {
28+
(float)i / (data.bytes.length - 1),
29+
rect.height() / 2 + ((byte) (data.bytes[i] + 128)) * (rect.height() / 2) / 128
30+
};
31+
32+
float[] polarPoint = toPolar(cartPoint, rect);
33+
mPoints[i * 4] = polarPoint[0];
34+
mPoints[i * 4 + 1] = polarPoint[1];
35+
36+
float[] cartPoint2 = {
37+
(float)(i + 1) / (data.bytes.length - 1),
38+
rect.height() / 2 + ((byte) (data.bytes[i + 1] + 128)) * (rect.height() / 2) / 128
39+
};
40+
41+
float[] polarPoint2 = toPolar(cartPoint2, rect);
42+
mPoints[i * 4 + 2] = polarPoint2[0];
43+
mPoints[i * 4 + 3] = polarPoint2[1];
44+
}
45+
46+
mCanvas.drawLines(mPoints, mPaint);
47+
48+
// Controls the pulsing rate
49+
modulation += 0.04;
50+
}
51+
52+
@Override
53+
public void onRender(FFTData data, Rect rect)
54+
{
55+
// Do nothing, we only display audio data
56+
}
57+
58+
float modulation = 0;
59+
float aggresive = 0.33f;
60+
private float[] toPolar(float[] cartesian, Rect rect)
61+
{
62+
double cX = rect.width()/2;
63+
double cY = rect.height()/2;
64+
double angle = (cartesian[0]) * 2 * Math.PI;
65+
double radius = ((rect.width()/2) * (1 - aggresive) + aggresive * cartesian[1]/2) * (1.2 + Math.sin(modulation))/2.2;
66+
float[] out = {
67+
(float)(cX + radius * Math.sin(angle)),
68+
(float)(cY + radius * Math.cos(angle))
69+
};
70+
return out;
71+
}
72+
}

src/com/pheelicks/visualizer/VisualizerView.java

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import android.view.View;
2222

2323
/**
24-
* A class that draws waveform data received from a
24+
* A class that draws visualizations of data received from a
2525
* {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture }
2626
*/
2727
class VisualizerView extends View {
@@ -30,7 +30,6 @@ class VisualizerView extends View {
3030
private byte[] mBytes;
3131
private byte[] mFFTBytes;
3232
private float[] mPoints;
33-
private float[] mFFTPoints;
3433
private Rect mRect = new Rect();
3534

3635
private Paint mCirclePaint = new Paint();
@@ -125,6 +124,7 @@ private void rotateColours()
125124

126125
BarGraphRenderer mBarGraphRendererTop;
127126
BarGraphRenderer mBarGraphRendererBottom;
127+
CircleRenderer mCircleRenderer;
128128

129129
@Override
130130
protected void onDraw(Canvas canvas) {
@@ -140,25 +140,6 @@ protected void onDraw(Canvas canvas) {
140140

141141
mRect.set(0, 0, getWidth(), getHeight());
142142

143-
for (int i = 0; i < mBytes.length - 1; i++) {
144-
float[] cartPoint = {
145-
(float)i / (mBytes.length - 1),
146-
mRect.height() / 2 + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128
147-
};
148-
149-
float[] polarPoint = toPolar(cartPoint);
150-
mPoints[i * 4] = polarPoint[0];
151-
mPoints[i * 4 + 1] = polarPoint[1];
152-
153-
float[] cartPoint2 = {
154-
(float)(i + 1) / (mBytes.length - 1),
155-
mRect.height() / 2 + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128
156-
};
157-
158-
float[] polarPoint2 = toPolar(cartPoint2);
159-
mPoints[i * 4 + 2] = polarPoint2[0];
160-
mPoints[i * 4 + 3] = polarPoint2[1];
161-
}
162143

163144
if(mCanvasBitmap == null)
164145
{
@@ -178,10 +159,9 @@ protected void onDraw(Canvas canvas) {
178159
paint2.setAntiAlias(true);
179160
paint2.setColor(Color.argb(200, 11, 111, 233));
180161
mBarGraphRendererTop = new BarGraphRenderer(mCanvas, 4, paint2, true);
181-
}
182-
183-
mCanvas.drawLines(mPoints, mCirclePaint);
184162

163+
mCircleRenderer = new CircleRenderer(mCanvas, paint2);
164+
}
185165

186166
// Draw normal line - offset by amplitude
187167
for (int i = 0; i < mBytes.length - 1; i++) {
@@ -212,6 +192,10 @@ protected void onDraw(Canvas canvas) {
212192
mCanvas.drawLines(mPoints, mLinePaint);
213193
}
214194

195+
196+
AudioData audioData = new AudioData(mBytes);
197+
mCircleRenderer.render(audioData, mRect);
198+
215199
// FFT time!!!!
216200
if (mFFTBytes == null) {
217201
return;
@@ -237,23 +221,6 @@ protected void onDraw(Canvas canvas) {
237221
}
238222

239223
canvas.drawBitmap(mCanvasBitmap, new Matrix(), null);
240-
modulation += 0.04;
241224
}
242225

243-
float modulation = 0;
244-
float aggresive = 0.33f;
245-
private float[] toPolar(float[] cartesian)
246-
{
247-
double cX = mRect.width()/2;
248-
double cY = mRect.height()/2;
249-
double angle = (cartesian[0]) * 2 * Math.PI;
250-
double radius = ((mRect.width()/2) * (1 - aggresive) + aggresive * cartesian[1]/2) * (1.2 + Math.sin(modulation))/2.2;
251-
float[] out = {
252-
(float)(cX + radius * Math.sin(angle)),
253-
(float)(cY + radius * Math.cos(angle))
254-
};
255-
return out;
256-
}
257-
258-
259226
}

0 commit comments

Comments
 (0)