@@ -4,7 +4,14 @@ import android.Manifest
44import android.content.Context
55import android.content.pm.PackageManager
66import android.media.AudioFormat
7+ import android.media.AudioManager
78import android.media.AudioRecord
9+ import android.media.MediaRecorder
10+ import android.media.audiofx.AcousticEchoCanceler
11+ import android.media.audiofx.AudioEffect
12+ import android.media.audiofx.AutomaticGainControl
13+ import android.media.audiofx.NoiseSuppressor
14+ import android.os.Build
815import android.util.Log
916import androidx.core.content.ContextCompat
1017import io.github.teamclouday.androidMic.domain.service.AudioPacket
@@ -36,6 +43,7 @@ class MicAudioManager(
3643 }
3744
3845 private val recorder: AudioRecord
46+ private val effects: List <AudioEffect >
3947 private val bufferSize: Int
4048 private val buffer: ByteArray
4149 private val bufferFloat: FloatArray
@@ -44,6 +52,10 @@ class MicAudioManager(
4452
4553 private var isMuted = false
4654
55+ // true when the unprocessed source was requested but the device can't provide it
56+ var isUnprocessedFallback = false
57+ private set
58+
4759 init {
4860 // check microphone
4961 require(ctx.packageManager.hasSystemFeature(PackageManager .FEATURE_MICROPHONE )) {
@@ -71,20 +83,41 @@ class MicAudioManager(
7183 " Microphone buffer size ($bufferSize ) is invalid\n Audio format is likely not supported"
7284 }
7385
86+ val isUnprocessed = isUnprocessedSource(audioSource)
87+
88+ // the unprocessed source behaves like the default (processed) one when the device
89+ // doesn't support it, voice recognition is a better match in that case
90+ var source = audioSource
91+ if (isUnprocessed && ! isUnprocessedSupported(ctx)) {
92+ Log .w(TAG , " unprocessed audio source not supported, using voice recognition" )
93+ source = MediaRecorder .AudioSource .VOICE_RECOGNITION
94+ isUnprocessedFallback = true
95+ }
96+
7497 // init recorder
75- recorder = AudioRecord (
76- audioSource,
77- sampleRate,
78- channelConfig,
79- audioFormat,
80- bufferSize,
81- )
98+ var rec = AudioRecord (source, sampleRate, channelConfig, audioFormat, bufferSize)
99+
100+ if (rec.state != AudioRecord .STATE_INITIALIZED && ! isUnprocessedFallback && isUnprocessed) {
101+ Log .w(TAG , " unprocessed audio source failed to initialize, using voice recognition" )
102+ rec.release()
103+ rec = AudioRecord (
104+ MediaRecorder .AudioSource .VOICE_RECOGNITION ,
105+ sampleRate,
106+ channelConfig,
107+ audioFormat,
108+ bufferSize,
109+ )
110+ isUnprocessedFallback = true
111+ }
112+ recorder = rec
82113
83114 // check if recorder is initialized
84115 require(recorder.state == AudioRecord .STATE_INITIALIZED ) {
85116 " Microphone recording failed to initialize"
86117 }
87118
119+ effects = if (isUnprocessed) disablePreProcessing(recorder.audioSessionId) else emptyList()
120+
88121 buffer = ByteArray (bufferSize)
89122 bufferFloat = FloatArray (bufferSize / 4 ) // float is 4 bytes
90123 bufferFloatConvert = ByteBuffer .allocate(bufferSize).order(ByteOrder .nativeOrder())
@@ -177,7 +210,44 @@ class MicAudioManager(
177210 fun shutdown () {
178211 recorder.stop()
179212 recorder.release()
213+ effects.forEach { it.release() }
180214 streamJob?.cancel()
181215 Log .d(TAG , " shutdown" )
182216 }
217+
218+ private fun isUnprocessedSource (source : Int ): Boolean {
219+ return Build .VERSION .SDK_INT >= Build .VERSION_CODES .N && source == MediaRecorder .AudioSource .UNPROCESSED
220+ }
221+
222+ private fun isUnprocessedSupported (ctx : Context ): Boolean {
223+ val audioManager = ctx.getSystemService(Context .AUDIO_SERVICE ) as AudioManager
224+ return audioManager.getProperty(AudioManager .PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED )
225+ .toBoolean()
226+ }
227+
228+ // some vendors attach gain control, noise suppression or echo cancellation
229+ // to every input, explicitly turn them off to keep the signal raw
230+ private fun disablePreProcessing (sessionId : Int ): List <AudioEffect > {
231+ val effects = mutableListOf<AudioEffect >()
232+
233+ if (AutomaticGainControl .isAvailable()) {
234+ AutomaticGainControl .create(sessionId)?.let { effects.add(it) }
235+ }
236+ if (NoiseSuppressor .isAvailable()) {
237+ NoiseSuppressor .create(sessionId)?.let { effects.add(it) }
238+ }
239+ if (AcousticEchoCanceler .isAvailable()) {
240+ AcousticEchoCanceler .create(sessionId)?.let { effects.add(it) }
241+ }
242+
243+ effects.forEach {
244+ try {
245+ it.enabled = false
246+ } catch (e: IllegalStateException ) {
247+ Log .w(TAG , " failed to disable ${it.javaClass.simpleName} : ${e.message} " )
248+ }
249+ }
250+
251+ return effects
252+ }
183253}
0 commit comments