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.
This commit is contained in:
Stephen Seo 2018-09-21 16:10:00 +09:00
parent 8c3bdc0ad1
commit 676f71e313
2 changed files with 28 additions and 11 deletions

View file

@ -10,6 +10,7 @@ import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer; import javax.sound.midi.Sequencer;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip; import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.LineUnavailableException;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -119,10 +120,10 @@ public class BattleMusic
sillyMusic.add(f); sillyMusic.add(f);
} }
initialized = true;
pickNextBattle(); pickNextBattle();
pickNextSilly(); pickNextSilly();
initialized = true;
} }
private void pickNextBattle() private void pickNextBattle()
@ -145,32 +146,41 @@ public class BattleMusic
nextSilly = sillyMusic.get((int)(Math.random() * sillyMusic.size())); nextSilly = sillyMusic.get((int)(Math.random() * sillyMusic.size()));
} }
public void playBattle() public void playBattle(float volume)
{ {
if(!initialized) if(!initialized || volume <= 0.0f)
{ {
return; return;
} }
play(nextBattle); else if(volume > 1.0f)
{
volume = 1.0f;
}
play(nextBattle, volume);
pickNextBattle(); pickNextBattle();
playingIsSilly = false; playingIsSilly = false;
} }
public void playSilly() public void playSilly(float volume)
{ {
if(!initialized) if(!initialized || volume <= 0.0f)
{ {
return; return;
} }
play(nextSilly); else if(volume > 1.0f)
{
volume = 1.0f;
}
play(nextSilly, volume);
pickNextSilly(); pickNextSilly();
playingIsSilly = true; playingIsSilly = true;
} }
private void play(File next) private void play(File next, float volume)
{ {
if(initialized && next != null) if(initialized && next != null)
{ {
logger.debug("play called with file " + next.getName() + " and vol " + volume);
Minecraft.getMinecraft().addScheduledTask(() -> { Minecraft.getMinecraft().addScheduledTask(() -> {
Minecraft.getMinecraft().getSoundHandler().pauseSounds(); Minecraft.getMinecraft().getSoundHandler().pauseSounds();
}); });
@ -193,6 +203,7 @@ public class BattleMusic
logger.error("Failed to play battle music (midi)"); logger.error("Failed to play battle music (midi)");
return; return;
} }
sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY); sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
sequencer.start(); sequencer.start();
} }
@ -215,6 +226,11 @@ public class BattleMusic
logger.error("Failed to load battle music (wav)"); logger.error("Failed to load battle music (wav)");
return; 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.loop(Clip.LOOP_CONTINUOUSLY);
clip.start(); clip.start();
} }

View file

@ -8,6 +8,7 @@ import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentString;
public class ClientProxy extends CommonProxy public class ClientProxy extends CommonProxy
@ -92,13 +93,13 @@ public class ClientProxy extends CommonProxy
@Override @Override
public void playBattleMusic() public void playBattleMusic()
{ {
battleMusic.playBattle(); battleMusic.playBattle(Minecraft.getMinecraft().gameSettings.getSoundLevel(SoundCategory.MUSIC));
} }
@Override @Override
public void playSillyMusic() public void playSillyMusic()
{ {
battleMusic.playSilly(); battleMusic.playSilly(Minecraft.getMinecraft().gameSettings.getSoundLevel(SoundCategory.MUSIC));
} }
@Override @Override