Smarter relative volume computation
This commit is contained in:
15
app/src/main/java/protect/babymonitor/AudioCodecDefines.java
Normal file
15
app/src/main/java/protect/babymonitor/AudioCodecDefines.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package protect.babymonitor;
|
||||
|
||||
import android.media.AudioFormat;
|
||||
|
||||
public class AudioCodecDefines {
|
||||
public static final int FREQUENCY = 11025;
|
||||
public static final int ENCODING = AudioFormat.ENCODING_PCM_16BIT;
|
||||
public static final int CHANNEL_CONFIGURATION_IN = AudioFormat.CHANNEL_IN_MONO;
|
||||
public static final int CHANNEL_CONFIGURATION_OUT = AudioFormat.CHANNEL_OUT_MONO;
|
||||
|
||||
|
||||
private AudioCodecDefines() {
|
||||
throw new IllegalStateException("Do not instantiate!");
|
||||
}
|
||||
}
|
||||
@@ -44,9 +44,9 @@ public class ListenActivity extends Activity
|
||||
NotificationManagerCompat _mNotifyMgr;
|
||||
|
||||
Thread _listenThread;
|
||||
private final int frequency = 11025;
|
||||
private final int channelConfiguration = AudioFormat.CHANNEL_OUT_MONO;
|
||||
private final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
|
||||
private final int frequency = AudioCodecDefines.FREQUENCY;
|
||||
private final int channelConfiguration = AudioCodecDefines.CHANNEL_CONFIGURATION_OUT;
|
||||
private final int audioEncoding = AudioCodecDefines.ENCODING;
|
||||
private final int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
|
||||
private final int byteBufferSize = bufferSize*2;
|
||||
|
||||
|
||||
@@ -59,9 +59,9 @@ public class MonitorActivity extends Activity {
|
||||
}
|
||||
});
|
||||
|
||||
final int frequency = 11025;
|
||||
final int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
|
||||
final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
|
||||
final int frequency = AudioCodecDefines.FREQUENCY;
|
||||
final int channelConfiguration = AudioCodecDefines.CHANNEL_CONFIGURATION_IN;
|
||||
final int audioEncoding = AudioCodecDefines.ENCODING;
|
||||
|
||||
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
|
||||
final AudioRecord audioRecord = new AudioRecord(
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user