From 676f71e313306cdeb2448fce90082fbc4bd5e577 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 21 Sep 2018 16:10:00 +0900 Subject: [PATCH] Add volume control to battle music Battle music volume is set based on the music volume slider in the Minecraft settings. The default Midi device provided in Java does not appear to support changing the volume of playing midi file after some testing. For now, only ".wav" files obey the Music volume slider settings. --- .../client/BattleMusic.java | 34 ++++++++++++++----- .../client/ClientProxy.java | 5 +-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleMusic.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleMusic.java index e4da0c8..8834a40 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleMusic.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleMusic.java @@ -10,6 +10,7 @@ import javax.sound.midi.MidiSystem; import javax.sound.midi.Sequencer; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; +import javax.sound.sampled.FloatControl; import javax.sound.sampled.LineUnavailableException; import org.apache.logging.log4j.Logger; @@ -119,10 +120,10 @@ public class BattleMusic sillyMusic.add(f); } + initialized = true; + pickNextBattle(); pickNextSilly(); - - initialized = true; } private void pickNextBattle() @@ -145,32 +146,41 @@ public class BattleMusic nextSilly = sillyMusic.get((int)(Math.random() * sillyMusic.size())); } - public void playBattle() + public void playBattle(float volume) { - if(!initialized) + if(!initialized || volume <= 0.0f) { return; } - play(nextBattle); + else if(volume > 1.0f) + { + volume = 1.0f; + } + play(nextBattle, volume); pickNextBattle(); playingIsSilly = false; } - public void playSilly() + public void playSilly(float volume) { - if(!initialized) + if(!initialized || volume <= 0.0f) { return; } - play(nextSilly); + else if(volume > 1.0f) + { + volume = 1.0f; + } + play(nextSilly, volume); pickNextSilly(); playingIsSilly = true; } - private void play(File next) + private void play(File next, float volume) { if(initialized && next != null) { + logger.debug("play called with file " + next.getName() + " and vol " + volume); Minecraft.getMinecraft().addScheduledTask(() -> { Minecraft.getMinecraft().getSoundHandler().pauseSounds(); }); @@ -193,6 +203,7 @@ public class BattleMusic logger.error("Failed to play battle music (midi)"); return; } + sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY); sequencer.start(); } @@ -215,6 +226,11 @@ public class BattleMusic logger.error("Failed to load battle music (wav)"); return; } + + // set volume + FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); + gainControl.setValue(volume * 20.0f - 20.0f); // in decibels + clip.loop(Clip.LOOP_CONTINUOUSLY); clip.start(); } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ClientProxy.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ClientProxy.java index 7c63079..84eb76f 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ClientProxy.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ClientProxy.java @@ -8,6 +8,7 @@ import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod; import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; +import net.minecraft.util.SoundCategory; import net.minecraft.util.text.TextComponentString; public class ClientProxy extends CommonProxy @@ -92,13 +93,13 @@ public class ClientProxy extends CommonProxy @Override public void playBattleMusic() { - battleMusic.playBattle(); + battleMusic.playBattle(Minecraft.getMinecraft().gameSettings.getSoundLevel(SoundCategory.MUSIC)); } @Override public void playSillyMusic() { - battleMusic.playSilly(); + battleMusic.playSilly(Minecraft.getMinecraft().gameSettings.getSoundLevel(SoundCategory.MUSIC)); } @Override