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
@@ -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.
*
* <p>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
* <p>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.
*
* <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.
*
* <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). */
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<Double> onLevel, Consumer<Boolean> onTransmitting) {
public MicrophoneTest(AudioBackend audio, Consumer<Double> onLevel, Consumer<Boolean> 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<VoiceInput> change) {
public void configure(Consumer<VoiceInput> 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;
}
@@ -1,4 +1,4 @@
package eu.lixko.ts3j.client.ui.options;
package eu.lixko.ts3j.client.audio;
import org.junit.jupiter.api.Test;
@@ -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<Settings> 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;