Move MicrophoneTest to core; Android mic test in sound settings
test / test (push) Canceled after 0s
test / test (push) Canceled after 0s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package eu.lixko.ts3j.client.android
|
||||
|
||||
import eu.lixko.ts3j.client.audio.AudioBackend
|
||||
import eu.lixko.ts3j.client.audio.InputLevel
|
||||
import eu.lixko.ts3j.client.audio.MicrophoneTest
|
||||
import eu.lixko.ts3j.client.audio.VoiceInput
|
||||
import eu.lixko.ts3j.client.config.Settings
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -17,36 +18,41 @@ data class MicLevel(
|
||||
|
||||
/**
|
||||
* The level meter behind the voice-activation settings. While connected it listens to the
|
||||
* connection's own microphone, so it shows exactly what gets sent; otherwise it runs a test
|
||||
* microphone of its own for as long as someone watches.
|
||||
* connection's own microphone, so it shows exactly what gets sent, until a microphone test
|
||||
* takes over with a capture chain of its own.
|
||||
*/
|
||||
class LevelMonitor(private val settings: Settings, private val audio: AudioBackend) {
|
||||
class LevelMonitor(private val settings: Settings, audio: AudioBackend) {
|
||||
|
||||
private val _level = MutableStateFlow(MicLevel())
|
||||
val level: StateFlow<MicLevel> = _level
|
||||
|
||||
private val _testing = MutableStateFlow(false)
|
||||
val testing: StateFlow<Boolean> = _testing
|
||||
private val _loopback = MutableStateFlow(false)
|
||||
val loopback: StateFlow<Boolean> = _loopback
|
||||
private val _testFailed = MutableStateFlow(false)
|
||||
/** Whether the last test could not get hold of the microphone. */
|
||||
val testFailed: StateFlow<Boolean> = _testFailed
|
||||
|
||||
private val test = MicrophoneTest(audio, ::onLevel) { _level.value = _level.value.copy(transmitting = it) }
|
||||
|
||||
private var mayTest = false
|
||||
/** The connection's microphone, while someone watches. */
|
||||
private var source: VoiceInput? = null
|
||||
/** The connection's microphone, when that is the one listened to. */
|
||||
private var borrowed: VoiceInput? = null
|
||||
private var test: VoiceInput? = null
|
||||
|
||||
/**
|
||||
* Starts or stops listening.
|
||||
*
|
||||
* @param source the connection's microphone while it captures, else null
|
||||
* @param busy whether a connection is under way, which will bring its own microphone
|
||||
*/
|
||||
@Synchronized
|
||||
fun update(watching: Boolean, source: VoiceInput?, busy: Boolean, mayCapture: Boolean) {
|
||||
when {
|
||||
!watching -> release()
|
||||
source != null -> if (borrowed !== source) {
|
||||
release()
|
||||
source.setLevelListener(::onLevel)
|
||||
borrowed = source
|
||||
}
|
||||
busy || !mayCapture -> release()
|
||||
test == null -> startTest()
|
||||
}
|
||||
fun update(watching: Boolean, source: VoiceInput?, mayCapture: Boolean) {
|
||||
this.source = source.takeIf { watching }
|
||||
mayTest = watching && mayCapture
|
||||
if (!mayTest) stopTest()
|
||||
follow()
|
||||
}
|
||||
|
||||
/** The own microphone's transmit state, which the connection reports. */
|
||||
@@ -54,34 +60,54 @@ class LevelMonitor(private val settings: Settings, private val audio: AudioBacke
|
||||
if (borrowed != null) _level.value = _level.value.copy(transmitting = transmitting)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun setTesting(on: Boolean) {
|
||||
if (!on) {
|
||||
stopTest()
|
||||
} else if (mayTest && !test.isRunning) {
|
||||
val started = test.start(settings)
|
||||
_testFailed.value = !started
|
||||
if (started) {
|
||||
test.configure { it.setMode(testMode()) }
|
||||
test.setOutputDevice(settings.outputDevice.get())
|
||||
_testing.value = true
|
||||
}
|
||||
}
|
||||
follow()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun setLoopback(on: Boolean) {
|
||||
if (!test.isRunning) return
|
||||
test.setLoopback(on)
|
||||
_loopback.value = on
|
||||
}
|
||||
|
||||
/** Puts changed voice settings into effect on the test microphone. */
|
||||
@Synchronized
|
||||
fun apply() {
|
||||
test?.let {
|
||||
test.setOutputDevice(settings.outputDevice.get())
|
||||
test.configure {
|
||||
it.apply(settings)
|
||||
it.setMode(testMode())
|
||||
}
|
||||
}
|
||||
|
||||
private fun startTest() {
|
||||
release()
|
||||
val input = audio.createInput(settings)
|
||||
input.setMode(testMode())
|
||||
input.setLevelListener(::onLevel)
|
||||
input.setTalkListener { _level.value = _level.value.copy(transmitting = it) }
|
||||
try {
|
||||
input.start()
|
||||
} catch (e: RuntimeException) {
|
||||
return // no microphone to be had; the meter stays idle
|
||||
}
|
||||
test = input
|
||||
private fun stopTest() {
|
||||
if (!test.isRunning) return
|
||||
test.stop()
|
||||
_testing.value = false
|
||||
_loopback.value = false
|
||||
_level.value = MicLevel()
|
||||
}
|
||||
|
||||
private fun release() {
|
||||
/** Listens to the connection's microphone unless the test has the meter. */
|
||||
private fun follow() {
|
||||
val wanted = source.takeUnless { test.isRunning }
|
||||
if (borrowed === wanted) return
|
||||
borrowed?.setLevelListener(null)
|
||||
borrowed = null
|
||||
test?.stop()
|
||||
test = null
|
||||
wanted?.setLevelListener(::onLevel)
|
||||
borrowed = wanted
|
||||
_level.value = MicLevel()
|
||||
}
|
||||
|
||||
|
||||
@@ -107,6 +107,9 @@ class SessionController internal constructor(
|
||||
}
|
||||
|
||||
val micLevel: StateFlow<MicLevel> = levels.level
|
||||
val micTesting: StateFlow<Boolean> = levels.testing
|
||||
val micLoopback: StateFlow<Boolean> = levels.loopback
|
||||
val micTestFailed: StateFlow<Boolean> = levels.testFailed
|
||||
@Volatile
|
||||
private var watchingLevel = false
|
||||
|
||||
@@ -337,10 +340,17 @@ class SessionController internal constructor(
|
||||
}
|
||||
|
||||
private fun updateLevels() {
|
||||
val owner = _micOwner.value
|
||||
levels.update(watchingLevel, owner?.microphone, owner?.isBusy == true, hasMicPermission())
|
||||
levels.update(watchingLevel, _micOwner.value?.microphone, hasMicPermission())
|
||||
}
|
||||
|
||||
/** Runs the microphone test; it ends by itself when the level is no longer watched. */
|
||||
fun testMicrophone(on: Boolean) {
|
||||
updateLevels() // the permission may have just been granted
|
||||
levels.setTesting(on)
|
||||
}
|
||||
|
||||
fun setMicLoopback(on: Boolean) = levels.setLoopback(on)
|
||||
|
||||
// ---- notifications ----
|
||||
|
||||
/** Shows a private chat a notification was tapped for. */
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package eu.lixko.ts3j.client.android.ui
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION
|
||||
@@ -16,6 +17,7 @@ import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
@@ -105,9 +107,7 @@ fun SoundSettingsScreen(settings: Settings, sessions: SessionController, audio:
|
||||
if (inputMode == Settings.InputMode.PUSH_TO_TALK) FloatingButtonToggle(audio)
|
||||
val gated = inputMode == Settings.InputMode.VOICE_ACTIVATION && vadMode != Settings.VadMode.AUTOMATIC
|
||||
LevelMeter(level, threshold.takeIf { gated })
|
||||
if (!level.live) {
|
||||
Hint(if (sessions.hasMicPermission()) "The microphone is not available right now." else "Allow the microphone to see its level.")
|
||||
}
|
||||
MicrophoneTestControls(sessions, level.transmitting)
|
||||
|
||||
if (inputMode == Settings.InputMode.VOICE_ACTIVATION) {
|
||||
Section("Voice activation")
|
||||
@@ -216,6 +216,35 @@ private val VAD_MODES = listOf(
|
||||
Settings.VadMode.HYBRID to "Hybrid",
|
||||
)
|
||||
|
||||
/** Runs the capture chain as a connection would, optionally playing back what would be sent. */
|
||||
@Composable
|
||||
private fun MicrophoneTestControls(sessions: SessionController, transmitting: Boolean) {
|
||||
val testing by sessions.micTesting.collectAsStateWithLifecycle()
|
||||
val loopback by sessions.micLoopback.collectAsStateWithLifecycle()
|
||||
val failed by sessions.micTestFailed.collectAsStateWithLifecycle()
|
||||
var denied by remember { mutableStateOf(false) }
|
||||
val allow = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
||||
denied = !granted
|
||||
if (granted) sessions.testMicrophone(true)
|
||||
}
|
||||
Row(Modifier.padding(horizontal = 16.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
OutlinedButton({
|
||||
if (testing || sessions.hasMicPermission()) sessions.testMicrophone(!testing)
|
||||
else allow.launch(Manifest.permission.RECORD_AUDIO)
|
||||
}) { Text(if (testing) "Stop Test" else "Begin Test") }
|
||||
if (testing) {
|
||||
Text(if (transmitting) "Transmitting" else "Not transmitting", Modifier.padding(start = 16.dp),
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
}
|
||||
}
|
||||
if (testing) {
|
||||
Toggle("Hear myself", loopback, sessions::setMicLoopback)
|
||||
if (loopback) Hint("Use headphones to avoid feedback.")
|
||||
}
|
||||
if (failed) Hint("Capture device unavailable")
|
||||
if (denied) Hint("Allow the microphone to test it.")
|
||||
}
|
||||
|
||||
/** The push-to-talk button over other apps, which first needs the user to allow the app to draw there. */
|
||||
@Composable
|
||||
private fun FloatingButtonToggle(audio: AudioSetup) {
|
||||
|
||||
Reference in New Issue
Block a user