package com.ts3client.ui; import com.formdev.flatlaf.util.UIScale; import com.ts3client.audio.AudioBackend; import com.ts3client.audio.InputLevel; import com.ts3client.audio.OpusParameters; import com.ts3client.audio.VoiceInput; import com.ts3client.config.Settings; import com.ts3client.hotkey.Hotkey; import com.ts3client.hotkey.HotkeyAction; import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JSlider; import javax.swing.JToggleButton; import java.awt.GridBagConstraints; import java.awt.Insets; import java.util.List; import java.util.function.Consumer; import java.util.function.Supplier; /** * Options dialog's "Voice Activation" tab: input mode (VAD / push-to-talk / continuous), * the detection tuning with a live input meter, and the Opus encoder controls. * *
Owns the microphone test (start/stop button, loopback, indicator): it runs a real
* capture chain built from the form's current values, plus whatever the "Playback / Capture"
* tab currently has on screen, via {@code audioSnapshot}.
*/
final class VoiceActivationPanel extends FormPanel {
private static final int MIN_BITRATE_KBITS = 8;
private static final int MAX_BITRATE_KBITS = 160;
private final Settings settings;
private final VoiceInput liveMic;
private final HotkeyService hotkeys;
private final HotkeysPanel hotkeysPanel;
private final Supplier
"
+ "Volume Gate: transmit when loud enough.
"
+ "Hybrid: loud enough and detected as speech.");
addRow(this, c, row++, new JLabel("Detection:"), vadModeCombo);
thresholdSlider = new JSlider((int) InputLevel.MIN_DB, (int) InputLevel.MAX_DB,
(int) Math.round(settings.vadThresholdDb));
JLabel thresholdLabel = new JLabel(Math.round(settings.vadThresholdDb) + " dB");
thresholdSlider.addChangeListener(e -> {
meter.setThreshold(thresholdSlider.getValue());
thresholdLabel.setText(thresholdSlider.getValue() + " dB");
applyLive(m -> m.setThresholdDb(thresholdSlider.getValue()));
});
addRow(this, c, row++, new JLabel("Volume gate:"), sliderWithLabel(thresholdSlider, thresholdLabel));
speechSlider = new JSlider(0, 100, (int) Math.round(settings.speechThreshold * 100));
JLabel speechLabel = new JLabel(Math.round(settings.speechThreshold * 100) + "%");
speechSlider.addChangeListener(e -> {
speechLabel.setText(speechSlider.getValue() + "%");
applyLive(m -> m.setSpeechThreshold(speechSlider.getValue() / 100.0));
});
addRow(this, c, row++, new JLabel("Speech threshold:"), sliderWithLabel(speechSlider, speechLabel));
pttKeyButton = new JButton(pushToTalkHotkeyText());
pttKeyButton.setToolTipText("Push-to-talk is a global hotkey; this opens its binding");
pttKeyButton.addActionListener(e -> editPushToTalkHotkey());
addRow(this, c, row++, new JLabel("Push-to-talk hotkey:"), pttKeyButton);
vadOverPttCheck = new JCheckBox("Also detect voice while push-to-talk", settings.vadOverPtt);
vadOverPttCheck.addActionListener(e ->
micTest.configure(m -> m.setVadOverPtt(vadOverPttCheck.isSelected())));
c.gridx = 0;
c.gridy = row++;
c.gridwidth = 2;
add(vadOverPttCheck, c);
c.gridwidth = 1;
int kbits = Math.max(MIN_BITRATE_KBITS, Math.min(MAX_BITRATE_KBITS, settings.bitrate / 1000));
bitrateSlider = new JSlider(MIN_BITRATE_KBITS, MAX_BITRATE_KBITS, kbits);
JLabel bitrateLabel = new JLabel(kbits + " kbit/s");
bitrateSlider.addChangeListener(e -> {
bitrateLabel.setText(bitrateSlider.getValue() + " kbit/s");
pushOpusLive();
});
addRow(this, c, row++, new JLabel("Opus bitrate:"),
sliderWithLabel(bitrateSlider, bitrateLabel, 70));
complexitySlider = new JSlider(0, 10, settings.complexity);
limitWidth(complexitySlider, SLIDER_WIDTH);
complexitySlider.addChangeListener(e -> pushOpusLive());
addRow(this, c, row++, new JLabel("Opus complexity:"), complexitySlider);
vbrCheck = new JCheckBox("Variable bitrate (VBR)", settings.vbr);
fecCheck = new JCheckBox("Forward error correction (FEC)", settings.fec);
musicCheck = new JCheckBox("Music codec (stereo, higher fidelity)", settings.music);
musicCheck.setToolTipText("Transmits OPUS_MUSIC: stereo when the capture "
+ "device has two channels, and without the voice pre-processing "
+ "(noise removal, typing attenuation, AGC).
"
+ "Voice mode (OPUS_VOICE) is mono, as in the official client.");
vbrCheck.addActionListener(e -> pushOpusLive());
fecCheck.addActionListener(e -> pushOpusLive());
musicCheck.addActionListener(e -> pushOpusLive());
c.gridx = 0;
c.gridy = row++;
c.gridwidth = 2;
add(vbrCheck, c);
c.gridy = row++;
add(fecCheck, c);
c.gridy = row++;
add(musicCheck, c);
c.gridwidth = 1;
Runnable syncEnabled = () -> {
boolean vad = vadRadio.isSelected();
boolean ptt = pttRadio.isSelected();
boolean vadContext = vad || (ptt && vadOverPttCheck.isSelected());
Settings.VadMode vm = currentVadMode();
boolean usesGate = vm != Settings.VadMode.AUTOMATIC;
boolean usesSpeech = vm != Settings.VadMode.VOLUME_GATE;
vadModeCombo.setEnabled(vadContext);
thresholdSlider.setEnabled(vadContext && usesGate);
speechSlider.setEnabled(vadContext && usesSpeech);
pttKeyButton.setEnabled(ptt);
vadOverPttCheck.setEnabled(ptt);
meter.setShowThreshold(vadContext && usesGate);
if (liveMic != null) {
liveMic.setMode(ptt ? Settings.InputMode.PUSH_TO_TALK
: vad ? Settings.InputMode.VOICE_ACTIVATION
: Settings.InputMode.CONTINUOUS);
liveMic.setVadMode(vm);
liveMic.setVadOverPtt(vadOverPttCheck.isSelected());
}
};
vadRadio.addActionListener(e -> syncEnabled.run());
pttRadio.addActionListener(e -> syncEnabled.run());
contRadio.addActionListener(e -> syncEnabled.run());
vadModeCombo.addActionListener(e -> syncEnabled.run());
vadOverPttCheck.addActionListener(e -> syncEnabled.run());
syncEnabled.run();
c.gridx = 0;
c.gridy = row;
c.weighty = 1;
add(Box.createGlue(), c);
}
/** Copies the form into {@code target}, without touching anything else. */
void writeInto(Settings target) {
target.inputMode = pttRadio.isSelected() ? Settings.InputMode.PUSH_TO_TALK
: contRadio.isSelected() ? Settings.InputMode.CONTINUOUS
: Settings.InputMode.VOICE_ACTIVATION;
target.vadMode = currentVadMode();
target.vadThresholdDb = thresholdSlider.getValue();
target.speechThreshold = speechSlider.getValue() / 100.0;
target.vadOverPtt = vadOverPttCheck.isSelected();
target.bitrate = bitrateSlider.getValue() * 1000;
target.complexity = complexitySlider.getValue();
target.vbr = vbrCheck.isSelected();
target.fec = fecCheck.isSelected();
target.music = musicCheck.isSelected();
}
/** Applies a change to the microphone test, if it is running; used by the Devices tab too. */
void configureTest(Consumer