-
Notifications
You must be signed in to change notification settings - Fork 0
Dev blake audio #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blakeelias
wants to merge
20
commits into
dev
Choose a base branch
from
dev-blake-audio
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
37b7351
Add audio recording classes: AudioRecorder (a service) and AudioRecor…
blakeelias 245ceeb
Move audio and video recording to same package
blakeelias e907031
Record a continuous amount of audio until interrupted, then send a br…
blakeelias d88b871
Refactor pollRingBuffer() to mergeBuffers()
blakeelias 2e81246
Remove saveAudio()
blakeelias 1c6904d
Create constant WAV_HEADER_LENGTH
blakeelias 300c2d0
Remove comments in WAV header creation
blakeelias 7d6ac2a
Rename createMemoraDirectory() to createDirectory()
blakeelias a80722e
Remove empty constructor
blakeelias 6060a41
Remove call to setThreadPriority
blakeelias d3bc49d
Fix code style / indentation issues
blakeelias 9b99698
Convert remaining references to com.wearscript.video into com.wearscr…
blakeelias bca7cf9
Remove one more reference to com.wearscript.video
blakeelias d7c07d2
Add constant FILEPATH
blakeelias a01f430
Remove commented code
blakeelias f8a0f44
Move save-file logic inside of while recording loop
blakeelias 9aef108
Save WAV header with long length so that we don't need to edit it whe…
blakeelias dad4016
Add ability to save an updated audio file whenever we want to while c…
blakeelias 440958f
Accept audio file name passed in when recording is started
blakeelias 5b251ea
Have AudioRecordThread accept filePath instead of fileName
blakeelias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
video/src/main/java/com/wearscript/record/AudioRecordThread.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package com.wearscript.record; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.media.AudioFormat; | ||
import android.media.AudioRecord; | ||
import android.media.MediaRecorder.AudioSource; | ||
import android.os.Environment; | ||
import android.util.Log; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
public class AudioRecordThread extends Thread { | ||
|
||
private static final String LOG_TAG = "Audio"; | ||
|
||
private static final int RECORDER_SAMPLERATE = 8000; | ||
private static final int ENCODING_TYPE = AudioFormat.ENCODING_PCM_16BIT; | ||
public static final int WAV_HEADER_LENGTH = 44; | ||
public static final String FILEPATH = "filepath"; | ||
private final int CHANNEL_TYPE = AudioFormat.CHANNEL_IN_MONO; | ||
private final int NUM_CHANNELS = 1; | ||
private byte BITS_PER_SAMPLE = 16; | ||
private final int AUDIO_SOURCE = AudioSource.MIC; | ||
private final int BYTE_RATE = RECORDER_SAMPLERATE * NUM_CHANNELS * (BITS_PER_SAMPLE / 8); | ||
|
||
public static final String directory = Environment.getExternalStorageDirectory() + File.separator + "wearscript"; | ||
public static final String directoryAudio = directory + File.separator+"audio"; | ||
|
||
private final int bufferSize = 160; //Each buffer holds 1/100th of a second. | ||
private boolean pollingBuffer = false; | ||
|
||
AudioRecord recorder = null; | ||
FileOutputStream os = null; | ||
String filePath; | ||
|
||
/** | ||
* Give the thread high priority so that it's not cancelled unexpectedly, and start it | ||
*/ | ||
|
||
private ArrayList<byte[]> buffers; | ||
private byte[] totalBuffer; | ||
|
||
Context context; | ||
|
||
public AudioRecordThread(Context context, String filePath) | ||
{ | ||
this.context = context; | ||
writeWavHeader(filePath); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
Log.d(LOG_TAG, "Running Audio Thread"); | ||
|
||
buffers = new ArrayList<byte[]>(); | ||
int N = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, CHANNEL_TYPE,ENCODING_TYPE); | ||
Log.d(LOG_TAG, "" + N); | ||
try { | ||
recorder = new AudioRecord(AUDIO_SOURCE, RECORDER_SAMPLERATE, CHANNEL_TYPE, ENCODING_TYPE, N*10); | ||
} | ||
catch (Throwable e) { | ||
Log.d(LOG_TAG, e.toString()); | ||
return; | ||
} | ||
recorder.startRecording(); | ||
|
||
try { | ||
while (!interrupted()) { | ||
buffers.add(new byte[bufferSize]); | ||
recorder.read(buffers.get(buffers.size() - 1), 0, bufferSize); | ||
} | ||
} | ||
catch (Throwable x) { | ||
Log.d(LOG_TAG, "Error reading voice audio", x); | ||
} | ||
/* | ||
* Frees the thread's resources after the loop completes so that it can be run again | ||
*/ | ||
finally { | ||
recorder.stop(); | ||
recorder.release(); | ||
context.stopService(new Intent(context, AudioRecorder.class)); | ||
Log.d(LOG_TAG, "Thread Terminated"); | ||
} | ||
} | ||
|
||
private void mergeBuffers() { | ||
Log.d(LOG_TAG, "in mergeBuffers()"); | ||
int i; | ||
int j; | ||
int ix = 0; | ||
|
||
ArrayList<byte[]> copy = (ArrayList<byte[]>) buffers.clone(); | ||
buffers = new ArrayList<byte[]>(); | ||
|
||
totalBuffer = new byte[copy.size() * bufferSize]; | ||
for (i = 0; i < copy.size(); i++) { | ||
for(j = 0; j<bufferSize; j++) { | ||
byte x = copy.get(i)[j]; | ||
totalBuffer[ix] = x; | ||
ix++; | ||
} | ||
} | ||
} | ||
|
||
private String audioFileName(String fileName) { | ||
return directoryAudio + File.separator + fileName + ".wav"; | ||
} | ||
|
||
private void writeWavHeader(String filePath) { | ||
byte header[] = new byte[WAV_HEADER_LENGTH]; | ||
|
||
try { | ||
this.filePath = filePath; | ||
os = new FileOutputStream(filePath); | ||
Log.d(LOG_TAG, "file path: " + filePath); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
/* Test whether Android media player will play back a WAV file whose header indicates the | ||
* wrong length file! */ | ||
int totalAudioLen = Integer.MAX_VALUE - 36; | ||
int totalDataLen = Integer.MAX_VALUE; | ||
|
||
header[0] = 'R'; // RIFF/WAVE header | ||
header[1] = 'I'; | ||
header[2] = 'F'; | ||
header[3] = 'F'; | ||
header[4] = (byte) (totalDataLen & 0xff); | ||
header[5] = (byte) ((totalDataLen >> 8) & 0xff); | ||
header[6] = (byte) ((totalDataLen >> 16) & 0xff); | ||
header[7] = (byte) ((totalDataLen >> 24) & 0xff); | ||
header[8] = 'W'; | ||
header[9] = 'A'; | ||
header[10] = 'V'; | ||
header[11] = 'E'; | ||
header[12] = 'f'; // 'fmt ' chunk | ||
header[13] = 'm'; | ||
header[14] = 't'; | ||
header[15] = ' '; | ||
header[16] = 16; // 4 bytes: size of 'fmt ' chunk | ||
header[17] = 0; | ||
header[18] = 0; | ||
header[19] = 0; | ||
header[20] = 1; // format = 1 | ||
header[21] = 0; | ||
header[22] = (byte) NUM_CHANNELS; | ||
header[23] = 0; | ||
header[24] = (byte) (RECORDER_SAMPLERATE & 0xff); | ||
header[25] = (byte) ((RECORDER_SAMPLERATE >> 8) & 0xff); | ||
header[26] = (byte) ((RECORDER_SAMPLERATE >> 16) & 0xff); | ||
header[27] = (byte) ((RECORDER_SAMPLERATE >> 24) & 0xff); | ||
header[28] = (byte) (BYTE_RATE & 0xff); | ||
header[29] = (byte) ((BYTE_RATE >> 8) & 0xff); | ||
header[30] = (byte) ((BYTE_RATE >> 16) & 0xff); | ||
header[31] = (byte) ((BYTE_RATE >> 24) & 0xff); | ||
header[32] = (byte) (NUM_CHANNELS * BITS_PER_SAMPLE / 8); | ||
header[33] = 0; | ||
header[34] = BITS_PER_SAMPLE; | ||
header[35] = 0; | ||
header[36] = 'd'; | ||
header[37] = 'a'; | ||
header[38] = 't'; | ||
header[39] = 'a'; | ||
header[40] = (byte) (totalAudioLen & 0xff); | ||
header[41] = (byte) ((totalAudioLen >> 8) & 0xff); | ||
header[42] = (byte) ((totalAudioLen >> 16) & 0xff); | ||
header[43] = (byte) ((totalAudioLen >> 24) & 0xff); | ||
|
||
try { | ||
os.write(header, 0, header.length); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void stopRecording() { | ||
Log.d(LOG_TAG, "in stopRecording()"); | ||
writeAudioDataToFile(); | ||
|
||
try { | ||
os.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
interrupt(); | ||
} | ||
|
||
public void writeAudioDataToFile() { | ||
mergeBuffers(); | ||
|
||
try { | ||
os.write(totalBuffer, 0, totalBuffer.length); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
Intent intent = new Intent("com.wearscript.record.FILE_WRITTEN_AUDIO").putExtra(FILEPATH, filePath); | ||
context.sendBroadcast(intent); | ||
Log.d(LOG_TAG, "Sending broadcast: com.wearscript.record.FILE_WRITTEN_AUDIO"); | ||
} | ||
} | ||
|
61 changes: 61 additions & 0 deletions
61
video/src/main/java/com/wearscript/record/AudioRecorder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.wearscript.record; | ||
|
||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.os.IBinder; | ||
import android.util.Log; | ||
|
||
import java.io.File; | ||
|
||
public class AudioRecorder extends Service { | ||
|
||
private final String LOG_TAG = "AudioRecorder"; | ||
|
||
private AudioRecordThread recorder; | ||
public static String MILLIS_EXTRA_KEY = "millis"; | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
|
||
Log.d(LOG_TAG, "Service Started"); | ||
createDirectory(); | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
Log.d(LOG_TAG, "Service Destroy"); | ||
recorder.interrupt(); | ||
super.onDestroy(); | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
// TODO: Return the communication channel to the service. | ||
throw new UnsupportedOperationException("Not yet implemented"); | ||
} | ||
|
||
private void createDirectory() { | ||
File directory = new File(AudioRecordThread.directoryAudio); | ||
if (!directory.isDirectory()){ | ||
directory.mkdirs(); | ||
} | ||
} | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
if (intent.getAction() != null) { | ||
if (intent.getAction().equals("com.wearscript.record.RECORD_AUDIO")) { | ||
recorder = new AudioRecordThread(this, intent.getStringExtra("filePath")); | ||
recorder.start(); | ||
} else if (intent.getAction().equals("com.wearscript.record.SAVE_AUDIO")) { | ||
recorder.writeAudioDataToFile(); | ||
} else if (intent.getAction().equals("com.wearscript.record.STOP_AUDIO")) { | ||
recorder.stopRecording(); | ||
stopSelf(); | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put open brace at the end of previous line. Please apply this convention throughout