package com.ts3client.ui; import com.formdev.flatlaf.util.ScaledEmptyBorder; import com.formdev.flatlaf.util.UIScale; import com.ts3client.net.ChannelSettings; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.ButtonGroup; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JSlider; import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; /** * The channel editor's "Audio" tab: the codec preset, or the codec and quality picked by * hand, with the bandwidth one talking client then costs. */ final class ChannelAudioPanel extends JPanel implements ChannelDialog.Tab { /** Codec names in TeamSpeak's own id order, which is what the combo box index is. */ private static final String[] CODEC_NAMES = { "Speex Narrowband", "Speex Wideband", "Speex Ultra-Wideband", "CELT Mono", "Opus Voice", "Opus Music"}; /** * Bandwidth per talking client in KiB/s at quality 0 and 10, per codec, as published by * TeamSpeak. The steps in between are interpolated: the client shows a figure per * quality step, and only the two ends of each codec's range are documented. */ private static final double[][] BANDWIDTH_RANGE = { {2.49, 5.22}, {2.69, 7.37}, {2.73, 7.57}, {6.10, 13.92}, {2.73, 7.71}, {3.08, 11.87}}; private static final int CODEC_OPUS_VOICE = 4; private static final int CODEC_OPUS_MUSIC = 5; private final JRadioButton voiceMobile = new JRadioButton("Voice Mobile"); private final JRadioButton voiceDesktop = new JRadioButton("Voice Desktop"); private final JRadioButton music = new JRadioButton("Music"); private final JRadioButton custom = new JRadioButton("Custom"); private final JComboBox codec = new JComboBox<>(CODEC_NAMES); private final JSlider quality = new JSlider(0, 10, 6); private final JLabel qualityValue = new JLabel("6"); private final JLabel bandwidth = new JLabel(); private final JPanel customSettings = new JPanel(new GridBagLayout()); ChannelAudioPanel() { super(new BorderLayout(UIScale.scale(8), UIScale.scale(8))); setBorder(new ScaledEmptyBorder(8, 8, 8, 8)); ButtonGroup presets = new ButtonGroup(); presets.add(voiceMobile); presets.add(voiceDesktop); presets.add(music); presets.add(custom); voiceMobile.addActionListener(a -> applyPreset(CODEC_OPUS_VOICE, 4)); voiceDesktop.addActionListener(a -> applyPreset(CODEC_OPUS_VOICE, 6)); music.addActionListener(a -> applyPreset(CODEC_OPUS_MUSIC, 6)); custom.addActionListener(a -> setCustomEnabled(true)); codec.addActionListener(a -> updateBandwidth()); quality.setMajorTickSpacing(1); quality.setPaintTicks(true); quality.setSnapToTicks(true); quality.addChangeListener(e -> updateBandwidth()); add(presetBox(), BorderLayout.WEST); add(customBox(), BorderLayout.CENTER); add(bandwidthRow(), BorderLayout.SOUTH); } @Override public void read(ChannelSettings settings) { codec.setSelectedIndex(Math.max(0, Math.min(CODEC_NAMES.length - 1, settings.codec))); quality.setValue(Math.max(0, Math.min(10, settings.codecQuality))); if (settings.codec == CODEC_OPUS_VOICE && settings.codecQuality == 4) voiceMobile.setSelected(true); else if (settings.codec == CODEC_OPUS_VOICE && settings.codecQuality == 6) voiceDesktop.setSelected(true); else if (settings.codec == CODEC_OPUS_MUSIC && settings.codecQuality == 6) music.setSelected(true); else custom.setSelected(true); setCustomEnabled(custom.isSelected()); updateBandwidth(); } @Override public void write(ChannelSettings settings) { settings.codec = codec.getSelectedIndex(); settings.codecQuality = quality.getValue(); } private void applyPreset(int codecId, int qualityValue) { codec.setSelectedIndex(codecId); quality.setValue(qualityValue); setCustomEnabled(false); updateBandwidth(); } private void setCustomEnabled(boolean enabled) { customSettings.setEnabled(enabled); for (java.awt.Component child : customSettings.getComponents()) child.setEnabled(enabled); } private void updateBandwidth() { qualityValue.setText(Integer.toString(quality.getValue())); int index = Math.max(0, Math.min(BANDWIDTH_RANGE.length - 1, codec.getSelectedIndex())); double[] range = BANDWIDTH_RANGE[index]; double value = range[0] + (range[1] - range[0]) * quality.getValue() / 10.0; bandwidth.setText(String.format("%.2f KiB/s", value)); } private JPanel presetBox() { JPanel box = new JPanel(); box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS)); box.setBorder(BorderFactory.createTitledBorder("Presets")); for (JRadioButton radio : new JRadioButton[]{voiceMobile, voiceDesktop, music, custom}) { radio.setAlignmentX(0f); box.add(radio); } box.add(Box.createVerticalGlue()); return box; } private JPanel customBox() { customSettings.setBorder(BorderFactory.createTitledBorder("Custom Settings")); GridBagConstraints c = new GridBagConstraints(); c.insets = UIScale.scale(new Insets(4, 4, 4, 4)); c.anchor = GridBagConstraints.WEST; c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; customSettings.add(new JLabel("Codec:"), c); c.gridx = 1; c.weightx = 1; c.gridwidth = 2; customSettings.add(codec, c); c.gridwidth = 1; c.gridx = 0; c.gridy = 1; c.weightx = 0; customSettings.add(new JLabel("Quality:"), c); c.gridx = 1; c.weightx = 1; customSettings.add(quality, c); c.gridx = 2; c.weightx = 0; customSettings.add(qualityValue, c); c.gridx = 0; c.gridy = 2; c.weighty = 1; customSettings.add(Box.createVerticalGlue(), c); return customSettings; } private JPanel bandwidthRow() { JPanel row = new JPanel(new BorderLayout(UIScale.scale(6), 0)); row.add(new JLabel("Bandwidth usage:"), BorderLayout.WEST); row.add(bandwidth, BorderLayout.CENTER); return row; } }