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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user