From 19d28f718b4c7b7ac97e12b006d4e67b977fe6c8 Mon Sep 17 00:00:00 2001 From: ericek111 Date: Tue, 10 Nov 2026 19:15:11 +0000 Subject: [PATCH] Move MicrophoneTest to core; Android mic test in sound settings Co-Authored-By: Claude Sonnet 5.5 --- .../lixko/ts3j/client/android/LevelMonitor.kt | 92 ++++++++++++------- .../ts3j/client/android/SessionController.kt | 14 ++- .../client/android/ui/SoundSettingsScreen.kt | 35 ++++++- .../ts3j/client/audio}/MicrophoneTest.java | 43 ++++----- .../client/audio}/MicrophoneTestTest.java | 2 +- .../ui/options/VoiceActivationSection.java | 6 +- 6 files changed, 126 insertions(+), 66 deletions(-) rename ts3-client/{swing/src/main/java/eu/lixko/ts3j/client/ui/options => core/src/main/java/eu/lixko/ts3j/client/audio}/MicrophoneTest.java (79%) rename ts3-client/{swing/src/test/java/eu/lixko/ts3j/client/ui/options => core/src/test/java/eu/lixko/ts3j/client/audio}/MicrophoneTestTest.java (97%) diff --git a/android/app/src/main/java/eu/lixko/ts3j/client/android/LevelMonitor.kt b/android/app/src/main/java/eu/lixko/ts3j/client/android/LevelMonitor.kt index cb4785f..396731d 100644 --- a/android/app/src/main/java/eu/lixko/ts3j/client/android/LevelMonitor.kt +++ b/android/app/src/main/java/eu/lixko/ts3j/client/android/LevelMonitor.kt @@ -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 = _level + private val _testing = MutableStateFlow(false) + val testing: StateFlow = _testing + private val _loopback = MutableStateFlow(false) + val loopback: StateFlow = _loopback + private val _testFailed = MutableStateFlow(false) + /** Whether the last test could not get hold of the microphone. */ + val testFailed: StateFlow = _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() } diff --git a/android/app/src/main/java/eu/lixko/ts3j/client/android/SessionController.kt b/android/app/src/main/java/eu/lixko/ts3j/client/android/SessionController.kt index b34de07..d331d69 100644 --- a/android/app/src/main/java/eu/lixko/ts3j/client/android/SessionController.kt +++ b/android/app/src/main/java/eu/lixko/ts3j/client/android/SessionController.kt @@ -107,6 +107,9 @@ class SessionController internal constructor( } val micLevel: StateFlow = levels.level + val micTesting: StateFlow = levels.testing + val micLoopback: StateFlow = levels.loopback + val micTestFailed: StateFlow = 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. */ diff --git a/android/app/src/main/java/eu/lixko/ts3j/client/android/ui/SoundSettingsScreen.kt b/android/app/src/main/java/eu/lixko/ts3j/client/android/ui/SoundSettingsScreen.kt index 09fec89..5c159bb 100644 --- a/android/app/src/main/java/eu/lixko/ts3j/client/android/ui/SoundSettingsScreen.kt +++ b/android/app/src/main/java/eu/lixko/ts3j/client/android/ui/SoundSettingsScreen.kt @@ -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) { diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/MicrophoneTest.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/audio/MicrophoneTest.java similarity index 79% rename from ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/MicrophoneTest.java rename to ts3-client/core/src/main/java/eu/lixko/ts3j/client/audio/MicrophoneTest.java index 4ae8ef6..fae6be7 100644 --- a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/MicrophoneTest.java +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/audio/MicrophoneTest.java @@ -1,26 +1,24 @@ -package eu.lixko.ts3j.client.ui.options; +package eu.lixko.ts3j.client.audio; -import eu.lixko.ts3j.client.audio.AudioBackend; -import eu.lixko.ts3j.client.audio.AudioPlayback; -import eu.lixko.ts3j.client.audio.VoiceInput; import eu.lixko.ts3j.client.config.Settings; -import javax.swing.SwingUtilities; import java.util.concurrent.ArrayBlockingQueue; import java.util.function.Consumer; /** - * Drives the settings dialog's microphone test from a real capture chain. + * The settings' microphone test, driven from a real capture chain. * - *

The dialog runs its own {@link VoiceInput} rather than borrowing the connected - * one, whose listeners belong to the connection. Because it is the same class that feeds + *

It runs its own {@link VoiceInput} rather than borrowing the connected one, whose + * listeners belong to the connection. Because it is the same class that feeds * the server, the level and the gate shown here are exactly what would be transmitted — * pre-processing, voice detection, hangover and pre-roll included. * *

Loopback is optional: when enabled, the frames that would be sent are played back * locally so you can hear precisely what the other side would. + * + *

Level and transmit changes are reported on the capture thread. */ -final class MicrophoneTest { +public final class MicrophoneTest { /** Frames buffered for loopback before the oldest is dropped (~100 ms). */ private static final int LOOPBACK_QUEUE_FRAMES = 5; @@ -38,14 +36,14 @@ final class MicrophoneTest { private Thread loopbackThread; private String outputDevice = ""; - MicrophoneTest(AudioBackend audio, Consumer onLevel, Consumer onTransmitting) { + public MicrophoneTest(AudioBackend audio, Consumer onLevel, Consumer onTransmitting) { this.audio = audio; this.onLevel = onLevel; this.onTransmitting = onTransmitting; } /** Whether a capture chain is currently running. */ - boolean isRunning() { + public boolean isRunning() { return mic != null; } @@ -54,11 +52,11 @@ final class MicrophoneTest { * * @return false if the capture device could not be opened */ - boolean start(Settings settings) { + public boolean start(Settings settings) { stop(); VoiceInput input = audio.createInput(settings); - input.setLevelListener(db -> SwingUtilities.invokeLater(() -> onLevel.accept(db))); - input.setTalkListener(talking -> SwingUtilities.invokeLater(() -> onTransmitting.accept(talking))); + input.setLevelListener(onLevel); + input.setTalkListener(onTransmitting); input.setMonitorListener(this::enqueueForLoopback); this.outputDevice = settings.outputDevice.get(); try { @@ -74,7 +72,7 @@ final class MicrophoneTest { * Stops the capture chain. The caller owns the UI reset: doing it here would race with * a restart, whose own state has already been put on screen. */ - void stop() { + public void stop() { setLoopback(false); if (mic != null) { mic.stop(); @@ -83,17 +81,17 @@ final class MicrophoneTest { } /** Applies a change to the test chain, if it is running. */ - void configure(Consumer change) { + public void configure(Consumer change) { VoiceInput m = mic; if (m != null) change.accept(m); } /** The playback device to loop back through; takes effect on the next enable. */ - void setOutputDevice(String deviceId) { + public void setOutputDevice(String deviceId) { this.outputDevice = deviceId == null ? "" : deviceId; } - void setLoopback(boolean enabled) { + public void setLoopback(boolean enabled) { if (enabled == loopbackEnabled) { return; } @@ -157,14 +155,7 @@ final class MicrophoneTest { /** Converts float samples in [-1, 1] to 16-bit little-endian PCM. */ static byte[] toPcm16(float[] samples) { byte[] pcm = new byte[samples.length * 2]; - for (int i = 0; i < samples.length; i++) { - float f = samples[i]; - if (f > 1f) f = 1f; - else if (f < -1f) f = -1f; - int s = Math.round(f * 32767f); - pcm[2 * i] = (byte) (s & 0xFF); - pcm[2 * i + 1] = (byte) ((s >> 8) & 0xFF); - } + Pcm16.encode(samples, samples.length, pcm); return pcm; } diff --git a/ts3-client/swing/src/test/java/eu/lixko/ts3j/client/ui/options/MicrophoneTestTest.java b/ts3-client/core/src/test/java/eu/lixko/ts3j/client/audio/MicrophoneTestTest.java similarity index 97% rename from ts3-client/swing/src/test/java/eu/lixko/ts3j/client/ui/options/MicrophoneTestTest.java rename to ts3-client/core/src/test/java/eu/lixko/ts3j/client/audio/MicrophoneTestTest.java index f038f08..d06c6ad 100644 --- a/ts3-client/swing/src/test/java/eu/lixko/ts3j/client/ui/options/MicrophoneTestTest.java +++ b/ts3-client/core/src/test/java/eu/lixko/ts3j/client/audio/MicrophoneTestTest.java @@ -1,4 +1,4 @@ -package eu.lixko.ts3j.client.ui.options; +package eu.lixko.ts3j.client.audio; import org.junit.jupiter.api.Test; diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/VoiceActivationSection.java b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/VoiceActivationSection.java index f7bf3e0..9931592 100644 --- a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/VoiceActivationSection.java +++ b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/options/VoiceActivationSection.java @@ -4,6 +4,7 @@ import com.formdev.flatlaf.util.ScaledEmptyBorder; import com.formdev.flatlaf.util.UIScale; 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 eu.lixko.ts3j.client.hotkey.Hotkey; @@ -24,6 +25,7 @@ import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JSlider; import javax.swing.JToggleButton; +import javax.swing.SwingUtilities; import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.Insets; @@ -67,7 +69,9 @@ final class VoiceActivationSection { VoiceActivationSection(Settings settings, AudioBackend audio, VoiceInput liveMic, HotkeyService hotkeys, HotkeysPanel hotkeysPanel, Supplier audioSnapshot) { this.liveMic = liveMic; - this.micTest = new MicrophoneTest(audio, this::onTestLevel, this::onTestTalking); + this.micTest = new MicrophoneTest(audio, + db -> SwingUtilities.invokeLater(() -> onTestLevel(db)), + talking -> SwingUtilities.invokeLater(() -> onTestTalking(talking))); this.hotkeys = hotkeys; this.hotkeysPanel = hotkeysPanel; this.audioSnapshot = audioSnapshot;