Skip to content

Commit

Permalink
Merge pull request #1 from GautamChibde/feature/line_bar_visualizer
Browse files Browse the repository at this point in the history
Feature/line bar visualizer
  • Loading branch information
GautamChibde authored Nov 23, 2017
2 parents 503f73a + ceb5cce commit 6c14775
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 121 deletions.
12 changes: 1 addition & 11 deletions audiovisualizer/src/main/java/com/chibde/BaseVisualizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

Expand Down Expand Up @@ -56,14 +54,6 @@ public BaseVisualizer(Context context, @Nullable AttributeSet attrs, int defStyl
init();
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public BaseVisualizer(Context context,
@Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
init();
}

private void init(AttributeSet attributeSet) {
paint = new Paint();
}
Expand Down Expand Up @@ -94,7 +84,7 @@ public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
public void onFftDataCapture(Visualizer visualizer, byte[] bytes,
int samplingRate) {
}
}, Visualizer.getMaxCaptureRate() / 2, true, true);
}, Visualizer.getMaxCaptureRate() / 2, true, false);

visualizer.setEnabled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;

import com.chibde.BaseVisualizer;
Expand All @@ -34,7 +32,7 @@

public class BarVisualizer extends BaseVisualizer {

private int density = 50;
private float density = 50;
private int gap;

public BarVisualizer(Context context) {
Expand All @@ -52,14 +50,6 @@ public BarVisualizer(Context context,
super(context, attrs, defStyleAttr);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public BarVisualizer(Context context,
@Nullable AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void init() {
this.density = 50;
Expand All @@ -74,30 +64,27 @@ protected void init() {
*
* @param density density of the bar visualizer
*/
public void setDensity(int density) {
public void setDensity(float density) {
this.density = density;
if (density > 256) {
this.density = 256;
} else if (density < 10) {
this.density = 10;
}
this.density = density;
}

@Override
protected void onDraw(Canvas canvas) {
if (bytes != null) {

int barWidth = getWidth() / density;
int div = (int) Math.ceil(bytes.length / density);
float barWidth = getWidth() / density;
float div = bytes.length / density;
paint.setStrokeWidth(barWidth - gap);

int k = 0;

for (int i = barWidth / 2; i < getWidth() && k < bytes.length; i += barWidth) {
for (int i = 0; i < density; i++) {
int x = (int) Math.ceil(i * div);
int top = canvas.getHeight() +
((byte) (Math.abs(bytes[k]) + 128)) * canvas.getHeight() / 128;
canvas.drawLine(i, getHeight(), i, top, paint);
k += div;
((byte) (Math.abs(bytes[x]) + 128)) * canvas.getHeight() / 128;
canvas.drawLine(i * barWidth, getHeight(), i * barWidth, top, paint);
}
super.onDraw(canvas);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;

import com.chibde.BaseVisualizer;
Expand Down Expand Up @@ -51,14 +49,6 @@ public BlazingColorVisualizer(Context context,
super(context, attrs, defStyleAttr);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public BlazingColorVisualizer(Context context,
@Nullable AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void init() {
shader = new LinearGradient(0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;

import com.chibde.BaseVisualizer;
Expand Down Expand Up @@ -53,14 +51,6 @@ public CircleBarVisualizer(Context context,
super(context, attrs, defStyleAttr);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CircleBarVisualizer(Context context,
@Nullable AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void init() {
paint.setStyle(Paint.Style.STROKE);
Expand All @@ -72,7 +62,7 @@ protected void init() {
protected void onDraw(Canvas canvas) {
if (radius == -1) {
radius = getHeight() < getWidth() ? getHeight() : getWidth();
radius = (int) (radius * 0.8 / 2);
radius = (int) (radius * 0.65 / 2);
double circumference = 2 * Math.PI * radius;
paint.setStrokeWidth((float) (circumference / 120));
circlePaint.setStyle(Paint.Style.STROKE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;

import com.chibde.BaseVisualizer;
Expand Down Expand Up @@ -47,17 +45,8 @@ public CircleVisualizer(Context context, @Nullable AttributeSet attrs, int defSt
super(context, attrs, defStyleAttr);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CircleVisualizer(Context context,
@Nullable AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void init() {

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.chibde.visualizer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.chibde.BaseVisualizer;

/**
* Custom view that creates a Line and Bar visualizer effect for
* the android {@link android.media.MediaPlayer}
* <p>
* Created by gautam chibde on 22/11/17.
*/

public class LineBarVisualizer extends BaseVisualizer {
private Paint middleLine;
private float density;
private int gap;

public LineBarVisualizer(Context context) {
super(context);
}

public LineBarVisualizer(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public LineBarVisualizer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void init() {
density = 50;
gap = 4;
middleLine = new Paint();
middleLine.setColor(Color.BLUE);
}

/**
* Sets the density to the Bar visualizer i.e the number of bars
* to be displayed. Density can vary from 10 to 256.
* by default the value is set to 50.
*
* @param density density of the bar visualizer
*/
public void setDensity(float density) {
if (this.density > 180) {
this.middleLine.setStrokeWidth(1);
this.gap = 1;
} else {
this.gap = 4;
}
this.density = density;
if (density > 256) {
this.density = 250;
this.gap = 0;
} else if (density <= 10) {
this.density = 10;
}
}

@Override
protected void onDraw(Canvas canvas) {
if (middleLine.getColor() != Color.BLUE) {
middleLine.setColor(color);
}
if (bytes != null) {
float barWidth = getWidth() / density;
float div = bytes.length / density;
canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, middleLine);
paint.setStrokeWidth(barWidth - gap);

for (int i = 0; i < density; i++) {
int x = (int) Math.ceil(i * div);
int top = canvas.getHeight() / 2
+ (128 - Math.abs(bytes[x]))
* (canvas.getHeight() / 2) / 128;

int bottom = canvas.getHeight() / 2
- (128 - Math.abs(bytes[x]))
* (canvas.getHeight() / 2) / 128;

canvas.drawLine(i * barWidth, bottom, i * barWidth, getHeight() / 2, paint);
canvas.drawLine(i * barWidth, top, i * barWidth, getHeight() / 2, paint);
}
super.onDraw(canvas);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package com.chibde.visualizer;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

Expand Down Expand Up @@ -52,14 +50,6 @@ public LineVisualizer(Context context,
super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public LineVisualizer(Context context,
@Nullable AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void init() {
}
Expand Down
7 changes: 6 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<activity
android:name=".visualizer.CircleVisualizerActivity"
android:label="@string/circle_visualizer">

</activity>
<activity
android:name=".visualizer.CircleBarVisualizerActivity"
Expand All @@ -45,5 +44,11 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
<activity android:name=".visualizer.LineBarVisualizerActivity"
android:label="@string/line_bar_visualizer">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,25 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
} else {
throw new NullPointerException("Provide layout file for the activity");
}
if (getActionBar() != null) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(WRITE_EXTERNAL_STORAGE_PERMS, AUDIO_PERMISSION_REQUEST_CODE);
} else {
setPlayer();
}
setActionBar();
initialize();
}

private void initialize() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M
&& checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(WRITE_EXTERNAL_STORAGE_PERMS, AUDIO_PERMISSION_REQUEST_CODE);
} else {
setPlayer();
}
}

private void setActionBar() {
if (getActionBar() != null) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}

private void setPlayer() {
mediaPlayer = MediaPlayer.create(this, R.raw.red_e);
mediaPlayer.setLooping(false);
Expand All @@ -83,7 +88,12 @@ public void onRequestPermissionsResult(
@NonNull int[] grantResults) {
switch (requestCode) {
case AUDIO_PERMISSION_REQUEST_CODE:
setPlayer();
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setPlayer();
} else {
this.finish();
}
}
}

Expand Down
Loading

0 comments on commit 6c14775

Please sign in to comment.