Skip to content

Commit

Permalink
Added location gathering and automatic post form creation upon the no…
Browse files Browse the repository at this point in the history
…tifyEvent function. Make sure to choose a unit and scale it to Lat and Lng when creating reference data
  • Loading branch information
Alexander James Wallar committed Jan 22, 2014
1 parent ea51a0d commit 27a400c
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 84 deletions.
1 change: 1 addition & 0 deletions app/Locabean/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
Expand Down
45 changes: 37 additions & 8 deletions app/Locabean/src/com/locaudio/api/Locaudio.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.locaudio.api;

import android.content.Context;

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

import com.musicg.wave.Wave;

public class Locaudio extends Requests {
private static final String NAMES_ROUTE = "names";
private static final String NOTIFY_ROUTE = "notify";
Expand All @@ -13,7 +18,8 @@ public Locaudio(String ipAddress, int port) {
}

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

AsyncGetRequest<SoundLocation[]> agr = new AsyncGetRequest<SoundLocation[]>(
SoundLocation[].class, LOCATIONS_ROUTE, soundName) {
Expand Down Expand Up @@ -41,11 +47,12 @@ public void runOnceReceivedResponse(SoundLocation[] response) {
}

};

return agr.getResponse(this);
}

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

AsyncGetRequest<String[]> agr = new AsyncGetRequest<String[]>(
String[].class, NAMES_ROUTE) {
Expand All @@ -56,9 +63,9 @@ public void runOnceReceivedResponse(String[] response) {
}

};

agr.execute(this);

return agr;
}

Expand All @@ -75,19 +82,41 @@ public void runOnceReceivedResponse(String[] response) {
return agr.getResponse(this);
}

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

AsyncPostRequest<NotifyResponse> apr = new AsyncPostRequest<NotifyResponse>(
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<NotifyResponse> notifyEvent(final NotifyForm event,
final Function<NotifyResponse, Void> callback) {

AsyncPostRequest<NotifyResponse> apr = new AsyncPostRequest<NotifyResponse>(
NotifyResponse.class, event.toMap(), NOTIFY_ROUTE) {
NotifyResponse.class, event, NOTIFY_ROUTE) {

@Override
public void runOnceReceivedResponse(NotifyResponse response) {
callback.call(response);
}

};

apr.execute(this);

return apr;
Expand All @@ -97,7 +126,7 @@ public void runOnceReceivedResponse(NotifyResponse response) {
public NotifyResponse notifyEvent(final NotifyForm event) {

AsyncPostRequest<NotifyResponse> apr = new AsyncPostRequest<NotifyResponse>(
NotifyResponse.class, event.toMap(), NOTIFY_ROUTE) {
NotifyResponse.class, event, NOTIFY_ROUTE) {

@Override
public void runOnceReceivedResponse(NotifyResponse response) {
Expand Down
52 changes: 42 additions & 10 deletions app/Locabean/src/com/locaudio/api/NotifyForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {

private float x;
private float y;
Expand All @@ -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;
Expand All @@ -24,7 +31,7 @@ public NotifyForm(float x, float y, float soundPressureLevel,
this.timestamp = timestamp;
this.fingerprint = fingerprint;
}

public Map<String, String> toMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("x", "" + this.x);
Expand All @@ -34,30 +41,55 @@ public Map<String, String> 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;
}
}
}
9 changes: 9 additions & 0 deletions app/Locabean/src/com/locaudio/interfaces/Mappable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.locaudio.interfaces;

import java.util.Map;

public interface Mappable<T, R> {

public Map<T, R> toMap();

}
47 changes: 46 additions & 1 deletion app/Locabean/src/com/locaudio/io/WaveWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
}

}
Loading

0 comments on commit 27a400c

Please sign in to comment.