diff --git a/android/app/src/main/java/com/ts3client/android/ui/SoundSettingsScreen.kt b/android/app/src/main/java/com/ts3client/android/ui/SoundSettingsScreen.kt index 33e932a..5c1140c 100644 --- a/android/app/src/main/java/com/ts3client/android/ui/SoundSettingsScreen.kt +++ b/android/app/src/main/java/com/ts3client/android/ui/SoundSettingsScreen.kt @@ -39,6 +39,8 @@ import com.ts3client.config.Settings import kotlin.math.roundToInt private val DENOISE_LEVELS = listOf("Off", "6 dB", "12 dB", "18 dB", "21 dB") +private const val MIN_BITRATE_KBITS = 8f +private const val MAX_BITRATE_KBITS = 160f /** Audio devices, transmission, voice activation, processing and playback. */ @Composable @@ -51,6 +53,11 @@ fun SoundSettingsScreen(vm: SessionController, onBack: () -> Unit) { var inputMode by remember { mutableStateOf(settings.inputMode) } var vadMode by remember { mutableStateOf(settings.vadMode) } var agc by remember { mutableStateOf(settings.agc) } + var vbr by remember { mutableStateOf(settings.vbr) } + var fec by remember { mutableStateOf(settings.fec) } + var music by remember { mutableStateOf(settings.music) } + var redundancy by remember { mutableStateOf(settings.redundancyDelay) } + var copies by remember { mutableStateOf(settings.redundancyCopies) } var mutedTalkWarning by remember { mutableStateOf(settings.mutedTalkWarning) } var threshold by remember { mutableFloatStateOf(settings.vadThresholdDb.toFloat()) } var inputDevice by remember { mutableStateOf(settings.inputDevice) } @@ -147,6 +154,36 @@ fun SoundSettingsScreen(vm: SessionController, onBack: () -> Unit) { } } + Section("Encoder") + Level("Bitrate", settings.bitrate / 1000f, MIN_BITRATE_KBITS..MAX_BITRATE_KBITS, { "${it.roundToInt()} kbit/s" }) { + change { bitrate = it.roundToInt() * 1000 } + } + Level("Complexity", settings.complexity.toFloat(), 0f..10f, { "${it.roundToInt()}" }, steps = 9) { + change { complexity = it.roundToInt() } + } + Toggle("Variable bitrate (VBR)", vbr) { + vbr = it + change { this.vbr = it } + } + Toggle("Forward error correction (FEC)", fec) { + fec = it + change { this.fec = it } + } + Toggle("Music codec (stereo, higher fidelity)", music) { + music = it + change { this.music = it } + } + Picker("Redundancy", Settings.REDUNDANCY_DELAYS.map { it to if (it == 0) "Off" else "$it ms" }, redundancy) { + redundancy = it + change { redundancyDelay = it } + } + if (redundancy > 0) { + Picker("Copies", (1..Settings.MAX_REDUNDANCY_COPIES).map { it to "$it" }, copies) { + copies = it + change { redundancyCopies = it } + } + } + Section("Playback") Level("Volume", settings.outputVolume.toFloat(), 0f..2f, { "${(it * 100).roundToInt()} %" }) { change { outputVolume = it.toDouble() } diff --git a/ts3-client/core/src/main/java/com/ts3client/audio/CaptureVoiceInput.java b/ts3-client/core/src/main/java/com/ts3client/audio/CaptureVoiceInput.java index 10d3a46..d5ce8c0 100644 --- a/ts3-client/core/src/main/java/com/ts3client/audio/CaptureVoiceInput.java +++ b/ts3-client/core/src/main/java/com/ts3client/audio/CaptureVoiceInput.java @@ -63,6 +63,8 @@ public final class CaptureVoiceInput implements VoiceInput { private volatile boolean vadOverPtt; private volatile double inputGain; private volatile CodecType codec = CodecType.OPUS_VOICE; + private volatile int redundancyDelay; + private volatile int redundancyCopies; private final SpeechProbabilityDetector speechDetector = new RnnSpeechDetector(VoiceFormat.SAMPLE_RATE); @@ -109,6 +111,7 @@ public final class CaptureVoiceInput implements VoiceInput { this.inputGain = settings.inputVolume; this.params = OpusParameters.from(settings); this.codec = codecFor(params); + setRedundancy(settings.redundancyDelay, settings.redundancyCopies); processor.setNoiseSuppression(settings.denoise); processor.setSuppressionLevel(SuppressionLevel.fromDenoiserLevel(settings.denoiserLevel)); processor.setTransientSuppression(settings.typingAttenuation); @@ -192,6 +195,12 @@ public final class CaptureVoiceInput implements VoiceInput { } } + @Override + public void setRedundancy(int delayMillis, int copies) { + this.redundancyDelay = Math.max(0, delayMillis); + this.redundancyCopies = Math.max(1, Math.min(Settings.MAX_REDUNDANCY_COPIES, copies)); + } + private static OpusCodec.Application applicationFor(OpusParameters p) { return p.music ? OpusCodec.Application.AUDIO : OpusCodec.Application.VOIP; } @@ -551,6 +560,16 @@ public final class CaptureVoiceInput implements VoiceInput { return codec; } + @Override + public int getRedundancyDelay() { + return redundancyDelay; + } + + @Override + public int getRedundancyCopies() { + return redundancyCopies; + } + @Override public byte[] provide() { byte[] p = queue.poll(); diff --git a/ts3-client/core/src/main/java/com/ts3client/audio/VoiceInput.java b/ts3-client/core/src/main/java/com/ts3client/audio/VoiceInput.java index 80e6133..7da0f86 100644 --- a/ts3-client/core/src/main/java/com/ts3client/audio/VoiceInput.java +++ b/ts3-client/core/src/main/java/com/ts3client/audio/VoiceInput.java @@ -66,6 +66,9 @@ public interface VoiceInput extends Microphone { /** Applies Opus encoder parameters, taking effect immediately if capturing. */ void setOpusParameters(OpusParameters parameters); + /** Sends every voice packet {@code copies} more times, {@code delayMillis} apart; a delay of 0 sends it once. */ + void setRedundancy(int delayMillis, int copies); + void setPushToTalk(boolean pressed); /** Receives the live input level on {@link InputLevel}'s scale, once per captured frame. */ @@ -99,5 +102,6 @@ public interface VoiceInput extends Microphone { setAgc(settings.agc); setAgcBoost(settings.agcBoostDb); setOpusParameters(OpusParameters.from(settings)); + setRedundancy(settings.redundancyDelay, settings.redundancyCopies); } } diff --git a/ts3-client/core/src/main/java/com/ts3client/config/Settings.java b/ts3-client/core/src/main/java/com/ts3client/config/Settings.java index 2237715..5080aca 100644 --- a/ts3-client/core/src/main/java/com/ts3client/config/Settings.java +++ b/ts3-client/core/src/main/java/com/ts3client/config/Settings.java @@ -122,6 +122,17 @@ public final class Settings { public int packetLoss = 5; /** Encode as music (OPUS_MUSIC) rather than voice (OPUS_VOICE). */ public boolean music = false; + /** + * Milliseconds between a voice packet and each of its {@link #redundancyCopies}, 0 for off. + * Listeners' jitter buffers grow to wait for the last copy, so it buys loss resilience with + * about {@code redundancyDelay * redundancyCopies} of latency. + */ + public int redundancyDelay = 0; + /** The {@link #redundancyDelay} choices the settings offer; whole 20 ms voice frames. */ + public static final int[] REDUNDANCY_DELAYS = {0, 20, 40, 60, 80, 120, 200}; + /** How many times every voice packet is sent again. */ + public int redundancyCopies = 1; + public static final int MAX_REDUNDANCY_COPIES = 4; /** Master playback gain, 0..1 (may exceed 1 for boost up to 2). */ public double outputVolume = 1.0; /** @@ -278,6 +289,8 @@ public final class Settings { fec = parseB(props.getProperty("fec"), fec); packetLoss = parseI(props.getProperty("packetLoss"), packetLoss); music = parseB(props.getProperty("music"), music); + redundancyDelay = parseI(props.getProperty("redundancyDelay"), redundancyDelay); + redundancyCopies = parseI(props.getProperty("redundancyCopies"), redundancyCopies); outputVolume = parseD(props.getProperty("outputVolume"), outputVolume); masterVolume = parseD(props.getProperty("masterVolume"), masterVolume); inputVolume = parseD(props.getProperty("inputVolume"), inputVolume); @@ -336,6 +349,8 @@ public final class Settings { props.setProperty("fec", Boolean.toString(fec)); props.setProperty("packetLoss", Integer.toString(packetLoss)); props.setProperty("music", Boolean.toString(music)); + props.setProperty("redundancyDelay", Integer.toString(redundancyDelay)); + props.setProperty("redundancyCopies", Integer.toString(redundancyCopies)); props.setProperty("outputVolume", Double.toString(outputVolume)); props.setProperty("masterVolume", Double.toString(masterVolume)); props.setProperty("inputVolume", Double.toString(inputVolume)); diff --git a/ts3-client/swing/src/main/java/com/ts3client/ui/EncoderSection.java b/ts3-client/swing/src/main/java/com/ts3client/ui/EncoderSection.java index 68c88cc..556cf76 100644 --- a/ts3-client/swing/src/main/java/com/ts3client/ui/EncoderSection.java +++ b/ts3-client/swing/src/main/java/com/ts3client/ui/EncoderSection.java @@ -5,6 +5,7 @@ import com.ts3client.audio.VoiceInput; import com.ts3client.config.Settings; import javax.swing.JCheckBox; +import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; @@ -24,6 +25,8 @@ final class EncoderSection { private final JCheckBox vbrCheck; private final JCheckBox fecCheck; private final JCheckBox musicCheck; + private final JComboBox redundancyCombo; + private final JComboBox copiesCombo; private final JLabel bitrateLabel; @@ -53,6 +56,43 @@ final class EncoderSection { fecCheck.addActionListener(e -> pushLive()); musicCheck.addActionListener(e -> pushLive()); + String[] delays = new String[Settings.REDUNDANCY_DELAYS.length]; + for (int i = 0; i < delays.length; i++) { + int ms = Settings.REDUNDANCY_DELAYS[i]; + delays[i] = ms == 0 ? "Off" : ms + " ms"; + } + redundancyCombo = new JComboBox<>(delays); + redundancyCombo.setSelectedIndex(nearestDelay(settings.redundancyDelay)); + Integer[] copies = new Integer[Settings.MAX_REDUNDANCY_COPIES]; + for (int i = 0; i < copies.length; i++) copies[i] = i + 1; + copiesCombo = new JComboBox<>(copies); + copiesCombo.setSelectedItem(Math.max(1, Math.min(Settings.MAX_REDUNDANCY_COPIES, settings.redundancyCopies))); + copiesCombo.setEnabled(selectedDelay() > 0); + redundancyCombo.addActionListener(e -> { + copiesCombo.setEnabled(selectedDelay() > 0); + pushRedundancy(); + }); + copiesCombo.addActionListener(e -> pushRedundancy()); + } + + private void pushRedundancy() { + if (liveMic != null) liveMic.setRedundancy(selectedDelay(), selectedCopies()); + } + + private int selectedCopies() { + return (Integer) copiesCombo.getSelectedItem(); + } + + private static int nearestDelay(int ms) { + int best = 0; + for (int i = 1; i < Settings.REDUNDANCY_DELAYS.length; i++) { + if (Math.abs(Settings.REDUNDANCY_DELAYS[i] - ms) < Math.abs(Settings.REDUNDANCY_DELAYS[best] - ms)) best = i; + } + return best; + } + + private int selectedDelay() { + return Settings.REDUNDANCY_DELAYS[redundancyCombo.getSelectedIndex()]; } /** Adds the section's rows to {@code form} from {@code row} on; returns the next free row. */ @@ -62,6 +102,8 @@ final class EncoderSection { FormPanel.addWide(form, c, row++, vbrCheck); FormPanel.addWide(form, c, row++, fecCheck); FormPanel.addWide(form, c, row++, musicCheck); + FormPanel.addRow(form, c, row++, new JLabel("Redundancy:"), redundancyCombo); + FormPanel.addRow(form, c, row++, new JLabel("Copies:"), copiesCombo); return row; } @@ -72,6 +114,8 @@ final class EncoderSection { target.vbr = vbrCheck.isSelected(); target.fec = fecCheck.isSelected(); target.music = musicCheck.isSelected(); + target.redundancyDelay = selectedDelay(); + target.redundancyCopies = selectedCopies(); } private void pushLive() { diff --git a/ts3j b/ts3j index 4cc27db..90bf6e0 160000 --- a/ts3j +++ b/ts3j @@ -1 +1 @@ -Subproject commit 4cc27db0e830b2cb509d4c98832a1d657fc48256 +Subproject commit 90bf6e030c718abf2010e851960bbaaf35483296