From 5c11c98afed1caf23bccb4e8f007f8435246452a Mon Sep 17 00:00:00 2001
From: Brian Matzon import
org.lwjgl.openal.AL;
-/** 1.4 Buffer and Source creation 1.3 Buffer and Source creation
-//create one IntBuffer as buffer and one as source
+
+ //create one IntBuffer as buffer and one as source There, all set for actually loading some sounddata into the buffer. There, all set for actually loading some sound data into the buffer.
1.2 Basic setup
Lets start out by creating a skeleton class for some very basic sound. We'll
-start of by creating the required OpenAL objects
+start of by creating the required OpenAL object
-import org.lwjgl.openal.ALC;
-import org.lwjgl.openal.ALCcontext;
-import org.lwjgl.openal.ALCdevice;
-
public class PlayTest {
/** OpenAL instance */
protected AL al;
-
- /** OpenAL Context instance */
- protected ALC alc;
-
- /** OpenAL context */
- protected ALCcontext context;
-
- /** OpenAL device */
- protected ALCdevice device;
-
+
+
/**
* Creates an instance of PlayTest
*/
public PlayTest() {
try {
- al = new AL();
- alc = new ALC();
-
+ al = new AL();
al.create();
- alc.create();
} catch (Exception e) {
e.printStackTrace();
}
}
-}
-
-
-1.3 OpenAL initialization
-Now that we have created a basic class containing instances of the relevant
-OpenAL classes, lets start of by initializing OpenAL - that is create a device
-and a context:
-
- * Initializes OpenAL
- */
- protected void alInitialize() {
- //get default device
- device = alc.openDevice(null);
-
- //create context (no attributes specified)
- context = alc.createContext(device, 0);
-
- //make context current
- alc.makeContextCurrent(context);
- }
-
-
-Having opened a device, create a context to that device using createContext.
-createContext takes two arguments: device to use and a list of attributes
-(see specification for list of attributes). Since we're going by default context,
-we just specify 0 for attributes.
-
-Finish of by making the created context current. Do this by calling makeContextCurrent,
-supplying just created context as argument.
-
-Now that we have opened a device and gotten a context, we need to create
+}
+Now that we have created the AL instance, we need to create
two things to actually get some sound. We need to create a buffer to hold
-sounddata, and a source that is to play the sounddata.
+sound data, and a source that is to play the sound data.
Lets start of by creating one source, and one buffer:
+
+ //createIntBuffer is a utility method which allocates a direct ByteBuffer in native order
IntBuffer buffers = createIntBuffer(1);
IntBuffer sources = createIntBuffer(1);
//generate buffers and sources
al.genBuffers(1, Sys.getDirectBufferAddress(buffers));
- al.genSources(1, Sys.getDirectBufferAddress(sources));
+ al.genSources(1, Sys.getDirectBufferAddress(sources));
+
+
+
Now that we have a buffer, we need to load some sound data into this buffer.
This is done using the al.bufferData method. In our example we will
"cheat" a bit, by using the WaveData class to load
@@ -138,11 +89,11 @@ a wave file, and copy this into the buffer:
wavefile.dispose();
//set up source input
al.sourcei(sources.get(0), AL.BUFFER, buffers.get(0));
al.deleteBuffers(1, Sys.getDirectBufferAddress(buffers));
//shutdown
- alc.makeContextCurrent(null);
- alc.destroyContext(context);
- alc.closeDevice(device);