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:
@@ -1,182 +0,0 @@
|
||||
package eu.lixko.ts3j.client.ui.options;
|
||||
|
||||
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.
|
||||
*
|
||||
* <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
|
||||
* 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.
|
||||
*/
|
||||
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 = "";
|
||||
|
||||
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() {
|
||||
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
|
||||
*/
|
||||
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.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.
|
||||
*/
|
||||
void stop() {
|
||||
setLoopback(false);
|
||||
if (mic != null) {
|
||||
mic.stop();
|
||||
mic = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Applies a change to the test chain, if it is running. */
|
||||
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) {
|
||||
this.outputDevice = deviceId == null ? "" : deviceId;
|
||||
}
|
||||
|
||||
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];
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -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;
|
||||
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
package eu.lixko.ts3j.client.ui.options;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user