Skip to content

Commit

Permalink
Merge pull request #466 from RADAR-base/audio-improve
Browse files Browse the repository at this point in the history
Minor improvements for audio plugin
  • Loading branch information
yatharthranjan authored Nov 11, 2024
2 parents b73a0e4 + 09d91eb commit 6347ede
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import android.os.Looper
import android.os.Process
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import org.radarbase.android.data.DataCache
import org.radarbase.android.source.AbstractSourceManager
import org.radarbase.android.source.SourceStatusListener
Expand Down Expand Up @@ -98,6 +99,26 @@ class PhoneAudioInputManager(service: PhoneAudioInputService) : AbstractSourceMa
@Volatile
private var currentlyRecording: Boolean = false

private val connectedMicrophonesObserver: Observer<List<AudioDeviceInfo>> = Observer { connectedMicrophones ->
audioRecordingHandler.execute {
if (connectedMicrophones?.size == 0) {
logger.warn("No connected microphone")
}
if (state.microphonePrioritized && state.finalizedMicrophone.value !in connectedMicrophones) {
state.microphonePrioritized = false
logger.info("Microphone prioritized: false")
}
logger.info("PhoneAudioInputManager: Connected microphones: {}",
connectedMicrophones.map { it.productName })

if (!state.microphonePrioritized) {
connectedMicrophones.also(::runDeviceSelectionLogic)
} else {
state.finalizedMicrophone.value?.let(setPreferredDeviceAndUpdate)
}
}
}

init {
name = service.getString(R.string.phone_audio_input_display_name)
bitsPerSample = if (audioFormat == AudioFormat.ENCODING_PCM_16BIT) 16 else 8
Expand Down Expand Up @@ -239,25 +260,8 @@ class PhoneAudioInputManager(service: PhoneAudioInputService) : AbstractSourceMa
}

private fun observeMicrophones() {
state.connectedMicrophones.observe(service) { connectedMicrophones ->
audioRecordingHandler.execute {
if (connectedMicrophones?.size == 0) {
logger.warn("No connected microphone")
}
if (state.microphonePrioritized && state.finalizedMicrophone.value !in connectedMicrophones) {
state.microphonePrioritized = false
logger.info("Microphone prioritized: false")
}
logger.info(
"PhoneAudioInputManager: Connected microphones: {}",
connectedMicrophones.map { it.productName })

if (!state.microphonePrioritized) {
connectedMicrophones.also(::runDeviceSelectionLogic)
} else {
state.finalizedMicrophone.value?.let(setPreferredDeviceAndUpdate)
}
}
if (!state.connectedMicrophones.hasActiveObservers()) {
state.connectedMicrophones.observeForever(connectedMicrophonesObserver)
}
}

Expand Down Expand Up @@ -408,6 +412,7 @@ class PhoneAudioInputManager(service: PhoneAudioInputService) : AbstractSourceMa
audioRecord?.release()
clearAudioDirectory()
}
mainHandler.post { state.connectedMicrophones.removeObserver(connectedMicrophonesObserver) }
recordProcessingHandler.stop()
}
companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ object AudioDeviceUtils {
microphones.add(device)
logger.info("Devices found: $device")
}
microphones.forEach {
logger.info("Name: ${it.productName}, type: ${it.type. toLogFriendlyType()}, encodings: ${it.encodings.joinToString(", ") { encoding -> AudioTypeFormatUtil.toLogFriendlyEncoding(encoding) }}, SampleRates: ${it.sampleRates.joinToString(", ") { srs-> srs.toString() }} channels: ${it.channelCounts.joinToString (", "){ channels -> channels.toString() }} Id: ${it.id}")
}
}
return microphones
}
Expand Down

0 comments on commit 6347ede

Please sign in to comment.