Skip to content

Commit

Permalink
IT WORKS
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander James Wallar committed Jan 28, 2014
1 parent 649e063 commit 84945c3
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 125 deletions.
57 changes: 6 additions & 51 deletions app/Locabean/res/layout/activity_node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NodeActivity" >

<TextView
android:id="@+id/nameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnStart"
android:layout_alignParentTop="true"
android:layout_marginTop="79dp"
android:text="@string/name"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/confidenceLabel"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -48,50 +38,15 @@
android:layout_alignLeft="@+id/nameTextView"
android:text="@string/confidenceItl" />

<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnStart"
android:layout_alignParentBottom="true"
android:layout_marginBottom="27dp"
android:text="@string/send" />

<Button
android:id="@+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btnStop"
android:layout_alignLeft="@+id/btnStop"
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:id="@+id/nameLabel"
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:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:layout_marginTop="148dp"
android:text="@string/name"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btnSend"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:text="@string/stop" />

</RelativeLayout>
47 changes: 47 additions & 0 deletions app/Locabean/res/layout/setup_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<EditText
android:id="@+id/editText"
android:hint="Enter some thing"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<requestFocus />
</EditText>

<LinearLayout

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"

android:layout_marginTop="10dp">


<Button
android:id="@+id/save"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SAVE"
/>

<Button
android:id="@+id/cancel"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
/>

</LinearLayout>


</LinearLayout>
44 changes: 44 additions & 0 deletions app/Locabean/src/com/locaudio/api/AcquisitionThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.locaudio.api;

import android.content.Context;
import android.os.Looper;

import com.locaudio.functional.UIFunction;
import com.locaudio.io.WaveWriter;
import com.musicg.wave.Wave;

public class AcquisitionThread extends Thread {

private static final int RECORD_LENGTH = 5;

private Locaudio locaudio;
private UIFunction<NotifyResponse> postCallback;
private Context context;
private boolean shouldContinue;

public AcquisitionThread(Locaudio locaudio, Context context,
UIFunction<NotifyResponse> postCallback) {
//super();
this.locaudio = locaudio;
this.context = context;
this.postCallback = postCallback;
this.shouldContinue = true;
}

public void kill() {
this.shouldContinue = false;
}

@Override
public void run() {
Looper.prepare();
while (this.shouldContinue) {

Wave wave = WaveWriter.record(RECORD_LENGTH);

locaudio.notifyEvent(this.context, wave, this.postCallback);

}
}

}
11 changes: 8 additions & 3 deletions app/Locabean/src/com/locaudio/api/Locaudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.content.Context;

import com.locaudio.functional.Function;
import com.locaudio.io.WaveWriter;
import com.locaudio.functional.UIFunction;
import com.locaudio.net.*;

import com.musicg.wave.Wave;
Expand Down Expand Up @@ -83,7 +83,7 @@ public void runOnceReceivedResponse(String[] response) {
}

public AsyncPostRequest<NotifyResponse> notifyEvent(Context context,
final Function<NotifyResponse, Void> callback) {
Wave wave, final Function<NotifyResponse, Void> callback) {

AsyncPostRequest<NotifyResponse> apr = new AsyncPostRequest<NotifyResponse>(
NotifyResponse.class, NOTIFY_ROUTE) {
Expand All @@ -96,7 +96,6 @@ public void runOnceReceivedResponse(NotifyResponse response) {

};

Wave wave = WaveWriter.getWave();
apr.setPostForm(NotifyForm.getDefaultNotifyForm(wave, context));
apr.execute(this);

Expand Down Expand Up @@ -136,4 +135,10 @@ public void runOnceReceivedResponse(NotifyResponse response) {

return apr.getResponse(this);
}

public AcquisitionThread getAcquisitionThread(Context context,
UIFunction<NotifyResponse> callback) {
return new AcquisitionThread(this, context, callback);

}
}
20 changes: 18 additions & 2 deletions app/Locabean/src/com/locaudio/io/WaveWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public static AudioRecord getAudioRecord() {
RECORDER_AUDIO_ENCODING, BUFFER_SIZE);
}

