Compare commits

..
8 Commits
Author SHA1 Message Date
erikbrockoandClaude Sonnet 5.5 19d28f718b Move MicrophoneTest to core; Android mic test in sound settings
test / test (push) Canceled after 0s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-10 19:15:11 +00:00
erikbrockoandClaude Opus 5.5 2619d9ff3d Android: Test button beside the sound pack picker
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 20:44:43 +00:00
erikbrockoandClaude Opus 5.5 380ea5acd6 Show away over muted or disabled speakers and microphone
test / test (push) Canceled after 0s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 08:38:50 +00:00
erikbrockoandClaude Opus 5.5 047ad1c1b9 Desktop: wrap long links instead of scrolling the chat sideways
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 08:34:30 +00:00
erikbrockoandClaude Opus 5.5 c3a4a0658e Desktop: block incoming whispers, Server Connection Info, ServerQuery toggle
- Block Incoming Whispers: toolbar toggle and the three TS3 hotkey actions;
  mutes all whispers per server, kept across reconnects.
- Server Connection Info window (server menu or toolbar), as in TS3:
  version with build date, uptime, ping, loss and live In/Out traffic.
  The shared figures panel moves out of ConnectionInfoDialog.
- ServerQuery clients are hidden in the tree unless toggled on (toolbar or
  the bookmark's "Show ServerQuery clients"), remembered per bookmark.
- "Show Server Tabs" in the toolbar menu hides the server tab strip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 21:13:48 +00:00
erikbrockoandClaude Opus 5.5 f6593527be Voice: stop clipping the start of words from native talkers
The native client keeps numbering voice packets through silence, so a new
word arrives far ahead of where playback stopped. Playback concealed its
way towards it and the jitter buffer's loss reset threw away the held
start of the word. Once the talker has let go, playback now skips to the
next burst, keeping the delay it held, and starts a fresh Opus decoder as
the talker's encoder does.

Also: a late copy of a passed packet no longer keeps the buffer looking
non-empty, and a burst whose stop was lost rewinds its concealment so the
next burst does not land behind playback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 20:20:09 +00:00
erikbrockoandClaude Opus 5.5 42870a702c Soundboard: play modes, optional mic, live volume, dark-friendly icon
- Mode, saved: one sound at a time, each sound once (a click restarts
  it) or mayhem (every click layers another copy). A tile click plays by
  the mode; Stop is in the context menu.
- "Mix in microphone": when off, a playing sound replaces the open mic
  rather than mixing with it.
- Volume, local volume, master volume and per-sound volume now reach
  sounds already playing; a sound turned down to nothing no longer keys
  the microphone.
- Toolbar and menu use the pack's SOUNDPACK icon, readable on dark themes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 00:21:50 +00:00
erikbrockoandClaude Opus 5.5 b33f1fd25a Soundboard: WAV and Opus sounds sent with the microphone
Core: ClipMixer mixes clips into the capturing VoiceInput's frames (after
APM/VAD), transmitting past the gate while one plays; a mute still wins and
the clip runs out unheard. Sessions owns the mixer so sounds follow the
microphone between servers, and drops them when nobody captures. Soundboard
decodes (SoundFiles: wave or Ogg Opus, sniffed by content) off the caller's
thread, caches, and plays a local copy via the new stoppable
SoundPlayer.play(float[], volume). Sounds live in soundboard.properties.

