I'm developing a game in Java, and have made an attempt at coding a simple sound system for it made of two class files: one that handles them, and one for each sound file loaded.
My problem is that every once in a while I get a huge lagspike which means a complete stop for like a second or two, really interrupting gameplay. I'm suspecting Garbage control but I'm not entirely sure. The only thing I know for a fact is that the source of the problem lies in the sounds.
Here's my SoundHandler.java:
(The loading of sounds is unimportant, as far as I know it should be unrelated to the problem)
package Classes;
import java.io.*;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.FloatControl;
public class SoundHandler {
Sound Sounds[] = new Sound[1];
public SoundHandler()
{
if(!LoadSounds("Sounds.cfg"))
System.out.print("Failiure upon loading sounds!");
}
public boolean LoadSounds(String FileName)
{
String line = "";
String SoundName = "";
String SoundFile = "";
String[] token3 = new String[10];
boolean EndOfFile = false;
int LineCount = 0;
BufferedReader characterfile = null;
try
{
characterfile = new BufferedReader(new FileReader(GameMain.GameFolder+"Data/"+FileName));
}
catch(FileNotFoundException fileex)
{
System.out.println(FileName+": file not found.");
return false;
}
while(!EndOfFile && line != null)
{
try
{
line = characterfile.readLine();
if(line != null)
{
if(line.indexOf("//") == -1 && !line.equals(""))
LineCount++;
}
}
catch(IOException ioexception)
{
if(LineCount == 0)
{
System.out.println(FileName+": error loading file.");
return false;
}
EndOfFile = true;
}
}
try { characterfile.close(); } catch(IOException ioexception) { characterfile = null; }
Sounds = new Sound[LineCount];
EndOfFile = false;
LineCount = 0;
try
{
characterfile = new BufferedReader(new FileReader(GameMain.GameFolder+"Data/"+FileName));
}
catch(FileNotFoundException fileex)
{
System.out.println(FileName+": file not found.");
return false;
}
try
{
line = characterfile.readLine();
if(line != null)
if(line.indexOf("//") == -1 && !line.equals(""))
LineCount++;
}
catch(IOException ioexception) { }
while(EndOfFile == false && line != null) {
if(line.indexOf("//") == -1 && !line.equals(""))
{
line = line.trim();
line = line.replaceAll("\t\t", "\t");
line = line.replaceAll("\t\t", "\t");
line = line.replaceAll("\t\t", "\t");
line = line.replaceAll("\t\t", "\t");
int Spot = line.indexOf("\t");
SoundName = line.substring(0,Spot);
SoundFile = line.substring(Spot+1);
Sounds[LineCount-1] = new Sound(SoundName,SoundFile);
}
try {
line = characterfile.readLine();
if(line != null)
if(line.indexOf("//") == -1 && !line.equals(""))
LineCount++;
} catch(IOException ioexception1) { EndOfFile = true; }
}
try { characterfile.close(); } catch(IOException ioexception) { }
return true;
}
public File GetSoundFile(String Name)
{
File result = null;
for(int i = 0; i < Sounds.length; i++)
if(Sounds[i].Name.equals(Name))
result = Sounds[i].File;
return result;
}
public Sound GetSound(String Name)
{
Sound result = null;
for(int i = 0; i < Sounds.length; i++)
if(Sounds[i].Name.equals(Name))
result = Sounds[i];
return result;
}
public int GetSoundID(String Name)
{
int result = 0;
for(int i = 0; i < Sounds.length; i++)
if(Sounds[i].Name.equals(Name))
result = i;
return result;
}
Double MasterVolume = 0.75;
public float CalcVolume(double Vol)
{
float result = 0f;
result = -40.0f + (float)(MasterVolume*Vol*40);
if(result < -40.0f)
result = -40.0f;
if(result > 0.0f)
result = 0.0f;
return result;
}
public boolean PlaySound(String SoundName, double Vol)
{
int ID = GetSoundID(SoundName);
try
{
Clip CurrentClip;
Sounds[ID].Reset(false);
CurrentClip = (Clip) AudioSystem.getLine(Sounds[ID].info);
CurrentClip.addLineListener(new LineListener() {
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
event.getLine().close();
}
}
});
CurrentClip.open(Sounds[ID].sound);
FloatControl Volume;
Volume = (FloatControl) CurrentClip.getControl(FloatControl.Type.MASTER_GAIN);
Volume.setValue(CalcVolume(Vol));
CurrentClip.start();
}
catch(LineUnavailableException e) { e.printStackTrace(); }
catch(IOException e) { e.printStackTrace(); }
return true;
}
}
Here's my Sound.java:
package Classes;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
public class Sound {
public String Name = "";
public File File = null;
AudioInputStream sound;
DataLine.Info info;
Clip clip;
public Sound(String iName, String FileName)
{
Name = iName;
File = new File(GameMain.GameFolder+"Sound/"+FileName+".wav");
Reset(true);
}
public void Reset(boolean init)
{
try
{
if(!init)
sound.close();
sound = AudioSystem.getAudioInputStream(File);
info = new DataLine.Info(Clip.class, sound.getFormat());
}
catch(IOException e) { e.printStackTrace(); }
catch(UnsupportedAudioFileException e) { e.printStackTrace(); }
}
}
I've tried searching around for a general way to implement sounds in a game, most I've encountered didn't feature multiple instances of the same sound file running at once, this is what I have so far. I'm sure there are a few tiny inefficiencies especially in the loading code, but there's got to be some leak I'm missing. Any tips for custom GC usage are also welcome. Thanks in advance.