OpenAL in Applet

Started by paul p, February 03, 2009, 05:03:31

Previous topic - Next topic

paul p

I can play a audio .wav file in an Applet through a browser using openAL as long as I have a delay loop after the alSourcePlay method


AL10.alSourcePlay(source.get(0));

while(AL10.alGetSourcei(source.get(0), AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING){}



However I want the audio to play while I display some openGL graphics on a AWTGLCanvas

So I have played with creating a separate Thread for the audio within the Applet without success.

Any suggestions as to how I should tackle this.

Paul 



Matzon

when you do an alSourcePlay it should keep playing until someone call stop on it. It doesn't make sense that you have to check its starte ...
consider isolating the issue in an non-applet environment and work from there.

paul p

Thanks for your reply.

I agree with you. With applications and applets viewed with Applet Viewer I can play sounds with AL10.alSourcePlay(source.get(0))  in combination with graphics on a AWTGLCanvas. The same Applet will not produce a sound viewed within Internet Explorer or Firefox. I have tested that the .wav file gets loaded by the Applet.

I can create a simple Applet that will produce a sound with AL10.alSourcePlay(source.get(0)) viewed in Internet Explorer and Firefox but there is no AWTGLCanvas added. I can only produce a sound in the browsers with an AWTGLCanvas added if I use


AL10.alSourcePlay(source.get(0));

while(AL10.alGetSourcei(source.get(0), AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING){}


in the start() method of the Applet before the graphics appear. If I do not have the while loop the sound is not heard.


As a test I tried calling the above code from within the paintGL() method of my AWTCanvas. Then viewed with Applet Viewer it works as expected and my animation within AWTCanvas stops while the sound is played. If viewed within a browser it never comes out of the while loop. No error or exceptions appear within the Java Console.

The class I use for loading and playing the sound is below. I instantiate this within the Applet class.  The play() method is used to play the sound (bell.wav)

Any suggestions would be welcome.

Paul



package forum;

import java.io.IOException;
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.util.WaveData;


public class OpenALBasics {

  IntBuffer buffer = BufferUtils.createIntBuffer(1);
  IntBuffer source = BufferUtils.createIntBuffer(1);
  FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, -10.0f });
  FloatBuffer listenerPos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
  FloatBuffer listenerOri = BufferUtils.createFloatBuffer(6).put(new float[] { 0.0f, 0.0f, -1.0f,  0.0f, 1.0f, 0.0f });

  public OpenALBasics() {

    sourcePos.flip();
    listenerPos.flip();
    listenerOri.flip();
  }

  int loadALData() {

    AL10.alGenBuffers(buffer);

    if(AL10.alGetError() != AL10.AL_NO_ERROR)
      return AL10.AL_FALSE;
   
    WaveData waveFile = WaveData.create("bell.wav");
    AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data, waveFile.samplerate);
    waveFile.dispose();

    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) );

    if(AL10.alGetError() == AL10.AL_NO_ERROR)
      return AL10.AL_TRUE;

    return AL10.AL_FALSE;
  }

  public void execute() {

    try {
   AL.create(null, 15, 22050, true);

    AL10.alGetError();

    if(loadALData() == AL10.AL_FALSE) {
      System.out.println("Error loading data.");
      return;
    }
    AL10.alListener(AL10.AL_POSITION,    listenerPos);

    AL10.alListener(AL10.AL_ORIENTATION, listenerOri);

        } catch (LWJGLException le) {
       le.printStackTrace();

return;
    }
  }

  public void play()
  {
          AL10.alSourcePlay(source.get(0));
         while(AL10.alGetSourcei(source.get(0), AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING){}

  }

    @Override
    protected void finalize() throws Throwable
    {
        super.finalize();
         AL10.alDeleteSources(source);
        AL10.alDeleteBuffers(buffer);
    }

   public static void main(String[] args) {
   OpenALBasics ob = new OpenALBasics();
   ob.execute();
   ob.play();
   }
}






kappa

For Applets its best to use lwjgl's Display.setParent() method instead of using AWTGLCanvas, doing so will ensure the applet runs much more stable and smoother.

paul p

Thanks for the hint. That solved it! Needed a bit of web searching before I found some example code.

Paul