Desktop: Self > Soundboard window (tile grid, click to play/stop, drop files
to add, edit/move/remove, sent and local volume), toolbar action, and hotkeys
Play Sound, Play/Stop Sound, Stop All Sounds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 21:03:05 +00:00
58 changed files with 2731 additions and 212 deletions
@@ -5,6 +5,7 @@ import eu.lixko.ts3j.client.android.audio.AndroidAudioBackend
import eu.lixko.ts3j.client.android.audio.AudioRoute
import eu.lixko.ts3j.client.audio.AudioDevice
import eu.lixko.ts3j.client.config.Settings
import eu.lixko.ts3j.client.sound.SoundEvent
import eu.lixko.ts3j.client.sound.SoundNotifier
import eu.lixko.ts3j.client.sound.SoundPack
import kotlinx.coroutines.flow.MutableStateFlow
@@ -53,6 +54,8 @@ class AudioSetup internal constructor(private val context: Context, private val
settings.save()
}
fun playTestSound() = sounds.preview(SoundEvent.SPECIAL_SOUND_TEST)
/** Unpacks the packs the app ships with, when this version has not yet. */
fun installSoundPacks() {
if (!SoundPackInstaller(context).install()) return
@@ -2,6 +2,7 @@ package eu.lixko.ts3j.client.android
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 kotlinx.coroutines.flow.MutableStateFlow
@@ -17,36 +18,41 @@ data class MicLevel(
/**
* The level meter behind the voice-activation settings. While connected it listens to the
* connection's own microphone, so it shows exactly what gets sent; otherwise it runs a test
* microphone of its own for as long as someone watches.
* connection's own microphone, so it shows exactly what gets sent, until a microphone test
* takes over with a capture chain of its own.
*/
class LevelMonitor(private val settings: Settings, private val audio: AudioBackend) {
class LevelMonitor(private val settings: Settings, audio: AudioBackend) {
private val _level = MutableStateFlow(MicLevel())
val level: StateFlow<MicLevel> = _level
private val _testing = MutableStateFlow(false)
val testing: StateFlow<Boolean> = _testing
private val _loopback = MutableStateFlow(false)
val loopback: StateFlow<Boolean> = _loopback
private val _testFailed = MutableStateFlow(false)
/** Whether the last test could not get hold of the microphone. */
val testFailed: StateFlow<Boolean> = _testFailed
private val test = MicrophoneTest(audio, ::onLevel) { _level.value = _level.value.copy(transmitting = it) }
private var mayTest = false
/** The connection's microphone, while someone watches. */
private var source: VoiceInput? = null
/** The connection's microphone, when that is the one listened to. */
private var borrowed: VoiceInput? = null
private var test: VoiceInput? = null
/**
* Starts or stops listening.
*
* @param source the connection's microphone while it captures, else null
* @param busy whether a connection is under way, which will bring its own microphone
*/
@Synchronized
fun update(watching: Boolean, source: VoiceInput?, busy: Boolean, mayCapture: Boolean) {
when {
!watching -> release()
source != null -> if (borrowed !== source) {
release()
source.setLevelListener(::onLevel)
borrowed = source
}
busy || !mayCapture -> release()
test == null -> startTest()
}
fun update(watching: Boolean, source: VoiceInput?, mayCapture: Boolean) {
this.source = source.takeIf { watching }
mayTest = watching && mayCapture
if (!mayTest) stopTest()
follow()
}
/** The own microphone's transmit state, which the connection reports. */
@@ -54,34 +60,54 @@ class LevelMonitor(private val settings: Settings, private val audio: AudioBacke
if (borrowed != null) _level.value = _level.value.copy(transmitting = transmitting)
}
@Synchronized
fun setTesting(on: Boolean) {
if (!on) {
stopTest()
} else if (mayTest && !test.isRunning) {
val started = test.start(settings)
_testFailed.value = !started
if (started) {
test.configure { it.setMode(testMode()) }
test.setOutputDevice(settings.outputDevice.get())
_testing.value = true
}
}
follow()
}
@Synchronized
fun setLoopback(on: Boolean) {
if (!test.isRunning) return
test.setLoopback(on)
_loopback.value = on
}
/** Puts changed voice settings into effect on the test microphone. */
@Synchronized
fun apply() {
test?.let {
test.setOutputDevice(settings.outputDevice.get())
test.configure {
it.apply(settings)
it.setMode(testMode())
}
}
private fun startTest() {
release()
val input = audio.createInput(settings)
input.setMode(testMode())
input.setLevelListener(::onLevel)
input.setTalkListener { _level.value = _level.value.copy(transmitting = it) }
try {
input.start()
} catch (e: RuntimeException) {
return // no microphone to be had; the meter stays idle
}
test = input
private fun stopTest() {
if (!test.isRunning) return
test.stop()
_testing.value = false
_loopback.value = false
_level.value = MicLevel()
}
private fun release() {
/** Listens to the connection's microphone unless the test has the meter. */
private fun follow() {
val wanted = source.takeUnless { test.isRunning }
if (borrowed === wanted) return
borrowed?.setLevelListener(null)
borrowed = null
test?.stop()
test = null
wanted?.setLevelListener(::onLevel)
borrowed = wanted
_level.value = MicLevel()
}
@@ -107,6 +107,9 @@ class SessionController internal constructor(
}
val micLevel: StateFlow<MicLevel> = levels.level
val micTesting: StateFlow<Boolean> = levels.testing
val micLoopback: StateFlow<Boolean> = levels.loopback
val micTestFailed: StateFlow<Boolean> = levels.testFailed
@Volatile
private var watchingLevel = false
@@ -337,10 +340,17 @@ class SessionController internal constructor(
}
private fun updateLevels() {
val owner = _micOwner.value
levels.update(watchingLevel, owner?.microphone, owner?.isBusy == true, hasMicPermission())
levels.update(watchingLevel, _micOwner.value?.microphone, hasMicPermission())
}
/** Runs the microphone test; it ends by itself when the level is no longer watched. */
fun testMicrophone(on: Boolean) {
updateLevels() // the permission may have just been granted
levels.setTesting(on)
}
fun setMicLoopback(on: Boolean) = levels.setLoopback(on)
// ---- notifications ----
/** Shows a private chat a notification was tapped for. */
@@ -1,5 +1,6 @@
package eu.lixko.ts3j.client.android.ui
import android.Manifest
import android.content.Intent
import android.net.Uri
import android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION
@@ -7,6 +8,7 @@ import android.provider.Settings.canDrawOverlays
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
@@ -15,13 +17,16 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
@@ -102,9 +107,7 @@ fun SoundSettingsScreen(settings: Settings, sessions: SessionController, audio:
if (inputMode == Settings.InputMode.PUSH_TO_TALK) FloatingButtonToggle(audio)
val gated = inputMode == Settings.InputMode.VOICE_ACTIVATION && vadMode != Settings.VadMode.AUTOMATIC
LevelMeter(level, threshold.takeIf { gated })
if (!level.live) {
Hint(if (sessions.hasMicPermission()) "The microphone is not available right now." else "Allow the microphone to see its level.")
}
MicrophoneTestControls(sessions, level.transmitting)
if (inputMode == Settings.InputMode.VOICE_ACTIVATION) {
Section("Voice activation")
@@ -156,9 +159,14 @@ fun SoundSettingsScreen(settings: Settings, sessions: SessionController, audio:
if (packs.isEmpty()) {
Hint("This build of the app came without sound packs.")
} else {
Picker("Sound pack", packs.map { it.id() to it.name() }, pack) { id ->
packs.firstOrNull { it.id() == id }?.let(audio::setSoundPack)
pack = id
Row(verticalAlignment = Alignment.CenterVertically) {
Box(Modifier.weight(1f)) {
Picker("Sound pack", packs.map { it.id() to it.name() }, pack) { id ->
packs.firstOrNull { it.id() == id }?.let(audio::setSoundPack)
pack = id
}
}
TextButton(audio::playTestSound, Modifier.padding(end = 8.dp)) { Text("Test") }
}
}
Level("Volume", settings.soundVolume.get().toFloat(), 0f..1f, { "${(it * 100).roundToInt()} %" }) {
@@ -208,6 +216,35 @@ private val VAD_MODES = listOf(
Settings.VadMode.HYBRID to "Hybrid",
)
/** Runs the capture chain as a connection would, optionally playing back what would be sent. */
@Composable
private fun MicrophoneTestControls(sessions: SessionController, transmitting: Boolean) {
val testing by sessions.micTesting.collectAsStateWithLifecycle()
val loopback by sessions.micLoopback.collectAsStateWithLifecycle()
val failed by sessions.micTestFailed.collectAsStateWithLifecycle()
var denied by remember { mutableStateOf(false) }
val allow = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
denied = !granted
if (granted) sessions.testMicrophone(true)
}
Row(Modifier.padding(horizontal = 16.dp), verticalAlignment = Alignment.CenterVertically) {
OutlinedButton({
if (testing || sessions.hasMicPermission()) sessions.testMicrophone(!testing)
else allow.launch(Manifest.permission.RECORD_AUDIO)
}) { Text(if (testing) "Stop Test" else "Begin Test") }
if (testing) {
Text(if (transmitting) "Transmitting" else "Not transmitting", Modifier.padding(start = 16.dp),
color = MaterialTheme.colorScheme.onSurfaceVariant)
}
}
if (testing) {
Toggle("Hear myself", loopback, sessions::setMicLoopback)
if (loopback) Hint("Use headphones to avoid feedback.")
}
if (failed) Hint("Capture device unavailable")
if (denied) Hint("Allow the microphone to test it.")
}
/** The push-to-talk button over other apps, which first needs the user to allow the app to draw there. */
@Composable
private fun FloatingButtonToggle(audio: AudioSetup) {
+20 -3
View File
@@ -49,13 +49,22 @@ Android app in [`../android`](../android) does.
has learned carries over between talk bursts.
- **Playback mixing** — each speaker gets their own Opus decoder and audio line (a stream
of their own in the PipeWire mixer), so one slow decode never blocks the others.
- **Whisper receive** — targeted voice is decoded and played like normal voice.
- **Whisper receive** — targeted voice is decoded and played like normal voice;
**Block Incoming Whispers** (toolbar button or hotkey) mutes all of it per server.
- Per-client **mute**, master **deafen**, adjustable mic gain / playback volume.
- **On-the-fly Opus tuning** — bitrate, complexity, VBR, FEC and voice/music codec
can all be changed live from the options dialog and take effect on the running
encoder immediately (no reconnect); a voice↔music switch transparently rebuilds
the encoder because Opus fixes its application mode at creation.
- Configurable capture/playback **devices**.
- **Soundboard** (Self → Soundboard) — WAV and Ogg Opus files played onto the air: mixed
into what the microphone sends, transmitting past the voice-activation gate while one
plays (a mute still wins), and following the microphone to whichever server holds it.
Click a tile to play it: one sound at a time, each once (a click restarts it), or mayhem
(every click layers another copy). Your voice is mixed in, or left out while a sound
plays. Drop files on the window to add them. Per-sound and global volume, plus a local
volume for hearing them yourself, all applied to sounds already playing. Hotkeys: Play Sound,
Play/Stop Sound, Stop All Sounds. Saved in `~/.ts3jclient/soundboard.properties`.
### Server interaction
- Connect to any TS3 server (an identity is generated on first use if none exists).
@@ -118,8 +127,12 @@ Android app in [`../android`](../android) does.
- Change your nickname, mute/deafen from the toolbar.
- **Customize Toolbar** (right-click the toolbar), as in TeamSpeak 3: pick, order and
separate the buttons from ~70 actions (connection, self, dialogs, permissions,
recording, and channel/client actions on the tree's selection); search, drag and drop,
Reset to Defaults.
recording, channel/client actions on the tree's selection, server); search, drag and
drop, Reset to Defaults. The same menu hides the server tab strip.
- **Server Connection Info** (server's context menu or toolbar), as in TeamSpeak 3: the
server's version, uptime, ping, loss and In/Out traffic, live.
- **ServerQuery clients** are hidden in the tree unless toggled on (toolbar, or "Show
ServerQuery clients" in the bookmark), remembered per bookmarked server.
### Hotkeys
- **Global hotkeys**, as in TeamSpeak 3: single keys or combinations (any key can act
@@ -275,7 +288,11 @@ core/ eu.lixko.ts3j.client
│ ├── NotificationSettings per-action enabled / important flags
│ ├── SoundNotifier decides what is heard (pack + config + mute state)
│ ├── SoundPlayer renders a sound
│ ├── SoundFiles decodes wave / Ogg Opus files to mono 48 kHz
│ └── PackSoundPlayer decodes, resamples and mixes sounds onto one line
├── soundboard
│ ├── SoundboardSounds the saved sounds (soundboard.properties)
│ └── Soundboard plays them onto the air (audio/ClipMixer) and locally
└── teamspeak the official client's own data files
├── TeamSpeakSettingsDb locates settings.db; reads Contacts and the ProtobufItems sync store
├── SqliteReader minimal read-only SQLite 3 file reader (no driver)
@@ -6,6 +6,7 @@ import eu.lixko.ts3j.client.audio.opus.OpusEncoder;
import eu.lixko.ts3j.client.audio.processing.AudioProcessor;
import eu.lixko.ts3j.client.audio.processing.ns.SuppressionLevel;
import eu.lixko.ts3j.client.audio.vad.RnnSpeechDetector;
import eu.lixko.ts3j.client.config.Setting;
import eu.lixko.ts3j.client.config.Settings;
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -75,6 +76,8 @@ public final class CaptureVoiceInput implements VoiceInput {
private volatile Consumer<Boolean> talkListener; // local talk-state changes
private volatile Runnable mutedTalkListener; // speech detected while muted
private volatile AudioFrameListener monitorListener; // local monitoring of what is sent
private volatile ClipMixer clips;
private final Setting<Boolean> clipsWithMic;
private boolean mutedTalking;
private final AudioIo io;
@@ -110,6 +113,7 @@ public final class CaptureVoiceInput implements VoiceInput {
this.speechThreshold = settings.speechThreshold.get();
this.vadOverPtt = settings.vadOverPtt.get();
this.inputGain = settings.inputVolume.get();
this.clipsWithMic = settings.soundboardWithMic;
this.params = OpusParameters.from(settings);
this.codec = codecFor(params);
setRedundancy(settings.redundancyDelay.get(), settings.redundancyCopies.get());
@@ -262,6 +266,11 @@ public final class CaptureVoiceInput implements VoiceInput {
this.monitorListener = l;
}
@Override
public void setClips(ClipMixer clips) {
this.clips = clips;
}
@Override
public void setMutedTalkListener(Runnable l) {
this.mutedTalkListener = l;
@@ -314,6 +323,11 @@ public final class CaptureVoiceInput implements VoiceInput {
captureThread.start();
}
@Override
public boolean isCapturing() {
return running.get();
}
public synchronized void stop() {
running.set(false);
Thread capture = captureThread;
@@ -410,23 +424,27 @@ public final class CaptureVoiceInput implements VoiceInput {
boolean wasOpen = transmitting.get();
boolean open = decideGate(db, probability);
setTransmitting(open);
float[] frame = stereo ? pcm : mono;
int frameChannels = stereo ? encoderChannels : 1;
boolean clip = mixClips(frame, open, frameChannels);
setTransmitting(open || clip);
if (open && !muted.get() && encoder != null) {
if ((open || clip) && encoder != null) {
try {
// On the opening edge, send the buffered lead-in first so the
// word's onset isn't lost to the frame that detected it.
if (!wasOpen) {
if (open && !wasOpen) {
flushPreroll(stereo);
}
float[] sent = stereo ? pcm : mono;
packet = encoder.encode(sent);
monitor(sent, stereo ? encoderChannels : 1);
packet = encoder.encode(frame);
monitor(frame, frameChannels);
} catch (Exception ignored) {
}
}
if (!open) {
rememberForPreroll(stereo ? pcm : mono);
if (clip) {
prerollCount = 0; // that audio went out, under the clip
} else if (!open) {
rememberForPreroll(frame);
}
}
@@ -436,6 +454,27 @@ public final class CaptureVoiceInput implements VoiceInput {
}
}
/**
* Mixes the playing clips into {@code frame}; alone when the gate is shut or our voice is to
* be left out. While muted they
* are still consumed, so they keep time rather than wait for the unmute.
*
* @return whether the frame now carries a clip to transmit
*/
private boolean mixClips(float[] frame, boolean open, int channels) {
ClipMixer c = clips;
if (c == null || !c.isPlaying()) return false;
if (muted.get() || localMuted.get()) {
c.mixInto(new float[frame.length], VoiceFormat.FRAME_SIZE, channels);
return false;
}
if (open && clipsWithMic.get()) return c.mixInto(frame, VoiceFormat.FRAME_SIZE, channels);
float[] alone = new float[frame.length];
if (!c.mixInto(alone, VoiceFormat.FRAME_SIZE, channels)) return false;
System.arraycopy(alone, 0, frame, 0, frame.length);
return true;
}
private void offer(byte[] packet) {
queue.offer(packet);
// Guard against unbounded growth if the network stalls.
@@ -0,0 +1,151 @@
package eu.lixko.ts3j.client.audio;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Prerecorded clips (mono, 48&nbsp;kHz) waiting to be mixed into what the microphone sends,
* the soundboard's way onto the air. The capturing {@link VoiceInput} pulls one frame at a
* time, so clips advance at the capture clock and follow the microphone when another server
* takes it over.
*
* <p>Thread-safe: clips are started and stopped from the frontend while the capture thread mixes.
*/
public final class ClipMixer {
private static final class Clip {
final Object key;
final float[] pcm;
volatile double gain;
int position;
Clip(Object key, float[] pcm, double gain) {
this.key = key;
this.pcm = pcm;
this.gain = gain;
}
}
private final List<Clip> clips = new ArrayList<>();
private final List<Runnable> listeners = new ArrayList<>();
/** Starts {@code pcm} under {@code key}, from the top if that key is already playing. */
public void play(Object key, float[] pcm, double gain) {
if (pcm.length == 0) return;
synchronized (clips) {
clips.removeIf(c -> c.key.equals(key));
clips.add(new Clip(key, pcm, gain));
}
changed();
}
/** Starts another copy of {@code pcm} under {@code key}, mixed with any already playing. */
public void add(Object key, float[] pcm, double gain) {
if (pcm.length == 0) return;
synchronized (clips) {
clips.add(new Clip(key, pcm, gain));
}
changed();
}
/** Changes the gain of every clip playing under {@code key}, from its next frame on. */
public void setGain(Object key, double gain) {
synchronized (clips) {
for (Clip c : clips) {
if (c.key.equals(key)) c.gain = gain;
}
}
}
public void stop(Object key) {
boolean removed;
synchronized (clips) {
removed = clips.removeIf(c -> c.key.equals(key));
}
if (removed) changed();
}
public void stopAll() {
boolean any;
synchronized (clips) {
any = !clips.isEmpty();
clips.clear();
}
if (any) changed();
}
public boolean isPlaying(Object key) {
synchronized (clips) {
for (Clip c : clips) {
if (c.key.equals(key)) return true;
}
return false;
}
}
public boolean isPlaying() {
synchronized (clips) {
return !clips.isEmpty();
}
}
/** Told whenever a clip starts or stops, on whichever thread caused it (the capture thread when one ends). */
public void addListener(Runnable listener) {
synchronized (listeners) {
listeners.add(listener);
}
}
public void removeListener(Runnable listener) {
synchronized (listeners) {
listeners.remove(listener);
}
}
/**
* Adds the next {@code frames} samples of every playing clip to {@code frame} (interleaved,
* the mono clips copied to each channel), clamped to [-1, 1], and moves the clips on.
*
* Clips turned down to nothing move on without counting as mixed in, so they do not key the
* microphone.
*
* @return whether any clip was mixed in
*/
public boolean mixInto(float[] frame, int frames, int channels) {
boolean mixed = false;
boolean ended = false;
synchronized (clips) {
for (Iterator<Clip> it = clips.iterator(); it.hasNext(); ) {
Clip c = it.next();
int n = Math.min(frames, c.pcm.length - c.position);
double gain = c.gain;
if (gain > 0) {
for (int i = 0; i < n; i++) {
float s = (float) (c.pcm[c.position + i] * gain);
for (int ch = 0; ch < channels; ch++) frame[i * channels + ch] += s;
}
mixed = true;
}
c.position += n;
if (c.position >= c.pcm.length) {
it.remove();
ended = true;
}
}
}
if (mixed) {
for (int i = 0; i < frames * channels; i++) frame[i] = Math.max(-1f, Math.min(1f, frame[i]));
}
if (ended) changed();
return mixed;
}
private void changed() {
Runnable[] snapshot;
synchronized (listeners) {
snapshot = listeners.toArray(new Runnable[0]);
}
for (Runnable l : snapshot) l.run();
}
}
@@ -207,14 +207,54 @@ public final class JitterBuffer {
return total;
}
/** True when no packet is held and playback has nothing left to drain. */
/**
* True when playback has nothing left to drain. A packet that arrived after playback
* passed it may still be held, for the timing it reported, but it can never be played.
*/
public boolean isEmpty() {
if (resetState) {
for (int i = 0; i < MAX_PACKETS; i++) {
if (data[i] != null) return false;
}
return true;
}
for (int i = 0; i < MAX_PACKETS; i++) {
if (data[i] != null) return false;
if (data[i] != null && gt(timestamp[i] + span[i], pointerTimestamp)) return false;
}
return true;
}
/** Timestamp of the earliest packet still to be played; only meaningful when not {@link #isEmpty}. */
public int earliest() {
boolean found = false;
int earliest = pointerTimestamp;
for (int i = 0; i < MAX_PACKETS; i++) {
if (data[i] == null || (!resetState && !gt(timestamp[i] + span[i], pointerTimestamp))) continue;
if (!found || lt(timestamp[i], earliest)) {
earliest = timestamp[i];
found = true;
}
}
return earliest;
}
/**
* Moves playback to {@code timestamp}, keeping the delay estimate: held packets measure
* their slack against the moved timeline. Does nothing before the first packet has
* anchored the timeline.
*/
public void seek(int timestamp) {
if (resetState) return;
int delta = timestamp - pointerTimestamp;
for (int i = 0; i < MAX_PACKETS; i++) {
if (data[i] != null && arrival[i] != 0) arrival[i] += delta;
}
pointerTimestamp = timestamp;
nextStop = timestamp;
buffered = 0;
lostCount = 0;
}
/**
* Drops every held packet and forgets both the timeline and the network estimate.
*
@@ -1,26 +1,24 @@
package eu.lixko.ts3j.client.ui.options;
package eu.lixko.ts3j.client.audio;
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.
* The settings' microphone test, driven 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
* <p>It 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.
*
* <p>Level and transmit changes are reported on the capture thread.
*/
final class MicrophoneTest {
public final class MicrophoneTest {
/** Frames buffered for loopback before the oldest is dropped (~100 ms). */
private static final int LOOPBACK_QUEUE_FRAMES = 5;
@@ -38,14 +36,14 @@ final class MicrophoneTest {
private Thread loopbackThread;
private String outputDevice = "";
MicrophoneTest(AudioBackend audio, Consumer<Double> onLevel, Consumer<Boolean> onTransmitting) {
public 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() {
public boolean isRunning() {
return mic != null;
}
@@ -54,11 +52,11 @@ final class MicrophoneTest {
*
* @return false if the capture device could not be opened
*/
boolean start(Settings settings) {
public 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.setLevelListener(onLevel);
input.setTalkListener(onTransmitting);
input.setMonitorListener(this::enqueueForLoopback);
this.outputDevice = settings.outputDevice.get();
try {
@@ -74,7 +72,7 @@ final class MicrophoneTest {
* 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() {
public void stop() {
setLoopback(false);
if (mic != null) {
mic.stop();
@@ -83,17 +81,17 @@ final class MicrophoneTest {
}
/** Applies a change to the test chain, if it is running. */
void configure(Consumer<VoiceInput> change) {
public 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) {
public void setOutputDevice(String deviceId) {
this.outputDevice = deviceId == null ? "" : deviceId;
}
void setLoopback(boolean enabled) {
public void setLoopback(boolean enabled) {
if (enabled == loopbackEnabled) {
return;
}
@@ -157,14 +155,7 @@ final class MicrophoneTest {
/** 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);
}
Pcm16.encode(samples, samples.length, pcm);
return pcm;
}
@@ -16,6 +16,9 @@ public interface VoiceInput extends Microphone {
void stop();
/** Whether the microphone is open and being read, between a successful {@link #start} and {@link #stop}. */
boolean isCapturing();
void setMuted(boolean muted);
/**
@@ -87,6 +90,12 @@ public interface VoiceInput extends Microphone {
*/
void setMonitorListener(AudioFrameListener listener);
/**
* Mixes the clips playing on {@code clips} into what is sent, transmitting while any plays
* whatever the gate says; a mute still wins, and the clips run on unheard. Null detaches them.
*/
void setClips(ClipMixer clips);
/** Called when speech is detected while the microphone is muted. */
void setMutedTalkListener(Runnable listener);
@@ -10,11 +10,11 @@ import java.util.function.BiConsumer;
* One speaker's incoming voice: packets go into an adaptive {@link JitterBuffer} as they
* arrive, and every {@link #pull} plays out the next 20&nbsp;ms, concealing what went missing.
*
* <p>The buffer is driven the way the official client drives it. A speaker has one timeline,
* {@code unwrap16(packetId) * 60} ticks, which is never re-anchored between talk bursts: the
* sender stops numbering when it stops talking and playback stops at the same point, so the
* next burst lands where the last one left off and the delay the buffer has learned carries
* over. Only a long silence drops the timeline.
* <p>A speaker has one timeline, {@code unwrap16(packetId) * 60} ticks, kept across talk
* bursts so the delay the buffer has learned carries over. The official client keeps
* numbering while its user is silent, so a burst usually starts well ahead of where the last
* one stopped; once the talker has let go, playback skips to the next burst, holding the
* same delay. Only a long silence drops the timeline.
*
* <p>An empty packet is the talker letting go of the key. It never enters the buffer; its
* position is remembered, the burst ends once playback reaches it, and its slot is played as
@@ -64,6 +64,14 @@ final class VoiceStream {
private int lastSeq;
private int[] stopMarks = new int[8];
private int stopMarkCount;
/** Where the audio last played from a packet ends; the talker resumes from there. */
private int heard;
private boolean heardAny;
/** How far playback trailed the talker when they let go; the next burst is held back as much. */
private int stopLag = FRAME_TICKS;
/** Slots before this one are the silence ahead of a burst skipped to, not losses. */
private int burstStart;
private boolean skippedTo;
/** Channels of the latest packet: TeamSpeak streams {@code OPUS_MUSIC} in stereo. */
private int channels = 1;
private boolean playing;
@@ -76,7 +84,7 @@ final class VoiceStream {
private int extraTicks;
/** Frames of silence still to play before a fresh timeline starts, building up {@link #extraTicks}. */
private int leadInFrames;
/** Set when the timeline was dropped; the decoder follows on the next pull. */
/** Set when a burst ended or the timeline was dropped: the talker's encoder starts afresh. */
private boolean resetDecoder;
/** Recently received sequence numbers, each at {@code seq % SEEN_WINDOW}. */
private final int[] seen = new int[SEEN_WINDOW];
@@ -150,6 +158,9 @@ final class VoiceStream {
jitter.reset();
epoch = EPOCH_UNSET;
stopMarkCount = 0;
heardAny = false;
skippedTo = false;
stopLag = FRAME_TICKS;
resetDecoder = true;
forgetSeen();
}
@@ -158,6 +169,7 @@ final class VoiceStream {
int ticks = seq * FRAME_TICKS;
if (end) {
markStop(ticks);
if (playing) stopLag = Math.max(jitter.margin(), ticks - jitter.position());
return false;
}
boolean behind = !fresh && ticks - jitter.position() < 0;
@@ -199,8 +211,6 @@ final class VoiceStream {
boolean reset;
synchronized (lock) {
if (!playing) return IDLE;
reset = resetDecoder;
resetDecoder = false;
packetChannels = channels;
if (leadInFrames > 0) {
@@ -218,6 +228,7 @@ final class VoiceStream {
}
jitter.remainingSpan(remainingTicks());
} else {
skipToNextBurst();
boolean empty = jitter.isEmpty();
emptyPulls = empty ? emptyPulls + 1 : 0;
if (empty && (stoppedAt(jitter.position()) || emptyPulls >= EMPTY_PULLS_TO_END)) {
@@ -230,9 +241,12 @@ final class VoiceStream {
source = Source.PACKET;
packet = jitter.payload();
played++;
skippedTo = false;
heard(jitter.position());
}
case MISSING -> {
if (isStopMark(jitter.timestamp())) {
if (isStopMark(jitter.timestamp())
|| (skippedTo && jitter.timestamp() - burstStart < 0)) {
source = Source.SILENCE;
} else {
missed++;
@@ -240,6 +254,7 @@ final class VoiceStream {
if (packet != null) {
source = Source.RECOVER;
recovered++;
heard(jitter.position());
} else {
source = Source.CONCEAL;
}
@@ -254,6 +269,8 @@ final class VoiceStream {
jitter.tick();
}
}
reset = source != null && resetDecoder;
if (reset) resetDecoder = false;
}
if (source == null) {
markTalking(false);
@@ -431,11 +448,36 @@ final class VoiceStream {
return false;
}
/** Whether the talker let go at or shortly before {@code position}, so the burst is spoken out. */
/**
* Once the talker has let go, moves playback up to the next burst they started, which may
* be numbered far ahead, trailing it by the delay held when they stopped. Call under the lock.
*/
private void skipToNextBurst() {
int position = jitter.position();
if (jitter.isEmpty() || !stoppedAt(position)) return;
int next = jitter.earliest();
int target = next - stopLag;
if (target - position <= 0) return;
jitter.seek(target);
burstStart = next;
skippedTo = true;
resetDecoder = true;
}
private void heard(int position) {
heard = position;
heardAny = true;
}
/**
* Whether the talker let go between the last audio heard and {@code position}, so the
* burst is spoken out. An earlier burst's stop does not count.
*/
private boolean stoppedAt(int position) {
for (int i = 0; i < stopMarkCount; i++) {
int behind = position - stopMarks[i];
if (behind >= 0 && behind < STOP_MARK_WINDOW_TICKS) return true;
if (behind < 0 || behind >= STOP_MARK_WINDOW_TICKS) continue;
if (!heardAny || stopMarks[i] - heard >= 0) return true;
}
return false;
}
@@ -449,7 +491,14 @@ final class VoiceStream {
}
private void stopLocked() {
// Concealment past the last packet was a guess that the talker was still going. Take
// it back, or the next burst, numbered on from that packet, lands behind playback.
if (heardAny && jitter.position() - heard > 0) {
if (!stoppedAt(jitter.position())) markStop(heard);
jitter.seek(heard);
}
playing = false;
resetDecoder = true;
surplusFrames = 0;
concealOwed = 0;
leadInFrames = 0;
@@ -28,6 +28,8 @@ public final class Bookmark {
public boolean myTeamSpeakFeatures = true;
/** Offer whispering in this server's menus. */
public boolean showWhisperControls = false;
/** List the server's ServerQuery clients in the tree. */
public boolean showQueryClients = false;
/** The server's icon as last seen there, 0 for none or not yet known. */
public long iconId = 0;
@@ -52,6 +54,7 @@ public final class Bookmark {
b.connectOnStartup = connectOnStartup;
b.myTeamSpeakFeatures = myTeamSpeakFeatures;
b.showWhisperControls = showWhisperControls;
b.showQueryClients = showQueryClients;
b.iconId = iconId;
return b;
}
@@ -47,6 +47,31 @@ public final class Bookmarks {
return changed;
}
/** Whether a bookmark leading to the server lists its ServerQuery clients. */
public boolean showsQueryClients(String address, int port) {
for (Bookmark b : entries) {
if (b.pointsAt(address, port) && b.showQueryClients) return true;
}
return false;
}
/**
* Has every bookmark leading to the server list its ServerQuery clients, or not.
*
* @return whether any bookmark changed, and so needs saving
*/
public boolean rememberQueryClients(String address, int port, boolean show) {
if (address == null || address.isBlank()) return false;
boolean changed = false;
for (Bookmark b : entries) {
if (b.pointsAt(address, port) && b.showQueryClients != show) {
b.showQueryClients = show;
changed = true;
}
}
return changed;
}
/** The bookmark with this {@link Bookmark#id}, or null. */
public Bookmark find(String id) {
for (Bookmark b : entries) {
@@ -90,6 +115,7 @@ public final class Bookmarks {
bm.connectOnStartup = Boolean.parseBoolean(p.getProperty(prefix + "connectOnStartup", "false"));
bm.myTeamSpeakFeatures = Boolean.parseBoolean(p.getProperty(prefix + "myTeamSpeakFeatures", "true"));
bm.showWhisperControls = Boolean.parseBoolean(p.getProperty(prefix + "showWhisperControls", "false"));
bm.showQueryClients = Boolean.parseBoolean(p.getProperty(prefix + "showQueryClients", "false"));
bm.iconId = parseLong(p.getProperty(prefix + "iconId"));
if (bm.address != null && !bm.address.isBlank()) b.entries.add(bm);
}
@@ -114,6 +140,7 @@ public final class Bookmarks {
p.setProperty(prefix + "connectOnStartup", Boolean.toString(bm.connectOnStartup));
p.setProperty(prefix + "myTeamSpeakFeatures", Boolean.toString(bm.myTeamSpeakFeatures));
p.setProperty(prefix + "showWhisperControls", Boolean.toString(bm.showWhisperControls));
p.setProperty(prefix + "showQueryClients", Boolean.toString(bm.showQueryClients));
p.setProperty(prefix + "iconId", Long.toString(bm.iconId));
}
try {
@@ -49,6 +49,16 @@ public final class Settings {
HYBRID
}
/** What playing a soundboard sound does to the sounds already playing. */
public enum SoundboardMode {
/** Stops every other sound: one at a time. */
SINGLE,
/** Restarts the sound if it is playing; others go on. */
ONCE_EACH,
/** Starts another copy, mixed with any already playing. */
MAYHEM
}
/** Who may whisper to us, the way TeamSpeak's whisper options put it. */
public enum WhisperPolicy {
/** Use individual settings in contacts, else allow. */
@@ -169,6 +179,15 @@ public final class Settings {
/** Beep when speech is picked up while the microphone is muted. */
public final Setting<Boolean> mutedTalkWarning = flag("mutedTalkWarning", true);
// ---- soundboard ----
/** Gain on soundboard sounds as sent, 0..2. */
public final Setting<Double> soundboardVolume = decimal("soundboardVolume", 1.0);
/** How loud we hear our own soundboard sounds, 0..1; 0 plays them to the server only. */
public final Setting<Double> soundboardLocalVolume = decimal("soundboardLocalVolume", 0.5);
public final Setting<SoundboardMode> soundboardMode = choice("soundboardMode", SoundboardMode.ONCE_EACH);
/** Whether our voice goes out along with the sounds, when the microphone transmits; else they replace it. */
public final Setting<Boolean> soundboardWithMic = flag("soundboardWithMic", true);
// ---- notification sounds ----
/** Directory name of the active sound pack; empty picks the first one found. */
public final Setting<String> soundPack = text("soundPack", "default");
@@ -199,6 +218,8 @@ public final class Settings {
// ---- window chrome ----
/** Whether the status bar at the bottom of the window is shown. */
public final Setting<Boolean> showStatusBar = flag("showStatusBar", true);
/** The strip of server tabs while more than one server is open; hidden, only the one on screen shows. */
public final Setting<Boolean> showServerTabs = flag("showServerTabs", true);
/** The main toolbar's buttons in order, comma-separated action ids with "-" for a separator; empty for the default set. */
public final Setting<String> toolbarLayout = text("toolbarLayout", "");
/** Whether the Master Volume slider is shown on the toolbar. */
@@ -145,11 +145,11 @@ public enum HotkeyAction {
WHISPER_REPLY_DEACTIVATE(Category.SELF, 0x0180, "DeactivateReply",
"Deactivate Reply to a Whisper", true, Argument.NONE, false),
WHISPER_BLOCK_ACTIVATE(Category.SELF, 0x0180, "ActivateBlock",
"Activate Block Incoming Whispers", true, Argument.NONE, false),
"Activate Block Incoming Whispers", true, Argument.NONE, true),
WHISPER_BLOCK_DEACTIVATE(Category.SELF, 0x0180, "DeactivateBlock",
"Deactivate Block Incoming Whispers", true, Argument.NONE, false),
"Deactivate Block Incoming Whispers", true, Argument.NONE, true),
WHISPER_BLOCK_TOGGLE(Category.SELF, 0x0180, "ToggleBlock",
"Toggle Block Incoming Whispers", true, Argument.NONE, false),
"Toggle Block Incoming Whispers", true, Argument.NONE, true),
RECORDING_START(Category.SELF, 0x01c0, "Activate",
"Start Recording", true, Argument.NONE, false),
@@ -158,6 +158,14 @@ public enum HotkeyAction {
RECORDING_STOP(Category.SELF, 0x01c0, "Deactivate",
"Stop Recording", true, Argument.NONE, false),
/** Not in TS3, which has no soundboard: the type is our own, clear of TS3's range. */
SOUNDBOARD_PLAY(Category.SELF, 0x1000, "Play",
"Play Sound", false, Argument.SOUND, true),
SOUNDBOARD_TOGGLE(Category.SELF, 0x1000, "Toggle",
"Play/Stop Sound", true, Argument.SOUND, true),
SOUNDBOARD_STOP(Category.SELF, 0x1000, "Stop",
"Stop All Sounds", false, Argument.NONE, true),
VOLUME_INCREASE(Category.SELF, 0x01e0, "Increase",
"Increase Master Volume", true, Argument.NONE, true),
VOLUME_DECREASE(Category.SELF, 0x01e0, "Decrease",
@@ -257,7 +265,8 @@ public enum HotkeyAction {
Map.entry(0x01e0, "Master Volume"),
Map.entry(0x01f0, "Plugins"),
Map.entry(0x0250, "3D Sound"),
Map.entry(0x0270, "Permissions"));
Map.entry(0x0270, "Permissions"),
Map.entry(0x1000, "Soundboard"));
/** What the action's free-text parameter means, when it takes one. */
public enum Argument {
@@ -268,6 +277,8 @@ public enum HotkeyAction {
CHANNEL,
/** Name of a capture/playback/hotkey profile or sound pack. */
PROFILE,
/** Name of a soundboard sound. */
SOUND,
/** Anything else: nickname, away message, tab number, plugin command. */
TEXT
}
@@ -1,16 +1,16 @@
package eu.lixko.ts3j.client.net;
/**
* What the channel tree shows for a client, in the order the official client gives the
* states priority: the first that applies wins.
* What the channel tree shows for a client, in priority order: the first that applies wins.
* Unlike the official client, away outranks muted or disabled speakers and microphone.
*/
public enum ClientStatus {
QUERY,
AWAY,
SPEAKERS_DISABLED,
SPEAKERS_MUTED,
MIC_DISABLED,
MIC_MUTED,
AWAY,
COMMANDER_TALKING,
COMMANDER,
TALKING,
@@ -18,11 +18,11 @@ public enum ClientStatus {
public static ClientStatus of(ClientEntry c) {
if (c.isQuery()) return QUERY;
if (c.away) return AWAY;
if (!c.outputHardware) return SPEAKERS_DISABLED;
if (c.outputMuted) return SPEAKERS_MUTED;
if (!c.inputHardware) return MIC_DISABLED;
if (c.inputMuted) return MIC_MUTED;
if (c.away) return AWAY;
if (c.channelCommander) return c.talking ? COMMANDER_TALKING : COMMANDER;
return c.talking ? TALKING : IDLE;
}
@@ -691,6 +691,7 @@ final class ConnectionEventHandler implements TS3Listener {
else if ("notifytokenadd".equals(e.getCommand())) conn.privilegeKeys.onAdded(e);
else if ("notifycomplainlist".equals(e.getCommand())) conn.complaints.onListEntry(e);
else if ("notifyserverlog".equals(e.getCommand())) conn.serverLog.onLine(e);
else if ("notifyserverconnectioninfo".equals(e.getCommand())) conn.serverStats.onReport(e);
else conn.offlineMessages.onReply(e);
}
@@ -4,8 +4,13 @@ import eu.lixko.ts3j.client.audio.VoiceReceiveStats;
import eu.lixko.ts3j.client.text.ByteSize;
import java.text.DecimalFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Renders {@link ConnectionStats} the way the official Connection Info window does: a
@@ -43,6 +48,12 @@ public final class ConnectionStatsFormat {
new Tab("Keep Alive", ConnectionStats.Kind.KEEPALIVE),
new Tab("Control", ConnectionStats.Kind.CONTROL));
/** The server window's traffic table headings. */
public static final List<String> SERVER_COLUMNS = List.of("In", "Out");
private static final Pattern BUILD = Pattern.compile("^(.*?)\\s*\\[Build: (\\d+)]\\s*$");
private static final DateTimeFormatter BUILD_DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static final String QUOTA_TAB = "Quota";
public static final String VOICE_TAB = "Voice received";
@@ -112,6 +123,50 @@ public final class ConnectionStatsFormat {
new Row("Remaining " + direction + " this month:", remaining));
}
/** The server window's facts; {@code s} is {@link ServerStats#UNKNOWN} while loading. */
public static List<Row> serverSummaryRows(ServerModel model, String address, ServerStats s) {
return List.of(
new Row("Name", model.getServerName()),
new Row("Version", versionWithBuildDate(model.serverProperty("virtualserver_version"))),
new Row("Platform", orUnknown(model.serverProperty("virtualserver_platform"))),
new Row("Uptime", uptime(s.uptimeSeconds())),
new Row("Address", address),
new Row("Average ping", s.pingMs() < 0 ? UNKNOWN : Math.round(s.pingMs()) + " ms"),
new Row("Average packetloss", loss(s.packetLoss())));
}
/** The server window's table, under {@link #SERVER_COLUMNS}; {@code total} is left empty. */
public static List<TrafficRow> serverTrafficRows(ServerStats s) {
return List.of(
new TrafficRow("Packets transferred", "", count(s.packetsIn()), count(s.packetsOut())),
new TrafficRow("Bytes transferred", "", ByteSize.format(s.bytesIn()), ByteSize.format(s.bytesOut())),
new TrafficRow("Bandwidth last second", "",
ByteSize.rate(s.bandwidthInLastSecond()), ByteSize.rate(s.bandwidthOutLastSecond())),
new TrafficRow("Bandwidth last minute", "",
ByteSize.rate(s.bandwidthInLastMinute()), ByteSize.rate(s.bandwidthOutLastMinute())),
new TrafficRow("File transfer bandwidth", "",
ByteSize.rate(s.fileTransferBandwidthIn()), ByteSize.rate(s.fileTransferBandwidthOut())),
new TrafficRow("File transfer bytes transferred", "",
ByteSize.format(s.fileTransferBytesIn()), ByteSize.format(s.fileTransferBytesOut())));
}
/** {@code 3.13.7 [Build: 1655727713]} as {@code 3.13.7 (2022-06-20 12:21:53)}, the build being its time. */
public static String versionWithBuildDate(String version) {
if (version == null || version.isBlank()) return UNKNOWN;
Matcher m = BUILD.matcher(version);
if (!m.matches()) return version;
try {
Instant built = Instant.ofEpochSecond(Long.parseLong(m.group(2)));
return m.group(1) + " (" + BUILD_DATE.format(built.atZone(ZoneId.systemDefault())) + ")";
} catch (RuntimeException e) {
return version;
}
}
private static String orUnknown(String value) {
return value == null || value.isBlank() ? UNKNOWN : value;
}
/** The voice our playback received; every value is a dash when nothing was heard. */
public static List<Row> voiceRows(VoiceReceiveStats v) {
VoiceReceiveStats s = v == null ? VoiceReceiveStats.NONE : v;
@@ -158,6 +213,15 @@ public final class ConnectionStatsFormat {
return count(n) + " (" + new DecimalFormat("0.0").format(100.0 * n / of) + " %)";
}
/** {@code 0 hours 3 minutes 55 seconds}, with days in front from a day on. */
public static String uptime(long seconds) {
if (seconds < 0) return UNKNOWN;
long days = seconds / 86400;
String rest = (seconds % 86400) / 3600 + " hours " + (seconds % 3600) / 60 + " minutes "
+ seconds % 60 + " seconds";
return days > 0 ? days + " days " + rest : rest;
}
/** {@code 04:34}, or {@code 15:35:17} from an hour on. */
public static String duration(long ms) {
if (ms < 0) return UNKNOWN;
@@ -4,6 +4,7 @@ import com.github.manevolent.ts3j.command.SingleCommand;
import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter;
import com.github.manevolent.ts3j.protocol.ProtocolRole;
import eu.lixko.ts3j.client.audio.AudioBackend;
import eu.lixko.ts3j.client.audio.ClipMixer;
import eu.lixko.ts3j.client.audio.VoiceInput;
import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.config.Settings;
@@ -36,6 +37,7 @@ public final class OwnClient {
/** Set up by the connect thread and released by whichever thread notices the end; copied before use. */
private volatile VoiceInput microphone;
private volatile VoiceOutput playback;
private volatile ClipMixer clips;
/**
* Whether this connection owns the capture device. Only one connection may capture at a
@@ -152,6 +154,7 @@ public final class OwnClient {
ui.onTalkStateChanged(id, talking);
});
mic.setMutedTalkListener(this::onTalkingWhileMuted);
mic.setClips(clips);
microphone = mic;
}
@@ -175,6 +178,13 @@ public final class OwnClient {
}
}
/** Mixes these clips into what the microphone sends, from now and on every connect. */
public void setClips(ClipMixer clips) {
this.clips = clips;
VoiceInput mic = microphone;
if (mic != null) mic.setClips(clips);
}
/**
* Gives this connection the capture device, or takes it away. Capturing only starts once
* connected; calling this before or during a connect is fine, the requested state is
@@ -157,6 +157,10 @@ public final class Permissions {
return permits("b_virtualserver_log_view");
}
public boolean canViewServerConnectionInfo() {
return permits("b_virtualserver_connectioninfo_view");
}
public boolean canListPrivilegeKeys() {
return permits("b_virtualserver_token_list");
}
@@ -31,6 +31,8 @@ public final class ServerModel {
private int defaultChannelGroupId;
/** The channel new clients land in, and kicked ones go back to; 0 until the server names it. */
private int defaultChannelId;
/** Whether {@link #buildTree} lists ServerQuery clients; a view setting, so kept by {@link #clear}. */
private boolean showQueryClients;
public synchronized void clear() {
channels.clear();
@@ -458,7 +460,7 @@ public final class ServerModel {
}
}
for (ClientEntry cl : clients.values()) {
if (cl.isQuery()) continue; // hide server-query clients from the tree
if (cl.isQuery() && !showQueryClients) continue;
if (channels.containsKey(cl.channelId)) {
inside.computeIfAbsent(cl.channelId, id -> new ArrayList<>()).add(cl);
}
@@ -507,6 +509,14 @@ public final class ServerModel {
siblings.addAll(ordered);
}
public synchronized boolean showsQueryClients() {
return showQueryClients;
}
public synchronized void setShowQueryClients(boolean show) {
showQueryClients = show;
}
public synchronized int channelCount() {
return channels.size();
}
@@ -0,0 +1,60 @@
package eu.lixko.ts3j.client.net;
import java.util.Map;
/**
* The virtual server's connection figures, from {@code notifyserverconnectioninfo}: everything
* it exchanged with all its clients. Seen from the server, as the official window shows them:
* <em>in</em> is what it received, <em>out</em> what it sent. Unreported values are {@code -1}.
*
* @param packetLoss a fraction 0..1
*/
public record ServerStats(long uptimeSeconds, double pingMs, double packetLoss,
long packetsIn, long packetsOut, long bytesIn, long bytesOut,
long bandwidthInLastSecond, long bandwidthOutLastSecond,
long bandwidthInLastMinute, long bandwidthOutLastMinute,
long fileTransferBandwidthIn, long fileTransferBandwidthOut,
long fileTransferBytesIn, long fileTransferBytesOut) {
/** Nothing known yet. */
public static final ServerStats UNKNOWN = parse(Map.of());
static ServerStats parse(Map<String, String> m) {
return new ServerStats(
number(m, "connection_connected_time"),
decimal(m, "connection_ping"),
decimal(m, "connection_packetloss_total"),
number(m, "connection_packets_received_total"),
number(m, "connection_packets_sent_total"),
number(m, "connection_bytes_received_total"),
number(m, "connection_bytes_sent_total"),
number(m, "connection_bandwidth_received_last_second_total"),
number(m, "connection_bandwidth_sent_last_second_total"),
number(m, "connection_bandwidth_received_last_minute_total"),
number(m, "connection_bandwidth_sent_last_minute_total"),
number(m, "connection_filetransfer_bandwidth_received"),
number(m, "connection_filetransfer_bandwidth_sent"),
number(m, "connection_filetransfer_bytes_received_total"),
number(m, "connection_filetransfer_bytes_sent_total"));
}
private static long number(Map<String, String> m, String key) {
String v = m.get(key);
if (v == null) return -1;
try {
return Long.parseLong(v.trim());
} catch (NumberFormatException e) {
return -1;
}
}
private static double decimal(Map<String, String> m, String key) {
String v = m.get(key);
if (v == null) return -1;
try {
return Double.parseDouble(v.trim());
} catch (NumberFormatException e) {
return -1;
}
}
}
@@ -0,0 +1,41 @@
package eu.lixko.ts3j.client.net;
import com.github.manevolent.ts3j.command.SingleCommand;
import com.github.manevolent.ts3j.event.UnknownTeamspeakEvent;
import com.github.manevolent.ts3j.protocol.ProtocolRole;
import java.util.concurrent.CompletableFuture;
/**
* Asks for the virtual server's connection figures with {@code serverrequestconnectioninfo},
* which the server answers with a {@code notifyserverconnectioninfo} ahead of the command's
* result. Needs {@code b_virtualserver_connectioninfo_view}.
*
* <p>Requests run in the background and fail with a {@link RequestException}.
*/
public final class ServerStatsAdmin {
private final Uplink uplink;
private volatile ServerStats latest;
ServerStatsAdmin(Uplink uplink) {
this.uplink = uplink;
}
public CompletableFuture<ServerStats> read() {
return uplink.request(() -> {
synchronized (this) {
latest = null;
uplink.socket().executeCommand(new SingleCommand("serverrequestconnectioninfo", ProtocolRole.CLIENT))
.complete();
uplink.awaitEventsProcessed();
ServerStats stats = latest;
return stats == null ? ServerStats.UNKNOWN : stats;
}
});
}
void onReport(UnknownTeamspeakEvent e) {
latest = ServerStats.parse(e.getMap());
}
}
@@ -82,6 +82,7 @@ public final class TeamspeakConnection {
final PrivilegeKeyAdmin privilegeKeys = new PrivilegeKeyAdmin(uplink);
final ComplaintAdmin complaints;
final ServerLogAdmin serverLog = new ServerLogAdmin(uplink);
final ServerStatsAdmin serverStats = new ServerStatsAdmin(uplink);
final OfflineMessageAdmin offlineMessages;
private final AvatarAdmin avatarAdmin;
/** Ourselves: devices and status flags; package-private for {@link ConnectionEventHandler}. */
@@ -94,6 +95,8 @@ public final class TeamspeakConnection {
*/
/** The client whose whisper we heard last, to reply to; 0 for none yet. */
private volatile int lastWhisperer;
/** Whether incoming whispers go unheard, whoever sends them; kept across reconnects. */
private volatile boolean whispersBlocked;
private volatile LocalIdentity identity;
/**
* The connect in progress. A disconnect clears it and a newer connect replaces it, which
@@ -282,7 +285,7 @@ public final class TeamspeakConnection {
client.addListener(events);
client.setVoiceHandler(playback::handleVoice);
client.setWhisperHandler(whisper -> {
if (!contacts.isWhisperAllowed(whisper.getClientId())) return;
if (whispersBlocked || !contacts.isWhisperAllowed(whisper.getClientId())) return;
lastWhisperer = whisper.getClientId();
playback.handleWhisper(whisper);
});
@@ -643,6 +646,10 @@ public final class TeamspeakConnection {
return serverLog;
}
public ServerStatsAdmin serverStats() {
return serverStats;
}
/** Our offline message inbox, sending offline messages and finding their recipients. */
public OfflineMessageAdmin offlineMessages() {
return offlineMessages;
@@ -745,6 +752,14 @@ public final class TeamspeakConnection {
sock.setWhisperTarget(target);
}
public boolean areWhispersBlocked() {
return whispersBlocked;
}
public void setWhispersBlocked(boolean blocked) {
whispersBlocked = blocked;
}
/** The client who whispered to us last, if still on the server. */
public ClientEntry lastWhisperer() {
int id = lastWhisperer;
@@ -14,6 +14,7 @@ import eu.lixko.ts3j.client.myts.MyTsException;
import eu.lixko.ts3j.client.contacts.ContactStore;
import eu.lixko.ts3j.client.net.AvatarAdmin;
import eu.lixko.ts3j.client.net.BanAdmin;
import eu.lixko.ts3j.client.audio.ClipMixer;
import eu.lixko.ts3j.client.audio.VoiceInput;
import eu.lixko.ts3j.client.audio.VoiceOutput;
import eu.lixko.ts3j.client.myts.MyTeamSpeakLogin;
@@ -37,6 +38,7 @@ import eu.lixko.ts3j.client.net.PrivilegeKeyAdmin;
import eu.lixko.ts3j.client.net.Recording;
import eu.lixko.ts3j.client.net.Replies;
import eu.lixko.ts3j.client.net.ServerLogAdmin;
import eu.lixko.ts3j.client.net.ServerStatsAdmin;
import eu.lixko.ts3j.client.net.TeamspeakConnection;
import eu.lixko.ts3j.client.net.Whisper;
import eu.lixko.ts3j.client.recording.VoiceRecorder;
@@ -197,6 +199,16 @@ public final class ServerSession {
return conn.getModel();
}
public boolean showsQueryClients() {
return conn.getModel().showsQueryClients();
}
/** Lists the server's ServerQuery clients in the tree too, or hides them again. */
public void setShowQueryClients(boolean show) {
conn.getModel().setShowQueryClients(show);
listener.onModelChanged();
}
/** Our client id on the server, or -1 while not connected. */
public int selfClientId() {
return conn.getSelfClientId();
@@ -531,6 +543,11 @@ public final class ServerSession {
own.setMicrophoneActive(active);
}
/** Mixes these clips into what our microphone sends, while it captures. */
public void setClips(ClipMixer clips) {
own.setClips(clips);
}
public void setNickname(String nickname) {
if (conn.isConnected()) conn.setNickname(nickname);
}
@@ -584,6 +601,16 @@ public final class ServerSession {
return whisper != null && targets != null && targets.isEmpty();
}
/** Whether incoming whispers are blocked here, whatever the contacts allow. */
public boolean areWhispersBlocked() {
return conn.areWhispersBlocked();
}
public void setWhispersBlocked(boolean blocked) {
conn.setWhispersBlocked(blocked);
listener.onSessionChanged();
}
/** The client who whispered to us last, to whisper back to; null when none is on. */
public ClientEntry lastWhisperer() {
return conn.isConnected() ? conn.lastWhisperer() : null;
@@ -844,6 +871,10 @@ public final class ServerSession {
return conn.serverLog();
}
public ServerStatsAdmin serverStats() {
return conn.serverStats();
}
public OfflineMessageAdmin offlineMessages() {
return conn.offlineMessages();
}
@@ -1,5 +1,6 @@
package eu.lixko.ts3j.client.session;
import eu.lixko.ts3j.client.audio.ClipMixer;
import eu.lixko.ts3j.client.audio.VoiceInput;
import java.util.ArrayList;
@@ -20,6 +21,8 @@ public final class Sessions {
private final List<ServerSession> sessions = new ArrayList<>();
private final Runnable onMicrophoneMoved;
private ServerSession microphoneOwner;
/** The soundboard's sounds, sent by whichever server holds the microphone. */
private final ClipMixer clips = new ClipMixer();
/**
* Opens and closes capture lines, which can block briefly, off the frontend's thread. One
* thread, so quick successive hand-overs stop and start in the order they were made.
@@ -37,6 +40,7 @@ public final class Sessions {
public void add(ServerSession session) {
sessions.add(session);
session.setClips(clips);
}
/** Forgets a session that is being closed; the microphone moves on if it had it. */
@@ -64,6 +68,17 @@ public final class Sessions {
// ---- the microphone ----
/** Clips to send with the microphone; they follow it from server to server. */
public ClipMixer clips() {
return clips;
}
/** Whether some server is capturing with the microphone, so what it captures (and {@link #clips}) goes out. */
public boolean onAir() {
VoiceInput mic = microphoneOf(microphoneOwner);
return mic != null && mic.isCapturing();
}
/** The session capturing with the microphone, or null when none is. */
public ServerSession microphoneOwner() {
return microphoneOwner;
@@ -92,6 +107,7 @@ public final class Sessions {
return;
}
}
clips.stopAll();
if (previous != null) onMicrophoneMoved.run();
}
@@ -11,7 +11,6 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
@@ -44,7 +43,7 @@ public final class PackSoundPlayer implements SoundPlayer {
/** A decoded, 48 kHz mono sound being rendered. */
private static final class Voice {
final float[] pcm;
final double volume;
volatile double volume;
int position;
Voice(float[] pcm, double volume) {
@@ -89,11 +88,29 @@ public final class PackSoundPlayer implements SoundPlayer {
} catch (Exception e) {
return; // unplayable file: stay silent rather than break the action
}
if (pcm.length == 0) return;
play(pcm, volume);
}
@Override
public Playing play(float[] pcm, double volume) {
Voice voice = new Voice(pcm, Math.min(1.0, volume));
Playing handle = new Playing() {
@Override
public void stop() {
synchronized (lock) {
voices.remove(voice);
}
}
@Override
public void setVolume(double volume) {
voice.volume = Math.max(0.0, Math.min(1.0, volume));
}
};
if (pcm.length == 0) return handle;
synchronized (lock) {
if (!running) return;
voices.add(new Voice(pcm, Math.min(1.0, volume)));
if (!running) return handle;
voices.add(voice);
if (mixer == null) {
mixer = new Thread(this::mixLoop, "ts3j-sounds");
mixer.setDaemon(true);
@@ -101,6 +118,7 @@ public final class PackSoundPlayer implements SoundPlayer {
}
lock.notifyAll();
}
return handle;
}
@Override
@@ -212,18 +230,10 @@ public final class PackSoundPlayer implements SoundPlayer {
float[] cached = cache.get(key);
if (cached != null) return cached;
}
float[] pcm = readMono48k(file);
float[] pcm = SoundFiles.readMono48k(file, opus);
synchronized (cache) {
cache.put(key, pcm);
}
return pcm;
}
/** Decodes a sound file to mono 48 kHz float samples. */
private float[] readMono48k(File file) throws Exception {
if (file.getName().toLowerCase(Locale.ROOT).endsWith(OggOpusFile.EXTENSION)) {
return OggOpusFile.readMono(file, opus);
}
return WaveFile.readMono(file).at48k();
}
}
@@ -0,0 +1,36 @@
package eu.lixko.ts3j.client.sound;
import eu.lixko.ts3j.client.audio.opus.OpusCodec;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
/** Decodes the sound files the client plays: RIFF wave and Ogg Opus. */
public final class SoundFiles {
private SoundFiles() {
}
/** Whether the file's name marks a format {@link #readMono48k} reads. */
public static boolean isSupported(File file) {
String name = file.getName().toLowerCase(Locale.ROOT);
return name.endsWith(".wav") || name.endsWith(".wave") || name.endsWith(OggOpusFile.EXTENSION)
|| name.endsWith(".ogg");
}
/** Decodes a sound file to mono float samples in [-1, 1] at 48 kHz, telling the format by its content. */
public static float[] readMono48k(File file, OpusCodec opus) throws IOException {
byte[] magic = new byte[4];
try (InputStream in = new FileInputStream(file)) {
if (in.read(magic) < magic.length) throw new IOException("not a sound file");
}
if (new String(magic, StandardCharsets.US_ASCII).equals("OggS")) {
return OggOpusFile.readMono(file, opus);
}
return WaveFile.readMono(file).at48k();
}
}
@@ -16,6 +16,31 @@ public interface SoundPlayer {
*/
void play(File file, double volume);
/** A sound started with {@link #play(float[], double)}. */
interface Playing {
void stop();
/** @param volume linear gain, 0..1 */
void setVolume(double volume);
}
/**
* Plays decoded mono 48 kHz samples, mixing them with anything already playing.
*
* @param volume linear gain, 0..1
*/
default Playing play(float[] pcm, double volume) {
return new Playing() {
@Override
public void stop() {
}
@Override
public void setVolume(double volume) {
}
};
}
/** Speaks a sentence from a text-to-speech pack; a no-op where TTS is unavailable. */
default void say(String text, double volume) {
}
@@ -0,0 +1,190 @@
package eu.lixko.ts3j.client.soundboard;
import eu.lixko.ts3j.client.audio.ClipMixer;
import eu.lixko.ts3j.client.audio.opus.OpusCodec;
import eu.lixko.ts3j.client.config.Setting;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.sound.SoundFiles;
import eu.lixko.ts3j.client.sound.SoundPlayer;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.BooleanSupplier;
/**
* Plays {@link SoundboardSound}s onto the air, through the {@link ClipMixer} the microphone sends
* from, and to our own speakers at the soundboard's local volume.
*
* <p>Files are decoded off the caller's thread and kept, so a sound fired again starts at once.
*/
public final class Soundboard {
/** Decoded audio kept for replays: five minutes of 48 kHz samples, about 55 MB. */
private static final long MAX_CACHED_SAMPLES = 5L * 60 * 48_000;
private final ClipMixer mixer;
private final BooleanSupplier onAir;
private final OpusCodec opus;
private final SoundPlayer speakers;
private final Settings settings;
private final Map<String, float[]> cache = new LinkedHashMap<>(16, 0.75f, true);
private long cachedSamples;
/** Our local copies of the sounds on the air, stopped with them. */
private final Map<SoundboardSound, List<SoundPlayer.Playing>> local = new HashMap<>();
private final ExecutorService decoder = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r, "soundboard-decoder");
t.setDaemon(true);
return t;
});
/** @param onAir whether some server is capturing with the microphone, which is what sends the sounds */
public Soundboard(ClipMixer mixer, BooleanSupplier onAir, OpusCodec opus, SoundPlayer speakers, Settings settings) {
this.mixer = mixer;
this.onAir = onAir;
this.opus = opus;
this.speakers = speakers;
this.settings = settings;
mixer.addListener(this::forgetEnded);
Setting.onAnyChange(this::updateVolumes,
settings.soundboardVolume, settings.soundboardLocalVolume, settings.masterVolume);
}
/**
* Starts {@code sound} the way {@link Settings#soundboardMode} says: alone, from the top, or as
* another copy over the ones playing.
*
* @return completes once it plays; fails with a {@link SoundboardException} when no server
* takes our voice or the file cannot be read
*/
public CompletableFuture<Void> play(SoundboardSound sound) {
if (!onAir.getAsBoolean()) return CompletableFuture.failedFuture(new SoundboardException("Not connected", null));
File file = new File(sound.file);
return CompletableFuture.runAsync(() -> {
float[] pcm;
try {
pcm = decode(file);
} catch (IOException e) {
throw new SoundboardException(file.getName() + ": " + e.getMessage(), e);
}
switch (settings.soundboardMode.get()) {
case SINGLE -> {
stopAll();
mixer.play(sound, pcm, sent(sound));
}
case ONCE_EACH -> {
stopLocal(sound);
mixer.play(sound, pcm, sent(sound));
}
case MAYHEM -> mixer.add(sound, pcm, sent(sound));
}
// Played even when silent, so turning the local volume up mid-sound is heard.
synchronized (local) {
local.computeIfAbsent(sound, s -> new ArrayList<>()).add(speakers.play(pcm, heard(sound)));
}
}, decoder);
}
public void stop(SoundboardSound sound) {
mixer.stop(sound);
stopLocal(sound);
}
public void stopAll() {
mixer.stopAll();
synchronized (local) {
local.values().forEach(copies -> copies.forEach(SoundPlayer.Playing::stop));
local.clear();
}
}
/** Applies a changed {@link SoundboardSound#volume} to the sound if it is playing. */
public void updateVolume(SoundboardSound sound) {
mixer.setGain(sound, sent(sound));
synchronized (local) {
for (SoundPlayer.Playing copy : local.getOrDefault(sound, List.of())) copy.setVolume(heard(sound));
}
}
public boolean isPlaying(SoundboardSound sound) {
return mixer.isPlaying(sound);
}
public boolean isPlaying() {
return mixer.isPlaying();
}
/** Told whenever a sound starts or stops, on any thread. */
public void addListener(Runnable listener) {
mixer.addListener(listener);
}
public void removeListener(Runnable listener) {
mixer.removeListener(listener);
}
public void shutdown() {
stopAll();
decoder.shutdownNow();
}
private double sent(SoundboardSound sound) {
return sound.volume * settings.soundboardVolume.get();
}
private double heard(SoundboardSound sound) {
return Math.min(1.0, sound.volume * settings.soundboardLocalVolume.get() * settings.masterVolume.get());
}
/** The local copies stand for exactly the sounds playing, so they name every sound to update. */
private void updateVolumes() {
List<SoundboardSound> playing;
synchronized (local) {
playing = new ArrayList<>(local.keySet());
}
for (SoundboardSound sound : playing) updateVolume(sound);
}
private void stopLocal(SoundboardSound sound) {
synchronized (local) {
List<SoundPlayer.Playing> copies = local.remove(sound);
if (copies != null) copies.forEach(SoundPlayer.Playing::stop);
}
}
/** Drops the handles of sounds that went off the air: stopped, or played to the end. */
private void forgetEnded() {
synchronized (local) {
local.entrySet().removeIf(e -> {
if (mixer.isPlaying(e.getKey())) return false;
e.getValue().forEach(SoundPlayer.Playing::stop);
return true;
});
}
}
private float[] decode(File file) throws IOException {
String key = file.getAbsolutePath() + '@' + file.lastModified();
float[] pcm = cache.get(key);
if (pcm != null) return pcm;
if (!file.isFile()) throw new IOException("file not found");
pcm = SoundFiles.readMono48k(file, opus);
cache.put(key, pcm);
cachedSamples += pcm.length;
var eldest = cache.entrySet().iterator();
while (cachedSamples > MAX_CACHED_SAMPLES && cache.size() > 1) {
cachedSamples -= eldest.next().getValue().length;
eldest.remove();
}
return pcm;
}
}
@@ -0,0 +1,9 @@
package eu.lixko.ts3j.client.soundboard;
/** A soundboard sound could not be played; the message names the file and why. */
public final class SoundboardException extends RuntimeException {
public SoundboardException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,26 @@
package eu.lixko.ts3j.client.soundboard;
/** One sound on the soundboard: a wave or Ogg Opus file under a name. Equal by identity. */
public final class SoundboardSound {
public static final double MAX_VOLUME = 2.0;
public String name = "";
/** Absolute path of the sound file. */
public String file = "";
/** Linear gain, 0..{@link #MAX_VOLUME}. */
public double volume = 1.0;
public SoundboardSound() {
}
public SoundboardSound(String name, String file) {
this.name = name;
this.file = file;
}
@Override
public String toString() {
return name;
}
}
@@ -0,0 +1,81 @@
package eu.lixko.ts3j.client.soundboard;
import eu.lixko.ts3j.client.config.AppDirs;
import eu.lixko.ts3j.client.config.ProfileFiles;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/** The sounds on the soundboard, in the order they are shown; shared by every server. */
public final class SoundboardSounds {
private static File file() {
return AppDirs.file("soundboard.properties");
}
private final List<SoundboardSound> entries = new ArrayList<>();
public List<SoundboardSound> all() {
return entries;
}
/** The sound with this name, ignoring case, or null. */
public SoundboardSound find(String name) {
for (SoundboardSound s : entries) {
if (s.name.equalsIgnoreCase(name)) return s;
}
return null;
}
public static SoundboardSounds load() {
SoundboardSounds sounds = new SoundboardSounds();
if (!file().isFile()) return sounds;
Properties p = new Properties();
try (FileInputStream in = new FileInputStream(file())) {
p.load(in);
} catch (Exception e) {
return sounds;
}
int count = parseInt(p.getProperty("count"));
for (int i = 0; i < count; i++) {
String prefix = "sound." + i + ".";
SoundboardSound s = new SoundboardSound(p.getProperty(prefix + "name", ""), p.getProperty(prefix + "file", ""));
try {
s.volume = Math.max(0, Math.min(SoundboardSound.MAX_VOLUME,
Double.parseDouble(p.getProperty(prefix + "volume", "1"))));
} catch (NumberFormatException ignored) {
}
if (!s.file.isBlank()) sounds.entries.add(s);
}
return sounds;
}
public void save() {
Properties p = new Properties();
p.setProperty("count", Integer.toString(entries.size()));
for (int i = 0; i < entries.size(); i++) {
SoundboardSound s = entries.get(i);
String prefix = "sound." + i + ".";
p.setProperty(prefix + "name", s.name);
p.setProperty(prefix + "file", s.file);
p.setProperty(prefix + "volume", Double.toString(s.volume));
}
try {
AppDirs.createProfile();
ProfileFiles.write(file(), out -> p.store(out, "TeamSpook TS3j soundboard"));
} catch (Exception ignored) {
}
}
private static int parseInt(String v) {
if (v == null) return 0;
try {
return Integer.parseInt(v.trim());
} catch (NumberFormatException e) {
return 0;
}
}
}
@@ -1,4 +1,4 @@
package eu.lixko.ts3j.client.ui.options;
package eu.lixko.ts3j.client.audio;
import org.junit.jupiter.api.Test;
@@ -146,7 +146,7 @@ class VoiceStreamTest {
assertTrue(offer(2, 2), "the next packet starts a new burst");
assertEquals(0f, pull(), "the stop's slot is the talker's own silence, not a loss");
assertEquals(0.02f, pull());
assertEquals(0, opus.resets, "the decoder keeps its state between bursts");
assertEquals(1, opus.resets, "each burst starts a fresh decoder, as the talker's encoder does");
assertEquals(List.of(), opus.concealedFrameSizes);
}
@@ -160,6 +160,71 @@ class VoiceStreamTest {
assertEquals(List.of(true, false), talk);
}
/** The official client keeps numbering through silence, so its next burst starts far ahead. */
@Test
void skipsToABurstNumberedAheadOfWhereTheLastStopped() {
offer(0, 1);
offer(1, 2);
offerEnd(2);
pull();
pull();
assertEquals(VoiceStream.IDLE, stream.pull(out, 1, 1.0));
assertTrue(offer(60, 3));
List<Float> heard = new ArrayList<>();
for (int i = 0; i < 8; i++) {
offer(61 + i, 4 + i);
heard.add(pull());
}
assertEquals(0.03f, heard.stream().filter(v -> v != 0f).findFirst().orElseThrow(),
"the burst plays from its first packet: " + heard);
assertTrue(heard.indexOf(0.03f) <= 2, "after at most the delay held before: " + heard);
assertEquals(List.of(), opus.concealedFrameSizes);
assertEquals(1, opus.resets);
}
@Test
void aBurstThatArrivesBeforeTheLastDrainedStartsAFreshDecoder() {
offer(0, 1);
offer(1, 2);
offerEnd(2);
pull();
offer(60, 3);
offer(61, 4);
List<Float> heard = new ArrayList<>();
for (int i = 0; i < 6; i++) heard.add(pull());
assertTrue(heard.contains(0.02f) && heard.contains(0.03f), "both bursts play in full: " + heard);
assertEquals(1, opus.resets);
}
@Test
void theBurstAfterALostStopPlaysFromItsFirstPacket() {
offer(0, 1);
offer(1, 2);
while (stream.pull(out, 1, 1.0) != VoiceStream.IDLE) {
// plays both, then conceals until it gives up on the stop
}
assertTrue(offer(2, 3), "the talker resumes where the last burst's audio ended");
assertEquals(0.03f, pull());
}
@Test
void aLateCopyOfTheLastPacketDoesNotHoldTheBurstOpen() {
offer(0, 1);
offer(1, 2);
offerEnd(2);
pull();
pull();
offer(1, 2); // a copy, after playback passed it
assertEquals(VoiceStream.IDLE, stream.pull(out, 1, 1.0));
assertTrue(offer(3, 4));
float sample = pull();
// The late copy taught the buffer to wait a frame longer, so the start may be padded.
for (int i = 0; i < 3 && sample <= 0; i++) sample = pull();
assertEquals(0.04f, sample);
}
@Test
void playsALongPacketOneFrameAtATime() {
stream.offer(0, CodecType.OPUS_VOICE, new byte[]{40, 0});
@@ -0,0 +1,432 @@
package eu.lixko.ts3j.client.soundboard;
import eu.lixko.ts3j.client.audio.AudioCapture;
import eu.lixko.ts3j.client.audio.AudioDevice;
import eu.lixko.ts3j.client.audio.AudioIo;
import eu.lixko.ts3j.client.audio.AudioPlayback;
import eu.lixko.ts3j.client.audio.CaptureVoiceInput;
import eu.lixko.ts3j.client.audio.ClipMixer;
import eu.lixko.ts3j.client.audio.VoiceFormat;
import eu.lixko.ts3j.client.audio.desktop.NativeOpusCodec;
import eu.lixko.ts3j.client.audio.opus.OpusDecoder;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.sound.SoundPackConverter;
import eu.lixko.ts3j.client.sound.SoundPlayer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Soundboard sounds, wave and Ogg Opus, go out through the real capture pipeline while the
* microphone itself is silent below the gate, and decode back to the sound that was played.
*/
class SoundboardTest {
private static final int RATE = 44_100;
@TempDir
File dir;
private final NativeOpusCodec opus = new NativeOpusCodec();
private final Settings settings = new Settings();
private final SilentMic mic = new SilentMic();
private final ClipMixer mixer = new ClipMixer();
private final Speakers speakers = new Speakers();
private CaptureVoiceInput input;
private Soundboard soundboard;
private float[] tone;
@BeforeEach
void setUp() throws Exception {
settings.inputMode.set(Settings.InputMode.VOICE_ACTIVATION);
settings.vadMode.set(Settings.VadMode.VOLUME_GATE);
settings.soundboardLocalVolume.set(0.0);
input = new CaptureVoiceInput(settings, mic, opus);
input.setClips(mixer);
input.start();
mic.awaitFrameDone();
soundboard = new Soundboard(mixer, () -> true, opus, speakers, settings);
tone = new float[RATE * 3 / 4 + 77];
for (int i = 0; i < tone.length; i++) {
tone[i] = (float) (0.4 * Math.sin(2 * Math.PI * 440 * i / RATE) * Math.min(1, i / 2000.0));
}
}
@AfterEach
void tearDown() {
input.stop();
soundboard.shutdown();
}
@Test
void waveSoundIsSentWhileTheGateIsShut() throws Exception {
File wave = writeWave(new File(dir, "tone.wav"), tone);
assertSent(new SoundboardSound("tone", wave.getAbsolutePath()));
}
@Test
void opusSoundIsSent() throws Exception {
File source = new File(dir, "wav/pack");
Files.createDirectories(source.toPath());
Files.writeString(new File(source, "settings.ini").toPath(), "[info]\nname = Test\n");
writeWave(new File(source, "tone.wav"), tone);
SoundPackConverter.convert(new File(dir, "wav"), new File(dir, "opus"), opus);
File opusFile = new File(dir, "opus/pack/tone.opus");
assertTrue(opusFile.isFile());
assertSent(new SoundboardSound("tone", opusFile.getAbsolutePath()));
}
@Test
void mutedMicrophoneSendsNothingButTheSoundRunsOut() throws Exception {
input.setMuted(true);
SoundboardSound sound = new SoundboardSound("tone", writeWave(new File(dir, "tone.wav"), tone).getAbsolutePath());
soundboard.play(sound).get(5, TimeUnit.SECONDS);
List<byte[]> packets = run(sound);
assertTrue(packets.isEmpty(), packets.size() + " packets sent while muted");
assertFalse(soundboard.isPlaying(sound));
}
@Test
void unreadableFileFailsThePlay() throws Exception {
File junk = new File(dir, "junk.wav");
Files.writeString(junk.toPath(), "not a sound");
ExecutionException e = assertThrows(ExecutionException.class,
() -> soundboard.play(new SoundboardSound("junk", junk.getAbsolutePath())).get(5, TimeUnit.SECONDS));
assertInstanceOf(SoundboardException.class, e.getCause());
assertFalse(soundboard.isPlaying());
}
@Test
void volumeChangesReachAPlayingSound() throws Exception {
SoundboardSound sound = new SoundboardSound("tone", writeWave(new File(dir, "tone.wav"), tone).getAbsolutePath());
soundboard.play(sound).get(5, TimeUnit.SECONDS);
List<byte[]> packets = new ArrayList<>();
step(packets, 15);
settings.soundboardVolume.set(0.25);
step(packets, 15);
float[] decoded = decode(packets);
double ratio = rms(decoded, 20, 30) / rms(decoded, 5, 15);
assertEquals(0.25, ratio, 0.03, "sent level after turning the soundboard down");
assertEquals(0.0, speakers.volume, 1e-9);
settings.soundboardLocalVolume.set(0.5);
assertEquals(0.5 * settings.masterVolume.get(), speakers.volume, 1e-9);
sound.volume = 0.5;
soundboard.updateVolume(sound);
assertEquals(0.25 * settings.masterVolume.get(), speakers.volume, 1e-9);
settings.soundboardVolume.set(0.0);
step(packets, 5);
assertTrue(soundboard.isPlaying(sound), "still playing, silently");
assertFalse(input.isReady(), "a silenced sound does not key the microphone");
}
@Test
void voiceGoesOutWithTheSoundOnlyWhenMixedIn() throws Exception {
// A transmitting microphone hearing a steady 1 kHz voice.
input.stop();
mic.voice = true;
settings.inputMode.set(Settings.InputMode.CONTINUOUS);
settings.denoise.set(false);
input = new CaptureVoiceInput(settings, mic, opus);
input.setClips(mixer);
input.start();
mic.awaitFrameDone();
SoundboardSound sound = new SoundboardSound("tone", writeWave(new File(dir, "tone.wav"), tone).getAbsolutePath());
double with = sentCorrelation(sound);
settings.soundboardWithMic.set(false);
double without = sentCorrelation(sound);
assertTrue(with < 0.85, "the voice is mixed in, correlation " + with);
assertTrue(without > 0.95, "the sound alone, correlation " + without);
}
@Test
void oneAtATimeStopsTheOthers() throws Exception {
settings.soundboardMode.set(Settings.SoundboardMode.SINGLE);
SoundboardSound a = sound("a"), b = sound("b");
soundboard.play(a).get(5, TimeUnit.SECONDS);
soundboard.play(b).get(5, TimeUnit.SECONDS);
assertFalse(soundboard.isPlaying(a));
assertTrue(soundboard.isPlaying(b));
}
@Test
void eachOnceRestartsTheSoundAndLeavesTheOthers() throws Exception {
settings.soundboardMode.set(Settings.SoundboardMode.ONCE_EACH);
SoundboardSound a = sound("a"), b = sound("b");
soundboard.play(a).get(5, TimeUnit.SECONDS);
soundboard.play(b).get(5, TimeUnit.SECONDS);
soundboard.play(a).get(5, TimeUnit.SECONDS);
assertTrue(soundboard.isPlaying(a));
assertTrue(soundboard.isPlaying(b));
assertEquals(2, levelOfOneFrame() / singleLevel(), 0.01, "a and b, a only once");
}
@Test
void mayhemLayersTheSameSound() throws Exception {
settings.soundboardMode.set(Settings.SoundboardMode.MAYHEM);
SoundboardSound a = sound("a");
for (int i = 0; i < 2; i++) soundboard.play(a).get(5, TimeUnit.SECONDS);
assertEquals(2, levelOfOneFrame() / singleLevel(), 0.01, "two copies of a");
soundboard.stop(a);
assertFalse(soundboard.isPlaying());
}
private SoundboardSound sound(String name) throws IOException {
return new SoundboardSound(name, writeWave(new File(dir, name + ".wav"), tone).getAbsolutePath());
}
/** The peak of the tone's frame at 100 ms, alone. */
private double singleLevel() {
float[] pcm = resample(tone);
double peak = 0;
for (int i = 0; i < VoiceFormat.FRAME_SIZE; i++) peak = Math.max(peak, Math.abs(pcm[i + 4800]));
return peak;
}
/** Skips the mixer to 100 ms in and measures the peak of the frame there. */
private double levelOfOneFrame() {
float[] frame = new float[VoiceFormat.FRAME_SIZE];
for (int i = 0; i < 5; i++) mixer.mixInto(new float[VoiceFormat.FRAME_SIZE], VoiceFormat.FRAME_SIZE, 1);
mixer.mixInto(frame, VoiceFormat.FRAME_SIZE, 1);
double peak = 0;
for (float f : frame) peak = Math.max(peak, Math.abs(f));
return peak;
}
/** Plays the sound to its end and correlates what was sent with it. */
private double sentCorrelation(SoundboardSound sound) throws Exception {
soundboard.play(sound).get(5, TimeUnit.SECONDS);
List<byte[]> packets = new ArrayList<>();
while (soundboard.isPlaying(sound)) step(packets, 1);
float[] expected = resample(tone);
float[] decoded = decode(packets);
double best = 0;
for (int lag = 0; lag < 2400; lag++) best = Math.max(best, correlation(expected, decoded, lag));
return best;
}
private void step(List<byte[]> packets, int frames) throws InterruptedException {
for (int i = 0; i < frames; i++) {
mic.captureFrame();
byte[] p;
while ((p = input.provide()).length > 0) packets.add(p);
}
}
private static double rms(float[] pcm, int fromFrame, int toFrame) {
double sum = 0;
int from = fromFrame * VoiceFormat.FRAME_SIZE, to = toFrame * VoiceFormat.FRAME_SIZE;
for (int i = from; i < to; i++) sum += pcm[i] * pcm[i];
return Math.sqrt(sum / (to - from));
}
private void assertSent(SoundboardSound sound) throws Exception {
List<Boolean> talk = new ArrayList<>();
input.setTalkListener(talk::add);
soundboard.play(sound).get(5, TimeUnit.SECONDS);
assertTrue(soundboard.isPlaying(sound));
List<byte[]> packets = run(sound);
float[] expected = resample(tone);
int frames = (expected.length + VoiceFormat.FRAME_SIZE - 1) / VoiceFormat.FRAME_SIZE;
assertEquals(frames, packets.size(), "one packet per 20 ms of sound");
assertEquals(List.of(true, false), talk, "talking for the sound's length only");
float[] decoded = decode(packets);
double best = 0;
for (int lag = 0; lag < 1200; lag++) best = Math.max(best, correlation(expected, decoded, lag));
assertTrue(best > 0.95, "correlation " + best);
}
/** Steps the microphone frame by frame until the sound is over, collecting what would be sent. */
private List<byte[]> run(SoundboardSound sound) throws InterruptedException {
List<byte[]> packets = new ArrayList<>();
for (int i = 0; i < 500 && (soundboard.isPlaying(sound) || input.isReady()); i++) {
mic.captureFrame();
byte[] p;
while ((p = input.provide()).length > 0) packets.add(p);
}
// Once the sound is over the gate is shut again, so ts3j ends the stream.
mic.captureFrame();
assertFalse(input.isReady());
return packets;
}
private float[] decode(List<byte[]> packets) {
float[] out = new float[packets.size() * VoiceFormat.FRAME_SIZE];
float[] frame = new float[VoiceFormat.FRAME_SIZE];
try (OpusDecoder decoder = opus.createDecoder(VoiceFormat.SAMPLE_RATE, VoiceFormat.FRAME_SIZE, 1)) {
int at = 0;
for (byte[] p : packets) {
int n = decoder.decode(p, frame);
System.arraycopy(frame, 0, out, at, n);
at += n;
}
}
return out;
}
private static double correlation(float[] a, float[] b, int lag) {
double dot = 0, ea = 0, eb = 0;
for (int i = 0; i < a.length && i + lag < b.length; i++) {
dot += a[i] * b[i + lag];
ea += a[i] * a[i];
eb += b[i + lag] * b[i + lag];
}
return ea == 0 || eb == 0 ? 0 : dot / Math.sqrt(ea * eb);
}
private static float[] resample(float[] in) {
double ratio = VoiceFormat.SAMPLE_RATE / (double) RATE;
float[] out = new float[(int) (in.length * ratio)];
for (int i = 0; i < out.length; i++) {
double position = i / ratio;
int index = (int) position;
float a = in[Math.min(index, in.length - 1)];
float b = in[Math.min(index + 1, in.length - 1)];
out[i] = (float) (a + (b - a) * (position - index));
}
return out;
}
private static File writeWave(File file, float[] samples) throws IOException {
ByteBuffer b = ByteBuffer.allocate(44 + samples.length * 2).order(ByteOrder.LITTLE_ENDIAN);
b.put("RIFF".getBytes()).putInt(36 + samples.length * 2).put("WAVE".getBytes());
b.put("fmt ".getBytes()).putInt(16).putShort((short) 1).putShort((short) 1)
.putInt(RATE).putInt(RATE * 2).putShort((short) 2).putShort((short) 16);
b.put("data".getBytes()).putInt(samples.length * 2);
for (float s : samples) b.putShort((short) Math.round(s * 32767));
Files.write(file.toPath(), b.array());
return file;
}
/** Our own speakers, remembering the volume of the last sound played on them. */
private static final class Speakers implements SoundPlayer {
volatile double volume = -1;
@Override
public void play(File file, double volume) {
}
@Override
public Playing play(float[] pcm, double volume) {
this.volume = volume;
return new Playing() {
@Override
public void stop() {
}
@Override
public void setVolume(double v) {
Speakers.this.volume = v;
}
};
}
@Override
public void shutdown() {
}
}
/** A mono microphone that captures silence one frame at a time, when the test says so. */
private static final class SilentMic implements AudioIo {
private final LinkedBlockingQueue<Boolean> frames = new LinkedBlockingQueue<>();
private final Semaphore done = new Semaphore(0);
/** Hears a 1 kHz voice rather than silence. */
volatile boolean voice;
private long sample;
/** Lets one frame through and waits until the capture loop has dealt with it. */
void captureFrame() throws InterruptedException {
frames.put(true);
awaitFrameDone();
}
void awaitFrameDone() throws InterruptedException {
assertTrue(done.tryAcquire(5, TimeUnit.SECONDS), "capture loop stalled");
}
@Override
public List<AudioDevice> inputDevices() {
return List.of();
}
@Override
public List<AudioDevice> outputDevices() {
return List.of();
}
@Override
public AudioCapture openCapture(String deviceId, int preferredChannels) {
return new AudioCapture() {
private volatile boolean open = true;
@Override
public int channels() {
return 1;
}
@Override
public void start() {
}
@Override
public int read(byte[] buffer, int offset, int length) {
// Asking for the next frame means the last one is through the pipeline.
done.release();
try {
while (open) {
if (frames.poll(50, TimeUnit.MILLISECONDS) != null) {
Arrays.fill(buffer, offset, offset + length, (byte) 0);
for (int i = offset; voice && i + 1 < offset + length; i += 2, sample++) {
short s = (short) (0.3 * 32767 * Math.sin(2 * Math.PI * 1000 * sample / 48_000));
buffer[i] = (byte) s;
buffer[i + 1] = (byte) (s >> 8);
}
return length;
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return 0;
}
@Override
public void close() {
open = false;
}
};
}
@Override
public AudioPlayback openPlayback(String deviceId, int preferredChannels) {
throw new UnsupportedOperationException();
}
}
}
@@ -271,6 +271,14 @@ public final class MainCommands {
forEach(targets, s -> s.setCommander(commander));
}
void toggleWhispersBlocked() {
setWhispersBlocked(currentConnected(), !connectedTab().map(t -> t.session().areWhispersBlocked()).orElse(false));
}
public void setWhispersBlocked(List<ServerSession> targets, boolean blocked) {
forEach(targets, s -> s.setWhispersBlocked(blocked));
}
/** Asks for a new nickname, used on every server. */
public void changeNickname() {
String nickname = JOptionPane.showInputDialog(frame, "New nickname:", settings.nickname.get());
@@ -374,6 +382,14 @@ public final class MainCommands {
onTab(ServerTab::showServerLog);
}
void showServerConnectionInfo() {
onTab(ServerTab::showServerConnectionInfo);
}
void toggleQueryClients() {
connectedTab().ifPresent(tab -> frame.setShowQueryClients(tab, !tab.session().showsQueryClients()));
}
void showPrivilegeKeys() {
onTab(ServerTab::showPrivilegeKeys);
}
@@ -397,6 +413,22 @@ public final class MainCommands {
frame.showGiantPtt();
}
void showSoundboard() {
frame.soundboard().show();
}
public void playSound(String name) {
frame.soundboard().play(name);
}
public void toggleSound(String name) {
frame.soundboard().toggle(name);
}
public void stopSounds() {
frame.soundboard().stopAll();
}
void showIdentities() {
new IdentitiesDialog(frame, identities, settings, bookmarks, null).setVisible(true);
}
@@ -21,6 +21,7 @@ import eu.lixko.ts3j.client.session.SelfState;
import eu.lixko.ts3j.client.session.ServerLink;
import eu.lixko.ts3j.client.session.ServerSession;
import eu.lixko.ts3j.client.session.Sessions;
import eu.lixko.ts3j.client.soundboard.Soundboard;
import eu.lixko.ts3j.client.sound.SoundNotifier;
import eu.lixko.ts3j.client.sound.SoundPlayer;
import eu.lixko.ts3j.client.teamspeak.TeamSpeakImporter;
@@ -35,6 +36,7 @@ import eu.lixko.ts3j.client.ui.manage.ContactsDialog;
import eu.lixko.ts3j.client.ui.options.MyTeamSpeakPanel;
import eu.lixko.ts3j.client.ui.options.SettingsDialog;
import eu.lixko.ts3j.client.ui.server.ServerTab;
import eu.lixko.ts3j.client.ui.soundboard.SoundboardActions;
import eu.lixko.ts3j.client.ui.tray.TrayBackend;
import javax.swing.AbstractAction;
@@ -82,6 +84,7 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
/** Sound pack playback, shared by every connection. */
private final SoundNotifier sounds;
private final SoundPlayer soundPlayer;
private final SoundboardActions soundboard;
private final List<ServerTab> tabs = new ArrayList<>();
/** The tabs' sessions: who holds the microphone, and the state shared by every server. */
@@ -129,6 +132,8 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
this.sounds = new SoundNotifier(settings);
this.soundPlayer = audio.createSoundPlayer(settings);
this.sounds.setPlayer(soundPlayer);
this.soundboard = new SoundboardActions(this, settings,
new Soundboard(sessions.clips(), sessions::onAir, audio.opus(), soundPlayer, settings));
this.notifier = new DesktopNotifier(this, settings);
Setting.onAnyChange(this::applyOutputSettingsToAllTabs,
settings.outputDevice, settings.outputVolume, settings.masterVolume, settings.receiveBuffer);
@@ -172,6 +177,8 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
// Switching the icon pack in the options dialog re-decorates the whole window.
IconTheme.get().addListener(() -> SwingUtilities.invokeLater(this::rebuildIcons));
add(tabPane, BorderLayout.CENTER);
tabPane.setStripShown(settings.showServerTabs.get());
settings.showServerTabs.addListener(tabPane::setStripShown);
// The tree and the chat each claim Ctrl+F for themselves while they have the focus;
// from anywhere else it searches the chat.
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
@@ -352,9 +359,16 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
public void tabConnected(ServerTab tab) {
if (!tabs.contains(tab)) return;
sessions.connected(tab.session());
tab.session().setShowQueryClients(bookmarks.showsQueryClients(tab.session().address(), tab.session().port()));
tabUpdated(tab);
}
/** Lists the ServerQuery clients on {@code tab}, or hides them, from now on whenever it is bookmarked. */
void setShowQueryClients(ServerTab tab, boolean show) {
tab.session().setShowQueryClients(show);
if (bookmarks.rememberQueryClients(tab.session().address(), tab.session().port(), show)) bookmarks.save();
}
public void tabDisconnected(ServerTab tab) {
if (!tabs.contains(tab)) return;
sessions.disconnected(tab.session());
@@ -396,6 +410,10 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
sessions.setPushToTalk(talking);
}
SoundboardActions soundboard() {
return soundboard;
}
void showGiantPtt() {
if (giantPtt == null || !giantPtt.isDisplayable()) {
giantPtt = new GiantPttWindow(this, sessions::setForceTransmit, () -> {
@@ -479,6 +497,7 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
for (eu.lixko.ts3j.client.sound.SoundPack pack : sounds.availablePacks()) out.add(pack.name());
}
}
case SOUND -> out.addAll(soundboard.names());
case CHANNEL -> {
if (selected != null && selected.session().isConnected()) {
out.addAll(selected.session().channelPaths());
@@ -593,6 +612,7 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
}
sessions.shutdown();
contacts.flush();
soundboard.shutdown();
soundPlayer.shutdown();
hotkeys.dispose();
if (tray != null) tray.dispose();
@@ -139,11 +139,14 @@ final class MainMenuBar extends JMenuBar {
deleteAvatarItem.addActionListener(e -> commands.deleteAvatar());
JMenuItem giantPtt = new JMenuItem("Giant PTT", Icons.of("CAPTURE"));
giantPtt.addActionListener(e -> commands.showGiantPtt());
JMenuItem soundboard = new JMenuItem("Soundboard", Icons.ofAny("SOUNDPACK", "MUSIC"));
soundboard.addActionListener(e -> commands.showSoundboard());
self.add(awayMenu);
self.add(activateItem);
self.add(muteItem);
self.add(deafItem);
self.add(giantPtt);
self.add(soundboard);
self.addSeparator();
self.add(commanderItem);
self.addSeparator();
@@ -122,6 +122,13 @@ final class MainToolbar extends JToolBar {
private JPopupMenu buildContextMenu() {
JPopupMenu menu = new JPopupMenu();
JCheckBoxMenuItem tabsItem = new JCheckBoxMenuItem("Show Server Tabs", settings.showServerTabs.get());
tabsItem.addActionListener(e -> {
settings.showServerTabs.set(tabsItem.isSelected());
settings.save();
});
settings.showServerTabs.addListener(tabsItem::setSelected);
JCheckBoxMenuItem statusBarItem = new JCheckBoxMenuItem("Show Status Bar", settings.showStatusBar.get());
statusBarItem.addActionListener(e -> {
settings.showStatusBar.set(statusBarItem.isSelected());
@@ -144,6 +151,7 @@ final class MainToolbar extends JToolBar {
JMenuItem customize = new JMenuItem("Customize Toolbar…");
customize.addActionListener(e -> CustomizeToolbarDialog.show(this, settings, actions));
menu.add(tabsItem);
menu.add(statusBarItem);
menu.add(volumeItem);
menu.addSeparator();
@@ -27,7 +27,7 @@ import java.util.List;
* Container for the open server connections. With a single connection the
* server view fills the window as it always did; from the second connection on
* the views move into a {@link JTabbedPane} whose tab strip sits directly under
* the toolbar.
* the toolbar. With the strip turned off, the selected view fills the window alone.
*/
final class ServerTabPane extends JPanel {
@@ -42,8 +42,11 @@ final class ServerTabPane extends JPanel {
private final List<ServerTab> tabs = new ArrayList<>();
private final TabDragReorder dragReorder = new TabDragReorder(tabbed, this::moveTab);
/** True while the tab strip is in use, i.e. more than one connection is open. */
/** True while the tab strip is in use, i.e. more than one connection is open and it is wanted. */
private boolean tabbedMode;
private boolean stripShown = true;
/** The tab on screen; the one shown alone while there is no strip. */
private ServerTab selected;
/** Suppresses selection callbacks while we rearrange the pane ourselves. */
private boolean updating;
/** Remembered so a drag-reorder can rebuild the tab labels without the caller's help. */
@@ -114,12 +117,29 @@ final class ServerTabPane extends JPanel {
void removeTab(ServerTab tab) {
tabs.remove(tab);
if (selected == tab) selected = null;
relayout();
}
/** Shows or hides the tab strip; without it, tabs are switched by hotkeys or the menus. */
void setStripShown(boolean shown) {
if (stripShown == shown) return;
stripShown = shown;
relayout();
if (tabbedMode && selected != null) setSelected(selected);
refresh(lastMicTab);
}
void setSelected(ServerTab tab) {
int i = tabs.indexOf(tab);
if (!tabbedMode || i < 0 || tabbed.getSelectedIndex() == i) return;
if (i < 0) return;
ServerTab previous = selected;
selected = tab;
if (!tabbedMode) {
if (previous != tab) relayout();
return;
}
if (tabbed.getSelectedIndex() == i) return;
updating = true;
try {
tabbed.setSelectedIndex(i);
@@ -145,7 +165,7 @@ final class ServerTabPane extends JPanel {
* keeping whatever is currently selected on screen.
*/
private void relayout() {
boolean wantTabs = tabs.size() > 1;
boolean wantTabs = stripShown && tabs.size() > 1;
updating = true;
try {
removeAll();
@@ -154,7 +174,7 @@ final class ServerTabPane extends JPanel {
for (ServerTab tab : tabs) tabbed.addTab(tab.session().title(), tab.component());
add(tabbed, BorderLayout.CENTER);
} else if (!tabs.isEmpty()) {
add(tabs.get(0).component(), BorderLayout.CENTER);
add((selected != null ? selected : tabs.get(0)).component(), BorderLayout.CENTER);
}
tabbedMode = wantTabs;
} finally {
@@ -30,6 +30,7 @@ final class ToolbarAction {
RECORDING("Recording"),
CHANNEL("Channel"),
CLIENT("Client"),
SERVER("Server"),
MISC("Misc");
final String label;
@@ -44,6 +44,7 @@ final class ToolbarActions {
addRecording();
addChannel();
addClient();
addServer();
addMisc();
}
@@ -111,6 +112,11 @@ final class ToolbarActions {
add("deleteAvatar", Category.SELF, "Delete Avatar", commands::deleteAvatar, "DELETE_AVATAR")
.enabledWhen(() -> is(ServerSession::hasAvatar));
add("giantPtt", Category.SELF, "Giant PTT", commands::showGiantPtt, "CAPTURE");
add("soundboard", Category.SELF, "Soundboard", commands::showSoundboard, "SOUNDPACK", "MUSIC");
add("blockWhispers", Category.SELF, "Toggle Block Incoming Whispers", commands::toggleWhispersBlocked,
"TOGGLE_BLOCK_INCOMING_WHISPERS")
.toggle(() -> is(ServerSession::areWhispersBlocked))
.enabledWhen(this::connected);
}
private void addDialogs() {
@@ -256,6 +262,16 @@ final class ToolbarActions {
.enabledWhen(() -> anyClient().isPresent());
}
private void addServer() {
add("serverConnectionInfo", Category.SERVER, "Server Connection Info", commands::showServerConnectionInfo,
"SERVER_GREEN")
.enabledWhen(() -> permitted(Permissions::canViewServerConnectionInfo));
add("toggleQueryClients", Category.SERVER, "Toggle ServerQuery Clients", commands::toggleQueryClients,
"TOGGLE_SERVER_QUERY_CLIENTS", "SERVER_QUERY")
.toggle(() -> is(ServerSession::showsQueryClients))
.enabledWhen(this::connected);
}
private void addMisc() {
add("collapseAll", Category.MISC, "Collapse All", () -> collapseAll(true), "CHANNEL_COLLAPSE_ALL")
.enabledWhen(this::connected);
@@ -16,6 +16,7 @@ import javax.swing.text.Element;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
import javax.swing.text.html.ImageView;
import javax.swing.text.html.InlineView;
import javax.swing.text.html.StyleSheet;
import java.awt.Color;
import java.awt.Font;
@@ -152,7 +153,9 @@ public final class HtmlStyles {
@Override
public View create(Element elem) {
View view = super.create(elem);
return view instanceof ImageView ? new ZoomedImageView(elem) : view;
if (view instanceof ImageView) return new ZoomedImageView(elem);
if (view instanceof InlineView) return new WrappingInlineView(elem);
return view;
}
};
@@ -178,6 +181,22 @@ public final class HtmlStyles {
}
}
/**
* Text that may break anywhere once it has no space left to break at: by default the
* longest word (typically a link) is the pane's minimum width, and a scroll pane then
* scrolls sideways instead of wrapping it.
*/
private static final class WrappingInlineView extends InlineView {
WrappingInlineView(Element elem) {
super(elem);
}
@Override
public float getMinimumSpan(int axis) {
return axis == X_AXIS ? 0 : super.getMinimumSpan(axis);
}
}
private static final class ZoomedImageView extends ImageView {
ZoomedImageView(Element elem) {
super(elem);
@@ -87,6 +87,10 @@ public final class HotkeyActions implements HotkeyEngine.Handler {
case SOUND_UNMUTE -> frame.setSoundsMuted(false);
case SOUND_TOGGLE -> frame.setSoundsMuted(!frame.areSoundsMuted());
case SOUNDBOARD_PLAY -> commands.playSound(hotkey.argument);
case SOUNDBOARD_TOGGLE -> commands.toggleSound(hotkey.argument);
case SOUNDBOARD_STOP -> commands.stopSounds();
case VOLUME_INCREASE -> frame.adjustMasterVolume(VOLUME_STEP);
case VOLUME_DECREASE -> frame.adjustMasterVolume(-VOLUME_STEP);
@@ -96,6 +100,11 @@ public final class HotkeyActions implements HotkeyEngine.Handler {
else commands.changeNickname(hotkey.argument);
}
case WHISPER_BLOCK_ACTIVATE -> commands.setWhispersBlocked(targets, true);
case WHISPER_BLOCK_DEACTIVATE -> commands.setWhispersBlocked(targets, false);
case WHISPER_BLOCK_TOGGLE ->
commands.setWhispersBlocked(targets, !any(targets, ServerSession::areWhispersBlocked));
case RECORDING_START -> commands.startRecording(targets, false);
case RECORDING_START_MULTITRACK -> commands.startRecording(targets, true);
case RECORDING_STOP -> commands.stopRecording(targets);
@@ -291,6 +291,7 @@ public final class HotkeyDialog extends JDialog {
case BOOKMARK -> "Bookmark:";
case CHANNEL -> "Channel path:";
case PROFILE -> "Profile:";
case SOUND -> "Sound:";
default -> "Parameter:";
};
}
@@ -69,6 +69,7 @@ public final class BookmarksDialog extends JDialog {
private final JPasswordField channelPassword = new JPasswordField();
private final JCheckBox connectOnStartup = new JCheckBox("Connect on startup");
private final JCheckBox myTeamSpeakFeatures = new JCheckBox("Enable myTeamSpeak features");
private final JCheckBox showQueryClients = new JCheckBox("Show ServerQuery clients");
private final IdentityChooser identity;
private final JPanel details;
@@ -145,6 +146,12 @@ public final class BookmarksDialog extends JDialog {
connectOnStartup.setToolTipText("Connect to this server automatically when the client starts. "
+ "Several bookmarks may be flagged; each opens its own tab.");
// Takes effect at once on the server, when we are on it, as the toolbar's toggle would.
showQueryClients.addActionListener(e -> {
ServerSession on = sessionHere();
if (on != null) on.setShowQueryClients(showQueryClients.isSelected());
});
thisChannel.setToolTipText("This channel");
if (thisChannel.getIcon() == null) thisChannel.setText("This channel");
thisChannel.addActionListener(e -> {
@@ -168,6 +175,7 @@ public final class BookmarksDialog extends JDialog {
addRow(panel, row++, "Identity:", identity);
addRow(panel, row++, "", connectOnStartup);
addRow(panel, row++, "", myTeamSpeakFeatures);
addRow(panel, row++, "", showQueryClients);
GridBagConstraints filler = new GridBagConstraints();
filler.gridx = 0;
@@ -235,6 +243,7 @@ public final class BookmarksDialog extends JDialog {
channelPassword.setText(b == null ? "" : nullToEmpty(b.channelPassword));
connectOnStartup.setSelected(b != null && b.connectOnStartup);
myTeamSpeakFeatures.setSelected(b == null || b.myTeamSpeakFeatures);
showQueryClients.setSelected(b != null && b.showQueryClients);
identity.setSelectedIdentityId(b == null ? "" : nullToEmpty(b.identityId));
updateThisChannel();
}
@@ -288,6 +297,7 @@ public final class BookmarksDialog extends JDialog {
editing.identityId = identity.getSelectedIdentityId();
editing.connectOnStartup = connectOnStartup.isSelected();
editing.myTeamSpeakFeatures = myTeamSpeakFeatures.isSelected();
editing.showQueryClients = showQueryClients.isSelected();
list.repaint();
}
@@ -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;
@@ -4,7 +4,6 @@ import com.formdev.flatlaf.util.ScaledEmptyBorder;
import com.formdev.flatlaf.util.UIScale;
import eu.lixko.ts3j.client.net.ClientEntry;
import eu.lixko.ts3j.client.net.ConnectionStats;
import eu.lixko.ts3j.client.net.ConnectionStatsFormat.Row;
import eu.lixko.ts3j.client.net.ConnectionStatsFormat.TrafficRow;
import eu.lixko.ts3j.client.net.ConnectionStatsFormat;
import eu.lixko.ts3j.client.net.avatar.Avatar;
@@ -22,7 +21,6 @@ import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
@@ -55,7 +53,7 @@ public final class ConnectionInfoDialog extends JDialog {
private static final int AVATAR_SIZE = 80;
/** Sample texts sizing the columns, as the official window does, so they don't jitter. */
private static final String TOTAL_SAMPLE = "100.00 %";
private static final String VALUE_SAMPLE = "1023.00 KiB/s";
private static final String VALUE_SAMPLE = Figures.VALUE_SAMPLE;
private final ServerSession session;
private final int clientId;
@@ -212,85 +210,6 @@ public final class ConnectionInfoDialog extends JDialog {
return new ImageIcon(icon.getImage().getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH));
}
// ---- figures ----
private static JLabel label(String text) {
JLabel l = new JLabel(text);
l.setFont(Theme.uiFont());
return l;
}
/** Pins a label's width to that of {@code sample}, so a growing figure doesn't resize the window. */
private static JLabel sized(JLabel l, String sample) {
Dimension pref = l.getPreferredSize();
int width = l.getFontMetrics(l.getFont()).stringWidth(sample);
l.setPreferredSize(new Dimension(Math.max(pref.width, width), pref.height));
return l;
}
private static GridBagConstraints cell(int x, int y, int anchor, Insets insets) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = x;
c.gridy = y;
c.anchor = anchor;
c.insets = UIScale.scale(insets);
return c;
}
/**
* Labelled figures, one per row, in groups set apart by a rule; refreshed in place, and
* rows an update leaves out hide.
*/
private static final class Figures extends JPanel {
private final List<List<JLabel[]>> groups = new ArrayList<>();
private int lastRow;
Figures(List<List<Row>> rows, boolean valuesRight) {
super(new GridBagLayout());
setOpaque(false);
int y = 0;
for (List<Row> group : rows) {
if (y > 0) {
GridBagConstraints rule = cell(0, y++, GridBagConstraints.WEST, new Insets(6, 0, 6, 0));
rule.gridwidth = 2;
rule.fill = GridBagConstraints.HORIZONTAL;
add(new JSeparator(), rule);
}
List<JLabel[]> labels = new ArrayList<>();
for (Row row : group) {
JLabel key = label(row.label());
JLabel value = label(row.value());
labels.add(new JLabel[]{key, value});
add(key, cell(0, y, GridBagConstraints.WEST, new Insets(3, 0, 3, 16)));
GridBagConstraints vc = cell(1, y, valuesRight ? GridBagConstraints.EAST : GridBagConstraints.WEST,
new Insets(3, 0, 3, 0));
vc.weightx = 1;
add(value, vc);
lastRow = y++;
}
groups.add(labels);
}
}
/** Puts {@code c} after the last row's value. */
void addTrailing(JComponent c) {
add(c, cell(2, lastRow, GridBagConstraints.EAST, new Insets(0, 6, 0, 0)));
}
void update(List<List<Row>> rows) {
for (int g = 0; g < groups.size(); g++) {
List<JLabel[]> labels = groups.get(g);
List<Row> group = rows.get(g);
for (int i = 0; i < labels.size(); i++) {
boolean shown = i < group.size();
labels.get(i)[0].setVisible(shown);
labels.get(i)[1].setVisible(shown);
if (shown) labels.get(i)[1].setText(group.get(i).value());
}
}
}
}
/** One traffic category: a row per figure under Total / In / Out headings. */
private static final class TrafficTable extends JPanel {
private final ConnectionStats.Kind kind;
@@ -304,7 +223,7 @@ public final class ConnectionInfoDialog extends JDialog {
List<String> columns = ConnectionStatsFormat.COLUMNS;
for (int c = 0; c < columns.size(); c++) {
JLabel heading = label(columns.get(c));
JLabel heading = Figures.label(columns.get(c));
heading.setFont(heading.getFont().deriveFont(Font.ITALIC));
add(heading, column(c + 1, 0));
}
@@ -312,19 +231,19 @@ public final class ConnectionInfoDialog extends JDialog {
cells = new JLabel[rows.size()][];
for (int r = 0; r < rows.size(); r++) {
TrafficRow row = rows.get(r);
add(label(row.label()), cell(0, r + 1, GridBagConstraints.WEST, new Insets(3, 0, 3, 8)));
add(Figures.label(row.label()), Figures.cell(0, r + 1, GridBagConstraints.WEST, new Insets(3, 0, 3, 8)));
cells[r] = new JLabel[]{
sized(label(row.total()), TOTAL_SAMPLE), sized(label(row.in()), VALUE_SAMPLE),
sized(label(row.out()), VALUE_SAMPLE)};
Figures.sized(Figures.label(row.total()), TOTAL_SAMPLE), Figures.sized(Figures.label(row.in()), VALUE_SAMPLE),
Figures.sized(Figures.label(row.out()), VALUE_SAMPLE)};
for (int c = 0; c < cells[r].length; c++) add(cells[r][c], column(c + 1, r + 1));
}
GridBagConstraints filler = cell(0, rows.size() + 1, GridBagConstraints.WEST, new Insets(0, 0, 0, 0));
GridBagConstraints filler = Figures.cell(0, rows.size() + 1, GridBagConstraints.WEST, new Insets(0, 0, 0, 0));
filler.weighty = 1;
add(Box.createGlue(), filler);
}
private static GridBagConstraints column(int x, int y) {
GridBagConstraints c = cell(x, y, GridBagConstraints.WEST, new Insets(3, 8, 3, 8));
GridBagConstraints c = Figures.cell(x, y, GridBagConstraints.WEST, new Insets(3, 8, 3, 8));
c.weightx = 1;
return c;
}
@@ -0,0 +1,110 @@
package eu.lixko.ts3j.client.ui.server;
import com.formdev.flatlaf.util.UIScale;
import eu.lixko.ts3j.client.net.ConnectionStatsFormat.Row;
import eu.lixko.ts3j.client.ui.Theme;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
/**
* Labelled figures, one per row, in groups set apart by a rule; refreshed in place, and
* rows an update leaves out hide. The connection info windows' summaries.
*/
final class Figures extends JPanel {
/** A wide traffic figure, to size value columns by so they don't jitter. */
static final String VALUE_SAMPLE = "1023.00 KiB/s";
private final List<List<JLabel[]>> groups = new ArrayList<>();
private int lastRow;
Figures(List<List<Row>> rows, boolean valuesRight) {
super(new GridBagLayout());
setOpaque(false);
int y = 0;
for (List<Row> group : rows) {
if (y > 0) {
GridBagConstraints rule = cell(0, y++, GridBagConstraints.WEST, new Insets(6, 0, 6, 0));
rule.gridwidth = 2;
rule.fill = GridBagConstraints.HORIZONTAL;
add(new JSeparator(), rule);
}
List<JLabel[]> labels = new ArrayList<>();
for (Row row : group) {
JLabel key = label(row.label());
JLabel value = label(row.value());
labels.add(new JLabel[]{key, value});
add(key, cell(0, y, GridBagConstraints.WEST, new Insets(3, 0, 3, 16)));
GridBagConstraints vc = cell(1, y, valuesRight ? GridBagConstraints.EAST : GridBagConstraints.WEST,
new Insets(3, 0, 3, 0));
vc.weightx = 1;
add(value, vc);
lastRow = y++;
}
groups.add(labels);
}
}
/** Puts {@code c} after the last row's value. */
void addTrailing(JComponent c) {
add(c, cell(2, lastRow, GridBagConstraints.EAST, new Insets(0, 6, 0, 0)));
}
/** Puts {@code c} after the value of row {@code index}, counting rows across groups. */
void addTrailing(int index, JComponent c) {
int y = 0;
for (int g = 0; g < groups.size(); g++) {
if (g > 0) y++; // the rule
if (index < groups.get(g).size()) {
add(c, cell(2, y + index, GridBagConstraints.EAST, new Insets(0, 6, 0, 0)));
return;
}
index -= groups.get(g).size();
y += groups.get(g).size();
}
}
void update(List<List<Row>> rows) {
for (int g = 0; g < groups.size(); g++) {
List<JLabel[]> labels = groups.get(g);
List<Row> group = rows.get(g);
for (int i = 0; i < labels.size(); i++) {
boolean shown = i < group.size();
labels.get(i)[0].setVisible(shown);
labels.get(i)[1].setVisible(shown);
if (shown) labels.get(i)[1].setText(group.get(i).value());
}
}
}
static JLabel label(String text) {
JLabel l = new JLabel(text);
l.setFont(Theme.uiFont());
return l;
}
/** Pins a label's width to that of {@code sample}, so a growing figure doesn't resize the window. */
static JLabel sized(JLabel l, String sample) {
Dimension pref = l.getPreferredSize();
int width = l.getFontMetrics(l.getFont()).stringWidth(sample);
l.setPreferredSize(new Dimension(Math.max(pref.width, width), pref.height));
return l;
}
static GridBagConstraints cell(int x, int y, int anchor, Insets insets) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = x;
c.gridy = y;
c.anchor = anchor;
c.insets = UIScale.scale(insets);
return c;
}
}
@@ -0,0 +1,173 @@
package eu.lixko.ts3j.client.ui.server;
import com.formdev.flatlaf.util.ScaledEmptyBorder;
import com.formdev.flatlaf.util.UIScale;
import eu.lixko.ts3j.client.net.ConnectionStatsFormat;
import eu.lixko.ts3j.client.net.ConnectionStatsFormat.TrafficRow;
import eu.lixko.ts3j.client.net.RequestException;
import eu.lixko.ts3j.client.net.ServerStats;
import eu.lixko.ts3j.client.session.ServerSession;
import eu.lixko.ts3j.client.ui.Theme;
import eu.lixko.ts3j.client.ui.icons.Icons;
import eu.lixko.ts3j.client.ui.widget.Dialogs;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Live "Server Connection Info" window, laid out as TeamSpeak 3's: the server's name,
* version, uptime, address, ping and loss, above an In / Out table of everything the
* server exchanged with its clients. Polls once a second while open.
*/
final class ServerConnectionInfoDialog extends JDialog {
private static final int REFRESH_MS = 1000;
private static final int ADDRESS_ROW = 4;
private final ServerSession session;
private final javax.swing.Timer timer;
private final AtomicBoolean inFlight = new AtomicBoolean();
private final Figures summary;
private final JLabel[][] cells;
ServerConnectionInfoDialog(Frame owner, ServerSession session) {
super(owner, "Server Connection Info", false);
this.session = session;
summary = new Figures(List.of(summaryRows(ServerStats.UNKNOWN)), false);
JButton copy = new JButton(Icons.of("COPY"));
copy.setToolTipText("Copy server address to clipboard");
copy.setMargin(UIScale.scale(new Insets(1, 1, 1, 1)));
copy.addActionListener(e -> Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new StringSelection(address()), null));
summary.addTrailing(ADDRESS_ROW, copy);
List<TrafficRow> rows = ConnectionStatsFormat.serverTrafficRows(ServerStats.UNKNOWN);
cells = new JLabel[rows.size()][];
JPanel content = new JPanel(new BorderLayout(0, UIScale.scale(12)));
content.setBackground(Theme.windowBg());
content.setBorder(new ScaledEmptyBorder(10, 10, 10, 10));
content.add(summary, BorderLayout.NORTH);
content.add(buildTable(rows), BorderLayout.CENTER);
content.add(buildButtons(), BorderLayout.SOUTH);
setContentPane(content);
Dialogs.closeOnEscape(this);
// Disposing is what stops the refresh timer, so the window button must not merely hide.
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setLocationRelativeTo(owner);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
timer.stop();
}
});
timer = new javax.swing.Timer(REFRESH_MS, e -> refresh());
timer.setInitialDelay(0);
timer.start();
}
private JPanel buildTable(List<TrafficRow> rows) {
JPanel table = new JPanel(new GridBagLayout());
table.setOpaque(false);
List<String> columns = ConnectionStatsFormat.SERVER_COLUMNS;
for (int c = 0; c < columns.size(); c++) {
JLabel heading = Figures.label(columns.get(c));
heading.setFont(heading.getFont().deriveFont(Font.ITALIC));
table.add(heading, column(c + 1, 0));
}
GridBagConstraints rule = Figures.cell(0, 1, GridBagConstraints.WEST, new Insets(2, 0, 4, 0));
rule.gridwidth = columns.size() + 1;
rule.fill = GridBagConstraints.HORIZONTAL;
table.add(new JSeparator(), rule);
for (int r = 0; r < rows.size(); r++) {
TrafficRow row = rows.get(r);
table.add(Figures.label(row.label()), Figures.cell(0, r + 2, GridBagConstraints.WEST, new Insets(3, 0, 3, 16)));
cells[r] = new JLabel[]{Figures.sized(Figures.label(row.in()), Figures.VALUE_SAMPLE),
Figures.sized(Figures.label(row.out()), Figures.VALUE_SAMPLE)};
for (int c = 0; c < cells[r].length; c++) table.add(cells[r][c], column(c + 1, r + 2));
}
return table;
}
private static GridBagConstraints column(int x, int y) {
GridBagConstraints c = Figures.cell(x, y, GridBagConstraints.WEST, new Insets(3, 0, 3, 16));
c.weightx = 1;
return c;
}
private JPanel buildButtons() {
JPanel bar = new JPanel();
bar.setLayout(new BoxLayout(bar, BoxLayout.X_AXIS));
bar.setOpaque(false);
bar.add(Box.createHorizontalGlue());
JButton close = new JButton("Close", Icons.of("CLOSE"));
close.addActionListener(e -> dispose());
bar.add(close);
return bar;
}
private void refresh() {
if (!session.isConnected()) {
timer.stop();
return;
}
if (!inFlight.compareAndSet(false, true)) return; // a previous poll is still running
session.serverStats().read().whenComplete((stats, failure) -> SwingUtilities.invokeLater(() -> {
inFlight.set(false);
if (failure == null) apply(stats);
else failed(RequestException.of(failure));
}));
}
private void apply(ServerStats stats) {
if (!isDisplayable()) return;
summary.update(List.of(summaryRows(stats)));
List<TrafficRow> rows = ConnectionStatsFormat.serverTrafficRows(stats);
for (int r = 0; r < rows.size(); r++) {
cells[r][0].setText(rows.get(r).in());
cells[r][1].setText(rows.get(r).out());
}
// The uptime gains days, the version shows once known; grow rather than cut them off.
if (getPreferredSize().width > getWidth()) pack();
}
/** A refusal won't change by asking again, so it ends the polling; a lost connection already has. */
private void failed(RequestException failure) {
if (!isDisplayable() || failure.kind() == RequestException.Kind.NOT_CONNECTED) return;
timer.stop();
JOptionPane.showMessageDialog(this, failure.getMessage(), "Error requesting server connection info",
JOptionPane.ERROR_MESSAGE);
dispose();
}
private List<ConnectionStatsFormat.Row> summaryRows(ServerStats stats) {
return ConnectionStatsFormat.serverSummaryRows(session.model(), address(), stats);
}
private String address() {
return session.address() + ":" + session.port();
}
}
@@ -24,6 +24,11 @@ final class ServerMenu {
spacer.setToolTipText("Create a new spacer");
spacer.addActionListener(a -> actions.createSpacer());
menu.add(spacer);
menu.addSeparator();
JMenuItem info = new JMenuItem("Server Connection Info", Icons.of("SERVER_GREEN"));
info.setEnabled(actions.canViewServerConnectionInfo());
info.addActionListener(a -> actions.showServerConnectionInfo());
menu.add(info);
return menu;
}
}
@@ -67,6 +67,7 @@ public final class ServerTab {
/** The one open ban list window for this server, if any. */
/** The open ban list, complaints and other list windows, one of each kind. */
private final Map<Class<? extends AbstractServerListDialog>, AbstractServerListDialog> listWindows = new HashMap<>();
private ServerConnectionInfoDialog serverInfoWindow;
/** The unread count last reported, to tell new mail from messages being read. */
private int unreadMessages;
@@ -211,6 +212,16 @@ public final class ServerTab {
showListWindow(ServerLogDialog.class, () -> new ServerLogDialog(host, session, settings));
}
/** Opens the server's connection figures, or brings the already open window forward. */
public void showServerConnectionInfo() {
if (!session.isConnected()) return;
if (serverInfoWindow == null || !serverInfoWindow.isDisplayable()) {
serverInfoWindow = new ServerConnectionInfoDialog(host, session);
}
serverInfoWindow.setVisible(true);
serverInfoWindow.toFront();
}
public void showPrivilegeKeys() {
showListWindow(PrivilegeKeysDialog.class, () -> new PrivilegeKeysDialog(host, session, groupIcons));
}
@@ -203,6 +203,16 @@ final class ServerTabTreeActions implements ServerTreePanel.Actions {
treePanel.selectClient(client.id);
}
@Override
public boolean canViewServerConnectionInfo() {
return session.permissions().canViewServerConnectionInfo();
}
@Override
public void showServerConnectionInfo() {
tab.showServerConnectionInfo();
}
@Override
public void showConnectionInfo(ClientEntry client) {
if (!session.isConnected()) return;
@@ -157,6 +157,11 @@ public final class ServerTreePanel extends JPanel {
/** Opens the per-client volume dialog. */
void changeClientVolume(ClientEntry client);
/** Whether we may see the server's connection figures. */
boolean canViewServerConnectionInfo();
void showServerConnectionInfo();
}
private final DropIndicatorTree tree;
@@ -0,0 +1,76 @@
package eu.lixko.ts3j.client.ui.soundboard;
import com.formdev.flatlaf.util.UIScale;
import eu.lixko.ts3j.client.soundboard.SoundboardSound;
import eu.lixko.ts3j.client.ui.widget.FormPanel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.io.File;
/** Edits a soundboard sound's name, file and volume. */
final class SoundEditDialog {
private SoundEditDialog() {
}
/** @return whether the sound was changed */
static boolean edit(Component parent, SoundboardSound sound) {
JTextField name = new JTextField(sound.name, 24);
JTextField file = new JTextField(sound.file, 24);
JButton browse = new JButton("…");
browse.addActionListener(e -> {
JFileChooser chooser = new JFileChooser(new File(file.getText()).getParentFile());
chooser.setFileFilter(new FileNameExtensionFilter("Sounds (WAV, Opus)", "wav", "wave", "opus", "ogg"));
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
file.setText(chooser.getSelectedFile().getAbsolutePath());
}
});
int max = (int) Math.round(SoundboardSound.MAX_VOLUME * 100);
JSlider volume = new JSlider(0, max, (int) Math.round(sound.volume * 100));
JLabel percent = new JLabel();
volume.addChangeListener(e -> percent.setText(volume.getValue() + " %"));
percent.setText(volume.getValue() + " %");
JPanel fileRow = new JPanel(new BorderLayout(UIScale.scale(4), 0));
fileRow.add(file, BorderLayout.CENTER);
fileRow.add(browse, BorderLayout.EAST);
JPanel volumeRow = new JPanel(new BorderLayout(UIScale.scale(4), 0));
volumeRow.add(volume, BorderLayout.CENTER);
volumeRow.add(percent, BorderLayout.EAST);
FormPanel form = new FormPanel(false);
row(form, 0, "Name:", name);
row(form, 1, "File:", fileRow);
row(form, 2, "Volume:", volumeRow);
if (JOptionPane.showConfirmDialog(parent, form, "Edit Sound", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE) != JOptionPane.OK_OPTION) {
return false;
}
if (!name.getText().isBlank()) sound.name = name.getText().trim();
if (!file.getText().isBlank()) sound.file = file.getText().trim();
sound.volume = volume.getValue() / 100.0;
return true;
}
private static void row(FormPanel form, int y, String label, Component field) {
GridBagConstraints c = FormPanel.gbc();
c.gridy = y;
c.gridx = 0;
c.weightx = 0;
form.add(new JLabel(label), c);
c.gridx = 1;
c.weightx = 1;
form.add(field, c);
}
}
@@ -0,0 +1,82 @@
package eu.lixko.ts3j.client.ui.soundboard;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.soundboard.Soundboard;
import eu.lixko.ts3j.client.soundboard.SoundboardSound;
import eu.lixko.ts3j.client.soundboard.SoundboardSounds;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionException;
/** The soundboard as the window and the hotkeys reach it. Driven from the event dispatch thread. */
public final class SoundboardActions {
private final JFrame owner;
private final Settings settings;
private final Soundboard soundboard;
private final SoundboardSounds sounds = SoundboardSounds.load();
private SoundboardWindow window;
public SoundboardActions(JFrame owner, Settings settings, Soundboard soundboard) {
this.owner = owner;
this.settings = settings;
this.soundboard = soundboard;
}
public void show() {
if (window == null || !window.isDisplayable()) {
window = new SoundboardWindow(owner, settings, soundboard, sounds, this::play);
}
window.setVisible(true);
window.toFront();
}
/** Starts the sound with this name, from the top if it is playing. */
public void play(String name) {
SoundboardSound sound = sounds.find(name);
if (sound == null) {
report("No sound named " + name);
return;
}
play(sound);
}
/** Starts the sound with this name, or stops it while it plays. */
public void toggle(String name) {
SoundboardSound sound = sounds.find(name);
if (sound != null && soundboard.isPlaying(sound)) soundboard.stop(sound);
else play(name);
}
public void stopAll() {
soundboard.stopAll();
}
/** The sounds' names, for hotkey arguments. */
public List<String> names() {
List<String> out = new ArrayList<>();
for (SoundboardSound s : sounds.all()) out.add(s.name);
return out;
}
public void shutdown() {
soundboard.shutdown();
}
private void play(SoundboardSound sound) {
if (window != null) window.report("");
soundboard.play(sound).exceptionally(e -> {
Throwable cause = e instanceof CompletionException && e.getCause() != null ? e.getCause() : e;
SwingUtilities.invokeLater(() -> report(cause.getMessage()));
return null;
});
}
/** Shows a failure in the window; a hotkey pressed with it closed fails quietly rather than steal focus. */
private void report(String message) {
if (window != null && window.isDisplayable()) window.report(message);
}
}
@@ -0,0 +1,425 @@
package eu.lixko.ts3j.client.ui.soundboard;
import com.formdev.flatlaf.util.ScaledEmptyBorder;
import com.formdev.flatlaf.util.UIScale;
import eu.lixko.ts3j.client.config.Settings;
import eu.lixko.ts3j.client.config.Setting;
import eu.lixko.ts3j.client.sound.SoundFiles;
import eu.lixko.ts3j.client.soundboard.Soundboard;
import eu.lixko.ts3j.client.soundboard.SoundboardSound;
import eu.lixko.ts3j.client.soundboard.SoundboardSounds;
import eu.lixko.ts3j.client.ui.Theme;
import eu.lixko.ts3j.client.ui.icons.Icons;
import eu.lixko.ts3j.client.ui.widget.Dialogs;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.KeyStroke;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.List;
import java.util.function.Consumer;
/**
* The soundboard: a grid of sounds, each played onto the air with a click, as the chosen
* {@link Settings.SoundboardMode} has it. Files dropped on it are added; the context menu
* stops, edits, reorders and removes.
*/
final class SoundboardWindow extends JDialog {
private static final int TILE_WIDTH = 150;
private static final int TILE_HEIGHT = 48;
private final Settings settings;
private final Soundboard soundboard;
private final SoundboardSounds sounds;
private final Consumer<SoundboardSound> play;
private final DefaultListModel<SoundboardSound> model = new DefaultListModel<>();
private final JList<SoundboardSound> grid = new JList<>(model);
private final JButton stopAll = new JButton("Stop all", Icons.ofAny("STOP", "RECORDING_STOP"));
private final JLabel status = new JLabel(" ");
private final Runnable playingChanged = () -> SwingUtilities.invokeLater(this::playingChanged);
private File lastDirectory;
SoundboardWindow(JFrame owner, Settings settings, Soundboard soundboard, SoundboardSounds sounds,
Consumer<SoundboardSound> play) {
super(owner, "Soundboard", false);
this.settings = settings;
this.soundboard = soundboard;
this.sounds = sounds;
this.play = play;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
sounds.all().forEach(model::addElement);
grid.setLayoutOrientation(JList.HORIZONTAL_WRAP);
grid.setVisibleRowCount(-1);
grid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
grid.setFixedCellWidth(UIScale.scale(TILE_WIDTH));
grid.setFixedCellHeight(UIScale.scale(TILE_HEIGHT));
grid.setCellRenderer(new TileRenderer());
grid.setTransferHandler(new FileDrop());
grid.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int index = tileAt(e);
if (index < 0) {
grid.clearSelection();
if (e.isPopupTrigger()) emptyMenu().show(grid, e.getX(), e.getY());
return;
}
grid.setSelectedIndex(index);
if (e.isPopupTrigger()) {
tileMenu(model.get(index)).show(grid, e.getX(), e.getY());
} else if (SwingUtilities.isLeftMouseButton(e)) {
play.accept(model.get(index));
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (!e.isPopupTrigger()) return;
int index = tileAt(e);
if (index >= 0) tileMenu(model.get(index)).show(grid, e.getX(), e.getY());
else emptyMenu().show(grid, e.getX(), e.getY());
}
});
bindKey("ENTER", "play", () -> selected(play));
bindKey("DELETE", "remove", () -> selected(this::remove));
bindKey("F2", "edit", () -> selected(this::edit));
JScrollPane scroll = new JScrollPane(grid);
scroll.setPreferredSize(UIScale.scale(new Dimension(4 * TILE_WIDTH + 24, 4 * TILE_HEIGHT + 8)));
JButton add = new JButton("Add…", Icons.of("ADD"));
add.addActionListener(e -> chooseFiles());
stopAll.addActionListener(e -> soundboard.stopAll());
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT, UIScale.scale(6), 0));
buttons.add(add);
buttons.add(stopAll);
buttons.add(Box.createHorizontalStrut(UIScale.scale(12)));
buttons.add(new JLabel("Volume"));
buttons.add(volumeSlider(settings.soundboardVolume, 200));
buttons.add(new JLabel("Local"));
buttons.add(volumeSlider(settings.soundboardLocalVolume, 100));
JComboBox<Settings.SoundboardMode> mode = new JComboBox<>(Settings.SoundboardMode.values());
mode.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean selected, boolean focused) {
return super.getListCellRendererComponent(list, label((Settings.SoundboardMode) value),
index, selected, focused);
}
});
mode.setSelectedItem(settings.soundboardMode.get());
mode.addActionListener(e -> {
settings.soundboardMode.set((Settings.SoundboardMode) mode.getSelectedItem());
settings.save();
});
JCheckBox withMic = new JCheckBox("Mix in microphone", settings.soundboardWithMic.get());
withMic.addActionListener(e -> {
settings.soundboardWithMic.set(withMic.isSelected());
settings.save();
});
JPanel options = new JPanel(new FlowLayout(FlowLayout.LEFT, UIScale.scale(6), 0));
options.add(new JLabel("Mode"));
options.add(mode);
options.add(Box.createHorizontalStrut(UIScale.scale(12)));
options.add(withMic);
status.setForeground(Theme.blocked());
JPanel south = new JPanel();
south.setLayout(new BoxLayout(south, BoxLayout.Y_AXIS));
buttons.setAlignmentX(LEFT_ALIGNMENT);
options.setAlignmentX(LEFT_ALIGNMENT);
status.setAlignmentX(LEFT_ALIGNMENT);
south.add(buttons);
south.add(Box.createVerticalStrut(UIScale.scale(6)));
south.add(options);
south.add(Box.createVerticalStrut(UIScale.scale(4)));
south.add(status);
JPanel content = new JPanel(new BorderLayout(0, UIScale.scale(8)));
content.setBorder(new ScaledEmptyBorder(8, 8, 8, 8));
content.add(scroll, BorderLayout.CENTER);
content.add(south, BorderLayout.SOUTH);
setContentPane(content);
soundboard.addListener(playingChanged);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
soundboard.removeListener(playingChanged);
}
});
Dialogs.closeOnEscape(this);
playingChanged();
pack();
setLocationRelativeTo(owner);
}
/** Shows why a sound did not play; empty clears it. */
void report(String message) {
status.setText(message == null || message.isEmpty() ? " " : message);
}
// ---- playing ----
private void playingChanged() {
stopAll.setEnabled(soundboard.isPlaying());
grid.repaint();
}
// ---- editing ----
private void chooseFiles() {
JFileChooser chooser = new JFileChooser(lastDirectory);
chooser.setMultiSelectionEnabled(true);
chooser.setFileFilter(new FileNameExtensionFilter("Sounds (WAV, Opus)", "wav", "wave", "opus", "ogg"));
if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) return;
lastDirectory = chooser.getCurrentDirectory();
add(List.of(chooser.getSelectedFiles()));
}
private void add(List<File> files) {
int added = 0;
for (File f : files) {
if (!f.isFile() || !SoundFiles.isSupported(f)) continue;
SoundboardSound sound = new SoundboardSound(baseName(f), f.getAbsolutePath());
sounds.all().add(sound);
model.addElement(sound);
added++;
}
if (added > 0) {
sounds.save();
grid.setSelectedIndex(model.size() - 1);
grid.ensureIndexIsVisible(model.size() - 1);
report("");
} else {
report("Only WAV and Opus files");
}
}
private void edit(SoundboardSound sound) {
if (!SoundEditDialog.edit(this, sound)) return;
soundboard.updateVolume(sound);
sounds.save();
grid.repaint();
}
private void remove(SoundboardSound sound) {
soundboard.stop(sound);
sounds.all().remove(sound);
model.removeElement(sound);
sounds.save();
}
private void move(SoundboardSound sound, int delta) {
int from = sounds.all().indexOf(sound);
int to = from + delta;
if (from < 0 || to < 0 || to >= sounds.all().size()) return;
sounds.all().remove(from);
sounds.all().add(to, sound);
model.remove(from);
model.add(to, sound);
grid.setSelectedIndex(to);
sounds.save();
}
private JPopupMenu tileMenu(SoundboardSound sound) {
JPopupMenu menu = new JPopupMenu();
menu.add(item("Play", () -> play.accept(sound)));
JMenuItem stop = item("Stop", () -> soundboard.stop(sound));
stop.setEnabled(soundboard.isPlaying(sound));
menu.add(stop);
menu.addSeparator();
menu.add(item("Edit…", () -> edit(sound)));
JMenuItem left = item("Move left", () -> move(sound, -1));
left.setEnabled(sounds.all().indexOf(sound) > 0);
JMenuItem right = item("Move right", () -> move(sound, 1));
right.setEnabled(sounds.all().indexOf(sound) < sounds.all().size() - 1);
menu.add(left);
menu.add(right);
menu.addSeparator();
menu.add(item("Remove", () -> remove(sound)));
return menu;
}
private JPopupMenu emptyMenu() {
JPopupMenu menu = new JPopupMenu();
menu.add(item("Add…", this::chooseFiles));
return menu;
}
// ---- helpers ----
private int tileAt(MouseEvent e) {
int index = grid.locationToIndex(e.getPoint());
return index >= 0 && grid.getCellBounds(index, index).contains(e.getPoint()) ? index : -1;
}
private void selected(Consumer<SoundboardSound> action) {
SoundboardSound sound = grid.getSelectedValue();
if (sound != null) action.accept(sound);
}
private void bindKey(String key, String name, Runnable action) {
grid.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(key), name);
grid.getActionMap().put(name, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
action.run();
}
});
}
private static String label(Settings.SoundboardMode mode) {
return switch (mode) {
case SINGLE -> "One at a time";
case ONCE_EACH -> "Each once";
case MAYHEM -> "Mayhem";
};
}
private static JMenuItem item(String label, Runnable action) {
JMenuItem item = new JMenuItem(label);
item.addActionListener(e -> action.run());
return item;
}
private JSlider volumeSlider(Setting<Double> setting, int maxPercent) {
JSlider slider = new JSlider(0, maxPercent, (int) Math.round(setting.get() * 100));
slider.setPreferredSize(new Dimension(UIScale.scale(100), slider.getPreferredSize().height));
slider.addChangeListener(e -> {
setting.set(slider.getValue() / 100.0);
slider.setToolTipText(slider.getValue() + " %");
if (!slider.getValueIsAdjusting()) settings.save();
});
slider.setToolTipText(slider.getValue() + " %");
return slider;
}
private static String baseName(File f) {
String name = f.getName();
int dot = name.lastIndexOf('.');
return dot > 0 ? name.substring(0, dot) : name;
}
/** Accepts sound files dragged in from a file manager. */
private final class FileDrop extends TransferHandler {
@Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
}
@Override
@SuppressWarnings("unchecked")
public boolean importData(TransferSupport support) {
if (!canImport(support)) return false;
try {
add((List<File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
return true;
} catch (Exception e) {
return false;
}
}
}
/** A sound as a rounded tile, lit while it plays. */
private final class TileRenderer extends JComponent implements ListCellRenderer<SoundboardSound> {
private SoundboardSound sound;
private boolean selected;
private boolean playing;
TileRenderer() {
setBorder(BorderFactory.createEmptyBorder());
}
@Override
public Component getListCellRendererComponent(JList<? extends SoundboardSound> list, SoundboardSound value,
int index, boolean isSelected, boolean cellHasFocus) {
sound = value;
selected = isSelected;
playing = soundboard.isPlaying(value);
boolean missing = !new File(value.file).isFile();
setToolTipText(missing ? "Missing: " + value.file : value.file);
setFont(list.getFont());
setForeground(missing ? Theme.idleClient() : playing ? Color.WHITE : list.getForeground());
return this;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
int gap = UIScale.scale(3);
int arc = UIScale.scale(10);
int w = getWidth() - 2 * gap;
int h = getHeight() - 2 * gap;
g2.setColor(playing ? Theme.talking() : Theme.hover());
g2.fillRoundRect(gap, gap, w, h, arc, arc);
g2.setColor(selected ? Theme.accent() : Theme.border());
g2.drawRoundRect(gap, gap, w - 1, h - 1, arc, arc);
g2.setFont(getFont());
g2.setColor(getForeground());
FontMetrics fm = g2.getFontMetrics();
String text = ellipsize(sound.name, fm, w - 2 * UIScale.scale(8));
g2.drawString(text, (getWidth() - fm.stringWidth(text)) / 2,
(getHeight() - fm.getHeight()) / 2 + fm.getAscent());
g2.dispose();
}
private static String ellipsize(String text, FontMetrics fm, int width) {
if (fm.stringWidth(text) <= width) return text;
String dots = "…";
int end = text.length();
while (end > 0 && fm.stringWidth(text.substring(0, end) + dots) > width) end--;
return text.substring(0, end) + dots;
}
}
}