下面列出了android.media.audiofx.AcousticEchoCanceler#create ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void enableEchoCanceler() {
if (AcousticEchoCanceler.isAvailable() && acousticEchoCanceler == null) {
acousticEchoCanceler = AcousticEchoCanceler.create(microphoneId);
acousticEchoCanceler.setEnabled(true);
Log.i(TAG, "EchoCanceler enabled");
} else {
Log.e(TAG, "This device don't support EchoCanceler");
}
}
/**
* Attempt to set enhancers available on modern devices.
* <p/>
* These are hardware dependent, not build version. Although the APIs weren't available to
* devices until API Level 16
*/
@SuppressWarnings("NewApi")
private void setEnhancers(final int sessionId) {
if (!DEBUG) {
NoiseSuppressor.create(sessionId);
AcousticEchoCanceler.create(sessionId);
AutomaticGainControl.create(sessionId);
} else {
if (NoiseSuppressor.create(sessionId) == null) {
MyLog.i(CLS_NAME, "NoiseSuppressor null");
} else {
MyLog.i(CLS_NAME, "NoiseSuppressor success");
}
if (AcousticEchoCanceler.create(sessionId) == null) {
MyLog.i(CLS_NAME, "AcousticEchoCanceler null");
} else {
MyLog.i(CLS_NAME, "AcousticEchoCanceler success");
}
if (AutomaticGainControl.create(sessionId) == null) {
MyLog.i(CLS_NAME, "AutomaticGainControl null");
} else {
MyLog.i(CLS_NAME, "AutomaticGainControl success");
}
}
}
public SpeechRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes,
boolean noise, boolean gain, boolean echo)
throws IllegalArgumentException {
super(audioSource, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
Log.i("Trying to enhance audio because running on SDK " + Build.VERSION.SDK_INT);
int audioSessionId = getAudioSessionId();
if (noise) {
if (NoiseSuppressor.create(audioSessionId) == null) {
Log.i("NoiseSuppressor: failed");
} else {
Log.i("NoiseSuppressor: ON");
}
} else {
Log.i("NoiseSuppressor: OFF");
}
if (gain) {
if (AutomaticGainControl.create(audioSessionId) == null) {
Log.i("AutomaticGainControl: failed");
} else {
Log.i("AutomaticGainControl: ON");
}
} else {
Log.i("AutomaticGainControl: OFF");
}
if (echo) {
if (AcousticEchoCanceler.create(audioSessionId) == null) {
Log.i("AcousticEchoCanceler: failed");
} else {
Log.i("AcousticEchoCanceler: ON");
}
} else {
Log.i("AcousticEchoCanceler: OFF");
}
}
}
public SpeechRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes,
boolean noise, boolean gain, boolean echo)
throws IllegalArgumentException {
super(audioSource, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
Log.i("Trying to enhance audio because running on SDK " + Build.VERSION.SDK_INT);
int audioSessionId = getAudioSessionId();
if (noise) {
if (NoiseSuppressor.create(audioSessionId) == null) {
Log.i("NoiseSuppressor: failed");
} else {
Log.i("NoiseSuppressor: ON");
}
} else {
Log.i("NoiseSuppressor: OFF");
}
if (gain) {
if (AutomaticGainControl.create(audioSessionId) == null) {
Log.i("AutomaticGainControl: failed");
} else {
Log.i("AutomaticGainControl: ON");
}
} else {
Log.i("AutomaticGainControl: OFF");
}
if (echo) {
if (AcousticEchoCanceler.create(audioSessionId) == null) {
Log.i("AcousticEchoCanceler: failed");
} else {
Log.i("AcousticEchoCanceler: ON");
}
} else {
Log.i("AcousticEchoCanceler: OFF");
}
}
}
public SpeechRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes,
boolean noise, boolean gain, boolean echo)
throws IllegalArgumentException {
super(audioSource, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
Log.i("Trying to enhance audio because running on SDK " + Build.VERSION.SDK_INT);
int audioSessionId = getAudioSessionId();
if (noise) {
if (NoiseSuppressor.create(audioSessionId) == null) {
Log.i("NoiseSuppressor: failed");
} else {
Log.i("NoiseSuppressor: ON");
}
} else {
Log.i("NoiseSuppressor: OFF");
}
if (gain) {
if (AutomaticGainControl.create(audioSessionId) == null) {
Log.i("AutomaticGainControl: failed");
} else {
Log.i("AutomaticGainControl: ON");
}
} else {
Log.i("AutomaticGainControl: OFF");
}
if (echo) {
if (AcousticEchoCanceler.create(audioSessionId) == null) {
Log.i("AcousticEchoCanceler: failed");
} else {
Log.i("AcousticEchoCanceler: ON");
}
} else {
Log.i("AcousticEchoCanceler: OFF");
}
}
}
/**
* 音声をレコードして、MediaCodec に渡します.
*/
private void recordAudio() throws NativeInterfaceException {
int samplingRate = mSamplingRate.getValue();
int channels = mChannels == 1 ? AudioFormat.CHANNEL_IN_MONO : AudioFormat.CHANNEL_IN_STEREO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int bufferSize = AudioRecord.getMinBufferSize(samplingRate, channels, audioFormat) * 4;
int oneFrameDataCount = mSamplingRate.getValue() / mFrameSize.getFps();
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
samplingRate,
channels,
audioFormat,
bufferSize);
if (mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
if (mAudioRecordCallback != null) {
mAudioRecordCallback.onEncoderError();
}
return;
}
if (mUseAEC && AcousticEchoCanceler.isAvailable()) {
// ノイズキャンセラー
mEchoCanceler = AcousticEchoCanceler.create(mAudioRecord.getAudioSessionId());
if (mEchoCanceler != null) {
int ret = mEchoCanceler.setEnabled(true);
if (ret != AudioEffect.SUCCESS) {
if (DEBUG) {
Log.w(TAG, "AcousticEchoCanceler is not supported.");
}
}
}
}
OpusEncoder opusEncoder = null;
try {
opusEncoder = new OpusEncoder(mSamplingRate, mChannels, mFrameSize, mBitRate, mApplication);
mAudioRecord.startRecording();
short[] emptyBuffer = new short[oneFrameDataCount];
short[] pcmBuffer = new short[oneFrameDataCount];
byte[] opusFrameBuffer = opusEncoder.bufferAllocate();
while (!mStopFlag) {
int readSize = mAudioRecord.read(pcmBuffer, 0, oneFrameDataCount);
if (readSize > 0) {
int opusFrameBufferLength;
if (isMute()) {
opusFrameBufferLength = opusEncoder.encode(emptyBuffer, readSize, opusFrameBuffer);
} else {
opusFrameBufferLength = opusEncoder.encode(pcmBuffer, readSize, opusFrameBuffer);
}
if (opusFrameBufferLength > 0 && mAudioRecordCallback != null) {
mAudioRecordCallback.onPeriodicNotification(opusFrameBuffer, opusFrameBufferLength);
}
} else if (readSize == AudioRecord.ERROR_INVALID_OPERATION) {
if (DEBUG) {
Log.e(TAG, "Invalid operation error.");
}
break;
} else if (readSize == AudioRecord.ERROR_BAD_VALUE) {
if (DEBUG) {
Log.e(TAG, "Bad value error.");
}
break;
} else if (readSize == AudioRecord.ERROR) {
if (DEBUG) {
Log.e(TAG, "Unknown error.");
}
break;
}
}
} finally {
if (mEchoCanceler != null) {
mEchoCanceler.release();
mEchoCanceler = null;
}
if (opusEncoder != null) {
opusEncoder.release();
}
}
}
/**
* AudioRecord を開始します.
*/
private void startAudioRecord() {
AudioQuality audioQuality = getAudioQuality();
mBufferSize = AudioRecord.getMinBufferSize(audioQuality.getSamplingRate(),
audioQuality.getChannel(), audioQuality.getFormat()) * 2;
if (DEBUG) {
Log.d(TAG, "AudioQuality: " + audioQuality);
}
mMuteBuffer = new byte[mBufferSize];
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
audioQuality.getSamplingRate(),
audioQuality.getChannel(),
audioQuality.getFormat(),
mBufferSize);
if (mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
postOnError(new MediaEncoderException("AudioRecord is already initialized."));
return;
}
if (mAudioQuality.isUseAEC() && AcousticEchoCanceler.isAvailable()) {
// ノイズキャンセラー
mEchoCanceler = AcousticEchoCanceler.create(mAudioRecord.getAudioSessionId());
if (mEchoCanceler != null) {
int ret = mEchoCanceler.setEnabled(true);
if (ret != AudioEffect.SUCCESS) {
if (DEBUG) {
Log.w(TAG, "AcousticEchoCanceler is not supported.");
}
}
}
}
mAudioRecord.startRecording();
mAudioThread = new AudioRecordThread();
mAudioThread.setName("MicAACLATMEncoder");
mAudioThread.start();
}