summaryrefslogtreecommitdiffstats
path: root/www/devmaster/lesson5.html
diff options
context:
space:
mode:
Diffstat (limited to 'www/devmaster/lesson5.html')
-rw-r--r--www/devmaster/lesson5.html115
1 files changed, 81 insertions, 34 deletions
diff --git a/www/devmaster/lesson5.html b/www/devmaster/lesson5.html
index dbd582c..c1a9d7a 100644
--- a/www/devmaster/lesson5.html
+++ b/www/devmaster/lesson5.html
@@ -38,10 +38,20 @@ tutorial from <a href="http://devmaster.net/">DevMaster.net</a> to JOAL.
may find this interesting. Plus, we will be implementing the Alc layer directly
so that we can use some of that knowledge gained in lesson 4. On top of that
we will create a program you might even use!</p>
-<p align="justify">Well, here we go. I've decided to only go over bits of the
+<p align="justify">Well, here we go. <!-- I've decided to only go over bits of the
code that are significant, since most of the code has been repeated so far in
- the series. Check out the full source code in the download.</p>
-<pre class=code><span class=codeComment><font color="#0000FF">static</font> ALC alc;
+ the series. Check out the full source code in the download.--></p>
+<pre class=code>
+<font color="#0000FF">import</font> java.io.*;
+<font color="#0000FF">import</font> java.nio.*;
+<font color="#0000FF">import</font> java.util.*;
+
+<font color="#0000FF">import</font> com.jogamp.openal.*;
+<font color="#0000FF">import</font> com.jogamp.openal.util.*;
+
+<font color="#0000FF">public</font> <font color="#0000FF">class</font> SourceSharingBuffers {
+
+<span class=codeComment><font color="#0000FF">static</font> ALC alc;
<font color="#0000FF">static</font> AL al;<font color="#006600">
// These index the buffers.</font></span>
@@ -57,9 +67,24 @@ tutorial from <a href="http://devmaster.net/">DevMaster.net</a> to JOAL.
<font color="#006600"><span class=codeComment>// Buffers hold sound data.</span>
</font><font color="#0000FF">static int</font>[] buffers = <font color="#0000FF">new int</font>[NUM_BUFFERS];
-<span class=codeComment><font color="#006600">// A vector list of sources for multiple emissions.</font></span>
-Vector sources = <font color="#0000FF">new </font>Vector();
+<span class=codeComment><font color="#006600">// A list of sources for multiple emissions.</font></span>
+static List sources = <font color="#0000FF">new </font>ArrayList();
+
+// Position of the source sounds.
+static float[] sourcePos = { 0.0f, 0.0f, 0.0f };
+
+// Velocity of the source sounds.
+static float[] sourceVel = { 0.0f, 0.0f, 0.0f };
+
+// Position of the listener.
+static float[] listenerPos = { 0.0f, 0.0f, 0.0f };
+
+// Velocity of the listener.
+static float[] listenerVel = { 0.0f, 0.0f, 0.0f };
+
+// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
+static float[] listenerOri = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
</pre>
<p align="justify">First I've written out a few macros that we can use to index
the buffer array. We will be using several wav files so we need quite a few
@@ -69,11 +94,13 @@ Vector sources = <font color="#0000FF">new </font>Vector();
of them. This is also the first tutorial where we will deal with sources as
being a resource that will run out. And yes, they will run out; they are finite.</p>
<pre class=code><font color="#0000FF">static int </font>initOpenAL() {
-
- ALC.Device device;
- ALC.Context context;
+ al = ALFactory.getAL();
+ alc = ALFactory.getALC();
+ ALCdevice device;
+ ALCcontext context;
String deviceSpecifier;
- String deviceName = "DirectSound3D";
+ String deviceName = &quot;DirectSound3D&quot;; // You may choose to open a specific OpenAL device if you know its name.
+ deviceName = <font color="#0000FF">null</font>; // Passing a null String to alcOpenDevice will open the default device on your system!
<span class=codeComment><font color="#006600">// Get handle to device.</font></span>
device = alc.alcOpenDevice(deviceName);
@@ -81,7 +108,7 @@ Vector sources = <font color="#0000FF">new </font>Vector();
<font color="#006600"><span class=codeComment>// Get the device specifier.</span></font>
deviceSpecifier = alc.alcGetString(device, ALC.ALC_DEVICE_SPECIFIER);
- System.out.println("Using device &quot; + deviceSpecifier);
+ System.out.println(&quot;Using device &quot; + deviceSpecifier);
<span class=codeComment><font color="#006600">// Create audio context.</font></span>
context = alc.alcCreateContext(device, <font color="#0000FF">null</font>);
@@ -90,7 +117,7 @@ Vector sources = <font color="#0000FF">new </font>Vector();
alc.alcMakeContextCurrent(context);
<span class=codeComment><font color="#006600">// Check for an error.</font></span>
- <span class=codeKeyword><font color="#0000FF">if </font></span>(alc.alcGetError() != ALC.ALC_NO_ERROR)
+ <span class=codeKeyword><font color="#0000FF">if </font></span>(alc.alcGetError(device) != ALC.ALC_NO_ERROR)
<span class=codeKeyword><font color="#0000FF">return</font></span> AL.AL_FALSE;
<span class=codeKeyword><font color="#0000FF">return</font></span> AL.AL_TRUE;
@@ -101,8 +128,8 @@ Vector sources = <font color="#0000FF">new </font>Vector();
context for our application. This context is set to current and the function
will check if everything went smoothly before we return success.</p>
<pre class=code><span class=codeKeyword><font color="#0000FF">static void</font></span> exitOpenAL() {
- ALC.Context curContext;
- ALC.Device curDevice;
+ ALCcontext curContext;
+ ALCdevice curDevice;
<font color="#006600"><span class=codeComment>// Get the current context.</span></font>
curContext = alc.alcGetCurrentContext();
@@ -137,34 +164,34 @@ Vector sources = <font color="#0000FF">new </font>Vector();
<font color="#0000FF">int</font>[] loop = new <font color="#0000FF">int</font>[1];
<span class=codeComment><font color="#006600">// Load wav data into buffers.</font></span>
- al.alGenBuffers(NUM_BUFFERS, buffers);
+ al.alGenBuffers(NUM_BUFFERS, buffers, 0);
<span class=codeKeyword><font color="#0000FF">if</font></span>(al.alGetError() != AL.AL_NO_ERROR)
<span class=codeKeyword><font color="#0000FF">return</font></span> AL.AL_FALSE;
ALut.alutLoadWAVFile("wavdata/thunder.wav", format, data, size, freq, loop);
al.alBufferData(buffers[THUNDER], format[0], data[0], size[0], freq[0]);
- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]);
+<!-- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); -->
ALut.alutLoadWAVFile("wavdata/waterdrop.wav", format, data, size, freq, loop);
al.alBufferData(buffers[WATERDROP], format[0], data[0], size[0], freq[0]);
- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]);
+<!-- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); -->
ALut.alutLoadWAVFile("wavdata/stream.wav", format, data, size, freq, loop);
al.alBufferData(buffers[STREAM], format[0], data[0], size[0], freq[0]);
- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]);
+<!-- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); -->
ALut.alutLoadWAVFile("wavdata/rain.wav", format, data, size, freq, loop);
al.alBufferData(buffers[RAIN], format[0], data[0], size[0], freq[0]);
- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]);
+<!-- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); -->
ALut.alutLoadWAVFile("wavdata/ocean.wav", format, data, size, freq, loop);
al.alBufferData(buffers[OCEAN], format[0], data[0], size[0], freq[0]);
- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]);
+<!-- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); -->
ALut.alutLoadWAVFile("wavdata/chimes.wav", format, data, size, freq, loop);
al.alBufferData(buffers[CHIMES], format[0], data[0], size[0], freq[0]);
- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]);
+<!-- ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); -->
<font color="#006600"> <span class=codeComment>// Do another error check and return.</span>
</font><span class=codeKeyword><font color="#0000FF">if</font> </span>(al.alGetError() != AL.AL_NO_ERROR)
@@ -178,7 +205,7 @@ Vector sources = <font color="#0000FF">new </font>Vector();
<pre class=code><span class=codeKeyword><font color="#0000FF">static void</font></span><font color="#0000FF"> </font>addSource(int type) {
<font color="#0000FF">int[]</font> source = <font color="#0000FF">new int</font>[1];
- al.alGenSources(1, source);
+ al.alGenSources(1, source, 0);
<span class=codeKeyword><font color="#0000FF">if</font> </span>(al.alGetError() != AL.AL_NO_ERROR) {
System.err.println("Error generating audio source.");
@@ -186,17 +213,22 @@ Vector sources = <font color="#0000FF">new </font>Vector();
}
al.alSourcei (source[0], AL.AL_BUFFER, buffers[type]);
- al.alSourcef (source[0], AL.AL_PITCH, 1.0 );
- al.alSourcef (source[0], AL.AL_GAIN, 1.0 );
- al.alSourcefv(source[0], AL.AL_POSITION, sourcePos );
- al.alSourcefv(source[0], AL.AL_VELOCITY, sourceVel );
+ al.alSourcef (source[0], AL.AL_PITCH, 1.0f );
+ al.alSourcef (source[0], AL.AL_GAIN, 1.0f );
+ al.alSourcefv(source[0], AL.AL_POSITION, sourcePos , 0);
+ al.alSourcefv(source[0], AL.AL_VELOCITY, sourceVel , 0);
al.alSourcei (source[0], AL.AL_LOOPING, AL.AL_TRUE );
- al.alSourcePlay(source);
+ al.alSourcePlay(source[0]);
- sources.put(new Integer(source[0]));
+ sources.add(new Integer(source[0]));
}
+static void setListenerValues() {
+ al.alListenerfv(AL.AL_POSITION, listenerPos, 0);
+ al.alListenerfv(AL.AL_VELOCITY, listenerVel, 0);
+ al.alListenerfv(AL.AL_ORIENTATION, listenerOri, 0);
+}
</pre>
<p align="justify">Here's the function that will generate the sources for us.
This function will generate a single source for any one of the loaded buffers
@@ -208,17 +240,29 @@ Vector sources = <font color="#0000FF">new </font>Vector();
Iterator iter = sources.iterator();
<span class=codeKeyword><font color="#000000">while</font></span>(iter.hasNext()) {
- al.alDeleteSources(1, <font color="#0000FF">new int</font>[] { ((Integer)iter.next()).intValue() });
+ al.alDeleteSources(1, <font color="#0000FF">new int</font>[] { ((Integer)iter.next()).intValue() }, 0);
}
sources.clear();
- al.alDeleteBuffers(NUM_BUFFERS, buffers);
+ al.alDeleteBuffers(NUM_BUFFERS, buffers, 0);
exitOpenAL();
}
</pre>
-<p align="justify">This function has been modified a bit to accommodate the Vector.
+<p align="justify">This function has been modified a bit to accommodate the List.
We have to delete each source in the list individually and then clear the list
which will effectively destroy it.</p>
-<pre class=code><font color="#0000FF"> char</font>[] c = <font color="#0000FF">new char</font>[1];
+<pre class=code>
+ public static void main(String[] args) {
+ try {
+ initOpenAL();
+ } catch (ALException e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ if (loadALData() == AL.AL_FALSE)
+ System.exit(1);
+ setListenerValues();
+
+<font color="#0000FF"> char</font>[] c = <font color="#0000FF">new char</font>[1];
<font color="#0000FF"> </font><font color="#0000FF">while</font>(c[0] != 'q') {
<font color="#0000FF"> </font><font color="#0000FF"> </font><font color="#0000FF">try</font> {
@@ -240,12 +284,15 @@ Vector sources = <font color="#0000FF">new </font>Vector();
<font color="#0000FF"></font><font color="#0000FF"> case</font> 'r': addSource(RAIN); <font color="#0000FF">break</font>;
<font color="#0000FF"></font><font color="#0000FF"> case</font> 'o': addSource(OCEAN); <font color="#0000FF">break</font>;
<font color="#0000FF"></font><font color="#0000FF"> case</font> 'c': addSource(CHIMES); <font color="#0000FF">break</font>;
-<font color="#0000FF"></font>}
+<font color="#0000FF"></font> }
<font color="#0000FF"> </font><font color="#0000FF"> </font>} <font color="#0000FF">catch</font> (IOException e) {
System.exit(1);
<font color="#0000FF"> </font><font color="#0000FF"> </font>}
-<font color="#0000FF"> </font>}</pre>
-<p align="justify">Here is the programs inner loop taken straight out of our main.
+<font color="#0000FF"> </font>}
+ killALData();
+ } // main
+} // class</pre>
+<p align="justify"> Here is the programs inner loop<!-- taken straight out of our main-->.
Basically it waits for some keyboard input and on certain key hits it will create
a new source of a certain type and add it to the audio scene. Essentially what
we have created here is something like one of those nature tapes that people