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
@@ -0,0 +1,173 @@
package eu.lixko.ts3j.client.audio;
import eu.lixko.ts3j.client.config.Settings;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.function.Consumer;
/**
* The settings' microphone test, driven from a real capture chain.
*
* <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.
*/
public final class MicrophoneTest {
/** Frames buffered for loopback before the oldest is dropped (~100 ms). */
private static final int LOOPBACK_QUEUE_FRAMES = 5;
private final Consumer<Double> onLevel;
private final Consumer<Boolean> onTransmitting;
private final AudioBackend audio;
private VoiceInput mic;
private final ArrayBlockingQueue<byte[]> loopbackQueue =
new ArrayBlockingQueue<>(LOOPBACK_QUEUE_FRAMES);
private volatile boolean loopbackEnabled;
private volatile boolean loopbackRunning;
private Thread loopbackThread;
private String outputDevice = "";
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. */
public boolean isRunning() {
return mic != null;
}
/**
* (Re)starts the test chain against {@code settings}' devices and voice options.
*
* @return false if the capture device could not be opened
*/
public boolean start(Settings settings) {
stop();
VoiceInput input = audio.createInput(settings);
input.setLevelListener(onLevel);
input.setTalkListener(onTransmitting);
input.setMonitorListener(this::enqueueForLoopback);
this.outputDevice = settings.outputDevice.get();
try {
input.start();
} catch (RuntimeException e) {
return false; // Device busy or gone; leave the test switched off.
}
this.mic = input;
return true;
}
/**
* 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.
*/
public void stop() {
setLoopback(false);
if (mic != null) {
mic.stop();
mic = null;
}
}
/** Applies a change to the test chain, if it is running. */
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. */
public void setOutputDevice(String deviceId) {
this.outputDevice = deviceId == null ? "" : deviceId;
}
public void setLoopback(boolean enabled) {
if (enabled == loopbackEnabled) {
return;
}
loopbackEnabled = enabled;
if (enabled) {
loopbackRunning = true;
loopbackThread = new Thread(this::loopbackLoop, "settings-loopback");
loopbackThread.setDaemon(true);
loopbackThread.start();
} else {
loopbackRunning = false;
if (loopbackThread != null) {
loopbackThread.interrupt();
loopbackThread = null;
}
loopbackQueue.clear();
}
}
/**
* Converts a transmitted frame to 16-bit PCM and queues it. Runs on the capture thread,
* so it must not block: a full queue means playback has fallen behind and the oldest
* frame is dropped instead.
*/
private void enqueueForLoopback(float[] interleaved, int channels) {
if (!loopbackEnabled) {
return;
}
byte[] pcm = toPcm16(interleaved);
if (!loopbackQueue.offer(pcm)) {
loopbackQueue.poll();
loopbackQueue.offer(pcm);
}
}
private void loopbackLoop() {
AudioPlayback line = null;
try {
// The monitored frames are mono for voice; ask for a matching line so no
// channel juggling is needed, and up-mix only if the device insists on stereo.
line = audio.io().openPlayback(outputDevice, 1);
line.start();
int channels = line.channels();
while (loopbackRunning) {
byte[] frame = loopbackQueue.poll(100, java.util.concurrent.TimeUnit.MILLISECONDS);
if (frame == null) {
continue;
}
byte[] out = (channels == 1) ? frame : upmix(frame, channels);
line.write(out, 0, out.length);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception ignored) {
// The device may be busy or gone; the test simply runs without loopback.
} finally {
if (line != null) line.close();
}
}
/** Converts float samples in [-1, 1] to 16-bit little-endian PCM. */
static byte[] toPcm16(float[] samples) {
byte[] pcm = new byte[samples.length * 2];
Pcm16.encode(samples, samples.length, pcm);
return pcm;
}
/** Copies a mono frame across {@code channels} interleaved channels. */
static byte[] upmix(byte[] mono, int channels) {
byte[] out = new byte[mono.length * channels];
for (int i = 0, frames = mono.length / 2; i < frames; i++) {
for (int c = 0; c < channels; c++) {
out[2 * (i * channels + c)] = mono[2 * i];
out[2 * (i * channels + c) + 1] = mono[2 * i + 1];
}
}
return out;
}
}
@@ -0,0 +1,48 @@
package eu.lixko.ts3j.client.audio;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
class MicrophoneTestTest {
private static int sampleAt(byte[] pcm, int index) {
return (short) (((pcm[2 * index + 1] & 0xFF) << 8) | (pcm[2 * index] & 0xFF));
}
@Test
void convertsFloatSamplesToLittleEndianPcm() {
byte[] pcm = MicrophoneTest.toPcm16(new float[]{0f, 1f, -1f, 0.5f});
assertEquals(8, pcm.length);
assertEquals(0, sampleAt(pcm, 0));
assertEquals(32767, sampleAt(pcm, 1));
assertEquals(-32767, sampleAt(pcm, 2));
assertEquals(16384, sampleAt(pcm, 3), 1);
}
@Test
void clampsSamplesOutsideRange() {
byte[] pcm = MicrophoneTest.toPcm16(new float[]{2f, -2f});
assertEquals(32767, sampleAt(pcm, 0));
assertEquals(-32767, sampleAt(pcm, 1));
}
@Test
void upmixRepeatsMonoAcrossChannels() {
byte[] mono = MicrophoneTest.toPcm16(new float[]{0.25f, -0.25f});
byte[] stereo = MicrophoneTest.upmix(mono, 2);
assertEquals(mono.length * 2, stereo.length);
// Each mono sample appears once per channel, in order.
assertEquals(sampleAt(mono, 0), sampleAt(stereo, 0));
assertEquals(sampleAt(mono, 0), sampleAt(stereo, 1));
assertEquals(sampleAt(mono, 1), sampleAt(stereo, 2));
assertEquals(sampleAt(mono, 1), sampleAt(stereo, 3));
}
@Test
void upmixIsIdentityForOneChannel() {
byte[] mono = MicrophoneTest.toPcm16(new float[]{0.1f, 0.2f, 0.3f});
assertArrayEquals(mono, MicrophoneTest.upmix(mono, 1));
}
}