finished fx audio volume

This commit is contained in:
Bas de Jong
2025-10-06 22:27:14 +02:00
parent f525fc7ffc
commit 95c4a2fc8e
18 changed files with 82 additions and 6 deletions

View File

@@ -16,6 +16,10 @@ public class SettingsAsset extends JsonAsset<Settings> {
return getContent().volume;
}
public int getFxVolume() {
return getContent().fxVolume;
}
public Locale getLocale() {
return Locale.forLanguageTag(getContent().locale);
}
@@ -37,6 +41,11 @@ public class SettingsAsset extends JsonAsset<Settings> {
save();
}
public void setFxVolume(int fxVolume) {
getContent().fxVolume = fxVolume;
save();
}
public void setLocale(String locale) {
getContent().locale = locale;
save();

View File

@@ -25,6 +25,7 @@ public class SoundManager {
private final SnowflakeGenerator idGenerator = new SnowflakeGenerator(); // TODO: Don't create a new generator
private double volume = 1.0;
private double fxVolume = 1.0;
public SoundManager() {
// Get all Audio Resources and add them to a list.
@@ -40,7 +41,9 @@ public class SoundManager {
.listen(this::handleStopSound)
.listen(this::handleMusicStart)
.listen(this::handleVolumeChange)
.listen(this::handleFxVolumeChange)
.listen(this::handleGetCurrentVolume)
.listen(this::handleGetCurrentFxVolume)
.listen(AudioEvents.ClickButton.class, _ -> {
try {
playSound("medium-button-click.wav", false);
@@ -68,13 +71,20 @@ public class SoundManager {
this.audioResources.put(audioAsset.getName(), audioAsset.getResource());
}
private double limitVolume(double volume) {
if (volume > 1.0) return 1.0;
else return Math.max(volume, 0.0);
}
private void handleVolumeChange(AudioEvents.ChangeVolume event) {
double newVolume = event.newVolume() / 100;
if (newVolume > 1.0) this.volume = 1.0;
else this.volume = Math.max(newVolume, 0.0);
this.volume = limitVolume(event.newVolume() / 100);
for (MediaPlayer mediaPlayer : this.activeMusic) {
mediaPlayer.setVolume(this.volume);
}
}
private void handleFxVolumeChange(AudioEvents.ChangeFxVolume event) {
this.fxVolume = limitVolume(event.newVolume() / 100);
for (Clip clip : this.activeSoundEffects.values()){
updateClipVolume(clip);
}
@@ -85,14 +95,19 @@ public class SoundManager {
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float min = volumeControl.getMinimum();
float max = volumeControl.getMaximum();
float dB = (float) (Math.log10(Math.max(volume, 0.0001)) * 20.0); // convert linear to dB
float dB = (float) (Math.log10(Math.max(fxVolume, 0.0001)) * 20.0); // convert linear to dB
dB = Math.max(min, Math.min(max, dB));
volumeControl.setValue(dB);
}
}
private void handleGetCurrentVolume(AudioEvents.GetCurrentVolume event) {
new EventFlow().addPostEvent(new AudioEvents.GetCurrentVolumeReponse(volume * 100, event.snowflakeId()))
new EventFlow().addPostEvent(new AudioEvents.GetCurrentVolumeResponse(volume * 100, event.snowflakeId()))
.asyncPostEvent();
}
private void handleGetCurrentFxVolume(AudioEvents.GetCurrentFxVolume event) {
new EventFlow().addPostEvent(new AudioEvents.GetCurrentFxVolumeResponse(fxVolume * 100, event.snowflakeId()))
.asyncPostEvent();
}

View File

@@ -15,6 +15,7 @@ public class AudioEvents extends EventsBase {
public record StartBackgroundMusic() implements EventWithoutSnowflake {}
public record ChangeVolume(double newVolume) implements EventWithoutSnowflake {}
public record ChangeFxVolume(double newVolume) implements EventWithoutSnowflake {}
public record GetCurrentVolume(long snowflakeId) implements EventWithSnowflake {
@Override
public Map<String, Object> result() {
@@ -26,7 +27,7 @@ public class AudioEvents extends EventsBase {
return snowflakeId;
}
}
public record GetCurrentVolumeReponse(double currentVolume, long snowflakeId) implements EventWithSnowflake {
public record GetCurrentVolumeResponse(double currentVolume, long snowflakeId) implements EventWithSnowflake {
@Override
public Map<String, Object> result() {
return Map.of();
@@ -37,5 +38,30 @@ public class AudioEvents extends EventsBase {
return snowflakeId;
}
}
public record GetCurrentFxVolume(long snowflakeId) implements EventWithSnowflake {
@Override
public Map<String, Object> result() {
return Map.of();
}
@Override
public long eventSnowflake() {
return this.snowflakeId;
}
}
public record GetCurrentFxVolumeResponse(double currentVolume, long snowflakeId) implements EventWithSnowflake {
@Override
public Map<String, Object> result() {
return Map.of();
}
@Override
public long eventSnowflake() {
return this.snowflakeId;
}
}
public record ClickButton() implements EventWithoutSnowflake {}
}

View File

@@ -6,4 +6,5 @@ public class Settings {
public String theme = "dark";
public String layoutSize = "medium";
public int volume = 15;
public int fxVolume = 20;
}