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:
2026-10-30 00:21:50 +00:00
co-authored by Claude Opus 5.5
parent b33f1fd25a
commit 42870a702c
11 changed files with 356 additions and 59 deletions
+4 -2
View File
@@ -59,8 +59,10 @@ Android app in [`../android`](../android) does.
- **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,
Click a tile to play it: one sound at a time, each once (a click restarts it), or mayhem
(every click layers another copy). Your voice is mixed in, or left out while a sound
plays. Drop files on the window to add them. Per-sound and global volume, plus a local
volume for hearing them yourself, all applied to sounds already playing. Hotkeys: Play Sound,
Play/Stop Sound, Stop All Sounds. Saved in `~/.ts3jclient/soundboard.properties`.
### Server interaction
@@ -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&nbsp;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);
double gain = c.gain;
if (gain > 0) {
for (int i = 0; i < n; i++) {
float s = (float) (c.pcm[c.position + i] * c.gain);
float s = (float) (c.pcm[c.position + i] * gain);
for (int ch = 0; ch < channels; ch++) frame[i * channels + ch] += s;
}
c.position += n;
mixed = true;
}
c.position += n;
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 = () -> {
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;
});
}
@@ -51,6 +51,7 @@ class SoundboardTest {
private final Settings settings = new Settings();
private final SilentMic mic = new SilentMic();
private final ClipMixer mixer = new ClipMixer();
private final Speakers speakers = new Speakers();
private CaptureVoiceInput input;
private Soundboard soundboard;
private float[] tone;
@@ -64,15 +65,7 @@ class SoundboardTest {
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);
soundboard = new Soundboard(mixer, () -> true, opus, speakers, settings);
tone = new float[RATE * 3 / 4 + 77];
for (int i = 0; i < tone.length; i++) {
@@ -124,6 +117,132 @@ class SoundboardTest {
assertFalse(soundboard.isPlaying());
}
@Test
void volumeChangesReachAPlayingSound() throws Exception {
SoundboardSound sound = new SoundboardSound("tone", writeWave(new File(dir, "tone.wav"), tone).getAbsolutePath());
soundboard.play(sound).get(5, TimeUnit.SECONDS);
List<byte[]> packets = new ArrayList<>();
step(packets, 15);
settings.soundboardVolume.set(0.25);
step(packets, 15);
float[] decoded = decode(packets);
double ratio = rms(decoded, 20, 30) / rms(decoded, 5, 15);
assertEquals(0.25, ratio, 0.03, "sent level after turning the soundboard down");
assertEquals(0.0, speakers.volume, 1e-9);
settings.soundboardLocalVolume.set(0.5);
assertEquals(0.5 * settings.masterVolume.get(), speakers.volume, 1e-9);
sound.volume = 0.5;
soundboard.updateVolume(sound);
assertEquals(0.25 * settings.masterVolume.get(), speakers.volume, 1e-9);
settings.soundboardVolume.set(0.0);
step(packets, 5);
assertTrue(soundboard.isPlaying(sound), "still playing, silently");
assertFalse(input.isReady(), "a silenced sound does not key the microphone");
}
@Test
void voiceGoesOutWithTheSoundOnlyWhenMixedIn() throws Exception {
// A transmitting microphone hearing a steady 1 kHz voice.
input.stop();
mic.voice = true;
settings.inputMode.set(Settings.InputMode.CONTINUOUS);
settings.denoise.set(false);
input = new CaptureVoiceInput(settings, mic, opus);
input.setClips(mixer);
input.start();
mic.awaitFrameDone();
SoundboardSound sound = new SoundboardSound("tone", writeWave(new File(dir, "tone.wav"), tone).getAbsolutePath());
double with = sentCorrelation(sound);
settings.soundboardWithMic.set(false);
double without = sentCorrelation(sound);
assertTrue(with < 0.85, "the voice is mixed in, correlation " + with);
assertTrue(without > 0.95, "the sound alone, correlation " + without);
}
@Test
void oneAtATimeStopsTheOthers() throws Exception {
settings.soundboardMode.set(Settings.SoundboardMode.SINGLE);
SoundboardSound a = sound("a"), b = sound("b");
soundboard.play(a).get(5, TimeUnit.SECONDS);
soundboard.play(b).get(5, TimeUnit.SECONDS);
assertFalse(soundboard.isPlaying(a));
assertTrue(soundboard.isPlaying(b));
}
@Test
void eachOnceRestartsTheSoundAndLeavesTheOthers() throws Exception {
settings.soundboardMode.set(Settings.SoundboardMode.ONCE_EACH);
SoundboardSound a = sound("a"), b = sound("b");
soundboard.play(a).get(5, TimeUnit.SECONDS);
soundboard.play(b).get(5, TimeUnit.SECONDS);
soundboard.play(a).get(5, TimeUnit.SECONDS);
assertTrue(soundboard.isPlaying(a));
assertTrue(soundboard.isPlaying(b));
assertEquals(2, levelOfOneFrame() / singleLevel(), 0.01, "a and b, a only once");
}
@Test
void mayhemLayersTheSameSound() throws Exception {
settings.soundboardMode.set(Settings.SoundboardMode.MAYHEM);
SoundboardSound a = sound("a");
for (int i = 0; i < 2; i++) soundboard.play(a).get(5, TimeUnit.SECONDS);
assertEquals(2, levelOfOneFrame() / singleLevel(), 0.01, "two copies of a");
soundboard.stop(a);
assertFalse(soundboard.isPlaying());
}
private SoundboardSound sound(String name) throws IOException {
return new SoundboardSound(name, writeWave(new File(dir, name + ".wav"), tone).getAbsolutePath());
}
/** The peak of the tone's frame at 100 ms, alone. */
private double singleLevel() {
float[] pcm = resample(tone);
double peak = 0;
for (int i = 0; i < VoiceFormat.FRAME_SIZE; i++) peak = Math.max(peak, Math.abs(pcm[i + 4800]));
return peak;
}
/** Skips the mixer to 100 ms in and measures the peak of the frame there. */
private double levelOfOneFrame() {
float[] frame = new float[VoiceFormat.FRAME_SIZE];
for (int i = 0; i < 5; i++) mixer.mixInto(new float[VoiceFormat.FRAME_SIZE], VoiceFormat.FRAME_SIZE, 1);
mixer.mixInto(frame, VoiceFormat.FRAME_SIZE, 1);
double peak = 0;
for (float f : frame) peak = Math.max(peak, Math.abs(f));
return peak;
}
/** Plays the sound to its end and correlates what was sent with it. */
private double sentCorrelation(SoundboardSound sound) throws Exception {
soundboard.play(sound).get(5, TimeUnit.SECONDS);
List<byte[]> packets = new ArrayList<>();
while (soundboard.isPlaying(sound)) step(packets, 1);
float[] expected = resample(tone);
float[] decoded = decode(packets);
double best = 0;
for (int lag = 0; lag < 2400; lag++) best = Math.max(best, correlation(expected, decoded, lag));
return best;
}
private void step(List<byte[]> packets, int frames) throws InterruptedException {
for (int i = 0; i < frames; i++) {
mic.captureFrame();
byte[] p;
while ((p = input.provide()).length > 0) packets.add(p);
}
}
private static double rms(float[] pcm, int fromFrame, int toFrame) {
double sum = 0;
int from = fromFrame * VoiceFormat.FRAME_SIZE, to = toFrame * VoiceFormat.FRAME_SIZE;
for (int i = from; i < to; i++) sum += pcm[i] * pcm[i];
return Math.sqrt(sum / (to - from));
}
private void assertSent(SoundboardSound sound) throws Exception {
List<Boolean> talk = new ArrayList<>();
input.setTalkListener(talk::add);
@@ -204,11 +323,43 @@ class SoundboardTest {
return file;
}
/** Our own speakers, remembering the volume of the last sound played on them. */
private static final class Speakers implements SoundPlayer {
volatile double volume = -1;
@Override
public void play(File file, double volume) {
}
@Override
public Playing play(float[] pcm, double volume) {
this.volume = volume;
return new Playing() {
@Override
public void stop() {
}
@Override
public void setVolume(double v) {
Speakers.this.volume = v;
}
};
}
@Override
public void shutdown() {
}
}
/** A mono microphone that captures silence one frame at a time, when the test says so. */
private static final class SilentMic implements AudioIo {
private final LinkedBlockingQueue<Boolean> frames = new LinkedBlockingQueue<>();
private final Semaphore done = new Semaphore(0);
/** Hears a 1 kHz voice rather than silence. */
volatile boolean voice;
private long sample;
/** Lets one frame through and waits until the capture loop has dealt with it. */
void captureFrame() throws InterruptedException {
@@ -252,6 +403,11 @@ class SoundboardTest {
while (open) {
if (frames.poll(50, TimeUnit.MILLISECONDS) != null) {
Arrays.fill(buffer, offset, offset + length, (byte) 0);
for (int i = offset; voice && i + 1 < offset + length; i += 2, sample++) {
short s = (short) (0.3 * 32767 * Math.sin(2 * Math.PI * 1000 * sample / 48_000));
buffer[i] = (byte) s;
buffer[i + 1] = (byte) (s >> 8);
}
return length;
}
}
@@ -139,7 +139,7 @@ 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"));
JMenuItem soundboard = new JMenuItem("Soundboard", Icons.ofAny("SOUNDPACK", "MUSIC"));
soundboard.addActionListener(e -> commands.showSoundboard());
self.add(awayMenu);
self.add(activateItem);
@@ -111,7 +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");
add("soundboard", Category.SELF, "Soundboard", commands::showSoundboard, "SOUNDPACK", "MUSIC");
}
private void addDialogs() {
@@ -17,7 +17,10 @@ import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
@@ -56,8 +59,9 @@ 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.
* The soundboard: a grid of sounds, each played onto the air with a click, as the chosen
* {@link Settings.SoundboardMode} has it. Files dropped on it are added; the context menu
* stops, edits, reorders and removes.
*/
final class SoundboardWindow extends JDialog {
@@ -106,7 +110,7 @@ final class SoundboardWindow extends JDialog {
if (e.isPopupTrigger()) {
tileMenu(model.get(index)).show(grid, e.getX(), e.getY());
} else if (SwingUtilities.isLeftMouseButton(e)) {
toggle(model.get(index));
play.accept(model.get(index));
}
}
@@ -118,7 +122,7 @@ final class SoundboardWindow extends JDialog {
else emptyMenu().show(grid, e.getX(), e.getY());
}
});
bindKey("ENTER", "play", () -> selected(this::toggle));
bindKey("ENTER", "play", () -> selected(play));
bindKey("DELETE", "remove", () -> selected(this::remove));
bindKey("F2", "edit", () -> selected(this::edit));
@@ -138,12 +142,41 @@ final class SoundboardWindow extends JDialog {
buttons.add(new JLabel("Local"));
buttons.add(volumeSlider(settings.soundboardLocalVolume, 100));
JComboBox<Settings.SoundboardMode> mode = new JComboBox<>(Settings.SoundboardMode.values());
mode.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean selected, boolean focused) {
return super.getListCellRendererComponent(list, label((Settings.SoundboardMode) value),
index, selected, focused);
}
});
mode.setSelectedItem(settings.soundboardMode.get());
mode.addActionListener(e -> {
settings.soundboardMode.set((Settings.SoundboardMode) mode.getSelectedItem());
settings.save();
});
JCheckBox withMic = new JCheckBox("Mix in microphone", settings.soundboardWithMic.get());
withMic.addActionListener(e -> {
settings.soundboardWithMic.set(withMic.isSelected());
settings.save();
});
JPanel options = new JPanel(new FlowLayout(FlowLayout.LEFT, UIScale.scale(6), 0));
options.add(new JLabel("Mode"));
options.add(mode);
options.add(Box.createHorizontalStrut(UIScale.scale(12)));
options.add(withMic);
status.setForeground(Theme.blocked());
JPanel south = new JPanel();
south.setLayout(new BoxLayout(south, BoxLayout.Y_AXIS));
buttons.setAlignmentX(LEFT_ALIGNMENT);
options.setAlignmentX(LEFT_ALIGNMENT);
status.setAlignmentX(LEFT_ALIGNMENT);
south.add(buttons);
south.add(Box.createVerticalStrut(UIScale.scale(6)));
south.add(options);
south.add(Box.createVerticalStrut(UIScale.scale(4)));
south.add(status);
@@ -173,11 +206,6 @@ final class SoundboardWindow extends JDialog {
// ---- 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();
@@ -215,6 +243,7 @@ final class SoundboardWindow extends JDialog {
private void edit(SoundboardSound sound) {
if (!SoundEditDialog.edit(this, sound)) return;
soundboard.updateVolume(sound);
sounds.save();
grid.repaint();
}
@@ -240,8 +269,10 @@ final class SoundboardWindow extends JDialog {
private JPopupMenu tileMenu(SoundboardSound sound) {
JPopupMenu menu = new JPopupMenu();
boolean playing = soundboard.isPlaying(sound);
menu.add(item(playing ? "Stop" : "Play", () -> toggle(sound)));
menu.add(item("Play", () -> play.accept(sound)));
JMenuItem stop = item("Stop", () -> soundboard.stop(sound));
stop.setEnabled(soundboard.isPlaying(sound));
menu.add(stop);
menu.addSeparator();
menu.add(item("Edit…", () -> edit(sound)));
JMenuItem left = item("Move left", () -> move(sound, -1));
@@ -283,6 +314,14 @@ final class SoundboardWindow extends JDialog {
});
}
private static String label(Settings.SoundboardMode mode) {
return switch (mode) {
case SINGLE -> "One at a time";
case ONCE_EACH -> "Each once";
case MAYHEM -> "Mayhem";
};
}
private static JMenuItem item(String label, Runnable action) {
JMenuItem item = new JMenuItem(label);
item.addActionListener(e -> action.run());