diff options
author | Michael Bien <[email protected]> | 2010-03-29 18:35:57 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-03-29 18:35:57 +0200 |
commit | 4050d93e4747b5418379319e7216af6113ea23e5 (patch) | |
tree | db71cc08193e1f893a2f2f296930eb11f6d8897c /src/java/com/jogamp/openal/sound3d | |
parent | 869f7899143296f70fbedb7ead9750aa2e4da172 (diff) |
renamed package to com.jogamp.openal.*
Diffstat (limited to 'src/java/com/jogamp/openal/sound3d')
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/AudioSystem3D.java | 273 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Buffer.java | 144 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Context.java | 77 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Device.java | 59 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Listener.java | 171 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Source.java | 508 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Vec3f.java | 63 |
7 files changed, 1295 insertions, 0 deletions
diff --git a/src/java/com/jogamp/openal/sound3d/AudioSystem3D.java b/src/java/com/jogamp/openal/sound3d/AudioSystem3D.java new file mode 100644 index 0000000..1070d21 --- /dev/null +++ b/src/java/com/jogamp/openal/sound3d/AudioSystem3D.java @@ -0,0 +1,273 @@ +/** + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * -Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * -Redistribution in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. + * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING + * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR + * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS + * LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A + * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. + * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT + * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR + * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS + * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use in the + * design, construction, operation or maintenance of any nuclear facility. + */ + +package com.jogamp.openal.sound3d; + +import com.jogamp.openal.*; +import com.jogamp.openal.util.WAVData; +import com.jogamp.openal.util.WAVLoader; + +import java.io.*; + +import javax.sound.sampled.UnsupportedAudioFileException; + + +/** + * The AudioSystem3D class provides a set of methods for creating and + * manipulating a 3D audio environment. + * + * @author Athomas Goldberg + */ +public class AudioSystem3D { + private static AL al; + private static ALC alc; + private static Listener listener; + + /** + * Iniitalize the Sound3D environment. This must be called before + * other methods in the class can be used. + */ + public static void init() throws ALException { + al = ALFactory.getAL(); + alc = ALFactory.getALC(); + } + + /** + * Creates a new Sound3D Context for a specified device. + * + * @param device The device the Context is being created for. + * + * @return The new Sound3D context. + */ + public static Context createContext(Device device) { + Context result = null; + ALCcontext realContext = alc.alcCreateContext(device.realDevice, null); + result = new Context(alc, realContext, device); + return result; + } + + /** + * Makes the specified context the current context. + * + * @param context the context to make current. + */ + public static void makeContextCurrent(Context context) { + ALCcontext realContext = null; + + if (context != null) { + realContext = context.realContext; + } + + alc.alcMakeContextCurrent(realContext); + } + + /** + * Opens the specifified audio device. + * + * @param deviceName The specified device name, On windows this will be + * DirectSound3D. We will be automating device discovery in upcoming versions + * of this class. + * + * @return The device described by the specifed name. + */ + public static Device openDevice(String deviceName) { + Device result = null; + ALCdevice realDevice = alc.alcOpenDevice(deviceName); + result = new Device(alc, realDevice); + + return result; + } + + /** + * Generate an array of Sound3D buffers. + * + * @param numBuffers The number of Sound3D buffers to generate. + * + * @return an array of (initially enpty) Sound3D buffers. + */ + public static Buffer[] generateBuffers(int numBuffers) { + Buffer[] result = new Buffer[numBuffers]; + int[] arr = new int[numBuffers]; + al.alGenBuffers(numBuffers, arr, 0); + + for (int i = 0; i < numBuffers; i++) { + result[i] = new Buffer(al, arr[i]); + } + + return result; + } + + /** + * Loads a Sound3D buffer with the specified audio file. + * + * @param filename the name of the file to load. + * + * @return a new Sound3D buffer containing the audio data from the + * specified file. + * + * @throws IOException If the file cannot be found or some other IO error + * occurs. + * @throws UnsupportedAudioFileException If the format of the audio data is + * not supported + */ + public static Buffer loadBuffer(String filename) + throws IOException, UnsupportedAudioFileException { + Buffer result; + Buffer[] tmp = generateBuffers(1); + result = tmp[0]; + + WAVData wd = WAVLoader.loadFromFile(filename); + result.configure(wd.data, wd.format, wd.freq); + + return result; + } + + /** + * Loads a Sound3D buffer with the specified audio file. + * + * @param stream contains the stream associated with the audio file. + * + * @return a new Sound3D buffer containing the audio data from the + * passed stream. + * + * @throws IOException If the stream cannot be read or some other IO error + * occurs. + * @throws UnsupportedAudioFileException If the format of the audio data is + * not supported + */ + public static Buffer loadBuffer(InputStream stream) + throws IOException, UnsupportedAudioFileException { + Buffer result; + Buffer[] tmp = generateBuffers(1); + result = tmp[0]; + + if (!(stream instanceof BufferedInputStream)) { + stream = new BufferedInputStream(stream); + } + WAVData wd = WAVLoader.loadFromStream(stream); + + result.configure(wd.data, wd.format, wd.freq); + + return result; + } + + /** + * Loads a Sound3D Source with the specified audio file. This is + * functionally equivalent to generateSource(loadBuffer(fileName)); + * + * @param filename the name of the file to load. + * + * @return a new Sound3D Source containing the audio data from the + * specified file. + * + * @throws IOException If the file cannot be found or some other IO error + * occurs. + * @throws UnsupportedAudioFileException If the format of the audio data is + * not supported + */ + public static Source loadSource(String filename) + throws IOException, UnsupportedAudioFileException { + Buffer buffer = loadBuffer(filename); + + return generateSource(buffer); + } + + /** + * Loads a Sound3D Source with the specified audio stream. This is + * functionally equivalent to generateSource(loadBuffer(stream)); + * + * @param stream contains the stream associated with the audio file. + * + * @return a new Sound3D Source containing the audio data from the + * passed stream. + * + * @throws IOException If the file cannot be found or some other IO error + * occurs. + * @throws UnsupportedAudioFileException If the format of the audio data is + * not supported + */ + public static Source loadSource(InputStream stream) + throws IOException, UnsupportedAudioFileException { + Buffer buffer = loadBuffer(stream); + + return generateSource(buffer); + } + + /** + * Generates a set of uninitialized Source3D sources + * + * @param numSources the number of Sound3D sources to generate. + * + * @return an array of uninitialized sources. + */ + public static Source[] generateSources(int numSources) { + Source[] result = new Source[numSources]; + int[] arr = new int[numSources]; + al.alGenSources(numSources, arr, 0); + + for (int i = 0; i < numSources; i++) { + result[i] = new Source(al, arr[i]); + } + + return result; + } + + /** + * Generate a Sound3D source from an initialized Buffer. + * + * @param buff The buffer to generate the source from. + * + * @return the newly generated Source. + */ + public static Source generateSource(Buffer buff) { + Source result = null; + Source[] tmp = generateSources(1); + result = tmp[0]; + result.setBuffer(buff); + + return result; + } + + /** + * Get the listener object associated with this Sound3D environment. + * + * @return The listener object. + */ + public static Listener getListener() { + if (listener == null) { + listener = new Listener(al); + } + + return listener; + } +} diff --git a/src/java/com/jogamp/openal/sound3d/Buffer.java b/src/java/com/jogamp/openal/sound3d/Buffer.java new file mode 100644 index 0000000..54d120e --- /dev/null +++ b/src/java/com/jogamp/openal/sound3d/Buffer.java @@ -0,0 +1,144 @@ +/** +* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* -Redistribution of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* -Redistribution in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* Neither the name of Sun Microsystems, Inc. or the names of contributors may +* be used to endorse or promote products derived from this software without +* specific prior written permission. +* +* This software is provided "AS IS," without a warranty of any kind. +* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING +* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR +* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS +* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A +* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. +* IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT +* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR +* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, +* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS +* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +* +* You acknowledge that this software is not designed or intended for use in the +* design, construction, operation or maintenance of any nuclear facility. +*/ + +package com.jogamp.openal.sound3d; + +import com.jogamp.openal.AL; + +import java.nio.ByteBuffer; + + +/** + * The Sound3D Buffer is a container for audio data used in the Sound3D + * environment. + * + * @author Athomas Goldberg + */ +public class Buffer { + public final static int FORMAT_MONO8 = AL.AL_FORMAT_MONO8; + + public final static int FORMAT_MONO16 = AL.AL_FORMAT_MONO16; + + public final static int FORMAT_STEREO8 = AL.AL_FORMAT_STEREO8; + + public final static int FORMAT_STEREO16 = AL.AL_FORMAT_STEREO16; + final int bufferID; + private ByteBuffer data; + private boolean isConfigured = false; + private final AL al; + + Buffer(AL al, int bufferID) { + this.bufferID = bufferID; + this.al = al; + } + + /** + * Configure the Sound3D buffer + * + * @param data the raw audio data + * @param format the format of the data: <code>FORMAT_MONO8, FORMAT_MONO16, + * FORMAT_STEREO8</code> and <code>FORMAT_STEREO16</code> + * @param freq the frequency of the data + */ + public void configure(ByteBuffer data, int format, int freq) { + if (!isConfigured) { + this.data = data; + al.alBufferData(bufferID, format, data, data.capacity(), freq); + } + } + + /** + * Delete this buffer, and free its resources. + */ + public void delete() { + data = null; + al.alDeleteBuffers(1, new int[] { bufferID }, 0); + } + + /** + * Get the bit-depth of the data, (8 or 16) + * + * @return the bit-depth of the data + */ + public int getBitDepth() { + int[] i = new int[1]; + al.alGetBufferi(bufferID, AL.AL_BITS, i, 0); + + return i[0]; + } + + /** + * Get the number of channels of the data (1-Mono, 2-Stereo) + * + * @return the number of audio channels. + */ + public int getNumChannels() { + int[] i = new int[1]; + al.alGetBufferi(bufferID, AL.AL_CHANNELS, i, 0); + + return i[0]; + } + + /** + * Gets the raw data contained in this buffer. + * + * @return the raw buffer data. + */ + public ByteBuffer getData() { + return data; + } + + /** + * Gets the audio frequency of the data contained in this buffer. + * + * @return the frequency of the data + */ + public int getFrequency() { + int[] i = new int[1]; + al.alGetBufferi(bufferID, AL.AL_FREQUENCY, i, 0); + + return i[0]; + } + + /** + * Gets the size (in bytes) of the raw data containe in this buffer. + * + * @return the size of the data. + */ + public int getSize() { + int[] i = new int[1]; + al.alGetBufferi(bufferID, AL.AL_SIZE, i, 0); + + return i[0]; + } +} diff --git a/src/java/com/jogamp/openal/sound3d/Context.java b/src/java/com/jogamp/openal/sound3d/Context.java new file mode 100644 index 0000000..5562153 --- /dev/null +++ b/src/java/com/jogamp/openal/sound3d/Context.java @@ -0,0 +1,77 @@ +/** +* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* -Redistribution of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* -Redistribution in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* Neither the name of Sun Microsystems, Inc. or the names of contributors may +* be used to endorse or promote products derived from this software without +* specific prior written permission. +* +* This software is provided "AS IS," without a warranty of any kind. +* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING +* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR +* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS +* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A +* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. +* IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT +* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR +* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, +* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS +* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +* +* You acknowledge that this software is not designed or intended for use in the +* design, construction, operation or maintenance of any nuclear facility. +*/ + +package com.jogamp.openal.sound3d; + +import com.jogamp.openal.*; + + +/** + * This class provides a Sound3D Context associated with a specified device. + * + * @author Athomas Goldberg + */ +public class Context { + private final ALC alc; + final ALCcontext realContext; + final Device device; + + Context(ALC alc, ALCcontext realContext, Device device) { + this.alc = alc; + this.realContext = realContext; + this.device = device; + } + + /** + * Suspend this context + */ + public void suspend() { + alc.alcSuspendContext(realContext); + } + + /** + * destroys this context freeing its resources. + */ + public void destroy() { + alc.alcDestroyContext(realContext); + } + + /** + * Gets the device associated with this context. + * + * @return the device associated with this context. + */ + public Device getDevice() { + return device; + } +} diff --git a/src/java/com/jogamp/openal/sound3d/Device.java b/src/java/com/jogamp/openal/sound3d/Device.java new file mode 100644 index 0000000..086d3ad --- /dev/null +++ b/src/java/com/jogamp/openal/sound3d/Device.java @@ -0,0 +1,59 @@ +/** +* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* -Redistribution of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* -Redistribution in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* Neither the name of Sun Microsystems, Inc. or the names of contributors may +* be used to endorse or promote products derived from this software without +* specific prior written permission. +* +* This software is provided "AS IS," without a warranty of any kind. +* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING +* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR +* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS +* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A +* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. +* IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT +* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR +* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, +* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS +* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +* +* You acknowledge that this software is not designed or intended for use in the +* design, construction, operation or maintenance of any nuclear facility. +*/ + +package com.jogamp.openal.sound3d; + +import com.jogamp.openal.*; + + +/** + * This class provides a handle to a specific audio device. + * + * @author Athomas Goldberg + */ +public class Device { + private final ALC alc; + final ALCdevice realDevice; + + Device(ALC alc, ALCdevice realDevice) { + this.alc = alc; + this.realDevice = realDevice; + } + + /** + * closes the device, freeing its resources. + */ + public void close() { + alc.alcCloseDevice(realDevice); + } +} diff --git a/src/java/com/jogamp/openal/sound3d/Listener.java b/src/java/com/jogamp/openal/sound3d/Listener.java new file mode 100644 index 0000000..fe8b869 --- /dev/null +++ b/src/java/com/jogamp/openal/sound3d/Listener.java @@ -0,0 +1,171 @@ +/** +* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* -Redistribution of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* -Redistribution in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* Neither the name of Sun Microsystems, Inc. or the names of contributors may +* be used to endorse or promote products derived from this software without +* specific prior written permission. +* +* This software is provided "AS IS," without a warranty of any kind. +* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING +* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR +* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS +* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A +* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. +* IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT +* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR +* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, +* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS +* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +* +* You acknowledge that this software is not designed or intended for use in the +* design, construction, operation or maintenance of any nuclear facility. +*/ + +package com.jogamp.openal.sound3d; + +import com.jogamp.openal.AL; + + +/** + * This class represents the human listener in the Sound3D environment. It + * provides methods for controlling the position, orientation as well as other + * properties associated with the listener. + * + * @author Athomas Goldberg + */ +public class Listener { + private final AL al; + + Listener(AL al) { + this.al = al; + } + + /** + * Sets the Gain, or volume of the audio in the environment relative to the + * listener + * + * @param gain the gain, or volume + */ + public void setGain(float gain) { + al.alListenerf(AL.AL_GAIN, gain); + } + + /** + * Gets the value of the gain, or volume of the audio in the environment + * relative to the listener. + * + * @return the gain value. + */ + public float getGain() { + float[] f = new float[1]; + al.alGetListenerf(AL.AL_GAIN, f, 0); + + return f[0]; + } + + /** + * Sets the position in (x-y-z coordinates) of the Listener in the Sound3D + * environment. + * + * @param x The position of the listener along the X-axis in the Sound3D + * environment + * @param y The position of the listener along the Y-axis in the Sound3D + * environment + * @param z The position of the listener along the Z-axis in the Sound3D + * environment + */ + public void setPosition(float x, float y, float z) { + al.alListener3f(AL.AL_POSITION, x, y, z); + } + + /** + * Sets the position in (x-y-z coordinates) of the Listener in the Sound3D + * environment. + * + * @param position a Vec3f object containing the x,y and z coordinates of + * Listener. + */ + public void setPosition(Vec3f position) { + al.alListener3f(AL.AL_POSITION, position.v1, position.v2, position.v3); + } + + /** + * Gets the position in (x-y-z coordinates) of the Listener in the Sound3D + * environment. + * + * @return a Vec3f object containing the x,y and z coordinates of + * Listener. + */ + public Vec3f getPosition() { + Vec3f result = null; + float[] tmp = new float[3]; + al.alGetListenerfv(AL.AL_POSITION, tmp, 0); + result = new Vec3f(tmp[0], tmp[1], tmp[2]); + + return result; + } + + /** + * Sets the velocity in (x-y-z coordinates) of the Listener in the Sound3D + * environment. Used in determining doppler shift. + * + * @param velocity a Vec3f object containing the velicity in + * x,y and z coordinates of Listener. + */ + public void setVelocity(Vec3f velocity) { + al.alListener3f(AL.AL_VELOCITY, velocity.v1, velocity.v2, velocity.v3); + } + + /** + * Gets the velocity in (x-y-z coordinates) of the Listener in the Sound3D + * environment. Used in determining doppler shift. + * + * @return a Vec3f object containing the velicity in + * x,y and z coordinates of Listener. + */ + public Vec3f getVelocity() { + Vec3f result = null; + float[] tmp = new float[3]; + al.alGetListenerfv(AL.AL_VELOCITY, tmp, 0); + result = new Vec3f(tmp[0], tmp[1], tmp[2]); + + return result; + } + + /** + * Sets the orientation of the Listener in the Sound3D environment. + * Orientation is expressed as "up" and "at" vectors. + * + * @param orientation The first 3 elements of the array should contain + * the x,y,z up-vector, the second 3 elements should contain the x,z,z + * look-at vector. + */ + public void setOrientation(float[] orientation) { + al.alListenerfv(AL.AL_ORIENTATION, orientation, 0); + } + + /** + * Gets the orientation of the Listener in the Sound3D environment. + * Orientation is expressed as "up" and "at" vectors. + * + * @return an array containing the orientation of the listener. + * The first 3 elements of the array contain + * the x,y,z up-vector, the second 3 elements contain the x,z,z + * look-at vector. + */ + public float[] getOrientation() { + float[] tmp = new float[6]; + al.alGetListenerfv(AL.AL_ORIENTATION, tmp, 0); + return tmp; + } +} diff --git a/src/java/com/jogamp/openal/sound3d/Source.java b/src/java/com/jogamp/openal/sound3d/Source.java new file mode 100644 index 0000000..2e2f512 --- /dev/null +++ b/src/java/com/jogamp/openal/sound3d/Source.java @@ -0,0 +1,508 @@ +/** +* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* -Redistribution of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* -Redistribution in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* Neither the name of Sun Microsystems, Inc. or the names of contributors may +* be used to endorse or promote products derived from this software without +* specific prior written permission. +* +* This software is provided "AS IS," without a warranty of any kind. +* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING +* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR +* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS +* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A +* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. +* IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT +* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR +* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, +* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS +* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +* +* You acknowledge that this software is not designed or intended for use in the +* design, construction, operation or maintenance of any nuclear facility. +*/ + +package com.jogamp.openal.sound3d; + +import com.jogamp.openal.AL; + +/** + * This class is used to represent sound-producing objects in the Sound3D + * environment. It contains methods for setting the position, direction, pitch, + * gain and other properties along with methods for starting, pausing, rewinding + * and stopping sudio projecting from a source. + * + * @author Athomas Goldberg + */ +public final class Source { + private final AL al; + private final int sourceID; + private Buffer buffer; + + Source(AL al, int sourceID) { + this.al = al; + this.sourceID = sourceID; + } + + /** + * Beginning playing the audio in this source. + */ + public void play() { + al.alSourcePlay(sourceID); + } + + /** + * pauses the audio in this Source. + */ + public void pause() { + al.alSourcePause(sourceID); + } + + /** + * Stops the audio in this Source + */ + public void stop() { + al.alSourceStop(sourceID); + } + + /** + * Rewinds the audio in this source + */ + public void rewind() { + al.alSourceRewind(sourceID); + } + + /** + * Delete this source, freeing its resources. + */ + public void delete() { + al.alDeleteSources(1, new int[] { sourceID }, 0); + } + + /** + * Sets the pitch of the audio on this source. The pitch may be modified + * without altering the playback speed of the audio. + * + * @param pitch the pitch value of this source. + */ + public void setPitch(float pitch) { + al.alSourcef(sourceID, AL.AL_PITCH, pitch); + } + + /** + * Gets the pitch of the audio on this source. The pitch may be modified + * without altering the playback speed of the audio. + * + * @return the pitch value of this source. + */ + public float getPitch() { + float[] result = new float[1]; + al.alGetSourcef(sourceID, AL.AL_PITCH, result, 0); + + return result[0]; + } + + /** + * Sets the gain of the audio on this source. This can be used to contro + * the volume of the source. + * + * @param gain the gain of the audio on this source + */ + public void setGain(float gain) { + al.alSourcef(sourceID, AL.AL_GAIN, gain); + } + + /** + * Gets the gain of the audio on this source. This can be used to contro + * the volume of the source. + * + * @return the gain of the audio on this source + */ + public float getGain() { + float[] result = new float[1]; + al.alGetSourcef(sourceID, AL.AL_GAIN, result, 0); + + return result[0]; + } + + /** + * Sets the max distance where there will no longer be any attenuation of + * the source. + * + * @param maxDistance the max ditance for source attentuation. + */ + public void setMaxDistance(float maxDistance) { + al.alSourcef(sourceID, AL.AL_MAX_DISTANCE, maxDistance); + } + + /** + * Gets the max distance where there will no longer be any attenuation of + * the source. + * + * @return the max ditance for source attentuation. + */ + public float getMaxDistance() { + float[] result = new float[1]; + al.alGetSourcef(sourceID, AL.AL_MAX_DISTANCE, result, 0); + + return result[0]; + } + + /** + * Sets the rolloff rate of the source. The default value is 1.0 + * + * @param rolloffFactor the rolloff rate of the source. + */ + public void setRolloffFactor(float rolloffFactor) { + al.alSourcef(sourceID, AL.AL_ROLLOFF_FACTOR, rolloffFactor); + } + + /** + * Gets the rolloff rate of the source. The default value is 1.0 + * + * @return the rolloff rate of the source. + */ + public float getRolloffFactor() { + float[] result = new float[1]; + al.alGetSourcef(sourceID, AL.AL_ROLLOFF_FACTOR, result, 0); + + return result[0]; + } + + /** + * Sets the distance under which the volume for the source would normally + * drop by half, before being influenced by rolloff factor or max distance. + * + * @param referenceDistance the reference distance for the source. + */ + public void setReferenceDistance(float referenceDistance) { + al.alSourcef(sourceID, AL.AL_REFERENCE_DISTANCE, referenceDistance); + } + + /** + * Gets the distance under which the volume for the source would normally + * drop by half, before being influenced by rolloff factor or max distance. + * + * @return the reference distance for the source. + */ + public float getReferenceDistance() { + float[] result = new float[1]; + al.alGetSourcef(sourceID, AL.AL_REFERENCE_DISTANCE, result, 0); + + return result[0]; + } + + /** + * Sets the minimum gain for this source. + * + * @param minGain the minimum gain for this source. + */ + public void setMinGain(float minGain) { + al.alSourcef(sourceID, AL.AL_MIN_GAIN, minGain); + } + + /** + * Gets the minimum gain for this source. + * + * @return the minimum gain for this source. + */ + public float getMinGain() { + float[] result = new float[1]; + al.alGetSourcef(sourceID, AL.AL_MIN_GAIN, result, 0); + + return result[0]; + } + + /** + * Sets the maximum gain for this source. + * + * @param maxGain the maximum gain for this source + */ + public void setMaxGain(float maxGain) { + al.alSourcef(sourceID, AL.AL_MAX_GAIN, maxGain); + } + + /** + * SGets the maximum gain for this source. + * + * @return the maximum gain for this source + */ + public float getMaxGain() { + float[] result = new float[1]; + al.alGetSourcef(sourceID, AL.AL_MAX_GAIN, result, 0); + + return result[0]; + } + + /** + * Sets the gain when outside the oriented cone. + * + * @param coneOuterGain the gain when outside the oriented cone. + */ + public void setConeOuterGain(float coneOuterGain) { + al.alSourcef(sourceID, AL.AL_CONE_OUTER_GAIN, coneOuterGain); + } + + /** + * Gets the gain when outside the oriented cone. + * + * @return the gain when outside the oriented cone. + */ + public float getConeOuterGain() { + float[] result = new float[1]; + al.alGetSourcef(sourceID, AL.AL_CONE_OUTER_GAIN, result, 0); + + return result[0]; + } + + /** + * Sets the x,y,z position of the source. + * + * @param position a Vec3f object containing the x,y,z position of the + * source. + */ + public void setPosition(Vec3f position) { + al.alSource3f( + sourceID, + AL.AL_POSITION, + position.v1, + position.v2, + position.v3); + } + + /** + * Sets the x,y,z position of the source. + * + * @param x the x position of the source. + * @param y the y position of the source. + * @param z the z position of the source. + */ + public void setPosition(float x, float y, float z) { + al.alSource3f(sourceID, AL.AL_POSITION, x, y, z); + } + + /** + * Gets the x,y,z position of the source. + * + * @return a Vec3f object containing the x,y,z position of the + * source. + */ + public Vec3f getPosition() { + Vec3f result = null; + float[] pos = new float[3]; + al.alGetSourcefv(sourceID, AL.AL_POSITION, pos, 0); + result = new Vec3f(pos[0], pos[1], pos[2]); + + return result; + } + + /** + * Sets the velocity vector of the source. + * + * @param velocity the velocity vector of the source + */ + public void setVelocity(Vec3f velocity) { + al.alSource3f( + sourceID, + AL.AL_VELOCITY, + velocity.v1, + velocity.v2, + velocity.v3); + } + + /** + * Sets the velocity vector of the source. + * + * @param x the x velocity of the source. + * @param y the y velocity of the source. + * @param z the z velocity of the source. + */ + public void setVelocity(float x, float y, float z) { + al.alSource3f(sourceID, AL.AL_VELOCITY, x, y, z); + } + + /** + * Gets the velocity vector of the source. + * + * @return the velocity vector of the source + */ + public Vec3f getVelocity() { + Vec3f result = null; + float[] vel = new float[3]; + al.alGetSourcefv(sourceID, AL.AL_VELOCITY, vel, 0); + result = new Vec3f(vel[0], vel[1], vel[2]); + + return result; + } + + /** + * Sets the direction vector of the source. + * + * @param direction the direction vector of the source. + */ + public void setDirection(Vec3f direction) { + al.alSource3f( + sourceID, + AL.AL_DIRECTION, + direction.v1, + direction.v2, + direction.v3); + } + + /** + * Sets the direction vector of the source. + * + * @param x the x direction of the source. + * @param y the y direction of the source. + * @param z the z direction of the source. + */ + public void setDirection(float x, float y, float z) { + al.alSource3f(sourceID, AL.AL_DIRECTION, x, y, z); + } + + /** + * Gets the direction vector of the source. + * + * @return the direction vector of the source. + */ + public Vec3f getDirection() { + Vec3f result = null; + float[] dir = new float[3]; + al.alGetSourcefv(sourceID, AL.AL_DIRECTION, dir, 0); + result = new Vec3f(dir[0], dir[1], dir[2]); + + return result; + } + + /** + * Determines if the position of the source is relative to the listener. + * The default is false. + * @param isRelative true if the position of the source is relative + * to the listener, false if the position of the source is relative to the + * world. + */ + public void setSourceRelative(boolean isRelative) { + int rel = isRelative ? 1 : 0; + al.alSourcei(sourceID, AL.AL_SOURCE_RELATIVE, rel); + } + + /** + * Determines if the position of the source is relative to the listener. + * The default is false. + * @return true if the position of the source is relative + * to the listener, false if the position of the source is relative to the + * world. + */ + public boolean isSourceRelative() { + int[] result = new int[1]; + al.alGetSourcei(sourceID, AL.AL_SOURCE_RELATIVE, result, 0); + + return result[0] == 1; + } + + /** + * turns looping on or off. + * + * @param isLooping true-looping is on, false-looping is off + */ + public void setLooping(boolean isLooping) { + int loop = isLooping ? 1 : 0; + al.alSourcei(sourceID, AL.AL_LOOPING, loop); + } + + /** + * indicates whether looping is turned on or off. + * + * @return true-looping is on, false-looping is off + */ + public boolean getLooping() { + boolean result = false; + int[] tmp = new int[1]; + al.alGetSourcei(sourceID, AL.AL_LOOPING, tmp, 0); + return tmp[0] == AL.AL_TRUE; + } + + + /** + * Gets the number of buffers currently queued on this source. + * @return the number of buffers currently queued on this source. + */ + public int getBuffersQueued() { + int[] result = new int[1]; + al.alGetSourcei(sourceID, AL.AL_BUFFERS_QUEUED, result, 0); + + return result[0]; + } + + /** + * Gets the number of buffers already processed on this source. + * @return the number of buffers already processed on this source. + */ + public int getBuffersProcessed() { + int[] result = new int[1]; + al.alGetSourcei(sourceID, AL.AL_BUFFERS_PROCESSED, result, 0); + + return result[0]; + } + + /** + * Sets the buffer associated with this source. + * + * @param buffer the buffer associated with this source + */ + public void setBuffer(Buffer buffer) { + al.alSourcei(sourceID, AL.AL_BUFFER, buffer.bufferID); + this.buffer = buffer; + } + + /** + * Gets the buffer associated with this source. + * + * @return the buffer associated with this source + */ + public Buffer getBuffer() { + return buffer; + } + + /** + * Queues one or more buffers on a source. Useful for streaming audio, + * buffers will be played in the order they are queued. + * + * @param buffers a set of initialized (loaded) buffers. + */ + public void queueBuffers(Buffer[] buffers) { + int numBuffers = buffers.length; + int[] arr = new int[numBuffers]; + + for (int i = 0; i < numBuffers; i++) { + arr[i] = buffers[i].bufferID; + } + + al.alSourceQueueBuffers(sourceID, numBuffers, arr, 0); + } + + /** + * Unqueues one or more buffers on a source. + * + * @param buffers a set of previously queued buffers. + */ + public void unqueueBuffers(Buffer[] buffers) { + int numBuffers = buffers.length; + int[] arr = new int[numBuffers]; + + for (int i = 0; i < numBuffers; i++) { + arr[i] = buffers[i].bufferID; + } + + al.alSourceUnqueueBuffers(sourceID, numBuffers, arr, 0); + } +} diff --git a/src/java/com/jogamp/openal/sound3d/Vec3f.java b/src/java/com/jogamp/openal/sound3d/Vec3f.java new file mode 100644 index 0000000..3a5aea2 --- /dev/null +++ b/src/java/com/jogamp/openal/sound3d/Vec3f.java @@ -0,0 +1,63 @@ +/** +* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* -Redistribution of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* -Redistribution in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* Neither the name of Sun Microsystems, Inc. or the names of contributors may +* be used to endorse or promote products derived from this software without +* specific prior written permission. +* +* This software is provided "AS IS," without a warranty of any kind. +* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING +* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR +* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS +* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A +* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. +* IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT +* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR +* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, +* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS +* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +* +* You acknowledge that this software is not designed or intended for use in the +* design, construction, operation or maintenance of any nuclear facility. +*/ + +package com.jogamp.openal.sound3d; + +/** + * A onvenience class representing a 3-element float vector + * + * @author Athomas Goldberg + */ +public final class Vec3f { + /** the first element in the vector */ + public final float v1; + + /** the first element in the vector */ + public final float v2; + + /** the first element in the vector */ + public final float v3; + + /** + * Creates a new Vec3f object. + * + * @param v1 the first element in the vector + * @param v2 the second element in the vector + * @param v3 the third element in the vector + */ + public Vec3f(float v1, float v2, float v3) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + } +} |