Skip to content

Commit

Permalink
Nightly build, check out the Android @chutsu
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander James Wallar committed Jan 19, 2014
1 parent a6c266d commit 899a6fb
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 81 deletions.
89 changes: 8 additions & 81 deletions app/Locabean/src/com/locaudio/locabean/NodeActivity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.locaudio.locabean;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import android.app.Activity;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
Expand All @@ -19,9 +14,6 @@
import com.musicg.wave.Wave;

public class NodeActivity extends Activity {
private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";

private AudioRecord recorder = null;
private Thread recordingThread = null;
Expand Down Expand Up @@ -53,33 +45,6 @@ private void enableButtons(boolean isRecording) {
enableButton(R.id.btnStop, isRecording);
}

private String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);

if (!file.exists()) {
file.mkdirs();
}

return (file.getAbsolutePath() + "/test" + AUDIO_RECORDER_FILE_EXT_WAV);
}

private String getTempFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);

if (!file.exists()) {
file.mkdirs();
}

File tempFile = new File(filepath, AUDIO_RECORDER_TEMP_FILE);

if (tempFile.exists())
tempFile.delete();

return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
}

private void startRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
WaveWriter.RECORDER_SAMPLERATE, WaveWriter.RECORDER_CHANNELS,
Expand All @@ -95,48 +60,13 @@ private void startRecording() {

@Override
public void run() {
writeAudioDataToFile();
WaveWriter.writeAudioDataToFile(recorder, isRecording);
}
}, "AudioRecorder Thread");

recordingThread.start();
}

private void writeAudioDataToFile() {
byte data[] = new byte[WaveWriter.BUFFER_SIZE];
String filename = getTempFilename();
FileOutputStream os = null;

try {
os = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int read = 0;

if (null != os) {
while (isRecording) {
read = recorder.read(data, 0, WaveWriter.BUFFER_SIZE);

if (AudioRecord.ERROR_INVALID_OPERATION != read) {
try {
os.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}

try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

private void stopRecording() {
if (null != recorder) {
isRecording = false;
Expand All @@ -150,14 +80,10 @@ private void stopRecording() {
recordingThread = null;
}

WaveWriter.copyWaveFile(getTempFilename(), getFilename());
deleteTempFile();
}

private void deleteTempFile() {
File file = new File(getTempFilename());
WaveWriter.copyWaveFile(WaveWriter.getTempFilename(),
WaveWriter.getFilename());

file.delete();
WaveWriter.deleteTempFile();
}

private View.OnClickListener btnClick = new View.OnClickListener() {
Expand All @@ -176,9 +102,10 @@ public void onClick(View v) {
System.out.println("Stop Recording");
enableButtons(false);
stopRecording();

Wave wave = new Wave(getFilename());
fingerprintTextView.setText(Arrays.toString(wave.getFingerprint()));

Wave wave = new Wave(WaveWriter.getFilename());
fingerprintTextView.setText(Arrays.toString(wave
.getFingerprint()));
break;
}
}
Expand Down
76 changes: 76 additions & 0 deletions app/Locabean/src/com/locaudio/locabean/WaveWriter.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.locaudio.locabean;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.os.Environment;

public class WaveWriter {

private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
private static final String AUDIO_RECORDER_FILENAME = "locabean_audio";

@SuppressWarnings("deprecation")
protected static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_CONFIGURATION_MONO;
protected static final int RECORDER_BPP = 16;
Expand Down Expand Up @@ -107,4 +115,72 @@ protected static void copyWaveFile(String inFilename, String outFilename) {
}
}

protected static String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);

if (!file.exists()) {
file.mkdirs();
}

return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_FILENAME + AUDIO_RECORDER_FILE_EXT_WAV);
}

protected static String getTempFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);

if (!file.exists()) {
file.mkdirs();
}

File tempFile = new File(filepath, AUDIO_RECORDER_TEMP_FILE);

if (tempFile.exists())
tempFile.delete();

return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
}

protected static void deleteTempFile() {
File file = new File(getTempFilename());

file.delete();
}

protected static void writeAudioDataToFile(AudioRecord recorder,
boolean isRecording) {
byte data[] = new byte[WaveWriter.BUFFER_SIZE];
String filename = getTempFilename();
FileOutputStream os = null;

try {
os = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int read = 0;

if (null != os) {
while (isRecording) {
read = recorder.read(data, 0, WaveWriter.BUFFER_SIZE);

if (AudioRecord.ERROR_INVALID_OPERATION != read) {
try {
os.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}

try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

0 comments on commit 899a6fb

Please sign in to comment.