Skip to content

Commit d472d6d

Browse files
author
Felix Palmer
committed
Added CircleBarRenderer & changed All to Clear
1 parent de21ddd commit d472d6d

File tree

3 files changed

+157
-14
lines changed

3 files changed

+157
-14
lines changed

res/layout/main.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
android:text="Circle" >
4343
</Button>
4444

45+
<Button
46+
android:layout_width="0dp"
47+
android:layout_height="wrap_content"
48+
android:layout_margin="10dp"
49+
android:layout_weight="0.25"
50+
android:onClick="circleBarPressed"
51+
android:text="Circle Bar" >
52+
</Button>
53+
4554
<Button
4655
android:layout_width="0dp"
4756
android:layout_height="wrap_content"
@@ -56,8 +65,8 @@
5665
android:layout_height="wrap_content"
5766
android:layout_margin="10dp"
5867
android:layout_weight="0.25"
59-
android:onClick="allPressed"
60-
android:text="All" >
68+
android:onClick="clearPressed"
69+
android:text="Clear" >
6170
</Button>
6271
</LinearLayout>
6372

src/com/pheelicks/app/MainActivity.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
import android.app.Activity;
1212
import android.graphics.Color;
1313
import android.graphics.Paint;
14+
import android.graphics.PorterDuff.Mode;
15+
import android.graphics.PorterDuffXfermode;
1416
import android.media.MediaPlayer;
1517
import android.os.Bundle;
1618
import android.view.View;
1719

1820
import com.pheelicks.visualizer.R;
1921
import com.pheelicks.visualizer.VisualizerView;
2022
import com.pheelicks.visualizer.renderer.BarGraphRenderer;
23+
import com.pheelicks.visualizer.renderer.CircleBarRenderer;
2124
import com.pheelicks.visualizer.renderer.CircleRenderer;
2225
import com.pheelicks.visualizer.renderer.LineRenderer;
2326

@@ -99,13 +102,24 @@ private void addBarGraphRenderers()
99102
mVisualizerView.addRenderer(barGraphRendererTop);
100103
}
101104

105+
private void addCircleBarRenderer()
106+
{
107+
Paint paint = new Paint();
108+
paint.setStrokeWidth(8f);
109+
paint.setAntiAlias(true);
110+
paint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
111+
paint.setColor(Color.argb(255, 222, 92, 143));
112+
CircleBarRenderer circleBarRenderer = new CircleBarRenderer(paint, 32, true);
113+
mVisualizerView.addRenderer(circleBarRenderer);
114+
}
115+
102116
private void addCircleRenderer()
103117
{
104-
Paint paint3 = new Paint();
105-
paint3.setStrokeWidth(3f);
106-
paint3.setAntiAlias(true);
107-
paint3.setColor(Color.argb(255, 222, 92, 143));
108-
CircleRenderer circleRenderer = new CircleRenderer(paint3, true);
118+
Paint paint = new Paint();
119+
paint.setStrokeWidth(3f);
120+
paint.setAntiAlias(true);
121+
paint.setColor(Color.argb(255, 222, 92, 143));
122+
CircleRenderer circleRenderer = new CircleRenderer(paint, true);
109123
mVisualizerView.addRenderer(circleRenderer);
110124
}
111125

@@ -142,27 +156,26 @@ public void stopPressed(View view)
142156

143157
public void barPressed(View view)
144158
{
145-
mVisualizerView.clearRenderers();
146159
addBarGraphRenderers();
147160
}
148161

149162
public void circlePressed(View view)
150163
{
151-
mVisualizerView.clearRenderers();
152164
addCircleRenderer();
153165
}
154166

167+
public void circleBarPressed(View view)
168+
{
169+
addCircleBarRenderer();
170+
}
171+
155172
public void linePressed(View view)
156173
{
157-
mVisualizerView.clearRenderers();
158174
addLineRenderer();
159175
}
160176

