summaryrefslogtreecommitdiffstats
path: root/src/com/cyngn/eleven/widgets
diff options
context:
space:
mode:
authorlinus_lee <llee@cyngn.com>2014-11-19 17:52:53 -0800
committerlinus_lee <llee@cyngn.com>2014-12-08 15:19:02 -0800
commit8274981ad06f75f5c2ab15879083d9ee5f031554 (patch)
treec047db76e23e8aa260e76d8918653e8aa759f148 /src/com/cyngn/eleven/widgets
parenta81b94d5f5cbdbad0a5d83b81021ab45ed66c109 (diff)
downloadandroid_packages_apps_Eleven-8274981ad06f75f5c2ab15879083d9ee5f031554.tar.gz
android_packages_apps_Eleven-8274981ad06f75f5c2ab15879083d9ee5f031554.tar.bz2
android_packages_apps_Eleven-8274981ad06f75f5c2ab15879083d9ee5f031554.zip
Eleven: Add equalizer visualization
Change-Id: I9a3112cf4138e916ed53571236e54b67c30b53c4
Diffstat (limited to 'src/com/cyngn/eleven/widgets')
-rw-r--r--src/com/cyngn/eleven/widgets/EqualizerView.java165
1 files changed, 165 insertions, 0 deletions
diff --git a/src/com/cyngn/eleven/widgets/EqualizerView.java b/src/com/cyngn/eleven/widgets/EqualizerView.java
new file mode 100644
index 0000000..5814692
--- /dev/null
+++ b/src/com/cyngn/eleven/widgets/EqualizerView.java
@@ -0,0 +1,165 @@
+/*
+ * Copyright (C) 2014 Cyanogen, Inc.
+ */
+package com.cyngn.eleven.widgets;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.graphics.Canvas;
+import android.graphics.DashPathEffect;
+import android.graphics.Paint;
+import android.graphics.Rect;
+import android.util.AttributeSet;
+import android.util.Log;
+
+import com.cyngn.eleven.R;
+import com.cyngn.eleven.utils.MusicUtils;
+import com.cyngn.eleven.utils.PreferenceUtils;
+import com.pheelicks.visualizer.AudioData;
+import com.pheelicks.visualizer.FFTData;
+import com.pheelicks.visualizer.VisualizerView;
+import com.pheelicks.visualizer.renderer.Renderer;
+
+public class EqualizerView extends VisualizerView {
+ private boolean mLinked = false;
+ private boolean mStarted = false;
+ private boolean mPanelVisible = false;
+
+ private final Runnable mLinkVisualizer = new Runnable() {
+ @Override
+ public void run() {
+ if (!mLinked) {
+ animate().alpha(1).setDuration(300);
+ link(0);
+ mLinked = true;
+ }
+ }
+ };
+
+ private final Runnable mUnlinkVisualizer = new Runnable() {
+ @Override
+ public void run() {
+ if (mLinked) {
+ animate().alpha(0).setDuration(300);
+ unlink();
+ mLinked = false;
+ }
+ }
+ };
+
+ private static class TileBarGraphRenderer extends Renderer {
+ private int mDivisions;
+ private Paint mPaint;
+ private int mDbFuzz;
+ private int mDbFuzzFactor;
+
+ /**
+ * Renders the FFT data as a series of lines, in histogram form
+ *
+ * @param divisions - must be a power of 2. Controls how many lines to draw
+ * @param paint - Paint to draw lines with
+ * @param dbfuzz - final dB display adjustment
+ * @param dbFactor - dbfuzz is multiplied by dbFactor.
+ */
+ public TileBarGraphRenderer(int divisions, Paint paint, int dbfuzz, int dbFactor) {
+ super();
+ mDivisions = divisions;
+ mPaint = paint;
+ mDbFuzz = dbfuzz;
+ mDbFuzzFactor = dbFactor;
+ }
+
+ @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) {
+ for (int i = 0; i < data.bytes.length / mDivisions; i++) {
+ mFFTPoints[i * 4] = i * 4 * mDivisions;
+ mFFTPoints[i * 4 + 2] = i * 4 * mDivisions;
+ byte rfk = data.bytes[mDivisions * i];
+ byte ifk = data.bytes[mDivisions * i + 1];
+ float magnitude = (rfk * rfk + ifk * ifk);
+ int dbValue = (int) (10 * Math.log10(magnitude));
+
+ mFFTPoints[i * 4 + 1] = rect.height();
+ mFFTPoints[i * 4 + 3] = rect.height() - (dbValue * mDbFuzzFactor + mDbFuzz);
+ }
+
+ canvas.drawLines(mFFTPoints, mPaint);
+ }
+ }
+
+ public EqualizerView(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+ }
+
+ public EqualizerView(Context context, AttributeSet attrs) {
+ this(context, attrs, 0);
+ }
+
+ public EqualizerView(Context context) {
+ this(context, null, 0);
+ }
+
+ public void initialize(Context context) {
+ setEnabled(false);
+
+ Resources res = mContext.getResources();
+ Paint paint = new Paint();
+ paint.setStrokeWidth(res.getDimensionPixelSize(R.dimen.eqalizer_path_stroke_width));
+ paint.setAntiAlias(true);
+ paint.setColor(res.getColor(R.color.equalizer_fill_color));
+ paint.setPathEffect(new DashPathEffect(new float[]{
+ res.getDimensionPixelSize(R.dimen.eqalizer_path_effect_1),
+ res.getDimensionPixelSize(R.dimen.eqalizer_path_effect_2)
+ }, 0));
+
+ int bars = res.getInteger(R.integer.equalizer_divisions);
+ addRenderer(new TileBarGraphRenderer(bars, paint,
+ res.getInteger(R.integer.equalizer_db_fuzz),
+ res.getInteger(R.integer.equalizer_db_fuzz_factor)));
+ }
+
+ /**
+ * Follows Fragment onStart to determine if the containing fragment/activity is started
+ */
+ public void onStart() {
+ mStarted = true;
+ checkStateChanged();
+ }
+
+ /**
+ * Follows Fragment onStop to determine if the containing fragment/activity is stopped
+ */
+ public void onStop() {
+ mStarted = false;
+ checkStateChanged();
+ }
+
+ /**
+ * Separate method to toggle panel visibility - currently used when the user slides to
+ * improve performance of the sliding panel
+ */
+ public void setPanelVisible(boolean panelVisible) {
+ if (mPanelVisible != panelVisible) {
+ mPanelVisible = panelVisible;
+ checkStateChanged();
+ }
+ }
+
+ /**
+ * Checks the state of the EqualizerView to determine whether we want to link up the equalizer
+ */
+ public void checkStateChanged() {
+ if (mPanelVisible && mStarted
+ && PreferenceUtils.getInstance(mContext).getShowVisualizer()
+ && MusicUtils.getQueueSize() > 0) {
+ mLinkVisualizer.run();
+ } else {
+ mUnlinkVisualizer.run();
+ }
+ }
+}