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