the vermins
 

vermin

jworms.openal.* Package summary
OpenALListener
The listener who is listening to OpenAL soures (OpenALSource). The major property of OpenALListener is it's position.

  • Set the position of the OpenAL listener:

    OpenALListener.setPos(float x, float y, float z);

OpenALSource
The OpenALSource plays the sound (OpenALWave).

  • Using OpenALSource to play a OpenALWave:

    OpenALSource soundSource = new OpenALSource();

    // check if there is a free sound source ?
    if(soundSource.bind()){
      // set the position
      soundSource.setPos(x, y, z);
      // start playing (OpenALWave wave)
      soundSource.play(wave);
    }

OpenALUtils
OpenALUtils is a summary of static functions to initalize and exit OpenAL.

  • OpenAL framework:

    // initialize OpenAL
    OpenALUtils.alInit();

    /*
       Here goes the code to load all sound files.
       OpenALWave wave = OpenALWave.create("[.../sound.wav]");
    */

    // generate all OpenAL sound waves
    OpenALWave.alGenWaves();

    /*
       Here goes the code to play the sound (see OpenALSource).
    */

    // dispose OpenAL
    OpenALUtils.alExit();

OpenALWave
OpenALWave is used to load .wav files that then can be played by an OpenALSource.

  • Load an .wav file:

    try{
      OpenALWave wave = OpenALWave.create("[.../sound.wav]");
    }catch(Exception e){
      System.out.println("Failed loading waves due to ");
      e.printStackTrace();
    }

Basic OpenAL tutorial
This tutorial shows the use of multiple OpenAL sources to play different sounds simultaneously. After compiling and starting the OpenALTutorial.java you should hear footsteps on the left side and every 2 seconds an explosion. The explosion will move from right to left (in 5 steps) and then jump to the right side again. If you get an exception starting the program please check that "tutorial/...wav" sound files are located in the working directory.

Now we are going to explain the most important parts of the program.

1. Initializing OpenAL:
35   OpenALUtils.alInit();
2. Loading sound files:
37   // load the sounds
38   try{
39     explosion = OpenALWave.create("tutorial/Explosion.wav");
40     footSteps = OpenALWave.create("tutorial/Footsteps.wav");
41   catch(Exception e){
42     System.out.println("Failed starting OpenAL due to ");
43     e.printStackTrace();
44     System.exit(-1);
45   }
3. Set listener position:
51   OpenALListener.setPos(00100);
4. Generate sound files:
48   OpenALWave.alGenWaves();
5. Create OpenAL sources and play the sound:
69   // play an explosion every 2 seconds
70   if(explosionSoundSource.bind()){
71     explosionSoundSource.stop();
72 
73     // explosion sound moves from right to left side
74     explosionSoundSource.setPos(explosionX, 00);
75     explosionSoundSource.play(explosion);
76   }
6. Dispose OpenAL:
86   OpenALUtils.alExit();
Download and installation
To compile and run the tutorial you need Java Runtime Environment 1.4.2 and the LWJGL binaries for your operating system.

By clicking on a download link you agree to the disclaimer and distributing terms of Vermin ExTerminator (see download section)!

Source code:
Full source code of OpenALTutorial.java.
01 
02 package tutorial;
03 
04 import jworms.openAL.OpenALListener;
05 import jworms.openAL.OpenALSource;
06 import jworms.openAL.OpenALUtils;
07 import jworms.openAL.OpenALWave;
08 import org.lwjgl.openal.AL;
09 
10 /**
11  * Demonstrates the usage of OpenAL (jworms.openAL.*).
12  *
13  * usage: java tutorial.BigTextureTutorial
14  *        (close Strg + C or wait 20 seconds)
15  */
16 public class OpenALTutorial{
17   // OpenAL sound source for explosion
18   private OpenALSource footstepSoundSource = new OpenALSource();
19   // OpenAL sound source for explosion
20   private OpenALSource explosionSoundSource = new OpenALSource();
21 
22   // Sounds to play
23   private OpenALWave explosion = null;
24   private OpenALWave footSteps = null;
25 
26   public static void main(String[] arguments){
27     new OpenALTutorial();
28   }
29 
30   /**
31    * Initialize OpenAL and play explosion and footsteps sound.
32    */
33   public OpenALTutorial(){
34     // initialize OpenAL
35     OpenALUtils.alInit();
36 
37     // load the sounds
38     try{
39       explosion = OpenALWave.create("tutorial/Explosion.wav");
40       footSteps = OpenALWave.create("tutorial/Footsteps.wav");
41     catch(Exception e){
42       System.out.println("Failed starting OpenAL due to ");
43       e.printStackTrace();
44       System.exit(-1);
45     }
46 
47     // generate all OpenAL sound sources
48     OpenALWave.alGenWaves();
49 
50     // set OpenAL listener position
51     OpenALListener.setPos(00100);
52 
53     // play footsteps always left of the OpenAL listener
54     if(footstepSoundSource.bind()){
55       // right of the listener
56       footstepSoundSource.setPos(-30000);
57 
58       // play footsteps in loop
59       footstepSoundSource.setPropi(AL.AL_LOOPING, AL.AL_TRUE);
60       footstepSoundSource.play(footSteps);
61     }
62 
63     // close Strg + C or wait 20 seconds
64     int explosionX = 500;
65 
66     long startTime = System.currentTimeMillis();
67     while(startTime + 20 1000 > System.currentTimeMillis()){
68 
69       // play an explosion every 2 seconds
70       if(explosionSoundSource.bind()){
71         explosionSoundSource.stop();
72 
73         // explosion sound moves from right to left side
74         explosionSoundSource.setPos(explosionX, 00);
75         explosionSoundSource.play(explosion);
76       }
77 
78       // move the explosion from right to left (and then again right)
79       if(explosionX <= -500explosionX = 500else explosionX -= 250;
80 
81       // wait 2 seconds
82       tryThread.sleep(2000)catch(InterruptedException e){ }
83     }
84 
85     // dispose OpenAL
86     OpenALUtils.alExit();
87   }
88 }
Java2html