Added Sounds files

Added Classes Sound/SoundManager
This commit is contained in:
James Munson
2019-06-12 01:44:52 -07:00
parent bce942e50a
commit ba83bf11e9
20 changed files with 148 additions and 0 deletions

View File

@@ -283,4 +283,14 @@ public interface RuneLiteConfig extends Config
{
return 35;
}
@Range(max=100, min=0)
@ConfigItem(
keyName="volume",
name="Runelite Volume",
description="Sets the volume of custom Runelite sounds (not the client sounds)",
position=43
)
default int volume() { return 100; }
}

View File

@@ -0,0 +1,41 @@
package net.runelite.client.game;
public enum Sound
{
FIFTEEN_SECONDS(1, "net/runelite/client/game/sounds/15seconds.wav"),
FIVE_SECONDS(2, "net/runelite/client/game/sounds/5seconds.wav"),
ATTACK_WITH_MAGIC(3, "net/runelite/client/game/sounds/attackmagic.wav"),
ATTACK_WITH_MELEE(4, "net/runelite/client/game/sounds/attackmelee.wav"),
ATTACK_WITH_RANGED(5, "net/runelite/client/game/sounds/attackranged.wav"),
INCOMING(6, "net/runelite/client/game/sounds/incoming.wav"),
MOVE(7, "net/runelite/client/game/sounds/move.wav"),
PRAY_MAGIC(8, "net/runelite/client/game/sounds/praymagic.wav"),
PRAY_MELEE(9, "net/runelite/client/game/sounds/praymelee.wav"),
PRAY_RANGED(10, "net/runelite/client/game/sounds/prayranged.wav"),
REENABLE_PRAYER(11, "net/runelite/client/game/sounds/reenableprayer.wav"),
RUNAWAY(12, "net/runelite/client/game/sounds/runaway.wav"),
LOW_HEATLH(13, "net/runelite/client/game/sounds/lowhealth.wav"),
LOW_PRAYER(14, "net/runelite/client/game/sounds/lowprayer.wav"),
OUT_OF_COMBAT(15, "net/runelite/client/game/sounds/outofcombat.wav"),
RESTORED_SPECIAL_ATTACK(16, "net/runelite/client/game/sounds/restorespec.wav"),
IDLE(17, "net/runelite/client/game/sounds/idle.wav");
private String filePath;
private int id;
private Sound(int id, String filePath)
{
this.id = id;
this.filePath = filePath;
}
public String getFilePath()
{
return this.filePath;
}
public int getId()
{
return this.id;
}
}

View File

@@ -0,0 +1,97 @@
package net.runelite.client.game;
import com.google.inject.Inject;
import java.io.IOException;
import java.net.URL;
import javax.inject.Singleton;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.BooleanControl;
import javax.sound.sampled.Control;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import net.runelite.client.config.RuneLiteConfig;
import net.runelite.client.game.Sound;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class SoundManager
{
private static final Logger log = LoggerFactory.getLogger(SoundManager.class);
private final RuneLiteConfig runeliteConfig;
@Inject
private SoundManager(RuneLiteConfig runeLiteConfig) {
this.runeliteConfig = runeLiteConfig;
}
public void playSound(final Sound sound) throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
AudioInputStream in = AudioSystem.getAudioInputStream(this.getClass().getClassLoader().getResource(sound.getFilePath()));
AudioFormat outFormat = SoundManager.this.getOutFormat(in.getFormat());
DataLine.Info info = new DataLine.Info(SourceDataLine.class, outFormat);
SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info);
if (line != null)
{
line.open(outFormat, 2200);
if (line.isControlSupported(FloatControl.Type.MASTER_GAIN))
{
int volume = SoundManager.this.runeliteConfig.volume();
FloatControl gainControl = (FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN);
BooleanControl muteControl = (BooleanControl)line.getControl(BooleanControl.Type.MUTE);
if (volume == 0)
{
muteControl.setValue(true);
}
else
{
muteControl.setValue(false);
gainControl.setValue((float)(Math.log((double)volume / 100.0) / Math.log(10.0) * 20.0));
}
}
line.start();
SoundManager.this.stream(AudioSystem.getAudioInputStream(outFormat, in), line);
line.drain();
line.stop();
}
}
catch (IOException | LineUnavailableException | UnsupportedAudioFileException e)
{
throw new IllegalStateException(e);
}
}
}).start();
}
private AudioFormat getOutFormat(AudioFormat inFormat)
{
int ch = inFormat.getChannels();
float rate = inFormat.getSampleRate();
return new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
}
private void stream(AudioInputStream in, SourceDataLine line) throws IOException
{
byte[] buffer = new byte[2200];
int n = 0;
while (n != -1)
{
line.write(buffer, 0, n);
n = in.read(buffer, 0, buffer.length);
}
}
}