public AudioRecordManager() { bufferSize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat .ENCODING_PCM_16BIT); mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize * 2); }
private int getValidBufferSize(int audioSource, int fs, int channelConfiguration, int audioEncoding) { for (int bufferSize : new int[]{ 256, 512, 1024, 2048, 4096 }) { // add the rates you wish to check against AudioRecord audioRecordTemp = new AudioRecord(audioSource, fs, channelConfiguration, audioEncoding, bufferSize); if (audioRecordTemp != null && audioRecordTemp.getState() == AudioRecord.STATE_INITIALIZED) { return bufferSize; } } return 0; }
@TargetApi(18) public static AudioRecord getAudioRecord(AudioConfiguration audioConfiguration) { int frequency = audioConfiguration.frequency; int audioEncoding = audioConfiguration.encoding; int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO; if(audioConfiguration.channelCount == 2) { channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_STEREO; } int audioSource = MediaRecorder.AudioSource.MIC; if(audioConfiguration.aec) { audioSource = MediaRecorder.AudioSource.VOICE_COMMUNICATION; } AudioRecord audioRecord = new AudioRecord(audioSource, frequency, channelConfiguration, audioEncoding, getRecordBufferSize(audioConfiguration)); return audioRecord; } }
/** * Configures the recorder and starts it * @throws IOException * @throws IllegalStateException */ public void start() throws IllegalStateException, IOException { if (recorder == null) { minSize = AudioRecord.getMinBufferSize( 8000, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT); Log.e("AudioCodec", "Minimum size is " + minSize); recorder = new AudioRecord( MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT, minSize); recorder.startRecording(); } }
public AndroidAudioRecorder (int samplingRate, boolean isMono) { int channelConfig = isMono ? AudioFormat.CHANNEL_IN_MONO : AudioFormat.CHANNEL_IN_STEREO; int minBufferSize = AudioRecord.getMinBufferSize(samplingRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT); recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, samplingRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT, minBufferSize); if (recorder.getState() != AudioRecord.STATE_INITIALIZED) throw new GdxRuntimeException("Unable to initialize AudioRecorder.\nDo you have the RECORD_AUDIO permission?"); recorder.startRecording(); }
public AndroidAudioRecorder (int samplingRate, boolean isMono) { int channelConfig = isMono ? AudioFormat.CHANNEL_IN_MONO : AudioFormat.CHANNEL_IN_STEREO; int minBufferSize = AudioRecord.getMinBufferSize(samplingRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT); recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, samplingRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT, minBufferSize); if (recorder.getState() != AudioRecord.STATE_INITIALIZED) throw new GdxRuntimeException("Unable to initialize AudioRecorder.\nDo you have the RECORD_AUDIO permission?"); recorder.startRecording(); }
new AudioRecord(audioSource, SAMPLE_RATE, audioChannel, audioEncoding, bufferSize);
audioRecorder = new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, bufferSize); if (rUncompressed) audioRecorder = new AudioRecord(aSource, sRate, nChannels+1, aFormat, bufferSize);
AudioRecordRunnable(int sampleRate, int channelConfig, int audioFormat, int byteBufferSize, @NonNull AudioDataCallback audioDataCallback) { mAudioFormat = audioFormat; int minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, mAudioFormat); mByteBufferSize = byteBufferSize; mShortBufferSize = mByteBufferSize / 2; mByteBuffer = new byte[mByteBufferSize]; mShortBuffer = new short[mShortBufferSize]; mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, Math.max(minBufferSize, byteBufferSize)); mAudioDataCallback = audioDataCallback; }
@SuppressWarnings("ResultOfMethodCallIgnored") @Override public void runImpl() { int bufferSize = Math.max(BUFFER_BYTES_ELEMENTS * BUFFER_BYTES_PER_ELEMENT, AudioRecord.getMinBufferSize(RECORDER_SAMPLE_RATE, RECORDER_CHANNELS_IN, RECORDER_AUDIO_ENCODING)); AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLE_RATE, RECORDER_CHANNELS_IN, RECORDER_AUDIO_ENCODING, bufferSize); try { if (recorderState == RECORDER_STATE_STARTING) { recorderState = RECORDER_STATE_BUSY; } recorder.startRecording(); byte recordBuffer[] = new byte[bufferSize]; do { int bytesRead = recorder.read(recordBuffer, 0, bufferSize); if (bytesRead > 0) { recordingCallback.onDataReady(recordBuffer); } else { Log.e(AudioRecorder.class.getSimpleName(), "error: " + bytesRead); onRecordFailure(); } } while (recorderState == RECORDER_STATE_BUSY); } finally { recorder.release(); } onExit(); } }).start();
audioRecorder = new AudioRecord(source,sampleRate,nChannels,encoder,bufferSize);
minRecorderBufferSize = SAMPLING_RATE * 2; audioRecord = new AudioRecord( MediaRecorder.AudioSource.MIC, SAMPLING_RATE,
audioRecord = new AudioRecord(...); int state = audioRecord.getState(); if (state != AudioRecord.STATE_INITIALIZED) ...
public RecordThread(Handler handler) { this.mHandler = handler; mBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE_IN_HZ, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, mBufferSize); }
private void initialize() { mBufferSize = AudioRecord.getMinBufferSize(REQUIRED_FREQUENCY, REQUIRED_CHANNEL, REQUIRED_FORMAT); Log.d(TAG, "Recommended bufferSize: " + mBufferSize); if (mBufferSize > 0) { buffer = new short[mBufferSize]; mAudio = new AudioRecord(MediaRecorder.AudioSource.MIC, REQUIRED_FREQUENCY, REQUIRED_CHANNEL, REQUIRED_FORMAT, mBufferSize); if (mAudio.getState() == AudioRecord.STATE_UNINITIALIZED) { Log.d(TAG, "Unable to initialize AudioRecord. Ensure nothing else is using the microphone."); this.mListener.quit(OnUpdateListener.ERROR_CODE_MICROPHONE_LOCKED); mAudio = null; } } }
private void InitAudioRecord() { _iRecorderBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE_DEF, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT); _AudioRecorder = new AudioRecord(AudioSource.MIC, SAMPLE_RATE_DEF, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT, _iRecorderBufferSize); _RecorderBuffer = new byte[_iRecorderBufferSize]; _fdkaacEnc = new FdkAacEncode(); _fdkaacHandle = _fdkaacEnc.FdkAacInit(SAMPLE_RATE_DEF, CHANNEL_NUMBER_DEF); }
private void init() { synchronized (recognizerLock) { final int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ, CHANNEL_CONFIG, AUDIO_FORMAT); audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE_IN_HZ, CHANNEL_CONFIG, AUDIO_FORMAT, bufferSize); vad.setEnabled(config.isVoiceActivityDetectionEnabled()); vad.setSpeechListener(this); mediaPlayer = new MediaPlayer(); mediaPlayer.setOnErrorListener(this); mediaPlayer.setOnCompletionListener(this); } }
private void init() { synchronized (recognizerLock) { final int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ, CHANNEL_CONFIG, AUDIO_FORMAT); audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE_IN_HZ, CHANNEL_CONFIG, AUDIO_FORMAT, bufferSize); vad.setEnabled(config.isVoiceActivityDetectionEnabled()); vad.setSpeechListener(this); mediaPlayer = new MediaPlayer(); mediaPlayer.setOnErrorListener(this); mediaPlayer.setOnCompletionListener(this); } }
/** * Create audio record with params */ public void createMicrophone(int sampleRate, boolean isStereo, boolean echoCanceler, boolean noiseSuppressor) { this.sampleRate = sampleRate; if (!isStereo) channel = AudioFormat.CHANNEL_IN_MONO; audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, sampleRate, channel, audioFormat, getPcmBufferSize() * 4); audioPostProcessEffect = new AudioPostProcessEffect(audioRecord.getAudioSessionId()); if (echoCanceler) audioPostProcessEffect.enableEchoCanceler(); if (noiseSuppressor) audioPostProcessEffect.enableNoiseSuppressor(); String chl = (isStereo) ? "Stereo" : "Mono"; Log.i(TAG, "Microphone created, " + sampleRate + "hz, " + chl); created = true; }
public AACEncoder(final StreamPublisher.StreamPublisherParam params) throws IOException { this.samplingRate = params.samplingRate; bufferSize = params.audioBufferSize; mMediaCodec = MediaCodec.createEncoderByType(params.audioMIME); mMediaCodec.configure(params.createAudioMediaFormat(), null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mediaCodecInputStream = new MediaCodecInputStream(mMediaCodec, new MediaCodecInputStream.MediaFormatCallback() { @Override public void onChangeMediaFormat(MediaFormat mediaFormat) { params.setAudioOutputMediaFormat(mediaFormat); } }); mAudioRecord = new AudioRecord(params.audioSource, samplingRate, params.channelCfg, AudioFormat.ENCODING_PCM_16BIT, bufferSize); if (NoiseSuppressor.isAvailable()) { NoiseSuppressor noiseSuppressor = NoiseSuppressor.create(mAudioRecord.getAudioSessionId()); } }