Skip to content

Commit e42e07c

Browse files
authored
Merge pull request teamclouday#185 from emdaitaj/unprocessed-audio-source
Add unprocessed audio source (raw mic for RTA / measurement)
2 parents 379080b + 8f09897 commit e42e07c

6 files changed

Lines changed: 97 additions & 9 deletions

File tree

‎Android/app/src/main/java/io/github/teamclouday/androidMic/Preferences.kt‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ enum class AudioSource {
4343
Mic,
4444
Recognition,
4545
Communication,
46-
Performance;
46+
Performance,
47+
48+
// raw microphone signal, no gain control / noise suppression / EQ
49+
// useful for measurement tools (RTA, SPL meters, ...)
50+
Unprocessed;
4751

4852
fun getSource(): Int {
4953

@@ -56,6 +60,13 @@ enum class AudioSource {
5660
} else {
5761
MediaRecorder.AudioSource.MIC
5862
}
63+
// voice recognition is required by the CDD to have AGC and noise reduction
64+
// disabled, so it is the closest thing to raw audio on older devices
65+
Unprocessed -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
66+
MediaRecorder.AudioSource.UNPROCESSED
67+
} else {
68+
MediaRecorder.AudioSource.VOICE_RECOGNITION
69+
}
5970
}
6071
}
6172
}

‎Android/app/src/main/java/io/github/teamclouday/androidMic/domain/audio/MicAudioManager.kt‎

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import android.Manifest
44
import android.content.Context
55
import android.content.pm.PackageManager
66
import android.media.AudioFormat
7+
import android.media.AudioManager
78
import 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
815
import android.util.Log
916
import androidx.core.content.ContextCompat
1017
import 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\nAudio 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
}

‎Android/app/src/main/java/io/github/teamclouday/androidMic/domain/service/ForegroundService.kt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,11 @@ class ForegroundService : Service() {
359359
Log.d(TAG, "startAudio [recording]")
360360
states.isAudioStarted = true
361361

362-
reply(replyTo, ResponseData(msg = application.getString(R.string.mic_start_recording)))
362+
var startMsg = application.getString(R.string.mic_start_recording)
363+
if (managerAudio?.isUnprocessedFallback == true) {
364+
startMsg += "\n" + application.getString(R.string.unprocessed_not_supported)
365+
}
366+
reply(replyTo, ResponseData(msg = startMsg))
363367

364368
return true
365369
}

‎Android/app/src/main/res/values-fr/strings.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<string name="microphone_not_recording">Le microphone n\'enregistre pas</string>
4343
<string name="connected_device">Appareil connecté:\n</string>
4444
<string name="mic_start_recording">Le microphone a commencé à enregister</string>
45+
<string name="unprocessed_not_supported">La source audio non traitée n\'est pas prise en charge par cet appareil, utilisation de la reconnaissance vocale</string>
4546

4647
<string name="device_disconnected">Appareil déconnecté</string>
4748
<string name="recording_stopped">Arrêt de l\'enregistrement</string>

‎Android/app/src/main/res/values-zh-rCN/strings.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
<string name="connected_device">已连接设备:\n</string>
5151
<string name="mic_start_recording">麦克风已启动录制</string>
52+
<string name="unprocessed_not_supported">此设备不支持未处理音频源,已改用语音识别</string>
5253

5354
<string name="device_disconnected">设备已断开连接</string>
5455
<string name="recording_stopped">已停止录制</string>

‎Android/app/src/main/res/values/strings.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
<string name="connected_device">Connected Device:\n</string>
5656
<string name="mic_start_recording">Microphone has started to record</string>
57+
<string name="unprocessed_not_supported">Unprocessed audio source is not supported on this device, using voice recognition instead</string>
5758

5859
<string name="device_disconnected">Device disconnected</string>
5960
<string name="recording_stopped">Recording stopped</string>

0 commit comments

Comments
 (0)