Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements for audio plugin #466

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading