From 27a400ce94cd4887238888251158dd26732397c2 Mon Sep 17 00:00:00 2001 From: Alexander James Wallar Date: Wed, 22 Jan 2014 00:31:06 -0500 Subject: [PATCH] Added location gathering and automatic post form creation upon the notifyEvent function. Make sure to choose a unit and scale it to Lat and Lng when creating reference data --- app/Locabean/AndroidManifest.xml | 1 + .../src/com/locaudio/api/Locaudio.java | 45 ++++++++++--- .../src/com/locaudio/api/NotifyForm.java | 52 ++++++++++++--- .../src/com/locaudio/interfaces/Mappable.java | 9 +++ .../src/com/locaudio/io/WaveWriter.java | 47 ++++++++++++- .../com/locaudio/locabean/NodeActivity.java | 66 ++----------------- .../com/locaudio/net/AsyncPostRequest.java | 22 +++++-- .../com/locaudio/signal/WaveProcessing.java | 2 +- 8 files changed, 160 insertions(+), 84 deletions(-) create mode 100644 app/Locabean/src/com/locaudio/interfaces/Mappable.java diff --git a/app/Locabean/AndroidManifest.xml b/app/Locabean/AndroidManifest.xml index ea10272..11f2af4 100644 --- a/app/Locabean/AndroidManifest.xml +++ b/app/Locabean/AndroidManifest.xml @@ -10,6 +10,7 @@ + getSoundLocations( - final String soundName, final Function callback) { + final String soundName, + final Function callback) { AsyncGetRequest agr = new AsyncGetRequest( SoundLocation[].class, LOCATIONS_ROUTE, soundName) { @@ -41,11 +47,12 @@ public void runOnceReceivedResponse(SoundLocation[] response) { } }; - + return agr.getResponse(this); } - public AsyncGetRequest getNames(final Function callback) { + public AsyncGetRequest getNames( + final Function callback) { AsyncGetRequest agr = new AsyncGetRequest( String[].class, NAMES_ROUTE) { @@ -56,9 +63,9 @@ public void runOnceReceivedResponse(String[] response) { } }; - + agr.execute(this); - + return agr; } @@ -75,11 +82,33 @@ public void runOnceReceivedResponse(String[] response) { return agr.getResponse(this); } + public AsyncPostRequest notifyEvent(Context context, + final Function callback) { + + AsyncPostRequest apr = new AsyncPostRequest( + NotifyResponse.class, NOTIFY_ROUTE) { + + @Override + public void runOnceReceivedResponse(NotifyResponse response) { + + callback.call(response); + } + + }; + + Wave wave = WaveWriter.getWave(); + apr.setPostForm(NotifyForm.getDefaultNotifyForm(wave, context)); + apr.execute(this); + + return apr; + + } + public AsyncPostRequest notifyEvent(final NotifyForm event, final Function callback) { AsyncPostRequest apr = new AsyncPostRequest( - NotifyResponse.class, event.toMap(), NOTIFY_ROUTE) { + NotifyResponse.class, event, NOTIFY_ROUTE) { @Override public void runOnceReceivedResponse(NotifyResponse response) { @@ -87,7 +116,7 @@ public void runOnceReceivedResponse(NotifyResponse response) { } }; - + apr.execute(this); return apr; @@ -97,7 +126,7 @@ public void runOnceReceivedResponse(NotifyResponse response) { public NotifyResponse notifyEvent(final NotifyForm event) { AsyncPostRequest apr = new AsyncPostRequest( - NotifyResponse.class, event.toMap(), NOTIFY_ROUTE) { + NotifyResponse.class, event, NOTIFY_ROUTE) { @Override public void runOnceReceivedResponse(NotifyResponse response) { diff --git a/app/Locabean/src/com/locaudio/api/NotifyForm.java b/app/Locabean/src/com/locaudio/api/NotifyForm.java index 489712d..bd9df65 100644 --- a/app/Locabean/src/com/locaudio/api/NotifyForm.java +++ b/app/Locabean/src/com/locaudio/api/NotifyForm.java @@ -4,7 +4,14 @@ import java.util.HashMap; import java.util.Map; -public class NotifyForm { +import android.content.Context; + +import com.locaudio.interfaces.Mappable; +import com.locaudio.location.GPSTracker; +import com.locaudio.signal.WaveProcessing; +import com.musicg.wave.Wave; + +public class NotifyForm implements Mappable { private float x; private float y; @@ -13,9 +20,9 @@ public class NotifyForm { private byte[] fingerprint; public NotifyForm() { - + } - + public NotifyForm(float x, float y, float soundPressureLevel, float timestamp, byte[] fingerprint) { this.x = x; @@ -24,7 +31,7 @@ public NotifyForm(float x, float y, float soundPressureLevel, this.timestamp = timestamp; this.fingerprint = fingerprint; } - + public Map toMap() { Map map = new HashMap(); map.put("x", "" + this.x); @@ -34,30 +41,55 @@ public Map toMap() { map.put("fingerprint", Arrays.toString(this.fingerprint)); return map; } - + public NotifyForm setX(float x) { this.x = x; return this; } - + public NotifyForm setY(float y) { this.y = y; return this; } - + public NotifyForm setSoundPressureLevel(float soundPressureLevel) { this.soundPressureLevel = soundPressureLevel; return this; } - + public NotifyForm setTimestamp(float timestamp) { this.timestamp = timestamp; return this; } - + public NotifyForm setFingerprint(byte[] fingerprint) { this.fingerprint = fingerprint; return this; } - + + public static NotifyForm getDefaultNotifyForm(Wave wave, Context context) { + + GPSTracker gps = new GPSTracker(context); + + // check if GPS enabled + if (gps.canGetLocation()) { + + NotifyForm postForm = new NotifyForm(); + + double latitude = gps.getLatitude(); + double longitude = gps.getLongitude(); + + postForm.setFingerprint(wave.getFingerprint()); + postForm.setSoundPressureLevel(WaveProcessing + .determineAverageSoundPressureLevel(wave)); + postForm.setTimestamp(System.currentTimeMillis()); + postForm.setX((float) latitude); + postForm.setY((float) longitude); + return postForm; + + } else { + gps.showSettingsAlert(); + return null; + } + } } diff --git a/app/Locabean/src/com/locaudio/interfaces/Mappable.java b/app/Locabean/src/com/locaudio/interfaces/Mappable.java new file mode 100644 index 0000000..0e1658f --- /dev/null +++ b/app/Locabean/src/com/locaudio/interfaces/Mappable.java @@ -0,0 +1,9 @@ +package com.locaudio.interfaces; + +import java.util.Map; + +public interface Mappable { + + public Map toMap(); + +} diff --git a/app/Locabean/src/com/locaudio/io/WaveWriter.java b/app/Locabean/src/com/locaudio/io/WaveWriter.java index b39270d..b5ed99c 100644 --- a/app/Locabean/src/com/locaudio/io/WaveWriter.java +++ b/app/Locabean/src/com/locaudio/io/WaveWriter.java @@ -6,6 +6,8 @@ import java.io.FileOutputStream; import java.io.IOException; +import com.musicg.wave.Wave; + import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; @@ -19,7 +21,7 @@ public class WaveWriter { private static final String AUDIO_RECORDER_FILENAME = "locabean_audio"; private static final String AUDIO_RECORDER_THREAD_NAME = "LocabeanRecorderThread"; public static final int AUDIO_RECORDER_ON_STATE = 1; - + @SuppressWarnings("deprecation") protected static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_CONFIGURATION_MONO; protected static final int RECORDER_BPP = 16; @@ -29,6 +31,10 @@ public class WaveWriter { protected static final int BUFFER_SIZE = AudioRecord.getMinBufferSize( RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING); + private static AudioRecord recorder = null; + private static Thread recordingThread = null; + private static boolean isRecording = false; + public static void writeWaveFileHeader(FileOutputStream out, long totalAudioLen, long totalDataLen, long longSampleRate, int channels, long byteRate, int bitsPerSample) throws IOException { @@ -203,4 +209,43 @@ public static AudioRecord getAudioRecord() { RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, BUFFER_SIZE); } + + public static Wave getWave() { + return new Wave(getFilename()); + } + + public static void startRecording() { + recorder = getAudioRecord(); + + int i = recorder.getState(); + if (i == 1) + recorder.startRecording(); + + isRecording = true; + + recordingThread = getRecorderThread(recorder, isRecording); + + recordingThread.start(); + } + + public static void stopRecording() { + if (null != recorder) { + isRecording = false; + + int i = recorder.getState(); + if (i == AUDIO_RECORDER_ON_STATE) { + recorder.stop(); + } + + recorder.release(); + + recorder = null; + recordingThread = null; + } + + copyWaveFile(getTempFilename(), getFilename()); + + deleteTempFile(); + } + } diff --git a/app/Locabean/src/com/locaudio/locabean/NodeActivity.java b/app/Locabean/src/com/locaudio/locabean/NodeActivity.java index d4181ec..ad954b2 100644 --- a/app/Locabean/src/com/locaudio/locabean/NodeActivity.java +++ b/app/Locabean/src/com/locaudio/locabean/NodeActivity.java @@ -1,9 +1,6 @@ package com.locaudio.locabean; -// import java.util.Arrays; - import android.app.Activity; -import android.media.AudioRecord; import android.os.Bundle; import android.view.View; import android.widget.Button; @@ -15,24 +12,16 @@ import com.locaudio.io.WaveWriter; import com.locaudio.signal.WaveProcessing; import com.locaudio.api.Locaudio; -import com.locaudio.api.NotifyForm; import com.locaudio.api.NotifyResponse; public class NodeActivity extends Activity { - private AudioRecord recorder = null; - private Thread recordingThread = null; - private boolean isRecording = false; - private TextView nameTextView = null; private TextView confidenceTextView = null; private TextView splTextView = null; private Locaudio locaudio = null; - // temporary fix - private Activity self = this; - private static final String IP_ADDRESS = "192.168.1.9"; private static final int PORT = 8000; @@ -67,48 +56,13 @@ private void enableButtons(boolean isRecording) { enableButton(R.id.btnStop, isRecording); } - private void startRecording() { - recorder = WaveWriter.getAudioRecord(); - - int i = recorder.getState(); - if (i == 1) - recorder.startRecording(); - - isRecording = true; - - recordingThread = WaveWriter.getRecorderThread(recorder, isRecording); - - recordingThread.start(); - } - - private void stopRecording() { - if (null != recorder) { - isRecording = false; - - int i = recorder.getState(); - if (i == WaveWriter.AUDIO_RECORDER_ON_STATE) { - recorder.stop(); - } - - recorder.release(); - - recorder = null; - recordingThread = null; - } - - WaveWriter.copyWaveFile(WaveWriter.getTempFilename(), - WaveWriter.getFilename()); - - WaveWriter.deleteTempFile(); - } - private View.OnClickListener btnStartClick = new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("Start Recording"); enableButtons(true); - startRecording(); + WaveWriter.startRecording(); } }; @@ -117,8 +71,9 @@ public void onClick(View v) { public void onClick(View v) { System.out.println("Stop Recording"); enableButtons(false); - stopRecording(); - Wave wave = new Wave(WaveWriter.getFilename()); + WaveWriter.stopRecording(); + + Wave wave = WaveWriter.getWave(); splTextView.setText("" + WaveProcessing.determineAverageSoundPressureLevel(wave)); } @@ -127,22 +82,15 @@ public void onClick(View v) { private View.OnClickListener btnSendClick = new View.OnClickListener() { @Override public void onClick(View v) { - Wave wave = new Wave(WaveWriter.getFilename()); - NotifyForm postForm = new NotifyForm(); - postForm.setFingerprint(wave.getFingerprint()); - postForm.setSoundPressureLevel(100); - postForm.setTimestamp(10); - postForm.setX(1); - postForm.setY(0); - locaudio.notifyEvent(postForm, - new UIFunction(self) { + locaudio.notifyEvent(getApplicationContext(), + new UIFunction(NodeActivity.this) { @Override public Void body(NotifyResponse nr) { nameTextView.setText(nr.name); confidenceTextView.setText("" + nr.confidence); - + return null; } diff --git a/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java b/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java index 0ad2cd8..f2a4968 100644 --- a/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java +++ b/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java @@ -1,30 +1,35 @@ package com.locaudio.net; import java.io.IOException; -import java.util.Map; import java.util.concurrent.ExecutionException; import org.apache.http.client.ClientProtocolException; import android.os.AsyncTask; +import com.locaudio.interfaces.Mappable; import com.locaudio.net.Requests; public abstract class AsyncPostRequest extends AsyncTask { private Class tClass; - private Map postForm = null; + private Mappable postForm = null; private String[] urlParams = null; - public AsyncPostRequest(Class tClass, Map postForm, + public abstract void runOnceReceivedResponse(T response); + + public AsyncPostRequest(Class tClass, Mappable postForm, String... urlParams) { this.tClass = tClass; this.postForm = postForm; this.urlParams = urlParams; } - public abstract void runOnceReceivedResponse(T response); + public AsyncPostRequest(Class tClass, String... urlParams) { + this.tClass = tClass; + this.urlParams = urlParams; + } public T getResponse(Requests reqs) { try { @@ -41,9 +46,11 @@ public T getResponse(Requests reqs) { @Override protected T doInBackground(Requests... reqs) { + T resp = null; try { - resp = reqs[0].post(this.tClass, this.postForm, this.urlParams); + resp = reqs[0].post(this.tClass, this.postForm.toMap(), + this.urlParams); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { @@ -54,4 +61,9 @@ protected T doInBackground(Requests... reqs) { return resp; } + public AsyncPostRequest setPostForm(Mappable postForm) { + this.postForm = postForm; + return this; + } + } diff --git a/app/Locabean/src/com/locaudio/signal/WaveProcessing.java b/app/Locabean/src/com/locaudio/signal/WaveProcessing.java index 9155745..e5b1633 100644 --- a/app/Locabean/src/com/locaudio/signal/WaveProcessing.java +++ b/app/Locabean/src/com/locaudio/signal/WaveProcessing.java @@ -18,7 +18,7 @@ public static boolean isPeak(int prevVal, int curVal, int nextVal) { public static float determineAverageAmplitude(Wave wave) { - short threshold = 100; + short threshold = 300; int runningSum = 0; int count = 0;