First prototype for volume visualization
This commit is contained in:
5
app/src/main/java/protect/babymonitor/AudioListener.java
Normal file
5
app/src/main/java/protect/babymonitor/AudioListener.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package protect.babymonitor;
|
||||||
|
|
||||||
|
public interface AudioListener {
|
||||||
|
void onAudio(byte[] audioBytes);
|
||||||
|
}
|
||||||
@@ -22,7 +22,6 @@ import java.net.Socket;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
|
||||||
import android.media.AudioFormat;
|
import android.media.AudioFormat;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.media.AudioTrack;
|
import android.media.AudioTrack;
|
||||||
@@ -45,16 +44,16 @@ public class ListenActivity extends Activity
|
|||||||
NotificationManagerCompat _mNotifyMgr;
|
NotificationManagerCompat _mNotifyMgr;
|
||||||
|
|
||||||
Thread _listenThread;
|
Thread _listenThread;
|
||||||
private void streamAudio(final Socket socket) throws IllegalArgumentException, IllegalStateException, IOException
|
private final int frequency = 11025;
|
||||||
|
private final int channelConfiguration = AudioFormat.CHANNEL_OUT_MONO;
|
||||||
|
private final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
|
||||||
|
private final int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
|
||||||
|
private final int byteBufferSize = bufferSize*2;
|
||||||
|
|
||||||
|
private void streamAudio(final Socket socket, AudioListener listener) throws IllegalArgumentException, IllegalStateException, IOException
|
||||||
{
|
{
|
||||||
Log.i(TAG, "Setting up stream");
|
Log.i(TAG, "Setting up stream");
|
||||||
|
|
||||||
final int frequency = 11025;
|
|
||||||
final int channelConfiguration = AudioFormat.CHANNEL_OUT_MONO;
|
|
||||||
final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
|
|
||||||
final int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
|
|
||||||
final int byteBufferSize = bufferSize*2;
|
|
||||||
|
|
||||||
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
|
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
|
||||||
frequency,
|
frequency,
|
||||||
channelConfiguration,
|
channelConfiguration,
|
||||||
@@ -80,6 +79,9 @@ public class ListenActivity extends Activity
|
|||||||
if(read > 0)
|
if(read > 0)
|
||||||
{
|
{
|
||||||
audioTrack.write(buffer, 0, read);
|
audioTrack.write(buffer, 0, read);
|
||||||
|
byte[] readBytes = new byte[read];
|
||||||
|
System.arraycopy(buffer, 0, readBytes, 0, read);
|
||||||
|
listener.onAudio(readBytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,6 +122,20 @@ public class ListenActivity extends Activity
|
|||||||
final TextView statusText = (TextView) findViewById(R.id.textStatus);
|
final TextView statusText = (TextView) findViewById(R.id.textStatus);
|
||||||
statusText.setText(R.string.listening);
|
statusText.setText(R.string.listening);
|
||||||
|
|
||||||
|
final VolumeView volumeView = (VolumeView) findViewById(R.id.volume);
|
||||||
|
|
||||||
|
final AudioListener listener = new AudioListener() {
|
||||||
|
@Override
|
||||||
|
public void onAudio(final byte[] audioData) {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
volumeView.onAudioData(audioData);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
_listenThread = new Thread(new Runnable()
|
_listenThread = new Thread(new Runnable()
|
||||||
{
|
{
|
||||||
@@ -129,7 +145,7 @@ public class ListenActivity extends Activity
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
final Socket socket = new Socket(_address, _port);
|
final Socket socket = new Socket(_address, _port);
|
||||||
streamAudio(socket);
|
streamAudio(socket, listener);
|
||||||
}
|
}
|
||||||
catch (UnknownHostException e)
|
catch (UnknownHostException e)
|
||||||
{
|
{
|
||||||
|
|||||||
58
app/src/main/java/protect/babymonitor/VolumeView.java
Normal file
58
app/src/main/java/protect/babymonitor/VolumeView.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package protect.babymonitor;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
public class VolumeView extends View {
|
||||||
|
private double volume = 0;
|
||||||
|
private double maxVolume = 0;
|
||||||
|
|
||||||
|
public VolumeView(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public VolumeView(Context context, @Nullable AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public VolumeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
||||||
|
super(context, attrs, defStyleAttr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onAudioData(byte[] data) {
|
||||||
|
double sum = 0;
|
||||||
|
for (int i = 0; i < data.length; i++) {
|
||||||
|
double rel = Math.abs(data[i]) / (double)32767f;
|
||||||
|
sum+=rel;
|
||||||
|
}
|
||||||
|
volume = sum/data.length;
|
||||||
|
if (volume > maxVolume) {
|
||||||
|
maxVolume = volume;
|
||||||
|
}
|
||||||
|
invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDraw(Canvas canvas) {
|
||||||
|
double relativeBrightness = 0;
|
||||||
|
if (maxVolume > 0) {
|
||||||
|
relativeBrightness = volume / (float) maxVolume;
|
||||||
|
relativeBrightness = Math.max(0.2, relativeBrightness );
|
||||||
|
}
|
||||||
|
int blue;
|
||||||
|
int rest;
|
||||||
|
if (relativeBrightness > 0.5) {
|
||||||
|
blue = 255;
|
||||||
|
rest = (int) (2 * 255 * (relativeBrightness - 0.5));
|
||||||
|
} else {
|
||||||
|
blue = (int) (2 * 255 * relativeBrightness);
|
||||||
|
rest = 0;
|
||||||
|
}
|
||||||
|
int rgb = Color.rgb(rest, rest, blue);
|
||||||
|
canvas.drawColor(rgb);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,10 +5,10 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:keepScreenOn="true"
|
android:keepScreenOn="true"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
|
||||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
|
||||||
android:paddingTop="@dimen/activity_vertical_margin"
|
android:paddingTop="@dimen/activity_vertical_margin"
|
||||||
|
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||||
|
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||||
tools:context="protect.babymonitor.MonitorActivity">
|
tools:context="protect.babymonitor.MonitorActivity">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
@@ -55,4 +55,13 @@
|
|||||||
android:text=""
|
android:text=""
|
||||||
android:textSize="20sp" />
|
android:textSize="20sp" />
|
||||||
|
|
||||||
|
<Space
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="15dip" />
|
||||||
|
|
||||||
|
<protect.babymonitor.VolumeView
|
||||||
|
android:id="@+id/volume"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
Reference in New Issue
Block a user