Skip to content

Commit

Permalink
I am making Java do things it feels uncomfortable doing. I had to eas…
Browse files Browse the repository at this point in the history
…e it into it. First we went to the movies, then I took Java out to dinner. Now, she is coming back to my place to embrace the functionality that I will put forth. Ohhhhh, Java, take it
  • Loading branch information
Alexander James Wallar committed Jan 21, 2014
1 parent e0246f3 commit 9687576
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 18 deletions.
19 changes: 19 additions & 0 deletions app/Locabean/res/layout/activity_node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,23 @@
android:layout_marginBottom="27dp"
android:text="@string/record" />

<TextView
android:id="@+id/splTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/confidenceTextView"
android:layout_below="@+id/confidenceLabel"
android:layout_marginTop="27dp"
android:text="@string/soundPressureLevel" />

<TextView
android:id="@+id/splLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/splTextView"
android:layout_alignBottom="@+id/splTextView"
android:layout_alignLeft="@+id/confidenceLabel"
android:text="@string/averageSPL"
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
2 changes: 2 additions & 0 deletions app/Locabean/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
<string name="nameItl"><it>Sound Name</it></string>
<string name="confidenceItl"><it>Recognition Confidence</it></string>
<string name="send">Send</string>
<string name="averageSPL"><b>Average SPL:</b></string>
<string name="soundPressureLevel"><it>SPL</it></string>

</resources>
84 changes: 81 additions & 3 deletions app/Locabean/src/com/locaudio/api/Locaudio.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.locaudio.api;

import com.locaudio.functional.Function;
import com.locaudio.net.*;

public class Locaudio extends Requests {
Expand All @@ -11,21 +12,98 @@ public Locaudio(String ipAddress, int port) {
super(ipAddress, port);
}

public AsyncGetRequest<SoundLocation[]> getSoundLocations(
final String soundName, final Function<SoundLocation[]> callback) {

AsyncGetRequest<SoundLocation[]> agr = new AsyncGetRequest<SoundLocation[]>(
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<SoundLocation[]> agr = new AsyncGetRequest<SoundLocation[]>(
SoundLocation[].class, LOCATIONS_ROUTE, soundName);
SoundLocation[].class, LOCATIONS_ROUTE, soundName) {

@Override
public void runOnceReceivedResponse(SoundLocation[] response) {
}

};

return agr.getResponse(this);
}

public AsyncGetRequest<String[]> getNames(final Function<String[]> callback) {

AsyncGetRequest<String[]> agr = new AsyncGetRequest<String[]>(
String[].class, NAMES_ROUTE) {

@Override
public void runOnceReceivedResponse(String[] response) {
callback.run(response);
}

};

agr.execute(this);

return agr;
}

@Deprecated
public String[] getNames() {
AsyncGetRequest<String[]> agr = new AsyncGetRequest<String[]>(
String[].class, NAMES_ROUTE);
String[].class, NAMES_ROUTE) {

@Override
public void runOnceReceivedResponse(String[] response) {
}

};
return agr.getResponse(this);
}

public AsyncPostRequest<NotifyResponse> notifyEvent(final NotifyForm event,
final Function<NotifyResponse> callback) {

AsyncPostRequest<NotifyResponse> apr = new AsyncPostRequest<NotifyResponse>(
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<NotifyResponse> apr = new AsyncPostRequest<NotifyResponse>(
NotifyResponse.class, event.toMap(), NOTIFY_ROUTE);
NotifyResponse.class, event.toMap(), NOTIFY_ROUTE) {

@Override
public void runOnceReceivedResponse(NotifyResponse response) {
}

};

return apr.getResponse(this);
}
Expand Down
16 changes: 16 additions & 0 deletions app/Locabean/src/com/locaudio/functional/Function.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.locaudio.functional;

public abstract class Function<T> {
public abstract void run(T input);

@SuppressWarnings("rawtypes")
public static Function getEmptyFunction() {
return new Function() {

@Override
public void run(Object input) {
}

};
}
}
24 changes: 24 additions & 0 deletions app/Locabean/src/com/locaudio/functional/UIFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.locaudio.functional;

import android.app.Activity;

public abstract class UIFunction<T> extends Function<T> {

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);
}
});
}

}
27 changes: 24 additions & 3 deletions app/Locabean/src/com/locaudio/locabean/NodeActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.locaudio.locabean;

// import java.util.Arrays;

import android.app.Activity;
import android.media.AudioRecord;
import android.os.Bundle;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<NotifyResponse>(
self) {

@Override
public void runUI(NotifyResponse nr) {
nameTextView.setText(nr.name);
confidenceTextView.setText("" + nr.confidence);
}
});

break;
}
Expand Down
Loading

0 comments on commit 9687576

Please sign in to comment.