public static Wave getWave() {
private static Wave getWave() {
return new Wave(getFilename());
}

Expand All @@ -228,7 +228,7 @@ public static void startRecording() {
recordingThread.start();
}

public static void stopRecording() {
public static Wave stopRecording() {
if (null != recorder) {
isRecording = false;

Expand All @@ -246,6 +246,22 @@ public static void stopRecording() {
copyWaveFile(getTempFilename(), getFilename());

deleteTempFile();
return getWave();
}

public static Wave record(int seconds) {

try {
startRecording();
Thread.sleep(seconds * 1000);
stopRecording();
return getWave();
} catch (InterruptedException e) {
e.printStackTrace();
stopRecording();
return null;
}

}

}
100 changes: 31 additions & 69 deletions app/Locabean/src/com/locaudio/locabean/NodeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,61 @@

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.musicg.wave.Wave;

import com.locaudio.functional.UIFunction;
import com.locaudio.io.WaveWriter;
import com.locaudio.signal.WaveProcessing;
import com.locaudio.api.AcquisitionThread;
import com.locaudio.api.Locaudio;
import com.locaudio.api.NotifyResponse;

public class NodeActivity extends Activity {

private TextView nameTextView = null;
private TextView confidenceTextView = null;
private TextView splTextView = null;
private TextView nameTextView;
private TextView confidenceTextView;

private Locaudio locaudio = null;
private Locaudio locaudio;

private static final String IP_ADDRESS = "192.168.1.9";
private static final String IP_ADDRESS = "138.251.207.84";
private static final int PORT = 8000;

private AcquisitionThread acquisitionThread;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_node);

setupTextViews();
setupLocaudio();
}

setButtonHandlers();
enableButtons(false);
@Override
public void onStop() {
super.onStop();
acquisitionThread.kill();
}

private void setupTextViews() {
nameTextView = (TextView) findViewById(R.id.nameTextView);
confidenceTextView = (TextView) findViewById(R.id.confidenceTextView);
splTextView = (TextView) findViewById(R.id.splTextView);
}

private void setupLocaudio() {
locaudio = new Locaudio(IP_ADDRESS, PORT);
}

private void setButtonHandlers() {
((Button) findViewById(R.id.btnStart))
.setOnClickListener(btnStartClick);
((Button) findViewById(R.id.btnStop)).setOnClickListener(btnStopClick);
((Button) findViewById(R.id.btnSend)).setOnClickListener(btnSendClick);
}
acquisitionThread = locaudio.getAcquisitionThread(
getApplicationContext(), new UIFunction<NotifyResponse>(this) {

private void enableButton(int id, boolean isEnable) {
((Button) findViewById(id)).setEnabled(isEnable);
}
@Override
public Void body(NotifyResponse nr) {
nameTextView.setText(nr.name);
confidenceTextView.setText("" + nr.confidence);

private void enableButtons(boolean isRecording) {
enableButton(R.id.btnStart, !isRecording);
enableButton(R.id.btnStop, isRecording);
}
return null;
}

});

private View.OnClickListener btnStartClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Start Recording");
enableButtons(true);

WaveWriter.startRecording();
}
};

private View.OnClickListener btnStopClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Stop Recording");
enableButtons(false);
WaveWriter.stopRecording();

Wave wave = WaveWriter.getWave();
splTextView.setText(""
+ WaveProcessing.determineAverageSoundPressureLevel(wave));
}
};

private View.OnClickListener btnSendClick = new View.OnClickListener() {
@Override
public void onClick(View v) {

locaudio.notifyEvent(getApplicationContext(),
new UIFunction<NotifyResponse>(NodeActivity.this) {

@Override
public Void body(NotifyResponse nr) {
nameTextView.setText(nr.name);
confidenceTextView.setText("" + nr.confidence);

return null;
}

});
}
};
acquisitionThread.start();
}
}

0 comments on commit 84945c3

Please sign in to comment.