aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/pheelicks/visualizer/renderer/CircleBarRenderer.java
blob: 6d4e47f7ea5e6ee7feecd8e06a245f434b3382be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
 * Copyright 2011, Felix Palmer
 *
 * Licensed under the MIT license:
 * http://creativecommons.org/licenses/MIT/
 */

package com.pheelicks.visualizer.renderer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.pheelicks.visualizer.AudioData;
import com.pheelicks.visualizer.FFTData;

public class CircleBarRenderer extends Renderer
{
    private int mDivisions;
    private Paint mPaint;
    private boolean mCycleColor;

    /**
     * Renders the FFT data onto a pulsing, rotating circle
     * 
     * @param canvas
     * @param paint - Paint to draw lines with
     */
    public CircleBarRenderer(Paint paint, int divisions)
    {
        this(paint, divisions, false);
    }

    /**
     * Renders the audio data onto a pulsing circle
     * 
     * @param canvas
     * @param paint - Paint to draw lines with
     * @param divisions - must be a power of 2. Controls how many lines to draw
     * @param cycleColor - If true the color will change on each frame
     */
    public CircleBarRenderer(Paint paint, int divisions, boolean cycleColor)
    {
        super();
        mPaint = paint;
        mDivisions = divisions;
        mCycleColor = cycleColor;
    }

    @Override
    public void onRender(Canvas canvas, AudioData data, Rect rect)
    {
        // Do nothing, we only display FFT data
    }

    @Override
    public void onRender(Canvas canvas, FFTData data, Rect rect)
    {
        if (mCycleColor)
        {
            cycleColor();
        }

        for (int i = 0; i < data.bytes.length / mDivisions; i++) {
            // Calculate dbValue
            byte rfk = data.bytes[mDivisions * i];
            byte ifk = data.bytes[mDivisions * i + 1];
            float magnitude = (rfk * rfk + ifk * ifk);
            float dbValue = 75 * (float) Math.log10(magnitude);

            float[] cartPoint = {
                    (float) (i * mDivisions) / (data.bytes.length - 1),
                    rect.height() / 2 - dbValue / 4
            };

            float[] polarPoint = toPolar(cartPoint, rect);
            mFFTPoints[i * 4] = polarPoint[0];
            mFFTPoints[i * 4 + 1] = polarPoint[1];

            float[] cartPoint2 = {
                    (float) (i * mDivisions) / (data.bytes.length - 1),
                    rect.height() / 2 + dbValue
            };

            float[] polarPoint2 = toPolar(cartPoint2, rect);
            mFFTPoints[i * 4 + 2] = polarPoint2[0];
            mFFTPoints[i * 4 + 3] = polarPoint2[1];
        }

        canvas.drawLines(mFFTPoints, mPaint);

        // Controls the pulsing rate
        modulation += 0.13;
        angleModulation += 0.28;
    }

    float modulation = 0;
    float modulationStrength = 0.4f; // 0-1
    float angleModulation = 0;
    float aggresive = 0.4f;

    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 - modulationStrength) + modulationStrength * (1 + Math.sin(modulation)) / 2);
        float[] out = {
                (float) (cX + radius * Math.sin(angle + angleModulation)),
                (float) (cY + radius * Math.cos(angle + angleModulation))
        };
        return out;
    }

    private float colorCounter = 0;

    private void cycleColor()
    {
        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));
        mPaint.setColor(Color.argb(128, r, g, b));
        colorCounter += 0.03;
    }
}