Skip to content

Commit

Permalink
Update SoundManager to expose setVolume and change volume when settin…
Browse files Browse the repository at this point in the history
…g is changed
  • Loading branch information
sensualcoder committed Aug 2, 2023
1 parent 7973de9 commit a6ceeb8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
3 changes: 3 additions & 0 deletions megamek/src/megamek/client/ui/swing/ClientGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
public interface AudioService {
void loadSoundFiles();
void playSound(SoundType id);
void setVolume();
}
10 changes: 7 additions & 3 deletions megamek/src/megamek/client/ui/swing/audio/Sound.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

package megamek.client.ui.swing.audio;

import megamek.common.annotations.Nullable;

import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;

public class Sound
{
private final Clip clip;

public Sound(final Clip clip) {
public Sound(@Nullable final Clip clip) {
this.clip = clip;
}

Expand All @@ -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));
}
}
}
25 changes: 15 additions & 10 deletions megamek/src/megamek/client/ui/swing/audio/SoundManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Sound> sounds = new HashMap<>();
private final List<Sound> sounds = new ArrayList<>();

/**
* Loads the sound files from the paths given in the client settings
Expand All @@ -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
Expand All @@ -84,11 +86,16 @@ public void playSound(SoundType id) {
}

if(sound != null) {
setVolume(sound);
sound.play();
}
}

public void setVolume() {

Check notice

Code scanning / CodeQL

Missing Override annotation Note

This method overrides
AudioService.setVolume
; it is advisable to add an Override annotation.
for (var sound: sounds) {
setVolume(sound);
}
}

private @Nullable Clip loadSoundClip(@Nullable String filename) {
if (filename == null) {
return null;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit a6ceeb8

Please sign in to comment.