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 audioSnapshot; private final MicrophoneTest micTest; private final JRadioButton vadRadio; private final JRadioButton pttRadio; private final JRadioButton contRadio; private final JComboBox vadModeCombo; private final JSlider thresholdSlider; private final JSlider speechSlider; private final JCheckBox vadOverPttCheck; private final LevelMeter meter; private JToggleButton testButton; private JCheckBox loopbackCheck; private JLabel talkIndicator; private final JButton pttKeyButton; private final JSlider bitrateSlider; private final JSlider complexitySlider; private final JCheckBox vbrCheck; private final JCheckBox fecCheck; private final JCheckBox musicCheck; VoiceActivationPanel(Settings settings, AudioBackend audio, VoiceInput liveMic, HotkeyService hotkeys, HotkeysPanel hotkeysPanel, Supplier audioSnapshot) { this.settings = settings; this.liveMic = liveMic; this.micTest = new MicrophoneTest(audio, this::onTestLevel, this::onTestTalking); this.hotkeys = hotkeys; this.hotkeysPanel = hotkeysPanel; this.audioSnapshot = audioSnapshot; GridBagConstraints c = gbc(); vadRadio = new JRadioButton("Voice Activation Detection"); pttRadio = new JRadioButton("Push-To-Talk"); contRadio = new JRadioButton("Continuous"); ButtonGroup group = new ButtonGroup(); group.add(vadRadio); group.add(pttRadio); group.add(contRadio); switch (settings.inputMode) { case PUSH_TO_TALK: pttRadio.setSelected(true); break; case CONTINUOUS: contRadio.setSelected(true); break; default: vadRadio.setSelected(true); } int row = 0; c.gridx = 0; c.gridy = row++; c.gridwidth = 2; add(vadRadio, c); c.gridy = row++; add(pttRadio, c); c.gridy = row++; add(contRadio, c); c.gridwidth = 1; meter = new LevelMeter(); meter.setThreshold(settings.vadThresholdDb); c.gridx = 0; c.gridy = row; c.gridwidth = 2; c.insets = UIScale.scale(new Insets(10, 4, 2, 4)); add(new JLabel("Input level:"), c); c.gridy = ++row; add(meter, c); c.insets = UIScale.scale(new Insets(4, 4, 4, 4)); c.gridy = ++row; add(buildTestControls(), c); c.gridwidth = 1; row++; vadModeCombo = new JComboBox<>(new String[]{"Automatic", "Volume Gate", "Hybrid"}); vadModeCombo.setSelectedIndex(vadModeIndex(settings.vadMode)); vadModeCombo.addActionListener(e -> micTest.configure(m -> m.setVadMode(currentVadMode()))); vadModeCombo.setToolTipText("Automatic: intelligent speech detection.
" + "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 change) { micTest.configure(change); } /** The playback device to loop the test through; used by the Devices tab's output picker. */ void setTestOutputDevice(String deviceId) { micTest.setOutputDevice(deviceId); } /** Restarts the test chain, if running, so a device change on the Devices tab takes effect. */ void restartTest() { if (micTest.isRunning()) { boolean loopback = loopbackCheck.isSelected(); if (micTest.start(audioSnapshot.get())) { micTest.setLoopback(loopback); } else { setTesting(false); testButton.setSelected(false); } } } void stopTest() { micTest.stop(); } private static int vadModeIndex(Settings.VadMode m) { switch (m) { case AUTOMATIC: return 0; case VOLUME_GATE: return 1; default: return 2; } } private Settings.VadMode currentVadMode() { switch (vadModeCombo.getSelectedIndex()) { case 0: return Settings.VadMode.AUTOMATIC; case 1: return Settings.VadMode.VOLUME_GATE; default: return Settings.VadMode.HYBRID; } } private OpusParameters currentOpusParameters() { return new OpusParameters( bitrateSlider.getValue() * 1000, complexitySlider.getValue(), vbrCheck.isSelected(), fecCheck.isSelected(), settings.packetLoss, musicCheck.isSelected()); } /** Applies the current Opus controls to the running encoder immediately. */ private void pushOpusLive() { if (liveMic != null) liveMic.setOpusParameters(currentOpusParameters()); } private String pushToTalkHotkeyText() { Hotkey ptt = hotkeys.find(HotkeyAction.PTT_ACTIVATE); return ptt == null ? "No hotkey assigned" : hotkeys.display(ptt.combo); } /** * Push-to-talk is an ordinary hotkey, so this shortcut edits that binding — adding * it when there is none — rather than keeping a key of its own. */ private void editPushToTalkHotkey() { Hotkey existing = hotkeys.find(HotkeyAction.PTT_ACTIVATE); HotkeyDialog dlg = new HotkeyDialog(javax.swing.SwingUtilities.getWindowAncestor(this), hotkeys, existing == null ? new Hotkey(HotkeyAction.PTT_ACTIVATE, null) : existing); dlg.setVisible(true); if (!dlg.isConfirmed()) return; List updated = new java.util.ArrayList<>(); for (Hotkey h : hotkeys.all()) { if (h != existing) updated.add(h.copy()); } updated.add(dlg.result()); hotkeys.replaceAll(updated); pttKeyButton.setText(pushToTalkHotkeyText()); hotkeysPanel.reload(); } /** Applies a live change to the connected microphone and to the test one alike. */ private void applyLive(Consumer change) { if (liveMic != null) change.accept(liveMic); micTest.configure(change); } // ---- microphone test ---- /** * The test row: a toggle that runs the capture chain, an indicator showing whether the * gate is open, and an optional loopback so you can hear what is being sent. */ private JPanel buildTestControls() { JPanel row = new JPanel(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 8, 0)); testButton = new JToggleButton("Begin Test"); testButton.setToolTipText("Run the capture chain exactly as it runs while connected, " + "so the bar and the indicator show what would actually be transmitted."); testButton.addActionListener(e -> setTesting(testButton.isSelected())); loopbackCheck = new JCheckBox("Hear myself"); loopbackCheck.setToolTipText("Play the transmitted audio back through the playback " + "device. Use headphones to avoid feedback."); loopbackCheck.setEnabled(false); loopbackCheck.addActionListener(e -> micTest.setLoopback(loopbackCheck.isSelected())); talkIndicator = new JLabel("Not transmitting", Icons.clientIdle(), JLabel.LEFT); talkIndicator.setToolTipText("Lights up while your microphone is open, exactly as " + "other users would see you in the channel list."); row.add(testButton); row.add(loopbackCheck); row.add(talkIndicator); return row; } private void setTesting(boolean on) { if (on && !micTest.start(audioSnapshot.get())) { testButton.setSelected(false); testButton.setText("Begin Test"); loopbackCheck.setEnabled(false); resetTestIndicators(); talkIndicator.setText("Capture device unavailable"); return; } if (!on) { micTest.stop(); loopbackCheck.setSelected(false); resetTestIndicators(); } testButton.setText(on ? "Stop Test" : "Begin Test"); loopbackCheck.setEnabled(on); } private void resetTestIndicators() { onTestTalking(false); onTestLevel(InputLevel.SILENCE_DB); } private void onTestLevel(double db) { if (meter != null) meter.setLevel(db); } private void onTestTalking(boolean talking) { if (meter != null) meter.setTransmitting(talking); if (talkIndicator != null) { talkIndicator.setIcon(talking ? Icons.clientTalking() : Icons.clientIdle()); talkIndicator.setText(talking ? "Transmitting" : "Not transmitting"); } } }