diff --git a/app/Locabean/res/layout/activity_node.xml b/app/Locabean/res/layout/activity_node.xml index 743f07c..894342d 100644 --- a/app/Locabean/res/layout/activity_node.xml +++ b/app/Locabean/res/layout/activity_node.xml @@ -75,4 +75,23 @@ android:layout_marginBottom="27dp" android:text="@string/record" /> + + + + \ No newline at end of file diff --git a/app/Locabean/res/values/strings.xml b/app/Locabean/res/values/strings.xml index 2b1ab3c..ea11e87 100644 --- a/app/Locabean/res/values/strings.xml +++ b/app/Locabean/res/values/strings.xml @@ -11,5 +11,7 @@ Sound Name Recognition Confidence Send + Average SPL: + SPL diff --git a/app/Locabean/src/com/locaudio/api/Locaudio.java b/app/Locabean/src/com/locaudio/api/Locaudio.java index 2394cad..17d3cd2 100644 --- a/app/Locabean/src/com/locaudio/api/Locaudio.java +++ b/app/Locabean/src/com/locaudio/api/Locaudio.java @@ -1,5 +1,6 @@ package com.locaudio.api; +import com.locaudio.functional.Function; import com.locaudio.net.*; public class Locaudio extends Requests { @@ -11,21 +12,98 @@ public Locaudio(String ipAddress, int port) { super(ipAddress, port); } + public AsyncGetRequest getSoundLocations( + final String soundName, final Function callback) { + + AsyncGetRequest agr = new AsyncGetRequest( + SoundLocation[].class, LOCATIONS_ROUTE, soundName) { + + @Override + public void runOnceReceivedResponse(SoundLocation[] response) { + callback.run(response); + } + + }; + + agr.execute(this); + + return agr; + } + + @Deprecated public SoundLocation[] getSoundLocations(final String soundName) { + AsyncGetRequest agr = new AsyncGetRequest( - SoundLocation[].class, LOCATIONS_ROUTE, soundName); + SoundLocation[].class, LOCATIONS_ROUTE, soundName) { + + @Override + public void runOnceReceivedResponse(SoundLocation[] response) { + } + + }; + return agr.getResponse(this); } + public AsyncGetRequest getNames(final Function callback) { + + AsyncGetRequest agr = new AsyncGetRequest( + String[].class, NAMES_ROUTE) { + + @Override + public void runOnceReceivedResponse(String[] response) { + callback.run(response); + } + + }; + + agr.execute(this); + + return agr; + } + + @Deprecated public String[] getNames() { AsyncGetRequest agr = new AsyncGetRequest( - String[].class, NAMES_ROUTE); + String[].class, NAMES_ROUTE) { + + @Override + public void runOnceReceivedResponse(String[] response) { + } + + }; return agr.getResponse(this); } + public AsyncPostRequest notifyEvent(final NotifyForm event, + final Function callback) { + + AsyncPostRequest apr = new AsyncPostRequest( + NotifyResponse.class, event.toMap(), NOTIFY_ROUTE) { + + @Override + public void runOnceReceivedResponse(NotifyResponse response) { + callback.run(response); + } + + }; + + apr.execute(this); + + return apr; + } + + @Deprecated public NotifyResponse notifyEvent(final NotifyForm event) { + AsyncPostRequest apr = new AsyncPostRequest( - NotifyResponse.class, event.toMap(), NOTIFY_ROUTE); + NotifyResponse.class, event.toMap(), NOTIFY_ROUTE) { + + @Override + public void runOnceReceivedResponse(NotifyResponse response) { + } + + }; return apr.getResponse(this); } diff --git a/app/Locabean/src/com/locaudio/functional/Function.java b/app/Locabean/src/com/locaudio/functional/Function.java new file mode 100644 index 0000000..427876f --- /dev/null +++ b/app/Locabean/src/com/locaudio/functional/Function.java @@ -0,0 +1,16 @@ +package com.locaudio.functional; + +public abstract class Function { + public abstract void run(T input); + + @SuppressWarnings("rawtypes") + public static Function getEmptyFunction() { + return new Function() { + + @Override + public void run(Object input) { + } + + }; + } +} diff --git a/app/Locabean/src/com/locaudio/functional/UIFunction.java b/app/Locabean/src/com/locaudio/functional/UIFunction.java new file mode 100644 index 0000000..d0cf9bd --- /dev/null +++ b/app/Locabean/src/com/locaudio/functional/UIFunction.java @@ -0,0 +1,24 @@ +package com.locaudio.functional; + +import android.app.Activity; + +public abstract class UIFunction extends Function { + + private Activity activity; + + public UIFunction(Activity activity) { + this.activity = activity; + } + + public abstract void runUI(T input); + + public void run(final T input) { + this.activity.runOnUiThread(new Runnable() { + @Override + public void run() { + runUI(input); + } + }); + } + +} diff --git a/app/Locabean/src/com/locaudio/locabean/NodeActivity.java b/app/Locabean/src/com/locaudio/locabean/NodeActivity.java index b0f2bfd..b420509 100644 --- a/app/Locabean/src/com/locaudio/locabean/NodeActivity.java +++ b/app/Locabean/src/com/locaudio/locabean/NodeActivity.java @@ -1,5 +1,7 @@ package com.locaudio.locabean; +// import java.util.Arrays; + import android.app.Activity; import android.media.AudioRecord; import android.os.Bundle; @@ -9,7 +11,9 @@ import com.musicg.wave.Wave; +import com.locaudio.functional.UIFunction; 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; @@ -19,10 +23,16 @@ 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; @@ -36,6 +46,7 @@ public void onCreate(Bundle savedInstanceState) { nameTextView = (TextView) findViewById(R.id.nameTextView); confidenceTextView = (TextView) findViewById(R.id.confidenceTextView); + splTextView = (TextView) findViewById(R.id.splTextView); locaudio = new Locaudio(IP_ADDRESS, PORT); } @@ -106,6 +117,10 @@ public void onClick(View v) { System.out.println("Stop Recording"); enableButtons(false); stopRecording(); + Wave wave = new Wave(WaveWriter.getFilename()); + splTextView.setText("" + + WaveProcessing + .determineAverageSoundPressureLevel(wave)); break; } @@ -119,9 +134,15 @@ public void onClick(View v) { postForm.setX(1); postForm.setY(0); - NotifyResponse nr = locaudio.notifyEvent(postForm); - nameTextView.setText(nr.name); - confidenceTextView.setText("" + nr.confidence); + locaudio.notifyEvent(postForm, new UIFunction( + self) { + + @Override + public void runUI(NotifyResponse nr) { + nameTextView.setText(nr.name); + confidenceTextView.setText("" + nr.confidence); + } + }); break; } diff --git a/app/Locabean/src/com/locaudio/location/GPSTracker.java b/app/Locabean/src/com/locaudio/location/GPSTracker.java new file mode 100644 index 0000000..6c70f45 --- /dev/null +++ b/app/Locabean/src/com/locaudio/location/GPSTracker.java @@ -0,0 +1,212 @@ +/* + * DISCLAIMER: THIS CODE IS NOT MINE! + * Got it from here: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ + */ + +package com.locaudio.location; + +import android.app.AlertDialog; +import android.app.Service; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Bundle; +import android.os.IBinder; +import android.provider.Settings; +import android.util.Log; + +public class GPSTracker extends Service implements LocationListener { + + private final Context mContext; + + // flag for GPS status + boolean isGPSEnabled = false; + + // flag for network status + boolean isNetworkEnabled = false; + + // flag for GPS status + boolean canGetLocation = false; + + Location location; // location + double latitude; // latitude + double longitude; // longitude + + // The minimum distance to change Updates in meters + private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters + + // The minimum time between updates in milliseconds + private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute + + // Declaring a Location Manager + protected LocationManager locationManager; + + public GPSTracker(Context context) { + this.mContext = context; + getLocation(); + } + + public Location getLocation() { + try { + locationManager = (LocationManager) mContext + .getSystemService(LOCATION_SERVICE); + + // getting GPS status + isGPSEnabled = locationManager + .isProviderEnabled(LocationManager.GPS_PROVIDER); + + // getting network status + isNetworkEnabled = locationManager + .isProviderEnabled(LocationManager.NETWORK_PROVIDER); + + if (!isGPSEnabled && !isNetworkEnabled) { + // no network provider is enabled + } else { + this.canGetLocation = true; + // First get location from Network Provider + if (isNetworkEnabled) { + locationManager.requestLocationUpdates( + LocationManager.NETWORK_PROVIDER, + MIN_TIME_BW_UPDATES, + MIN_DISTANCE_CHANGE_FOR_UPDATES, this); + Log.d("Network", "Network"); + if (locationManager != null) { + location = locationManager + .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + if (location != null) { + latitude = location.getLatitude(); + longitude = location.getLongitude(); + } + } + } + // if GPS Enabled get lat/long using GPS Services + if (isGPSEnabled) { + if (location == null) { + locationManager.requestLocationUpdates( + LocationManager.GPS_PROVIDER, + MIN_TIME_BW_UPDATES, + MIN_DISTANCE_CHANGE_FOR_UPDATES, this); + Log.d("GPS Enabled", "GPS Enabled"); + if (locationManager != null) { + location = locationManager + .getLastKnownLocation(LocationManager.GPS_PROVIDER); + if (location != null) { + latitude = location.getLatitude(); + longitude = location.getLongitude(); + } + } + } + } + } + + } catch (Exception e) { + e.printStackTrace(); + } + + return location; + } + + /** + * Stop using GPS listener Calling this function will stop using GPS in your + * app + * */ + public void stopUsingGPS() { + if (locationManager != null) { + locationManager.removeUpdates(GPSTracker.this); + } + } + + /** + * Function to get latitude + * */ + public double getLatitude() { + if (location != null) { + latitude = location.getLatitude(); + } + + // return latitude + return latitude; + } + + /** + * Function to get longitude + * */ + public double getLongitude() { + if (location != null) { + longitude = location.getLongitude(); + } + + // return longitude + return longitude; + } + + /** + * Function to check GPS/wifi enabled + * + * @return boolean + * */ + public boolean canGetLocation() { + return this.canGetLocation; + } + + /** + * Function to show settings alert dialog On pressing Settings button will + * lauch Settings Options + * */ + public void showSettingsAlert() { + AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); + + // Setting Dialog Title + alertDialog.setTitle("GPS is settings"); + + // Setting Dialog Message + alertDialog + .setMessage("GPS is not enabled. Do you want to go to settings menu?"); + + // On pressing Settings button + alertDialog.setPositiveButton("Settings", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + Intent intent = new Intent( + Settings.ACTION_LOCATION_SOURCE_SETTINGS); + mContext.startActivity(intent); + } + }); + + // on pressing cancel button + alertDialog.setNegativeButton("Cancel", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + + // Showing Alert Message + alertDialog.show(); + } + + @Override + public void onLocationChanged(Location location) { + } + + @Override + public void onProviderDisabled(String provider) { + } + + @Override + public void onProviderEnabled(String provider) { + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + } + + @Override + public IBinder onBind(Intent arg0) { + return null; + } + +} \ No newline at end of file diff --git a/app/Locabean/src/com/locaudio/net/AsyncGetRequest.java b/app/Locabean/src/com/locaudio/net/AsyncGetRequest.java index cdaa03f..b1c4095 100644 --- a/app/Locabean/src/com/locaudio/net/AsyncGetRequest.java +++ b/app/Locabean/src/com/locaudio/net/AsyncGetRequest.java @@ -9,7 +9,8 @@ import com.locaudio.net.Requests; -public class AsyncGetRequest extends AsyncTask { +public abstract class AsyncGetRequest extends + AsyncTask { private Class tClass; private String[] urlParams = null; @@ -18,7 +19,9 @@ public AsyncGetRequest(Class tClass, String... urlParams) { this.tClass = tClass; this.urlParams = urlParams; } - + + public abstract void runOnceReceivedResponse(T response); + public T getResponse(Requests reqs) { try { this.execute(reqs); @@ -31,18 +34,20 @@ public T getResponse(Requests reqs) { return null; } } - + @Override protected T doInBackground(Requests... reqs) { + T resp = null; try { - return reqs[0].get(this.tClass, this.urlParams); + resp = reqs[0].get(this.tClass, this.urlParams); } catch (ClientProtocolException e) { e.printStackTrace(); - return null; } catch (IOException e) { e.printStackTrace(); - return null; } + + runOnceReceivedResponse(resp); + return resp; } } diff --git a/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java b/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java index 4d6e35d..0ad2cd8 100644 --- a/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java +++ b/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java @@ -10,7 +10,8 @@ import com.locaudio.net.Requests; -public class AsyncPostRequest extends AsyncTask { +public abstract class AsyncPostRequest extends + AsyncTask { private Class tClass; private Map postForm = null; @@ -22,7 +23,9 @@ public AsyncPostRequest(Class tClass, Map postForm, this.postForm = postForm; this.urlParams = urlParams; } - + + public abstract void runOnceReceivedResponse(T response); + public T getResponse(Requests reqs) { try { this.execute(reqs); @@ -38,15 +41,17 @@ public T getResponse(Requests reqs) { @Override protected T doInBackground(Requests... reqs) { + T resp = null; try { - return reqs[0].post(this.tClass, this.postForm, this.urlParams); + resp = reqs[0].post(this.tClass, this.postForm, this.urlParams); } catch (ClientProtocolException e) { e.printStackTrace(); - return null; } catch (IOException e) { e.printStackTrace(); - return null; } + + runOnceReceivedResponse(resp); + return resp; } } diff --git a/app/Locabean/src/com/locaudio/signal/WaveProcessing.java b/app/Locabean/src/com/locaudio/signal/WaveProcessing.java index 03533a9..c412546 100644 --- a/app/Locabean/src/com/locaudio/signal/WaveProcessing.java +++ b/app/Locabean/src/com/locaudio/signal/WaveProcessing.java @@ -4,8 +4,17 @@ public class WaveProcessing { - public static short determineAverageApplitude(Wave wave) { + public static float amplitudeToSoundPressureLevel(float amplitude) { + return (float) (20 * Math.log(amplitude) / Math.log(10)); + } + + public static float determineAverageAmplitude(Wave wave) { + return 0; } + public static float determineAverageSoundPressureLevel(Wave wave) { + return amplitudeToSoundPressureLevel(determineAverageAmplitude(wave)); + } + }