aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/pheelicks/visualizer/renderer/CircleRenderer.java
blob: 1f159b2f585b0c2388a4cd5e47142ddd33558699 (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
/**
 * 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 CircleRenderer extends Renderer
{
  private Paint mPaint;
  private boolean mCycleColor;

  /**
   * Renders the audio data onto a pulsing circle
   * @param canvas
   * @param paint - Paint to draw lines with
   */
  public CircleRenderer(Canvas canvas,
                          Paint paint)
  {
    this(canvas,
         paint,
         false);
  }

  /**
   * Renders the audio data onto a pulsing circle
   * @param canvas
   * @param paint - Paint to draw lines with
   * @param cycleColor - If true the color will change on each frame
   */
  public CircleRenderer(Canvas canvas,
                        Paint paint,
                        boolean cycleColor)
  {
    super(canvas);
    mPaint = paint;
    mCycleColor = cycleColor;
  }

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

    for (int i = 0; i < data.bytes.length - 1; i++) {
      float[] cartPoint = {
          (float)i / (data.bytes.length - 1),
          rect.height() / 2 + ((byte) (data.bytes[i] + 128)) * (rect.height() / 2) / 128
      };

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

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

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

    mCanvas.drawLines(mPoints, mPaint);

    // Controls the pulsing rate
    modulation += 0.04;
  }

  @Override
  public void onRender(FFTData data, Rect rect)
  {
    // Do nothing, we only display audio data
  }

  float modulation = 0;
  float aggresive = 0.33f;
  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.2 + Math.sin(modulation))/2.2;
    float[] out =  {
        (float)(cX + radius * Math.sin(angle)),
        (float)(cY + radius * Math.cos(angle))
    };
    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;
  }
}