Created
June 18, 2015 13:57
-
-
Save creotiv/c03d40d46133a2d121ec to your computer and use it in GitHub Desktop.
record & playback
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
| // Audio requirements | |
| import android.media.AudioTrack; | |
| import android.media.AudioManager; | |
| import android.media.AudioFormat; | |
| class AudioThread extends Thread { | |
| private boolean _audioRun = false; | |
| private int _reqWrite = 0; | |
| private int _ackWrite = 0; | |
| private boolean _init = true; | |
| private AudioTrack _bAudioTrack; | |
| private int _fragSize, ac; | |
| private short[] _audioBuffer; | |
| private void _bAudioInit() { | |
| int _i; | |
| if (_init) { | |
| _init = false; | |
| _minSize = AudioTrack.getMinBufferSize( | |
| samplerate, | |
| AudioFormat.CHANNEL_CONFIGURATION_MONO, | |
| AudioFormat.ENCODING_PCM_16BIT); | |
| if ((samplecount * 2) < _minSize) | |
| { | |
| bDbg("bristol", "BUFFER SIZE MISMATCH " + _minSize + " is bigger than buffer for " + samplecount + " samples"); | |
| _fragSize = _minSize / 2; | |
| } else | |
| _fragSize = samplecount; | |
| audiocycle = (long) (1000 * _fragSize / samplerate); | |
| _bAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, | |
| samplerate, | |
| AudioFormat.CHANNEL_CONFIGURATION_MONO, | |
| AudioFormat.ENCODING_PCM_16BIT, | |
| _fragSize * 2, | |
| AudioTrack.MODE_STREAM); | |
| _bAudioTrack.stop(); | |
| _bAudioTrack.flush(); | |
| _bAudioTrack.setStereoVolume(1.0f, 1.0f); | |
| _bAudioTrack.play(); | |
| _bAudioTrack.setPositionNotificationPeriod(_fragSize); | |
| _bAudioTrack.setNotificationMarkerPosition(22100); | |
| _audioBuffer = new short[_fragSize]; | |
| _bAudioTrack.setPlaybackPositionUpdateListener( | |
| new AudioTrack.OnPlaybackPositionUpdateListener() { | |
| public void onPeriodicNotification(AudioTrack track) { | |
| _reqWrite++; | |
| bDbg("bristol", netSfBristolDebug()); | |
| } | |
| public void onMarkerReached(AudioTrack track) { | |
| //bDbg("bristol", "onMarkerReached "); | |
| } | |
| } | |
| ); | |
| } | |
| _ackWrite = 0; | |
| _reqWrite = buffercount; | |
| for (_i = 0; _i < _fragSize; _i++) | |
| _audioBuffer[_i] = 0; | |
| } | |
| public void setRunning(boolean run) { | |
| if (run == false) | |
| { | |
| netSfBristolAudio(2, _audioBuffer); | |
| _bAudioTrack.flush(); | |
| } else { | |
| netSfBristolAudio(1, _audioBuffer); | |
| _bAudioInit(); | |
| } | |
| _audioRun = run; | |
| } | |
| @Override | |
| public void run() { | |
| while (_audioRun) { | |
| _ackWrite++; | |
| netSfBristolAudio(_fragSize, _audioBuffer); | |
| if (_bAudioTrack.write(_audioBuffer, 0, _fragSize) | |
| != _fragSize) | |
| bDbg("bristol", "audio write failed (0)"); | |
| } | |
| } | |
| } | |
| package app.test; | |
| import android.app.Activity; | |
| import android.media.AudioFormat; | |
| import android.media.AudioRecord; | |
| import android.media.MediaRecorder; | |
| import android.media.AudioRecord.OnRecordPositionUpdateListener; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| public class Test extends Activity { | |
| private int mAudioBufferSize; | |
| private int mAudioBufferSampleSize; | |
| private AudioRecord mAudioRecord; | |
| private boolean inRecordMode = false; | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| initAudioRecord(); | |
| } | |
| @Override | |
| public void onResume() { | |
| super.onResume(); | |
| inRecordMode = true; | |
| Thread t = new Thread(new Runnable() { | |
| @Override | |
| public void run() { | |
| getSamples(); | |
| } | |
| }); | |
| t.start(); | |
| } | |
| protected void onPause() { | |
| inRecordMode = false; | |
| super.onPause(); | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| if(mAudioRecord != null) { | |
| mAudioRecord.release(); | |
| } | |
| super.onDestroy(); | |
| } | |
| public OnRecordPositionUpdateListener mListener = new OnRecordPositionUpdateListener() { | |
| public void onPeriodicNotification(AudioRecord recorder) { | |
| } | |
| public void onMarkerReached(AudioRecord recorder) { | |
| inRecordMode = false; | |
| } | |
| }; | |
| private void initAudioRecord() { | |
| try { | |
| int sampleRate = 8000; | |
| int channelConfig = AudioFormat.CHANNEL_IN_MONO; | |
| int audioFormat = AudioFormat.ENCODING_PCM_16BIT; | |
| mAudioBufferSize = 2 * AudioRecord.getMinBufferSize(sampleRate, | |
| channelConfig, audioFormat); | |
| mAudioBufferSampleSize = mAudioBufferSize / 2; | |
| mAudioRecord = new AudioRecord( | |
| MediaRecorder.AudioSource.MIC, | |
| sampleRate, | |
| channelConfig, | |
| audioFormat, | |
| mAudioBufferSize); | |
| } catch (IllegalArgumentException e) { | |
| e.printStackTrace(); | |
| } | |
| mAudioRecord.setNotificationMarkerPosition(10000); | |
| mAudioRecord.setPositionNotificationPeriod(1000); | |
| mAudioRecord.setRecordPositionUpdateListener(mListener); | |
| int audioRecordState = mAudioRecord.getState(); | |
| if(audioRecordState != AudioRecord.STATE_INITIALIZED) { | |
| finish(); | |
| } | |
| } | |
| private void getSamples() { | |
| if(mAudioRecord == null) return; | |
| short[] audioBuffer = new short[mAudioBufferSampleSize]; | |
| mAudioRecord.startRecording(); | |
| int audioRecordingState = mAudioRecord.getRecordingState(); | |
| if(audioRecordingState != AudioRecord.RECORDSTATE_RECORDING) { | |
| finish(); | |
| } | |
| while(inRecordMode) { | |
| int samplesRead = mAudioRecord.read(audioBuffer, 0, mAudioBufferSampleSize); | |
| } | |
| mAudioRecord.stop(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment