]> git.seodisparate.com - TurnBasedMinecraftMod/commitdiff
Fix volume handling of battle/silly music
authorStephen Seo <seo.disparate@gmail.com>
Wed, 31 Jan 2024 02:57:52 +0000 (11:57 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 31 Jan 2024 02:57:52 +0000 (11:57 +0900)
Changelog.md
src/main/java/com/burnedkirby/TurnBasedMinecraft/client/BattleMusic.java

index 5f4eca22024b18e707c0d7caa6bbd20eb805d719..67360ebaf2feaf26c372d41f105b3960fe60d145 100644 (file)
@@ -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"
index bccb282c64d92a628d737057d4488adebc94d019..601f8397048e9346d2180aa24c50276eeabde296 100644 (file)
@@ -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);