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.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();
}

View file

@ -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