Soundboard: play modes, optional mic, live volume, dark-friendly icon
- Mode, saved: one sound at a time, each sound once (a click restarts it) or mayhem (every click layers another copy). A tile click plays by the mode; Stop is in the context menu. - "Mix in microphone": when off, a playing sound replaces the open mic rather than mixing with it. - Volume, local volume, master volume and per-sound volume now reach sounds already playing; a sound turned down to nothing no longer keys the microphone. - Toolbar and menu use the pack's SOUNDPACK icon, readable on dark themes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import eu.lixko.ts3j.client.audio.opus.OpusEncoder;
|
||||
import eu.lixko.ts3j.client.audio.processing.AudioProcessor;
|
||||
import eu.lixko.ts3j.client.audio.processing.ns.SuppressionLevel;
|
||||
import eu.lixko.ts3j.client.audio.vad.RnnSpeechDetector;
|
||||
import eu.lixko.ts3j.client.config.Setting;
|
||||
import eu.lixko.ts3j.client.config.Settings;
|
||||
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
@@ -76,6 +77,7 @@ public final class CaptureVoiceInput implements VoiceInput {
|
||||
private volatile Runnable mutedTalkListener; // speech detected while muted
|
||||
private volatile AudioFrameListener monitorListener; // local monitoring of what is sent
|
||||
private volatile ClipMixer clips;
|
||||
private final Setting<Boolean> clipsWithMic;
|
||||
private boolean mutedTalking;
|
||||
|
||||
private final AudioIo io;
|
||||
@@ -111,6 +113,7 @@ public final class CaptureVoiceInput implements VoiceInput {
|
||||
this.speechThreshold = settings.speechThreshold.get();
|
||||
this.vadOverPtt = settings.vadOverPtt.get();
|
||||
this.inputGain = settings.inputVolume.get();
|
||||
this.clipsWithMic = settings.soundboardWithMic;
|
||||
this.params = OpusParameters.from(settings);
|
||||
this.codec = codecFor(params);
|
||||
setRedundancy(settings.redundancyDelay.get(), settings.redundancyCopies.get());
|
||||
@@ -452,7 +455,8 @@ public final class CaptureVoiceInput implements VoiceInput {
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixes the playing clips into {@code frame}, alone when the gate is shut. While muted they
|
||||
* Mixes the playing clips into {@code frame}; alone when the gate is shut or our voice is to
|
||||
* be left out. While muted they
|
||||
* are still consumed, so they keep time rather than wait for the unmute.
|
||||
*
|
||||
* @return whether the frame now carries a clip to transmit
|
||||
@@ -464,7 +468,7 @@ public final class CaptureVoiceInput implements VoiceInput {
|
||||
c.mixInto(new float[frame.length], VoiceFormat.FRAME_SIZE, channels);
|
||||
return false;
|
||||
}
|
||||
if (open) return c.mixInto(frame, VoiceFormat.FRAME_SIZE, channels);
|
||||
if (open && clipsWithMic.get()) return c.mixInto(frame, VoiceFormat.FRAME_SIZE, channels);
|
||||
float[] alone = new float[frame.length];
|
||||
if (!c.mixInto(alone, VoiceFormat.FRAME_SIZE, channels)) return false;
|
||||
System.arraycopy(alone, 0, frame, 0, frame.length);
|
||||
|
||||
@@ -2,9 +2,7 @@ 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,
|
||||
@@ -17,32 +15,53 @@ import java.util.Map;
|
||||
public final class ClipMixer {
|
||||
|
||||
private static final class Clip {
|
||||
final Object key;
|
||||
final float[] pcm;
|
||||
final double gain;
|
||||
volatile double gain;
|
||||
int position;
|
||||
|
||||
Clip(float[] pcm, double gain) {
|
||||
Clip(Object key, float[] pcm, double gain) {
|
||||
this.key = key;
|
||||
this.pcm = pcm;
|
||||
this.gain = gain;
|
||||
}
|
||||
}
|
||||
|
||||
private final Map<Object, Clip> clips = new LinkedHashMap<>();
|
||||
private final List<Clip> clips = new ArrayList<>();
|
||||
private final List<Runnable> listeners = new ArrayList<>();
|
||||
|
||||
/** Starts {@code pcm} under {@code key}, from the top if that key is already playing. */
|
||||
public void play(Object key, float[] pcm, double gain) {
|
||||
if (pcm.length == 0 || gain <= 0) return;
|
||||
if (pcm.length == 0) return;
|
||||
synchronized (clips) {
|
||||
clips.put(key, new Clip(pcm, gain));
|
||||
clips.removeIf(c -> c.key.equals(key));
|
||||
clips.add(new Clip(key, pcm, gain));
|
||||
}
|
||||
changed();
|
||||
}
|
||||
|
||||
/** Starts another copy of {@code pcm} under {@code key}, mixed with any already playing. */
|
||||
public void add(Object key, float[] pcm, double gain) {
|
||||
if (pcm.length == 0) return;
|
||||
synchronized (clips) {
|
||||
clips.add(new Clip(key, pcm, gain));
|
||||
}
|
||||
changed();
|
||||
}
|
||||
|
||||
/** Changes the gain of every clip playing under {@code key}, from its next frame on. */
|
||||
public void setGain(Object key, double gain) {
|
||||
synchronized (clips) {
|
||||
for (Clip c : clips) {
|
||||
if (c.key.equals(key)) c.gain = gain;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void stop(Object key) {
|
||||
boolean removed;
|
||||
synchronized (clips) {
|
||||
removed = clips.remove(key) != null;
|
||||
removed = clips.removeIf(c -> c.key.equals(key));
|
||||
}
|
||||
if (removed) changed();
|
||||
}
|
||||
@@ -58,7 +77,10 @@ public final class ClipMixer {
|
||||
|
||||
public boolean isPlaying(Object key) {
|
||||
synchronized (clips) {
|
||||
return clips.containsKey(key);
|
||||
for (Clip c : clips) {
|
||||
if (c.key.equals(key)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,21 +107,27 @@ public final class ClipMixer {
|
||||
* Adds the next {@code frames} samples of every playing clip to {@code frame} (interleaved,
|
||||
* the mono clips copied to each channel), clamped to [-1, 1], and moves the clips on.
|
||||
*
|
||||
* Clips turned down to nothing move on without counting as mixed in, so they do not key the
|
||||
* microphone.
|
||||
*
|
||||
* @return whether any clip was mixed in
|
||||
*/
|
||||
public boolean mixInto(float[] frame, int frames, int channels) {
|
||||
boolean mixed = false;
|
||||
boolean ended = false;
|
||||
synchronized (clips) {
|
||||
for (Iterator<Clip> it = clips.values().iterator(); it.hasNext(); ) {
|
||||
for (Iterator<Clip> it = clips.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;
|
||||
double gain = c.gain;
|
||||
if (gain > 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
float s = (float) (c.pcm[c.position + i] * gain);
|
||||
for (int ch = 0; ch < channels; ch++) frame[i * channels + ch] += s;
|
||||
}
|
||||
mixed = true;
|
||||
}
|
||||
c.position += n;
|
||||
mixed = true;
|
||||
if (c.position >= c.pcm.length) {
|
||||
it.remove();
|
||||
ended = true;
|
||||
|
||||
@@ -49,6 +49,16 @@ public final class Settings {
|
||||
HYBRID
|
||||
}
|
||||
|
||||
/** What playing a soundboard sound does to the sounds already playing. */
|
||||
public enum SoundboardMode {
|
||||
/** Stops every other sound: one at a time. */
|
||||
SINGLE,
|
||||
/** Restarts the sound if it is playing; others go on. */
|
||||
ONCE_EACH,
|
||||
/** Starts another copy, mixed with any already playing. */
|
||||
MAYHEM
|
||||
}
|
||||
|
||||
/** Who may whisper to us, the way TeamSpeak's whisper options put it. */
|
||||
public enum WhisperPolicy {
|
||||
/** Use individual settings in contacts, else allow. */
|
||||
@@ -174,6 +184,9 @@ public final class Settings {
|
||||
public final Setting<Double> soundboardVolume = decimal("soundboardVolume", 1.0);
|
||||
/** How loud we hear our own soundboard sounds, 0..1; 0 plays them to the server only. */
|
||||
public final Setting<Double> soundboardLocalVolume = decimal("soundboardLocalVolume", 0.5);
|
||||
public final Setting<SoundboardMode> soundboardMode = choice("soundboardMode", SoundboardMode.ONCE_EACH);
|
||||
/** Whether our voice goes out along with the sounds, when the microphone transmits; else they replace it. */
|
||||
public final Setting<Boolean> soundboardWithMic = flag("soundboardWithMic", true);
|
||||
|
||||
// ---- notification sounds ----
|
||||
/** Directory name of the active sound pack; empty picks the first one found. */
|
||||
|
||||
@@ -43,7 +43,7 @@ public final class PackSoundPlayer implements SoundPlayer {
|
||||
/** A decoded, 48 kHz mono sound being rendered. */
|
||||
private static final class Voice {
|
||||
final float[] pcm;
|
||||
final double volume;
|
||||
volatile double volume;
|
||||
int position;
|
||||
|
||||
Voice(float[] pcm, double volume) {
|
||||
@@ -94,12 +94,20 @@ public final class PackSoundPlayer implements SoundPlayer {
|
||||
@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);
|
||||
Playing handle = new Playing() {
|
||||
@Override
|
||||
public void stop() {
|
||||
synchronized (lock) {
|
||||
voices.remove(voice);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVolume(double volume) {
|
||||
voice.volume = Math.max(0.0, Math.min(1.0, volume));
|
||||
}
|
||||
};
|
||||
if (pcm.length == 0 || volume <= 0) return handle;
|
||||
if (pcm.length == 0) return handle;
|
||||
synchronized (lock) {
|
||||
if (!running) return handle;
|
||||
voices.add(voice);
|
||||
|
||||
@@ -19,6 +19,9 @@ public interface SoundPlayer {
|
||||
/** A sound started with {@link #play(float[], double)}. */
|
||||
interface Playing {
|
||||
void stop();
|
||||
|
||||
/** @param volume linear gain, 0..1 */
|
||||
void setVolume(double volume);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,7 +30,14 @@ public interface SoundPlayer {
|
||||
* @param volume linear gain, 0..1
|
||||
*/
|
||||
default Playing play(float[] pcm, double volume) {
|
||||
return () -> {
|
||||
return new Playing() {
|
||||
@Override
|
||||
public void stop() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVolume(double volume) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,14 +2,17 @@ package eu.lixko.ts3j.client.soundboard;
|
||||
|
||||
import eu.lixko.ts3j.client.audio.ClipMixer;
|
||||
import eu.lixko.ts3j.client.audio.opus.OpusCodec;
|
||||
import eu.lixko.ts3j.client.config.Setting;
|
||||
import eu.lixko.ts3j.client.config.Settings;
|
||||
import eu.lixko.ts3j.client.sound.SoundFiles;
|
||||
import eu.lixko.ts3j.client.sound.SoundPlayer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -36,7 +39,7 @@ public final class Soundboard {
|
||||
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 Map<SoundboardSound, List<SoundPlayer.Playing>> local = new HashMap<>();
|
||||
|
||||
private final ExecutorService decoder = Executors.newSingleThreadExecutor(r -> {
|
||||
Thread t = new Thread(r, "soundboard-decoder");
|
||||
@@ -52,10 +55,13 @@ public final class Soundboard {
|
||||
this.speakers = speakers;
|
||||
this.settings = settings;
|
||||
mixer.addListener(this::forgetEnded);
|
||||
Setting.onAnyChange(this::updateVolumes,
|
||||
settings.soundboardVolume, settings.soundboardLocalVolume, settings.masterVolume);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts {@code sound}, from the top if it is playing already.
|
||||
* Starts {@code sound} the way {@link Settings#soundboardMode} says: alone, from the top, or as
|
||||
* another copy over the ones playing.
|
||||
*
|
||||
* @return completes once it plays; fails with a {@link SoundboardException} when no server
|
||||
* takes our voice or the file cannot be read
|
||||
@@ -63,8 +69,6 @@ public final class Soundboard {
|
||||
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 {
|
||||
@@ -72,12 +76,20 @@ public final class Soundboard {
|
||||
} 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));
|
||||
switch (settings.soundboardMode.get()) {
|
||||
case SINGLE -> {
|
||||
stopAll();
|
||||
mixer.play(sound, pcm, sent(sound));
|
||||
}
|
||||
case ONCE_EACH -> {
|
||||
stopLocal(sound);
|
||||
mixer.play(sound, pcm, sent(sound));
|
||||
}
|
||||
case MAYHEM -> mixer.add(sound, pcm, sent(sound));
|
||||
}
|
||||
// Played even when silent, so turning the local volume up mid-sound is heard.
|
||||
synchronized (local) {
|
||||
local.computeIfAbsent(sound, s -> new ArrayList<>()).add(speakers.play(pcm, heard(sound)));
|
||||
}
|
||||
}, decoder);
|
||||
}
|
||||
@@ -90,11 +102,19 @@ public final class Soundboard {
|
||||
public void stopAll() {
|
||||
mixer.stopAll();
|
||||
synchronized (local) {
|
||||
local.values().forEach(SoundPlayer.Playing::stop);
|
||||
local.values().forEach(copies -> copies.forEach(SoundPlayer.Playing::stop));
|
||||
local.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/** Applies a changed {@link SoundboardSound#volume} to the sound if it is playing. */
|
||||
public void updateVolume(SoundboardSound sound) {
|
||||
mixer.setGain(sound, sent(sound));
|
||||
synchronized (local) {
|
||||
for (SoundPlayer.Playing copy : local.getOrDefault(sound, List.of())) copy.setVolume(heard(sound));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isPlaying(SoundboardSound sound) {
|
||||
return mixer.isPlaying(sound);
|
||||
}
|
||||
@@ -117,10 +137,27 @@ public final class Soundboard {
|
||||
decoder.shutdownNow();
|
||||
}
|
||||
|
||||
private double sent(SoundboardSound sound) {
|
||||
return sound.volume * settings.soundboardVolume.get();
|
||||
}
|
||||
|
||||
private double heard(SoundboardSound sound) {
|
||||
return Math.min(1.0, sound.volume * settings.soundboardLocalVolume.get() * settings.masterVolume.get());
|
||||
}
|
||||
|
||||
/** The local copies stand for exactly the sounds playing, so they name every sound to update. */
|
||||
private void updateVolumes() {
|
||||
List<SoundboardSound> playing;
|
||||
synchronized (local) {
|
||||
playing = new ArrayList<>(local.keySet());
|
||||
}
|
||||
for (SoundboardSound sound : playing) updateVolume(sound);
|
||||
}
|
||||
|
||||
private void stopLocal(SoundboardSound sound) {
|
||||
synchronized (local) {
|
||||
SoundPlayer.Playing playing = local.remove(sound);
|
||||
if (playing != null) playing.stop();
|
||||
List<SoundPlayer.Playing> copies = local.remove(sound);
|
||||
if (copies != null) copies.forEach(SoundPlayer.Playing::stop);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +166,7 @@ public final class Soundboard {
|
||||
synchronized (local) {
|
||||
local.entrySet().removeIf(e -> {
|
||||
if (mixer.isPlaying(e.getKey())) return false;
|
||||
e.getValue().stop();
|
||||
e.getValue().forEach(SoundPlayer.Playing::stop);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user