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>
This commit is contained in:
@@ -56,6 +56,12 @@ Android app in [`../android`](../android) does.
|
||||
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 or stop it; drop files on the window to add them. Per-sound and
|
||||
global volume, plus a local volume for hearing them yourself. 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).
|
||||
@@ -275,7 +281,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)
|
||||
|
||||
@@ -75,6 +75,7 @@ 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 boolean mutedTalking;
|
||||
|
||||
private final AudioIo io;
|
||||
@@ -262,6 +263,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 +320,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 +421,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 +451,26 @@ public final class CaptureVoiceInput implements VoiceInput {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixes the playing clips into {@code frame}, alone when the gate is shut. 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) 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,123 @@
|
||||
package eu.lixko.ts3j.client.audio;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Prerecorded clips (mono, 48 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 float[] pcm;
|
||||
final double gain;
|
||||
int position;
|
||||
|
||||
Clip(float[] pcm, double gain) {
|
||||
this.pcm = pcm;
|
||||
this.gain = gain;
|
||||
}
|
||||
}
|
||||
|
||||
private final Map<Object, Clip> clips = new LinkedHashMap<>();
|
||||
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 || gain <= 0) return;
|
||||
synchronized (clips) {
|
||||
clips.put(key, new Clip(pcm, gain));
|
||||
}
|
||||
changed();
|
||||
}
|
||||
|
||||
public void stop(Object key) {
|
||||
boolean removed;
|
||||
synchronized (clips) {
|
||||
removed = clips.remove(key) != null;
|
||||
}
|
||||
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) {
|
||||
return clips.containsKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
*
|
||||
* @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.values().iterator(); it.hasNext(); ) {
|
||||
Clip c = it.next();
|
||||
int n = Math.min(frames, c.pcm.length - c.position);
|
||||
for (int i = 0; i < n; i++) {
|
||||
float s = (float) (c.pcm[c.position + i] * c.gain);
|
||||
for (int ch = 0; ch < channels; ch++) frame[i * channels + ch] += s;
|
||||
}
|
||||
c.position += n;
|
||||
mixed = true;
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -169,6 +169,12 @@ 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);
|
||||
|
||||
// ---- notification sounds ----
|
||||
/** Directory name of the active sound pack; empty picks the first one found. */
|
||||
public final Setting<String> soundPack = text("soundPack", "default");
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
@@ -531,6 +532,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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
@@ -89,11 +88,21 @@ 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 = () -> {
|
||||
synchronized (lock) {
|
||||
voices.remove(voice);
|
||||
}
|
||||
};
|
||||
if (pcm.length == 0 || volume <= 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 +110,7 @@ public final class PackSoundPlayer implements SoundPlayer {
|
||||
}
|
||||
lock.notifyAll();
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -212,18 +222,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,21 @@ public interface SoundPlayer {
|
||||
*/
|
||||
void play(File file, double volume);
|
||||
|
||||
/** A sound started with {@link #play(float[], double)}. */
|
||||
interface Playing {
|
||||
void stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 () -> {
|
||||
};
|
||||
}
|
||||
|
||||
/** 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,153 @@
|
||||
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.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.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
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, 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts {@code sound}, from the top if it is playing already.
|
||||
*
|
||||
* @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);
|
||||
double sent = sound.volume * settings.soundboardVolume.get();
|
||||
double heard = Math.min(1.0, sound.volume * settings.soundboardLocalVolume.get() * settings.masterVolume.get());
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
float[] pcm;
|
||||
try {
|
||||
pcm = decode(file);
|
||||
} catch (IOException e) {
|
||||
throw new SoundboardException(file.getName() + ": " + e.getMessage(), e);
|
||||
}
|
||||
stopLocal(sound);
|
||||
mixer.play(sound, pcm, sent);
|
||||
if (heard > 0) {
|
||||
synchronized (local) {
|
||||
local.put(sound, speakers.play(pcm, heard));
|
||||
}
|
||||
}
|
||||
}, decoder);
|
||||
}
|
||||
|
||||
public void stop(SoundboardSound sound) {
|
||||
mixer.stop(sound);
|
||||
stopLocal(sound);
|
||||
}
|
||||
|
||||
public void stopAll() {
|
||||
mixer.stopAll();
|
||||
synchronized (local) {
|
||||
local.values().forEach(SoundPlayer.Playing::stop);
|
||||
local.clear();
|
||||
}
|
||||
}
|
||||
|
||||
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 void stopLocal(SoundboardSound sound) {
|
||||
synchronized (local) {
|
||||
SoundPlayer.Playing playing = local.remove(sound);
|
||||
if (playing != null) 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().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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
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 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, new SoundPlayer() {
|
||||
@Override
|
||||
public void play(File file, double volume) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
}
|
||||
}, 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());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/** 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);
|
||||
|
||||
/** 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);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -397,6 +397,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);
|
||||
@@ -396,6 +401,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 +488,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 +603,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.of("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();
|
||||
|
||||
@@ -111,6 +111,7 @@ 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, "MUSIC");
|
||||
}
|
||||
|
||||
private void addDialogs() {
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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:";
|
||||
};
|
||||
}
|
||||
|
||||
+76
@@ -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);
|
||||
}
|
||||
}
|
||||
+82
@@ -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);
|
||||
}
|
||||
}
|
||||
+386
@@ -0,0 +1,386 @@
|
||||
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.JButton;
|
||||
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 and stopped with
|
||||
* another. Files dropped on it are added; the context menu 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)) {
|
||||
toggle(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(this::toggle));
|
||||
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));
|
||||
|
||||
status.setForeground(Theme.blocked());
|
||||
JPanel south = new JPanel();
|
||||
south.setLayout(new BoxLayout(south, BoxLayout.Y_AXIS));
|
||||
buttons.setAlignmentX(LEFT_ALIGNMENT);
|
||||
status.setAlignmentX(LEFT_ALIGNMENT);
|
||||
south.add(buttons);
|
||||
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 toggle(SoundboardSound sound) {
|
||||
if (soundboard.isPlaying(sound)) soundboard.stop(sound);
|
||||
else play.accept(sound);
|
||||
}
|
||||
|
||||
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;
|
||||
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();
|
||||
boolean playing = soundboard.isPlaying(sound);
|
||||
menu.add(item(playing ? "Stop" : "Play", () -> toggle(sound)));
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user