161-
public void allPressed(View view)
177+
public void clearPressed(View view)
162178
{
163179
mVisualizerView.clearRenderers();
164-
addBarGraphRenderers();
165-
addCircleRenderer();
166-
addLineRenderer();
167180
}
168181
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* Copyright 2011, Felix Palmer
3+
*
4+
* Licensed under the MIT license:
5+
* http://creativecommons.org/licenses/MIT/
6+
*/
7+
package com.pheelicks.visualizer.renderer;
8+
9+
import android.graphics.Canvas;
10+
import android.graphics.Color;
11+
import android.graphics.Paint;
12+
import android.graphics.Rect;
13+
14+
import com.pheelicks.visualizer.AudioData;
15+
import com.pheelicks.visualizer.FFTData;
16+
17+
public class CircleBarRenderer extends Renderer
18+
{
19+
private int mDivisions;
20+
private Paint mPaint;
21+
private boolean mCycleColor;
22+
23+
/**
24+
* Renders the FFT data onto a pulsing, rotating circle
25+
* @param canvas
26+
* @param paint - Paint to draw lines with
27+
*/
28+
public CircleBarRenderer(Paint paint, int divisions)
29+
{
30+
this(paint, divisions, false);
31+
}
32+
33+
/**
34+
* Renders the audio data onto a pulsing circle
35+
* @param canvas
36+
* @param paint - Paint to draw lines with
37+
* @param divisions - must be a power of 2. Controls how many lines to draw
38+
* @param cycleColor - If true the color will change on each frame
39+
*/
40+
public CircleBarRenderer(Paint paint, int divisions, boolean cycleColor)
41+
{
42+
super();
43+
mPaint = paint;
44+
mDivisions = divisions;
45+
mCycleColor = cycleColor;
46+
}
47+
48+
@Override
49+
public void onRender(Canvas canvas, AudioData data, Rect rect)
50+
{
51+
// Do nothing, we only display FFT data
52+
}
53+
54+
@Override
55+
public void onRender(Canvas canvas, FFTData data, Rect rect)
56+
{
57+
if(mCycleColor)
58+
{
59+
cycleColor();
60+
}
61+
62+
for (int i = 0; i < data.bytes.length / mDivisions; i++) {
63+
// Calculate dbValue
64+
byte rfk = data.bytes[mDivisions * i];
65+
byte ifk = data.bytes[mDivisions * i + 1];
66+
float magnitude = (rfk * rfk + ifk * ifk);
67+
float dbValue = 75 * (float)Math.log10(magnitude);
68+
69+
float[] cartPoint = {
70+
(float)(i * mDivisions) / (data.bytes.length - 1),
71+
rect.height() / 2 - dbValue / 4
72+
};
73+
74+
float[] polarPoint = toPolar(cartPoint, rect);
75+
mFFTPoints[i * 4] = polarPoint[0];
76+
mFFTPoints[i * 4 + 1] = polarPoint[1];
77+
78+
float[] cartPoint2 = {
79+
(float)(i * mDivisions) / (data.bytes.length - 1),
80+
rect.height() / 2 + dbValue
81+
};
82+
83+
float[] polarPoint2 = toPolar(cartPoint2, rect);
84+
mFFTPoints[i * 4 + 2] = polarPoint2[0];
85+
mFFTPoints[i * 4 + 3] = polarPoint2[1];
86+
}
87+
88+
canvas.drawLines(mFFTPoints, mPaint);
89+
90+
// Controls the pulsing rate
91+
modulation += 0.13;
92+
angleModulation += 0.28;
93+
}
94+
95+
float modulation = 0;
96+
float modulationStrength = 0.4f; // 0-1
97+
float angleModulation = 0;
98+
float aggresive = 0.4f;
99+
private float[] toPolar(float[] cartesian, Rect rect)
100+
{
101+
double cX = rect.width()/2;
102+
double cY = rect.height()/2;
103+
double angle = (cartesian[0]) * 2 * Math.PI;
104+
double radius = ((rect.width()/2) * (1 - aggresive) + aggresive * cartesian[1]/2) * ((1 - modulationStrength) + modulationStrength * (1 + Math.sin(modulation)) / 2);
105+
float[] out = {
106+
(float)(cX + radius * Math.sin(angle + angleModulation)),
107+
(float)(cY + radius * Math.cos(angle + angleModulation))
108+
};
109+
return out;
110+
}
111+
112+
private float colorCounter = 0;
113+
private void cycleColor()
114+
{
115+
int r = (int)Math.floor(128*(Math.sin(colorCounter) + 1));
116+
int g = (int)Math.floor(128*(Math.sin(colorCounter + 2) + 1));
117+
int b = (int)Math.floor(128*(Math.sin(colorCounter + 4) + 1));
118+
mPaint.setColor(Color.argb(128, r, g, b));
119+
colorCounter += 0.03;
120+
}
121+
}

0 commit comments

Comments
 (0)