aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/pheelicks/visualizer/VisualizerView.java
blob: 82d7100c1819d6ca43530584b4a3e3d2adee173a (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.pheelicks.visualizer;

// WARNING!!! This file has more magic numbers in it than you could shake a
// stick at

import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.media.audiofx.Visualizer;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;

/**
 * A class that draws waveform data received from a
 * {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture }
 */
class VisualizerView extends View {
  private static final String TAG = "VisualizerView";

  private byte[] mBytes;
  private byte[] mFFTBytes;
  private float[] mPoints;
  private float[] mFFTPoints;
  private Rect mRect = new Rect();

  private Paint mCirclePaint = new Paint();
  private Paint mLinePaint = new Paint();
  private Paint mSpecialLinePaint = new Paint();
  private Paint mProgressLinePaint = new Paint();
  private Paint mFlashPaint = new Paint();
  private Paint mFadePaint = new Paint();

  // Usual BS of 3 constructors
  public VisualizerView(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs);
    init();
  }

  public VisualizerView(Context context, AttributeSet attrs)
  {
    this(context, attrs, 0);
  }

  public VisualizerView(Context context)
  {
    this(context, null, 0);
  }

  private void init() {
    mBytes = null;

    mCirclePaint.setStrokeWidth(3f);
    mCirclePaint.setAntiAlias(true);
    mCirclePaint.setColor(Color.argb(255, 222, 92, 143));

    mLinePaint.setStrokeWidth(1f);
    mLinePaint.setAntiAlias(true);
    mLinePaint.setColor(Color.argb(88, 0, 128, 255));

    mSpecialLinePaint.setStrokeWidth(5f);
    mSpecialLinePaint.setAntiAlias(true);
    mSpecialLinePaint.setColor(Color.argb(188, 255, 255, 255));

    mProgressLinePaint.setStrokeWidth(4f);
    mProgressLinePaint.setAntiAlias(true);
    mProgressLinePaint.setColor(Color.argb(255, 22, 131, 255));


    mFlashPaint.setColor(Color.argb(122, 255, 255, 255));

    mFadePaint.setColor(Color.argb(238, 255, 255, 255)); // Adjust alpha to change how quickly the image fades
    mFadePaint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
  }

  public void updateVisualizer(byte[] bytes) {
    mBytes = bytes;
    rotateColours();
    invalidate();
  }

  boolean mFlash = false;
  long mFlashTime = 0;
  long mFlashPeriod = 4000;

  public void flash() {
    mFlash = true;
    long now = SystemClock.currentThreadTimeMillis();
    mFlashPeriod = now - mFlashTime;
    mFlashTime = now;
    invalidate();
  }

  public void updateVisualizerFFT(byte[] bytes) {
    mFFTBytes = bytes;
    invalidate();
  }


  float colorCounter = 0;
  private void rotateColours()
  {
    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));
    mLinePaint.setColor(Color.argb(128, r, g, b));
    mCirclePaint.setColor(Color.argb(255, g, b, r));
    colorCounter += 0.03;
  }

  Bitmap mCanvasBitmap;
  Canvas mCanvas;
  Random mRandom = new Random();
  float amplitude = 0;

  BarGraphRenderer mBarGraphRendererTop;
  BarGraphRenderer mBarGraphRendererBottom;

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mBytes == null) {
      return;
    }

    if (mPoints == null || mPoints.length < mBytes.length * 4) {
      mPoints = new float[mBytes.length * 4];
    }

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

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

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

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

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

    if(mCanvasBitmap == null)
    {
      mCanvasBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888);
    }
    if(mCanvas == null)
    {
      mCanvas = new Canvas(mCanvasBitmap);
      Paint paint = new Paint();
      paint.setStrokeWidth(50f);
      paint.setAntiAlias(true);
      paint.setColor(Color.argb(200, 233, 0, 44));
      mBarGraphRendererBottom = new BarGraphRenderer(mCanvas, 16, paint, false);

      Paint paint2 = new Paint();
      paint2.setStrokeWidth(12f);
      paint2.setAntiAlias(true);
      paint2.setColor(Color.argb(200, 11, 111, 233));
      mBarGraphRendererTop = new BarGraphRenderer(mCanvas, 4, paint2, true);
    }

    mCanvas.drawLines(mPoints, mCirclePaint);


    // Draw normal line - offset by amplitude
    for (int i = 0; i < mBytes.length - 1; i++) {
      mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
      mPoints[i * 4 + 1] =  mRect.height() / 2
          + ((byte) (mBytes[i] + 128)) * (mRect.height() / 3) / 128;
      mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
      mPoints[i * 4 + 3] = mRect.height() / 2
          + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 3) / 128;
    }

    // Calc amplitude for this waveform
    float accumulator = 0;
    for (int i = 0; i < mBytes.length - 1; i++) {
      accumulator += Math.abs(mBytes[i]);
    }

    float amp = accumulator/(128 * mBytes.length);
    if(amp > amplitude)
    {
      amplitude = amp;
      // Occassionally, make a prominent line
      mCanvas.drawLines(mPoints, mSpecialLinePaint);
    }
    else
    {
      amplitude *= 0.99;
      mCanvas.drawLines(mPoints, mLinePaint);
    }

    // FFT time!!!!
    if (mFFTBytes == null) {
      return;
    }

    FFTData fftData = new FFTData(mFFTBytes);

    mBarGraphRendererTop.render(fftData, mRect);
    mBarGraphRendererBottom.render(fftData, mRect);

    // We totally need a thing moving along the bottom
    float cX = mRect.width()*(SystemClock.currentThreadTimeMillis() - mFlashTime)/mFlashPeriod;

    mCanvas.drawLine(cX - 35, mRect.height(), cX, mRect.height(), mProgressLinePaint);

    // Fade out old contents
    mCanvas.drawPaint(mFadePaint);

    if(mFlash)
    {
      mFlash = false;
      mCanvas.drawPaint(mFlashPaint);
    }

    canvas.drawBitmap(mCanvasBitmap, new Matrix(), null);
    modulation += 0.04;
  }

  float modulation = 0;
  float aggresive = 0.33f;
  private float[] toPolar(float[] cartesian)
  {
    double cX = mRect.width()/2;
    double cY = mRect.height()/2;
    double angle = (cartesian[0]) * 2 * Math.PI;
    double radius = ((mRect.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;
  }


}