From 899a6fbee7fdf79692677e08d8e0c25771316e48 Mon Sep 17 00:00:00 2001 From: Alexander James Wallar Date: Sun, 19 Jan 2014 02:25:22 -0500 Subject: [PATCH] Nightly build, check out the Android @chutsu --- .../com/locaudio/locabean/NodeActivity.java | 89 ++----------------- .../src/com/locaudio/locabean/WaveWriter.java | 76 ++++++++++++++++ 2 files changed, 84 insertions(+), 81 deletions(-) diff --git a/app/Locabean/src/com/locaudio/locabean/NodeActivity.java b/app/Locabean/src/com/locaudio/locabean/NodeActivity.java index 7aa49f8..2cb6c42 100644 --- a/app/Locabean/src/com/locaudio/locabean/NodeActivity.java +++ b/app/Locabean/src/com/locaudio/locabean/NodeActivity.java @@ -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; @@ -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; @@ -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, @@ -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; @@ -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() { @@ -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; } } diff --git a/app/Locabean/src/com/locaudio/locabean/WaveWriter.java b/app/Locabean/src/com/locaudio/locabean/WaveWriter.java index 60f8198..03391cb 100644 --- a/app/Locabean/src/com/locaudio/locabean/WaveWriter.java +++ b/app/Locabean/src/com/locaudio/locabean/WaveWriter.java @@ -1,5 +1,6 @@ package com.locaudio.locabean; +import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -7,8 +8,15 @@ 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; @@ -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(); + } + } + } }