OpenAL sound static and pops

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.

Sorry, your spam filter has probably blacklisted our server so the confirmation email got zapped…

What’s your OS/sound card combo?

Cas :slight_smile:

Drats on the spam-filter thing, but yes, you’re probably right. My work is incredibly zealous about taking anti-spam measures.

I’ve tried it on several machines, all display the same issue:
An AMD64 3000+ platform, WindowsXP, Realtek AC97 audio
An AMD 2500+, Gentoo Linux, Nforce Audio
Dell Laptop, Windows XP, some whacked out generic audio thing (SigmaTel).

That’s why I was wondering if it was my code, or perhaps just the sound clips I’m using.

OK, so here’s what I did. Since you didn’t mention anything about the code, I’m guessing it’s fine (I figure, of anyone, princec would catch something pertaining to lwjgl code :wink: ). I went through and swapped out the wav files I was using with a bunch I swiped from game already installed on the machine (just to borrow them temporarily of course)… and they sounded just fine.

I guess it’s the wav files I was using. I guess that means I’m going to have to come up with a way to make higher quality sound clips now. :frowning:

Anyway, thank you for your prompt response princec. If there’s another way to get on your forum, I’d sure appreciate it (can you activate my account manually? - elias4444). Also, if there is something wrong with my code, please let me know. I’m pretty sure I followed the OpenAL examples on the lwjgl website pretty closely, and then just altered away until I had what I needed.

elias4444 I have the popping sound too on some of my sound samples. Now I know why, bad quality

The funny thing is that it sounds ok in winamp… Guess winamp have some filter

Yeah, I ran some experiments with the sound stuff. Apparently, the issue occurs with repeated playing of a sound, and the overlapping of sounds. For some reason, my low-quality sound clips had some “impurities” in the sound streams, which simply became deafly obvious when rapidly repeated over and over again. I tried playing the sound in a media player and simulated the repitition as best as I could (by looping it amongst other things) - sure enough, the problem was there.

(Activated your userid on puppygames)

Cas :slight_smile:

Thanks Cas! ;D