Hm… I hadn’t problems like that.
F5-F7 kicks a sample in (if you hold the button they are repeated damn fast).
Samples are: 8bit/mono/rate 11025
other keys:
F1-F4 = zoomfactor
r = restart
F9 = showfps on/off
F10 = fps capping on/off
F12 = showtime on/off (showfps has to be turned off, because they are overlapping)
Btw if capping is disabled the key response gets totally laggy.
The SoundManager class is a bit simpler there (older heh):
import java.io.*;
import java.net.URL;
import java.util.*;
import javax.sound.sampled.*;
public class SoundManager
{
private javax.sound.sampled.Line.Info lineInfo;
private Vector afs;
private Vector sizes;
private Vector infos;
private Vector audios;
private int num=0;
public SoundManager()
{
afs=new Vector();
sizes=new Vector();
infos=new Vector();
audios=new Vector();
}
public void addClip(String s)
throws IOException, UnsupportedAudioFileException, LineUnavailableException
{
URL url = getClass().getResource(s);
//InputStream inputstream = url.openStream();
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(loadStream(url.openStream()));
AudioFormat af = audioInputStream.getFormat();
int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
byte[] audio = new byte[size];
DataLine.Info info = new DataLine.Info(Clip.class, af, size);
audioInputStream.read(audio, 0, size);
afs.add(af);
sizes.add(new Integer(size));
infos.add(info);
audios.add(audio);
num++;
}
private ByteArrayInputStream loadStream(InputStream inputstream)
throws IOException
{
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
byte data[] = new byte[1024];
for(int i = inputstream.read(data); i != -1; i = inputstream.read(data))
bytearrayoutputstream.write(data, 0, i);
inputstream.close();
bytearrayoutputstream.close();
data = bytearrayoutputstream.toByteArray();
return new ByteArrayInputStream(data);
}
public void playSound(int x)
throws UnsupportedAudioFileException, LineUnavailableException
{
if(x>num)
{
System.out.println("playSound: sample nr["+x+"] is not available");
}
else
{
Clip clip = (Clip) AudioSystem.getLine((DataLine.Info)infos.elementAt(x));
clip.open((AudioFormat)afs.elementAt(x), (byte[])audios.elementAt(x), 0, ((Integer)sizes.elementAt(x)).intValue());
clip.start();
}
}
}
If there are ~10 samples overlapping played at the same time the framerate goes also down by ~50%. But my machine is rather old (500mhz) and the framerate is approximative capped at somewhat over 60fps (in 5msec sleep steps - with my pc sleep is set to 15msec).
So it wouldn’t be a problem with better timing and a maximum of let’s say 8 sounds at the same time - even with a pc crappy as mine
Btw it’s a bit nicer if addClip returns the index number of the clip.
int blamSnd=sou.addClip("/sfx/blam.wav");
Easy to add