Smarter relative volume computation

This commit is contained in:
edr
2020-04-22 21:42:31 +02:00
parent 471e4ead82
commit 10f2d0a253
4 changed files with 45 additions and 35 deletions

View File

@@ -35,7 +35,7 @@ public class VolumeView extends View {
private void init() {
volume = 0;
maxVolume = 0;
maxVolume = 0.25;
paint = new Paint();
volumeHistory = new LinkedList<>();
paint.setColor(Color.rgb(255, 127, 0));
@@ -44,10 +44,10 @@ public class VolumeView extends View {
public void onAudioData(byte[] data) {
double sum = 0;
for (int i = 0; i < data.length; i++) {
double rel = data[i] / (double)32767f;
sum+=Math.pow(rel, 4);
double rel = data[i] / ((double)128);
sum += Math.pow(rel, 2);
}
volume = sum/data.length;
volume = sum / data.length;
if (volume > maxVolume) {
maxVolume = volume;
}
@@ -62,12 +62,9 @@ public class VolumeView extends View {
protected void onDraw(Canvas canvas) {
int height = canvas.getHeight();
int width = canvas.getWidth();
Log.d("VOLUME_VIEW", "height: " + height +" width: "+ width);
double relativeBrightness = 0;
if (maxVolume > 0) {
double normalizedVolume = volume / (float) maxVolume;
relativeBrightness = Math.max(0.3, normalizedVolume);
}
double normalizedVolume = volume / maxVolume;
relativeBrightness = Math.max(0.3, normalizedVolume);
int blue;
int rest;
if (relativeBrightness > 0.5) {
@@ -79,27 +76,25 @@ public class VolumeView extends View {
}
int rgb = Color.rgb(rest, rest, blue);
canvas.drawColor(rgb);
if (maxVolume > 0) {
double margins = height * 0.1;
double graphHeight = height - 2*margins;
int leftMost = Math.max(0, volumeHistory.size() - width);
int yPrev = (int) (graphHeight - margins);
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;
}
if (i == leftMost && i > 0){
yPrev = (int) (margins + graphHeight - volumeHistory.get(i-1) / maxVolume * (graphHeight));
}
canvas.drawLine(xPrev, yPrev, xNext, yNext, paint);
yPrev = yNext;
double margins = height * 0.1;
double graphHeight = height - 2*margins;
int leftMost = Math.max(0, volumeHistory.size() - width);
int yPrev = (int) (graphHeight - margins);
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;
}
if (i == leftMost && i > 0){
yPrev = (int) (margins + graphHeight - volumeHistory.get(i-1) / maxVolume * (graphHeight));
}
canvas.drawLine(xPrev, yPrev, xNext, yNext, paint);
yPrev = yNext;
}
}