From a6ceeb804e4079fdd5527b27ff6e4276bcfa15b9 Mon Sep 17 00:00:00 2001 From: Sensualcoder Date: Wed, 2 Aug 2023 12:11:15 -0500 Subject: [PATCH] Update SoundManager to expose setVolume and change volume when setting is changed --- .../megamek/client/ui/swing/ClientGUI.java | 3 +++ .../client/ui/swing/audio/AudioService.java | 1 + .../megamek/client/ui/swing/audio/Sound.java | 10 +++++--- .../client/ui/swing/audio/SoundManager.java | 25 +++++++++++-------- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/megamek/src/megamek/client/ui/swing/ClientGUI.java b/megamek/src/megamek/client/ui/swing/ClientGUI.java index 8174cd061e0..988c9ef8873 100644 --- a/megamek/src/megamek/client/ui/swing/ClientGUI.java +++ b/megamek/src/megamek/client/ui/swing/ClientGUI.java @@ -349,6 +349,7 @@ public ClientGUI(Client client, MegaMekController c) { panDisplay.add(panMain, BorderLayout.CENTER); panDisplay.add(panSecondary, BorderLayout.SOUTH); add(panDisplay, BorderLayout.CENTER); + audioService.loadSoundFiles(); } @@ -2875,6 +2876,8 @@ public void preferenceChange(PreferenceChangeEvent e) { || (e.getName().equals(GUIPreferences.SOUND_BING_FILENAME_MY_TURN)) || (e.getName().equals(GUIPreferences.SOUND_BING_FILENAME_OTHERS_TURN))) { audioService.loadSoundFiles(); + } else if (e.getName().equals(GUIPreferences.MASTER_VOLUME)) { + audioService.setVolume(); } } } diff --git a/megamek/src/megamek/client/ui/swing/audio/AudioService.java b/megamek/src/megamek/client/ui/swing/audio/AudioService.java index f53fb104bc2..d5de80519d3 100644 --- a/megamek/src/megamek/client/ui/swing/audio/AudioService.java +++ b/megamek/src/megamek/client/ui/swing/audio/AudioService.java @@ -22,4 +22,5 @@ public interface AudioService { void loadSoundFiles(); void playSound(SoundType id); + void setVolume(); } diff --git a/megamek/src/megamek/client/ui/swing/audio/Sound.java b/megamek/src/megamek/client/ui/swing/audio/Sound.java index 9fb3cad3a61..b9db9601e65 100644 --- a/megamek/src/megamek/client/ui/swing/audio/Sound.java +++ b/megamek/src/megamek/client/ui/swing/audio/Sound.java @@ -19,6 +19,8 @@ package megamek.client.ui.swing.audio; +import megamek.common.annotations.Nullable; + import javax.sound.sampled.Clip; import javax.sound.sampled.FloatControl; @@ -26,7 +28,7 @@ public class Sound { private final Clip clip; - public Sound(final Clip clip) { + public Sound(@Nullable final Clip clip) { this.clip = clip; } @@ -42,7 +44,9 @@ public void setVolume(float volume) { throw new IllegalArgumentException("Invalid volume: " + volume); } - FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); - gainControl.setValue(20f * (float) Math.log10(volume)); + if(clip != null) { + FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); + gainControl.setValue(20f * (float) Math.log10(volume)); + } } } diff --git a/megamek/src/megamek/client/ui/swing/audio/SoundManager.java b/megamek/src/megamek/client/ui/swing/audio/SoundManager.java index 6587ab40a2f..efbc7d2e200 100644 --- a/megamek/src/megamek/client/ui/swing/audio/SoundManager.java +++ b/megamek/src/megamek/client/ui/swing/audio/SoundManager.java @@ -28,13 +28,13 @@ import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import java.io.File; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; public class SoundManager implements AudioService { private static final GUIPreferences GUIP = GUIPreferences.getInstance(); - private final Map sounds = new HashMap<>(); + private final List sounds = new ArrayList<>(); /** * Loads the sound files from the paths given in the client settings @@ -49,15 +49,17 @@ public void loadSoundFiles() { final Clip bingClipChat = loadSoundClip(GUIP.getSoundBingFilenameChat()); sound = new Sound(bingClipChat); - sounds.put(0, sound); + sounds.add(sound); final Clip bingClipMyTurn = loadSoundClip(GUIP.getSoundBingFilenameMyTurn()); sound = new Sound(bingClipMyTurn); - sounds.put(1, sound); + sounds.add(sound); final Clip bingClipOthersTurn = loadSoundClip(GUIP.getSoundBingFilenameOthersTurn()); sound = new Sound(bingClipOthersTurn); - sounds.put(2, sound); + sounds.add(sound); + + setVolume(); } @Override @@ -84,11 +86,16 @@ public void playSound(SoundType id) { } if(sound != null) { - setVolume(sound); sound.play(); } } + public void setVolume() { + for (var sound: sounds) { + setVolume(sound); + } + } + private @Nullable Clip loadSoundClip(@Nullable String filename) { if (filename == null) { return null; @@ -116,8 +123,6 @@ public void playSound(SoundType id) { private void setVolume(final Sound sound) { final float volume = GUIP.getMasterVolume() / 100.0f; - if(sound != null) { - sound.setVolume(volume); - } + sound.setVolume(volume); } }