Feature: add AudioProcessController, include AEC/AGC/NS

pull/209/head
xufuji456 3 years ago
parent c139d9fa4e
commit b294d3d11f
  1. 10
      AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt
  2. 100
      AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioProcessController.kt
  3. 17
      AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioRecordController.kt

@ -46,6 +46,16 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) {
mAudioEffectCallback = audioEffectCallback mAudioEffectCallback = audioEffectCallback
} }
/******************************************************************
AudioEffect
|
______________________|___________________________
| | | | |
Equalizer Virtualizer BassBoost PresetReverb DynamicsProcessing
******************************************************************/
/** /**
* Setup AudioEffect of Equalizer, which uses to adjust the gain of frequency. * Setup AudioEffect of Equalizer, which uses to adjust the gain of frequency.
* There are key params of bandbandLevelcenterFrequency in Equalizer. * There are key params of bandbandLevelcenterFrequency in Equalizer.

@ -0,0 +1,100 @@
package com.frank.androidmedia.controller
import android.media.audiofx.AcousticEchoCanceler
import android.media.audiofx.AudioEffect
import android.media.audiofx.AutomaticGainControl
import android.media.audiofx.NoiseSuppressor
import android.util.Log
import java.lang.Exception
/**
*
* @author frank
* @date 2022/3/23
*/
open class AudioProcessController {
/*************************************************************
AudioEffect
|
____________________|___________________
| | |
AcousticEchoCanceler AutomaticGainControl NoiseSuppressor
**************************************************************/
companion object {
val TAG: String = AudioProcessController::class.java.simpleName
}
private var noiseSuppressor: NoiseSuppressor? = null
private var automaticGainControl: AutomaticGainControl? = null
private var acousticEchoCanceler: AcousticEchoCanceler? = null
fun initAEC(audioSessionId: Int): Boolean {
if (!AcousticEchoCanceler.isAvailable()) {
Log.e(TAG, "AEC not available...")
return false
}
try {
acousticEchoCanceler = AcousticEchoCanceler.create(audioSessionId)
} catch (e: Exception) {
Log.e(TAG, "init AcousticEchoCanceler error=$e")
return false
}
val result = acousticEchoCanceler?.setEnabled(true)
if (result != AudioEffect.SUCCESS) {
acousticEchoCanceler?.release()
acousticEchoCanceler = null
return false
}
return true
}
fun initAGC(audioSessionId: Int): Boolean {
if (!AutomaticGainControl.isAvailable()) {
Log.e(TAG, "AGC not available...")
return false
}
try {
automaticGainControl = AutomaticGainControl.create(audioSessionId)
} catch (e: Exception) {
Log.e(TAG, "init AutomaticGainControl error=$e")
return false
}
val result = automaticGainControl?.setEnabled(true)
if (result != AudioEffect.SUCCESS) {
automaticGainControl?.release()
automaticGainControl = null
return false
}
return true
}
fun initNS(audioSessionId: Int): Boolean {
if (!NoiseSuppressor.isAvailable()) {
Log.e(TAG, "NS not available...")
return false
}
try {
noiseSuppressor = NoiseSuppressor.create(audioSessionId)
} catch (e: Exception) {
Log.e(TAG, "init NoiseSuppressor error=$e")
return false
}
val result = noiseSuppressor?.setEnabled(true)
if (result != AudioEffect.SUCCESS) {
noiseSuppressor?.release()
noiseSuppressor = null
return false
}
return true
}
fun release() {
noiseSuppressor?.release()
acousticEchoCanceler?.release()
automaticGainControl?.release()
}
}

@ -27,6 +27,9 @@ open class AudioRecordController {
private var mAudioRecord: AudioRecord? = null private var mAudioRecord: AudioRecord? = null
private var mRecordThread: RecordThread? = null private var mRecordThread: RecordThread? = null
private val enableAudioProcessor = false
private var mAudioProcessController: AudioProcessController? = null
private fun initAudioRecord() { private fun initAudioRecord() {
val sampleRate = 44100 val sampleRate = 44100
val audioFormat = AudioFormat.ENCODING_PCM_16BIT val audioFormat = AudioFormat.ENCODING_PCM_16BIT
@ -37,6 +40,16 @@ open class AudioRecordController {
channelConfig, channelConfig,
audioFormat, audioFormat,
minBufferSize) minBufferSize)
if (enableAudioProcessor) {
mAudioProcessController = AudioProcessController()
var result:Boolean? = mAudioProcessController?.initAEC(mAudioRecord?.audioSessionId!!)
Log.e(TAG, "init AEC result=$result")
result = mAudioProcessController?.initAGC(mAudioRecord?.audioSessionId!!)
Log.e(TAG, "init AGC result=$result")
result = mAudioProcessController?.initNS(mAudioRecord?.audioSessionId!!)
Log.e(TAG, "init NS result=$result")
}
} }
private class RecordThread(recordPath: String, audioRecord: AudioRecord, bufferSize: Int) : Thread() { private class RecordThread(recordPath: String, audioRecord: AudioRecord, bufferSize: Int) : Thread() {
@ -133,5 +146,9 @@ open class AudioRecordController {
mAudioRecord!!.release() mAudioRecord!!.release()
mAudioRecord = null mAudioRecord = null
} }
if (mAudioProcessController != null) {
mAudioProcessController!!.release()
mAudioProcessController = null
}
} }
} }
Loading…
Cancel
Save