Visualize volume over time
This commit is contained in:
@@ -3,45 +3,66 @@ package protect.babymonitor;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
|
import android.graphics.Paint;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class VolumeView extends View {
|
public class VolumeView extends View {
|
||||||
|
private static final int MAX_HISTORY = 10_000;
|
||||||
private double volume = 0;
|
private double volume = 0;
|
||||||
private double maxVolume = 0;
|
private double maxVolume = 0;
|
||||||
|
private Paint paint = new Paint();
|
||||||
|
private LinkedList<Double> volumeHistory = new LinkedList<>();
|
||||||
|
|
||||||
public VolumeView(Context context) {
|
public VolumeView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public VolumeView(Context context, @Nullable AttributeSet attrs) {
|
public VolumeView(Context context, @Nullable AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public VolumeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
public VolumeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
||||||
super(context, attrs, defStyleAttr);
|
super(context, attrs, defStyleAttr);
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
paint.setColor(Color.rgb(255, 127, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onAudioData(byte[] data) {
|
public void onAudioData(byte[] data) {
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
for (int i = 0; i < data.length; i++) {
|
for (int i = 0; i < data.length; i++) {
|
||||||
double rel = Math.abs(data[i]) / (double)32767f;
|
double rel = data[i] / (double)32767f;
|
||||||
sum+=Math.pow(rel, 4);
|
sum+=Math.pow(rel, 4);
|
||||||
}
|
}
|
||||||
volume = sum/data.length;
|
volume = sum/data.length;
|
||||||
if (volume > maxVolume) {
|
if (volume > maxVolume) {
|
||||||
maxVolume = volume;
|
maxVolume = volume;
|
||||||
}
|
}
|
||||||
|
volumeHistory.addLast(volume);
|
||||||
|
while (volumeHistory.size() > MAX_HISTORY) {
|
||||||
|
volumeHistory.removeFirst();
|
||||||
|
}
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
|
int height = canvas.getHeight();
|
||||||
|
int width = canvas.getWidth();
|
||||||
|
Log.d("VOLUME_VIEW", "height: " + height +" width: "+ width);
|
||||||
double relativeBrightness = 0;
|
double relativeBrightness = 0;
|
||||||
if (maxVolume > 0) {
|
if (maxVolume > 0) {
|
||||||
relativeBrightness = volume / (float) maxVolume;
|
double normalizedVolume = volume / (float) maxVolume;
|
||||||
relativeBrightness = Math.max(0.3, relativeBrightness);
|
relativeBrightness = Math.max(0.3, normalizedVolume);
|
||||||
}
|
}
|
||||||
int blue;
|
int blue;
|
||||||
int rest;
|
int rest;
|
||||||
@@ -54,5 +75,25 @@ public class VolumeView extends View {
|
|||||||
}
|
}
|
||||||
int rgb = Color.rgb(rest, rest, blue);
|
int rgb = Color.rgb(rest, rest, blue);
|
||||||
canvas.drawColor(rgb);
|
canvas.drawColor(rgb);
|
||||||
|
if (maxVolume > 0) {
|
||||||
|
double margins = height * 0.1;
|
||||||
|
double graphHeight = height - 2*margins;
|
||||||
|
int yPrev = (int) margins;
|
||||||
|
int leftMost = Math.max(0, volumeHistory.size() - width);
|
||||||
|
for (int i = leftMost; i < volumeHistory.size() && i - leftMost < width; i++) {
|
||||||
|
int xNext = i - leftMost;
|
||||||
|
int yNext = (int) (margins + graphHeight - volumeHistory.get(i) / maxVolume * (graphHeight));
|
||||||
|
int xPrev;
|
||||||
|
if (i == leftMost) {
|
||||||
|
xPrev = xNext;
|
||||||
|
} else {
|
||||||
|
xPrev = xNext - 1;
|
||||||
|
}
|
||||||
|
canvas.drawLine(xPrev, yPrev, xNext, yNext, paint);
|
||||||
|
yPrev = yNext;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user