Move MicrophoneTest to core; Android mic test in sound settings
test / test (push) Canceled after 0s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 19:15:11 +00:00
co-authored by Claude Sonnet 5.5
parent 2619d9ff3d
commit 19d28f718b
6 changed files with 126 additions and 66 deletions
@@ -2,6 +2,7 @@ package eu.lixko.ts3j.client.android
import eu.lixko.ts3j.client.audio.AudioBackend import eu.lixko.ts3j.client.audio.AudioBackend
import eu.lixko.ts3j.client.audio.InputLevel 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.audio.VoiceInput
import eu.lixko.ts3j.client.config.Settings import eu.lixko.ts3j.client.config.Settings
import kotlinx.coroutines.flow.MutableStateFlow 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 * 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 * connection's own microphone, so it shows exactly what gets sent, until a microphone test
* microphone of its own for as long as someone watches. * 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()) private val _level = MutableStateFlow(MicLevel())
val level: StateFlow<MicLevel> = _level 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. */ /** The connection's microphone, when that is the one listened to. */
private var borrowed: VoiceInput? = null private var borrowed: VoiceInput? = null
private var test: VoiceInput? = null
/** /**
* Starts or stops listening. * Starts or stops listening.
* *
* @param source the connection's microphone while it captures, else null * @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 @Synchronized
fun update(watching: Boolean, source: VoiceInput?, busy: Boolean, mayCapture: Boolean) { fun update(watching: Boolean, source: VoiceInput?, mayCapture: Boolean) {
when { this.source = source.takeIf { watching }
!watching -> release() mayTest = watching && mayCapture
source != null -> if (borrowed !== source) { if (!mayTest) stopTest()
release() follow()
source.setLevelListener(::onLevel)
borrowed = source
}
busy || !mayCapture -> release()
test == null -> startTest()
}
} }
/** The own microphone's transmit state, which the connection reports. */ /** 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) 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. */ /** Puts changed voice settings into effect on the test microphone. */
@Synchronized @Synchronized
fun apply() { fun apply() {
test?.let { test.setOutputDevice(settings.outputDevice.get())
test.configure {
it.apply(settings) it.apply(settings)
it.setMode(testMode()) it.setMode(testMode())
} }
} }
private fun startTest() { private fun stopTest() {
release() if (!test.isRunning) return
val input = audio.createInput(settings) test.stop()
input.setMode(testMode()) _testing.value = false
input.setLevelListener(::onLevel) _loopback.value = false
input.setTalkListener { _level.value = _level.value.copy(transmitting = it) } _level.value = MicLevel()
try {
input.start()
} catch (e: RuntimeException) {
return // no microphone to be had; the meter stays idle
}
test = input
} }
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?.setLevelListener(null)
borrowed = null wanted?.setLevelListener(::onLevel)
test?.stop() borrowed = wanted
test = null
_level.value = MicLevel() _level.value = MicLevel()
} }
@@ -107,6 +107,9 @@ class SessionController internal constructor(
} }
val micLevel: StateFlow<MicLevel> = levels.level 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 @Volatile
private var watchingLevel = false private var watchingLevel = false
@@ -337,10 +340,17 @@ class SessionController internal constructor(
} }
private fun updateLevels() { private fun updateLevels() {
val owner = _micOwner.value levels.update(watchingLevel, _micOwner.value?.microphone, hasMicPermission())
levels.update(watchingLevel, owner?.microphone, owner?.isBusy == true, 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 ---- // ---- notifications ----
/** Shows a private chat a notification was tapped for. */ /** Shows a private chat a notification was tapped for. */
@@ -1,5 +1,6 @@
package eu.lixko.ts3j.client.android.ui package eu.lixko.ts3j.client.android.ui
import android.Manifest
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION 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.rememberScrollState
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable 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) if (inputMode == Settings.InputMode.PUSH_TO_TALK) FloatingButtonToggle(audio)
val gated = inputMode == Settings.InputMode.VOICE_ACTIVATION && vadMode != Settings.VadMode.AUTOMATIC val gated = inputMode == Settings.InputMode.VOICE_ACTIVATION && vadMode != Settings.VadMode.AUTOMATIC
LevelMeter(level, threshold.takeIf { gated }) LevelMeter(level, threshold.takeIf { gated })
if (!level.live) { MicrophoneTestControls(sessions, level.transmitting)
Hint(if (sessions.hasMicPermission()) "The microphone is not available right now." else "Allow the microphone to see its level.")
}
if (inputMode == Settings.InputMode.VOICE_ACTIVATION) { if (inputMode == Settings.InputMode.VOICE_ACTIVATION) {
Section("Voice activation") Section("Voice activation")
@@ -216,6 +216,35 @@ private val VAD_MODES = listOf(
Settings.VadMode.HYBRID to "Hybrid", 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. */ /** The push-to-talk button over other apps, which first needs the user to allow the app to draw there. */
@Composable @Composable
private fun FloatingButtonToggle(audio: AudioSetup) { private fun FloatingButtonToggle(audio: AudioSetup) {
@@ -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 eu.lixko.ts3j.client.config.Settings;
import javax.swing.SwingUtilities;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.function.Consumer; 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.
* *
* <p>The dialog runs its own {@link VoiceInput} rather than borrowing the connected * <p>It runs its own {@link VoiceInput} rather than borrowing the connected one, whose
* one, whose listeners belong to the connection. Because it is the same class that feeds * 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 — * the server, the level and the gate shown here are exactly what would be transmitted —
* pre-processing, voice detection, hangover and pre-roll included. * pre-processing, voice detection, hangover and pre-roll included.
* *
* <p>Loopback is optional: when enabled, the frames that would be sent are played back * <p>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. * locally so you can hear precisely what the other side would.
*
* <p>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). */ /** Frames buffered for loopback before the oldest is dropped (~100 ms). */
private static final int LOOPBACK_QUEUE_FRAMES = 5; private static final int LOOPBACK_QUEUE_FRAMES = 5;
@@ -38,14 +36,14 @@ final class MicrophoneTest {
private Thread loopbackThread; private Thread loopbackThread;
private String outputDevice = ""; private String outputDevice = "";
MicrophoneTest(AudioBackend audio, Consumer<Double> onLevel, Consumer<Boolean> onTransmitting) { public MicrophoneTest(AudioBackend audio, Consumer<Double> onLevel, Consumer<Boolean> onTransmitting) {
this.audio = audio; this.audio = audio;
this.onLevel = onLevel; this.onLevel = onLevel;
this.onTransmitting = onTransmitting; this.onTransmitting = onTransmitting;
} }
/** Whether a capture chain is currently running. */ /** Whether a capture chain is currently running. */
boolean isRunning() { public boolean isRunning() {
return mic != null; return mic != null;
} }
@@ -54,11 +52,11 @@ final class MicrophoneTest {
* *
* @return false if the capture device could not be opened * @return false if the capture device could not be opened
*/ */
boolean start(Settings settings) { public boolean start(Settings settings) {
stop(); stop();
VoiceInput input = audio.createInput(settings); VoiceInput input = audio.createInput(settings);
input.setLevelListener(db -> SwingUtilities.invokeLater(() -> onLevel.accept(db))); input.setLevelListener(onLevel);
input.setTalkListener(talking -> SwingUtilities.invokeLater(() -> onTransmitting.accept(talking))); input.setTalkListener(onTransmitting);
input.setMonitorListener(this::enqueueForLoopback); input.setMonitorListener(this::enqueueForLoopback);
this.outputDevice = settings.outputDevice.get(); this.outputDevice = settings.outputDevice.get();
try { try {
@@ -74,7 +72,7 @@ final class MicrophoneTest {
* Stops the capture chain. The caller owns the UI reset: doing it here would race with * 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. * a restart, whose own state has already been put on screen.
*/ */
void stop() { public void stop() {
setLoopback(false); setLoopback(false);
if (mic != null) { if (mic != null) {
mic.stop(); mic.stop();
@@ -83,17 +81,17 @@ final class MicrophoneTest {
} }
/** Applies a change to the test chain, if it is running. */ /** Applies a change to the test chain, if it is running. */
void configure(Consumer<VoiceInput> change) { public void configure(Consumer<VoiceInput> change) {
VoiceInput m = mic; VoiceInput m = mic;
if (m != null) change.accept(m); if (m != null) change.accept(m);
} }
/** The playback device to loop back through; takes effect on the next enable. */ /** 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; this.outputDevice = deviceId == null ? "" : deviceId;
} }
void setLoopback(boolean enabled) { public void setLoopback(boolean enabled) {
if (enabled == loopbackEnabled) { if (enabled == loopbackEnabled) {
return; return;
} }
@@ -157,14 +155,7 @@ final class MicrophoneTest {
/** Converts float samples in [-1, 1] to 16-bit little-endian PCM. */ /** Converts float samples in [-1, 1] to 16-bit little-endian PCM. */
static byte[] toPcm16(float[] samples) { static byte[] toPcm16(float[] samples) {
byte[] pcm = new byte[samples.length * 2]; byte[] pcm = new byte[samples.length * 2];
for (int i = 0; i < samples.length; i++) { Pcm16.encode(samples, samples.length, pcm);
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);
}
return pcm; return pcm;
} }
@@ -1,4 +1,4 @@
package eu.lixko.ts3j.client.ui.options; package eu.lixko.ts3j.client.audio;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -4,6 +4,7 @@ import com.formdev.flatlaf.util.ScaledEmptyBorder;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
import eu.lixko.ts3j.client.audio.AudioBackend; import eu.lixko.ts3j.client.audio.AudioBackend;
import eu.lixko.ts3j.client.audio.InputLevel; 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.audio.VoiceInput;
import eu.lixko.ts3j.client.config.Settings; import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.hotkey.Hotkey; import eu.lixko.ts3j.client.hotkey.Hotkey;
@@ -24,6 +25,7 @@ import javax.swing.JPanel;
import javax.swing.JRadioButton; import javax.swing.JRadioButton;
import javax.swing.JSlider; import javax.swing.JSlider;
import javax.swing.JToggleButton; import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.Insets; import java.awt.Insets;
@@ -67,7 +69,9 @@ final class VoiceActivationSection {
VoiceActivationSection(Settings settings, AudioBackend audio, VoiceInput liveMic, HotkeyService hotkeys, VoiceActivationSection(Settings settings, AudioBackend audio, VoiceInput liveMic, HotkeyService hotkeys,
HotkeysPanel hotkeysPanel, Supplier<Settings> audioSnapshot) { HotkeysPanel hotkeysPanel, Supplier<Settings> audioSnapshot) {
this.liveMic = liveMic; 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.hotkeys = hotkeys;
this.hotkeysPanel = hotkeysPanel; this.hotkeysPanel = hotkeysPanel;
this.audioSnapshot = audioSnapshot; this.audioSnapshot = audioSnapshot;