From 86cbc5e7badec1ba562a5dfe2d3cf6a3bd0b55cf Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 31 Jan 2024 11:57:52 +0900 Subject: [PATCH] Fix volume handling of battle/silly music --- Changelog.md | 3 +++ .../TurnBasedMinecraft/client/BattleMusic.java | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Changelog.md b/Changelog.md index 5f4eca2..67360eb 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,8 @@ # Upcoming changes +Fix volume handling of battle/silly music. (Previous implementation did not +properly reduce volume based on Minecraft's "music volume" setting.) + # Version NeoForge-1.25.2 Fix invalid use of throwable potions. (Previously, the Player would "drink" diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/client/BattleMusic.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/client/BattleMusic.java index bccb282..601f839 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/client/BattleMusic.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/client/BattleMusic.java @@ -259,7 +259,7 @@ public class BattleMusic // set volume FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); - gainControl.setValue(volume * 20.0f - 20.0f); // in decibels + gainControl.setValue(BattleMusic.percentageToDecibels(volume)); clip.loop(Clip.LOOP_CONTINUOUSLY); clip.start(); @@ -387,6 +387,17 @@ public class BattleMusic return !sillyMusic.isEmpty(); } + /// Percentage must be between 0 and 1. + public static float percentageToDecibels(float percentage) { + if (percentage > 1.0F) { + return 0.0F; + } else if (percentage <= 0.0F) { + return Float.NEGATIVE_INFINITY; + } else { + return (float) (Math.log10(percentage) * 20.0); + } + } + private class MP3Streamer implements Runnable { private AtomicBoolean keepPlaying; @@ -438,7 +449,7 @@ public class BattleMusic sdl.open(audioFormat); { FloatControl volumeControl = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN); - volumeControl.setValue(volume * 20.0f - 20.0f); // in decibels + volumeControl.setValue(BattleMusic.percentageToDecibels(volume)); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -536,7 +547,7 @@ public class BattleMusic sdl.open(audioFormat); { FloatControl volumeControl = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN); - volumeControl.setValue(volume * 20.0f - 20.0f); // in decibels + volumeControl.setValue(BattleMusic.percentageToDecibels(volume)); } AudioInputStream ais = reader.getAudioInputStream(oggVorbisFile);