I’d post this on the officialy lwjgl forums, but after registering, I never got accepted for some reason. ???
I’m noticing that in my game code (which is using OpenAL now), I’m getting a lot of little popping sounds as the level of noise/action goes up. I am playing the same sound repeatedly, with overlapping - does that have something to do with it? Or could it just be my code:
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
import org.lwjgl.test.openal.WaveData;
public class SoundSystem {
private IntBuffer buffer = BufferUtils.createIntBuffer(1);
private IntBuffer source = BufferUtils.createIntBuffer(1);
private FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
private FloatBuffer sourceVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
private FloatBuffer listenerPos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
private FloatBuffer listenerVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
private FloatBuffer listenerOri = BufferUtils.createFloatBuffer(6).put(new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f });
public SoundSystem(String filepass) {
sourcePos.flip();
sourceVel.flip();
listenerPos.flip();
listenerVel.flip();
listenerOri.flip();
if (!(AL.isCreated())) {
try {
AL.create();
} catch (LWJGLException le) {
le.printStackTrace();
return;
}
AL10.alGetError();
}
// Load the sound into the object
if(loadALData(filepass) == AL10.AL_FALSE) {
System.out.println("Could not load file: " + filepass);
return;
}
setListenerValues();
}
int loadALData(String filepass) {
// Put sound into a buffer.
AL10.alGenBuffers(buffer);
if(AL10.alGetError() != AL10.AL_NO_ERROR)
return AL10.AL_FALSE;
WaveData waveFile = WaveData.create(filepass);
AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data, waveFile.samplerate);
waveFile.dispose();
// Bind the buffer with the source.
AL10.alGenSources(source);
if(AL10.alGetError() != AL10.AL_NO_ERROR)
return AL10.AL_FALSE;
AL10.alSourcei(source.get(0), AL10.AL_BUFFER, buffer.get(0) );
AL10.alSourcef(source.get(0), AL10.AL_PITCH, 1.0f );
AL10.alSourcef(source.get(0), AL10.AL_GAIN, 1.0f );
AL10.alSource (source.get(0), AL10.AL_POSITION, sourcePos );
AL10.alSource (source.get(0), AL10.AL_VELOCITY, sourceVel );
// Do another error check and return.
if(AL10.alGetError() == AL10.AL_NO_ERROR)
return AL10.AL_TRUE;
return AL10.AL_FALSE;
}
void setListenerValues() {
AL10.alListener(AL10.AL_POSITION, listenerPos);
AL10.alListener(AL10.AL_VELOCITY, listenerVel);
AL10.alListener(AL10.AL_ORIENTATION, listenerOri);
}
void killALData() {
AL10.alDeleteSources(source);
AL10.alDeleteBuffers(buffer);
}
public void play() {
AL10.alSourcePlay(source.get(0));
}
public void stop() {
AL10.alSourceStop(source.get(0));
}
public void pause() {
AL10.alSourcePause(source.get(0));
}
}
In this particular game, I create 3 sounds from the main object by invoking something like:
powerupsound = new SoundSystem(“sounds/powerup.wav”);
boopsound = new SoundSystem(“sounds/boop.wav”);
collidesound = new SoundSystem(“sounds/collide.wav”);
Then, in the game loop, whenever I need to play the sound, I invoke:
collidesound.play();
Am I doing something wrong here that would cause static and pops? I was able to reduce it a bit by checking on the isplaying state before playing the sound, but then I don’t get any simultaneous sounds in the game, which makes it kinda funny.