diff options
author | Sven Gothel <[email protected]> | 2023-05-18 06:15:32 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-05-18 06:15:32 +0200 |
commit | 7f73d50c90d05cf7388f23977ca956a4933019ad (patch) | |
tree | 1e14df61266798b9276e71290e5acc8844bfc260 | |
parent | 2e800ac4277d6234cb9dba2afe01a4d8c75fc989 (diff) |
Sound3D: Make all OO wrapper fully transparent and stand-alone, allow mix-and-match; Align Context makeCurrent/release w/ ALAudioSink ...
Align Context makeCurrent/release w/ ALAudioSink
- use ALC_EXT_thread_local_context if available
- use a recursive lock
- same release method as makeCurrent
- ALC_EXT_thread_local_context or traditional
- destroy also releases the context and fully unwinds lock
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/AudioSystem3D.java | 101 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Buffer.java | 59 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Context.java | 112 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Device.java | 31 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Listener.java | 26 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Source.java | 137 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/sound3d/Vec3f.java | 3 | ||||
-rw-r--r-- | src/test/com/jogamp/openal/test/manual/Sound3DTest.java | 15 |
8 files changed, 330 insertions, 154 deletions
diff --git a/src/java/com/jogamp/openal/sound3d/AudioSystem3D.java b/src/java/com/jogamp/openal/sound3d/AudioSystem3D.java index 7c6d725..72b777c 100644 --- a/src/java/com/jogamp/openal/sound3d/AudioSystem3D.java +++ b/src/java/com/jogamp/openal/sound3d/AudioSystem3D.java @@ -1,4 +1,5 @@ /** + * Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,14 +40,15 @@ import java.io.InputStream; import com.jogamp.openal.AL; import com.jogamp.openal.ALC; -import com.jogamp.openal.ALCcontext; -import com.jogamp.openal.ALCdevice; import com.jogamp.openal.ALException; +import com.jogamp.openal.ALExt; import com.jogamp.openal.ALFactory; import com.jogamp.openal.UnsupportedAudioFileException; import com.jogamp.openal.util.WAVData; import com.jogamp.openal.util.WAVLoader; +import jogamp.openal.Debug; + /** * The AudioSystem3D class provides a set of methods for creating and * manipulating a 3D audio environment. @@ -54,17 +56,51 @@ import com.jogamp.openal.util.WAVLoader; * @author Athomas Goldberg */ public class AudioSystem3D { - private static AL al; - private static ALC alc; - private static Listener listener; + static boolean DEBUG = Debug.debug("AudioSystem3D"); + static final AL al; + static final ALC alc; + static final ALExt alExt; + static final boolean staticAvailable; + static Listener listener; + + static { + ALC _alc = null; + AL _al = null; + ALExt _alExt = null; + try { + _alc = ALFactory.getALC(); + _al = ALFactory.getAL(); + _alExt = ALFactory.getALExt(); + } catch(final Throwable t) { + if( DEBUG ) { + System.err.println("AudioSystem3D: Caught "+t.getClass().getName()+": "+t.getMessage()); + t.printStackTrace(); + } + } + alc = _alc; + al = _al; + alExt = _alExt; + staticAvailable = null != alc && null != al && null != alExt; + } + + /** + * Initialize the Sound3D environment. + * @deprecated Not required to be called due to static initialization + */ + @Deprecated + public static void init() throws ALException { } /** - * Iniitalize the Sound3D environment. This must be called before - * other methods in the class can be used. + * Returns the <code>available state</code> of this instance. + * <p> + * The <code>available state</code> is affected by this instance + * overall availability, i.e. after instantiation. + * </p> */ - public static void init() throws ALException { - al = ALFactory.getAL(); - alc = ALFactory.getALC(); + public static boolean isAvailable() { return staticAvailable; } + + public int getALError() { + return al.alGetError(); } /** @@ -75,10 +111,7 @@ public class AudioSystem3D { * @return The new Sound3D context. */ public static Context createContext(final Device device) { - Context result = null; - final ALCcontext realContext = alc.alcCreateContext(device.realDevice, null); - result = new Context(alc, realContext, device); - return result; + return new Context(device); } /** @@ -86,31 +119,28 @@ public class AudioSystem3D { * * @param context the context to make current. */ - public static void makeContextCurrent(final Context context) { - ALCcontext realContext = null; - - if (context != null) { - realContext = context.realContext; - } + public static boolean makeContextCurrent(final Context context) { + return context.makeCurrent(); + } - alc.alcMakeContextCurrent(realContext); + /** + * Release the specified context. + * + * @param context the context to release. + */ + public static boolean releaseContext(final Context context) { + return context.release(); } /** - * Opens the specifified audio device. + * Opens the named 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. + * @param deviceName The specified device name, null for default. * - * @return The device described by the specifed name. + * @return The device described by the specified name */ public static Device openDevice(final String deviceName) { - Device result = null; - final ALCdevice realDevice = alc.alcOpenDevice(deviceName); - result = new Device(alc, realDevice); - - return result; + return new Device(deviceName); } /** @@ -126,7 +156,7 @@ public class AudioSystem3D { al.alGenBuffers(numBuffers, arr, 0); for (int i = 0; i < numBuffers; i++) { - result[i] = new Buffer(al, arr[i]); + result[i] = new Buffer(arr[i]); } return result; @@ -241,7 +271,7 @@ public class AudioSystem3D { al.alGenSources(numSources, arr, 0); for (int i = 0; i < numSources; i++) { - result[i] = new Source(al, arr[i]); + result[i] = new Source(arr[i]); } return result; @@ -250,7 +280,7 @@ public class AudioSystem3D { /** * Generate a Sound3D source from an initialized Buffer. * - * @param buff The buffer to generate the source from. + * @param buff The buffer to be associate with the source. * * @return the newly generated Source. */ @@ -270,9 +300,8 @@ public class AudioSystem3D { */ public static Listener getListener() { if (listener == null) { - listener = new Listener(al); + listener = new Listener(); } - return listener; } } diff --git a/src/java/com/jogamp/openal/sound3d/Buffer.java b/src/java/com/jogamp/openal/sound3d/Buffer.java index 8fad7d0..52a17bd 100644 --- a/src/java/com/jogamp/openal/sound3d/Buffer.java +++ b/src/java/com/jogamp/openal/sound3d/Buffer.java @@ -1,4 +1,5 @@ /** +* Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,7 +36,6 @@ package com.jogamp.openal.sound3d; import com.jogamp.openal.AL; import com.jogamp.openal.ALConstants; - import java.nio.ByteBuffer; @@ -46,36 +46,27 @@ import java.nio.ByteBuffer; * @author Athomas Goldberg */ public class Buffer { - public final static int FORMAT_MONO8 = AL.AL_FORMAT_MONO8; + 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 int alBufferID; private ByteBuffer data; - private final boolean isConfigured = false; - private final AL al; - Buffer(final AL al, final int bufferID) { - this.bufferID = bufferID; - this.al = al; + public Buffer(final int bufferID) { + this.alBufferID = bufferID; } - /** - * 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(final ByteBuffer data, final int format, final int freq) { - if (!isConfigured) { - this.data = data; - al.alBufferData(bufferID, format, data, data.capacity(), freq); - } + /** Return the OpenAL buffer ID, -1 if invalid. */ + public int getID() { return alBufferID; } + + /** Returns whether {@link #getID()} is valid, i.e. not {@link #delete()}'ed */ + public boolean isValid() { + return 0 <= alBufferID && AudioSystem3D.al.alIsBuffer(alBufferID); } /** @@ -83,7 +74,23 @@ public class Buffer { */ public void delete() { data = null; - al.alDeleteBuffers(1, new int[] { bufferID }, 0); + if( 0 <= alBufferID ) { + AudioSystem3D.al.alDeleteBuffers(1, new int[] { alBufferID }, 0); + alBufferID = -1; + } + } + + /** + * Configure the Sound3D buffer + * + * @param data the raw audio data + * @param alFormat the OpenAL format of the data, e.g. <code>FORMAT_MONO8, FORMAT_MONO16, + * FORMAT_STEREO8</code> and <code>FORMAT_STEREO16</code> + * @param freq the frequency of the data + */ + public void configure(final ByteBuffer data, final int alFormat, final int freq) { + this.data = data; + AudioSystem3D.al.alBufferData(alBufferID, alFormat, data, data.capacity(), freq); } /** @@ -93,7 +100,7 @@ public class Buffer { */ public int getBitDepth() { final int[] i = new int[1]; - al.alGetBufferi(bufferID, ALConstants.AL_BITS, i, 0); + AudioSystem3D.al.alGetBufferi(alBufferID, ALConstants.AL_BITS, i, 0); return i[0]; } @@ -105,7 +112,7 @@ public class Buffer { */ public int getNumChannels() { final int[] i = new int[1]; - al.alGetBufferi(bufferID, ALConstants.AL_CHANNELS, i, 0); + AudioSystem3D.al.alGetBufferi(alBufferID, ALConstants.AL_CHANNELS, i, 0); return i[0]; } @@ -126,7 +133,7 @@ public class Buffer { */ public int getFrequency() { final int[] i = new int[1]; - al.alGetBufferi(bufferID, ALConstants.AL_FREQUENCY, i, 0); + AudioSystem3D.al.alGetBufferi(alBufferID, ALConstants.AL_FREQUENCY, i, 0); return i[0]; } @@ -138,7 +145,7 @@ public class Buffer { */ public int getSize() { final int[] i = new int[1]; - al.alGetBufferi(bufferID, ALConstants.AL_SIZE, i, 0); + AudioSystem3D.al.alGetBufferi(alBufferID, ALConstants.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 index 7657d98..6cd19d6 100644 --- a/src/java/com/jogamp/openal/sound3d/Context.java +++ b/src/java/com/jogamp/openal/sound3d/Context.java @@ -1,4 +1,5 @@ /** +* Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,7 +34,10 @@ package com.jogamp.openal.sound3d; +import com.jogamp.common.util.locks.LockFactory; +import com.jogamp.common.util.locks.RecursiveLock; import com.jogamp.openal.*; +import com.jogamp.openal.util.ALHelpers; /** @@ -42,28 +46,116 @@ import com.jogamp.openal.*; * @author Athomas Goldberg */ public class Context { - private final ALC alc; - final ALCcontext realContext; - final Device device; + private final RecursiveLock lock = LockFactory.createRecursiveLock(); + private Device device; + private ALCcontext alCtx; + private boolean threadContextLocked; + private boolean hasALC_thread_local_context; - Context(final ALC alc, final ALCcontext realContext, final Device device) { - this.alc = alc; - this.realContext = realContext; + public Context(final ALCcontext realContext, final Device device) { this.device = device; + this.alCtx = realContext; + { + hasALC_thread_local_context = false; + final boolean v; + if( makeCurrent() ) { + v = AudioSystem3D.alc.alcIsExtensionPresent(null, ALHelpers.ALC_EXT_thread_local_context) || + AudioSystem3D.alc.alcIsExtensionPresent(device.getALDevice(), ALHelpers.ALC_EXT_thread_local_context); + release(); + } else { + v = false; + } + hasALC_thread_local_context = v; + } } /** - * Suspend this context + * Creates a new Context for a specified device. + * + * @param device The device the Context is being created for. */ - public void suspend() { - alc.alcSuspendContext(realContext); + public Context(final Device device) { + this.device = device; + this.alCtx = AudioSystem3D.alc.alcCreateContext(device.getALDevice(), null); + } + + /** + * Returns the OpenAL context. + */ + public ALCcontext getALContext() { + return alCtx; + } + + /** Returns whether {@link #getALContext()} is valid, i.e. not null, e.g. not {@link #destroy()}'ed. */ + public boolean isValid() { return null != alCtx; } + + public int getALCError() { + return AudioSystem3D.alc.alcGetError(device.getALDevice()); } /** * destroys this context freeing its resources. */ public void destroy() { - alc.alcDestroyContext(realContext); + lock.lock(); + try { + if( null != alCtx ) { + if( threadContextLocked ) { + AudioSystem3D.alExt.alcSetThreadContext(null); + } else { + AudioSystem3D.alc.alcMakeContextCurrent(null); + } + AudioSystem3D.alc.alcDestroyContext(alCtx); + alCtx = null; + } + device = null; + // unroll lock ! + while(lock.getHoldCount() > 1) { + lock.unlock(); + } + } finally { + lock.unlock(); + } + } + + /** + * Makes this context current. + */ + public boolean makeCurrent() { + final boolean r; + lock.lock(); + if( hasALC_thread_local_context ) { + threadContextLocked = true; + r = AudioSystem3D.alExt.alcSetThreadContext(alCtx); + } else { + threadContextLocked = false; + r = AudioSystem3D.alc.alcMakeContextCurrent(alCtx); + } + if( !r ) { + lock.unlock(); + } + return r; + } + + /** + * Release this context. + */ + public boolean release() { + final boolean r; + if( threadContextLocked ) { + r = AudioSystem3D.alExt.alcSetThreadContext(null); + } else { + r = AudioSystem3D.alc.alcMakeContextCurrent(null); + } + lock.unlock(); + return r; + } + + /** + * Suspend this context + */ + public void suspend() { + AudioSystem3D.alc.alcSuspendContext(alCtx); } /** diff --git a/src/java/com/jogamp/openal/sound3d/Device.java b/src/java/com/jogamp/openal/sound3d/Device.java index 3000faf..749d606 100644 --- a/src/java/com/jogamp/openal/sound3d/Device.java +++ b/src/java/com/jogamp/openal/sound3d/Device.java @@ -1,4 +1,5 @@ /** +* Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -42,18 +43,36 @@ import com.jogamp.openal.*; * @author Athomas Goldberg */ public class Device { - private final ALC alc; - final ALCdevice realDevice; + private ALCdevice alDev; - Device(final ALC alc, final ALCdevice realDevice) { - this.alc = alc; - this.realDevice = realDevice; + public Device(final ALCdevice realDevice) { + this.alDev = realDevice; } /** + * Create a new device by opening the named audio device. + * + * @param deviceName The specified device name, null for default. + */ + public Device(final String deviceName) { + this.alDev = AudioSystem3D.alc.alcOpenDevice(deviceName); + } + + /** + * Returns the OpenAL context. + */ + public ALCdevice getALDevice() { + return alDev; + } + + /** Returns whether {@link #getALDevice()} is valid, i.e. not null, e.g. not {@link #close()}. */ + public boolean isValid() { return null != alDev; } + + /** * closes the device, freeing its resources. */ public void close() { - alc.alcCloseDevice(realDevice); + AudioSystem3D.alc.alcCloseDevice(alDev); + alDev = null; } } diff --git a/src/java/com/jogamp/openal/sound3d/Listener.java b/src/java/com/jogamp/openal/sound3d/Listener.java index 27b2745..86a9307 100644 --- a/src/java/com/jogamp/openal/sound3d/Listener.java +++ b/src/java/com/jogamp/openal/sound3d/Listener.java @@ -1,4 +1,5 @@ /** +* Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,10 +34,8 @@ package com.jogamp.openal.sound3d; -import com.jogamp.openal.AL; import com.jogamp.openal.ALConstants; - /** * This class represents the human listener in the Sound3D environment. It * provides methods for controlling the position, orientation as well as other @@ -45,10 +44,7 @@ import com.jogamp.openal.ALConstants; * @author Athomas Goldberg */ public class Listener { - private final AL al; - - Listener(final AL al) { - this.al = al; + public Listener() { } /** @@ -58,7 +54,7 @@ public class Listener { * @param gain the gain, or volume */ public void setGain(final float gain) { - al.alListenerf(ALConstants.AL_GAIN, gain); + AudioSystem3D.al.alListenerf(ALConstants.AL_GAIN, gain); } /** @@ -69,7 +65,7 @@ public class Listener { */ public float getGain() { final float[] f = new float[1]; - al.alGetListenerf(ALConstants.AL_GAIN, f, 0); + AudioSystem3D.al.alGetListenerf(ALConstants.AL_GAIN, f, 0); return f[0]; } @@ -86,7 +82,7 @@ public class Listener { * environment */ public void setPosition(final float x, final float y, final float z) { - al.alListener3f(ALConstants.AL_POSITION, x, y, z); + AudioSystem3D.al.alListener3f(ALConstants.AL_POSITION, x, y, z); } /** @@ -97,7 +93,7 @@ public class Listener { * Listener. */ public void setPosition(final Vec3f position) { - al.alListener3f(ALConstants.AL_POSITION, position.v1, position.v2, position.v3); + AudioSystem3D.al.alListener3f(ALConstants.AL_POSITION, position.v1, position.v2, position.v3); } /** @@ -110,7 +106,7 @@ public class Listener { public Vec3f getPosition() { Vec3f result = null; final float[] tmp = new float[3]; - al.alGetListenerfv(ALConstants.AL_POSITION, tmp, 0); + AudioSystem3D.al.alGetListenerfv(ALConstants.AL_POSITION, tmp, 0); result = new Vec3f(tmp[0], tmp[1], tmp[2]); return result; @@ -124,7 +120,7 @@ public class Listener { * x,y and z coordinates of Listener. */ public void setVelocity(final Vec3f velocity) { - al.alListener3f(ALConstants.AL_VELOCITY, velocity.v1, velocity.v2, velocity.v3); + AudioSystem3D.al.alListener3f(ALConstants.AL_VELOCITY, velocity.v1, velocity.v2, velocity.v3); } /** @@ -137,7 +133,7 @@ public class Listener { public Vec3f getVelocity() { Vec3f result = null; final float[] tmp = new float[3]; - al.alGetListenerfv(ALConstants.AL_VELOCITY, tmp, 0); + AudioSystem3D.al.alGetListenerfv(ALConstants.AL_VELOCITY, tmp, 0); result = new Vec3f(tmp[0], tmp[1], tmp[2]); return result; @@ -152,7 +148,7 @@ public class Listener { * look-at vector. */ public void setOrientation(final float[] orientation) { - al.alListenerfv(ALConstants.AL_ORIENTATION, orientation, 0); + AudioSystem3D.al.alListenerfv(ALConstants.AL_ORIENTATION, orientation, 0); } /** @@ -166,7 +162,7 @@ public class Listener { */ public float[] getOrientation() { final float[] tmp = new float[6]; - al.alGetListenerfv(ALConstants.AL_ORIENTATION, tmp, 0); + AudioSystem3D.al.alGetListenerfv(ALConstants.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 index 5926d9b..b3b0da9 100644 --- a/src/java/com/jogamp/openal/sound3d/Source.java +++ b/src/java/com/jogamp/openal/sound3d/Source.java @@ -1,4 +1,5 @@ /** +* Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,7 +34,6 @@ package com.jogamp.openal.sound3d; -import com.jogamp.openal.AL; import com.jogamp.openal.ALConstants; /** @@ -45,48 +45,68 @@ import com.jogamp.openal.ALConstants; * @author Athomas Goldberg */ public final class Source { - private final AL al; - private final int sourceID; + private int sourceID; private Buffer buffer; - Source(final AL al, final int sourceID) { - this.al = al; + public Source(final int sourceID) { this.sourceID = sourceID; } + /** Return the OpenAL source ID, -1 if invalid. */ + public int getID() { return sourceID; } + + /** Returns whether {@link #getID()} is valid, i.e. not {@link #delete()}'ed */ + public boolean isValid() { + return 0 <= sourceID && AudioSystem3D.al.alIsSource(sourceID); + } + + /** + * Delete this source, freeing its resources. + */ + public void delete() { + if( 0 <= sourceID ) { + final Buffer b = buffer; + stop(); + if( null != b ) { + setBuffer(null); // buffer = null + } + AudioSystem3D.al.alDeleteSources(1, new int[] { sourceID }, 0); + if( null != b ) { + b.delete(); + } + sourceID = -1; + } else if( null != buffer ) { + buffer.delete(); + buffer = null; + } + } + /** * Beginning playing the audio in this source. */ public void play() { - al.alSourcePlay(sourceID); + AudioSystem3D.al.alSourcePlay(sourceID); } /** * pauses the audio in this Source. */ public void pause() { - al.alSourcePause(sourceID); + AudioSystem3D.al.alSourcePause(sourceID); } /** * Stops the audio in this Source */ public void stop() { - al.alSourceStop(sourceID); + AudioSystem3D.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); + AudioSystem3D.al.alSourceRewind(sourceID); } /** @@ -96,7 +116,7 @@ public final class Source { */ public boolean isPlaying() { final int[] result = new int[1]; - al.alGetSourcei(sourceID, ALConstants.AL_SOURCE_STATE, result, 0); + AudioSystem3D.al.alGetSourcei(sourceID, ALConstants.AL_SOURCE_STATE, result, 0); return result[0] == ALConstants.AL_PLAYING; } @@ -107,7 +127,7 @@ public final class Source { * @param pitch the pitch value of this source. */ public void setPitch(final float pitch) { - al.alSourcef(sourceID, ALConstants.AL_PITCH, pitch); + AudioSystem3D.al.alSourcef(sourceID, ALConstants.AL_PITCH, pitch); } /** @@ -118,7 +138,7 @@ public final class Source { */ public float getPitch() { final float[] result = new float[1]; - al.alGetSourcef(sourceID, ALConstants.AL_PITCH, result, 0); + AudioSystem3D.al.alGetSourcef(sourceID, ALConstants.AL_PITCH, result, 0); return result[0]; } @@ -130,7 +150,7 @@ public final class Source { * @param gain the gain of the audio on this source */ public void setGain(final float gain) { - al.alSourcef(sourceID, ALConstants.AL_GAIN, gain); + AudioSystem3D.al.alSourcef(sourceID, ALConstants.AL_GAIN, gain); } /** @@ -141,7 +161,7 @@ public final class Source { */ public float getGain() { final float[] result = new float[1]; - al.alGetSourcef(sourceID, ALConstants.AL_GAIN, result, 0); + AudioSystem3D.al.alGetSourcef(sourceID, ALConstants.AL_GAIN, result, 0); return result[0]; } @@ -153,7 +173,7 @@ public final class Source { * @param maxDistance the max ditance for source attentuation. */ public void setMaxDistance(final float maxDistance) { - al.alSourcef(sourceID, ALConstants.AL_MAX_DISTANCE, maxDistance); + AudioSystem3D.al.alSourcef(sourceID, ALConstants.AL_MAX_DISTANCE, maxDistance); } /** @@ -164,7 +184,7 @@ public final class Source { */ public float getMaxDistance() { final float[] result = new float[1]; - al.alGetSourcef(sourceID, ALConstants.AL_MAX_DISTANCE, result, 0); + AudioSystem3D.al.alGetSourcef(sourceID, ALConstants.AL_MAX_DISTANCE, result, 0); return result[0]; } @@ -175,7 +195,7 @@ public final class Source { * @param rolloffFactor the rolloff rate of the source. */ public void setRolloffFactor(final float rolloffFactor) { - al.alSourcef(sourceID, ALConstants.AL_ROLLOFF_FACTOR, rolloffFactor); + AudioSystem3D.al.alSourcef(sourceID, ALConstants.AL_ROLLOFF_FACTOR, rolloffFactor); } /** @@ -185,7 +205,7 @@ public final class Source { */ public float getRolloffFactor() { final float[] result = new float[1]; - al.alGetSourcef(sourceID, ALConstants.AL_ROLLOFF_FACTOR, result, 0); + AudioSystem3D.al.alGetSourcef(sourceID, ALConstants.AL_ROLLOFF_FACTOR, result, 0); return result[0]; } @@ -197,7 +217,7 @@ public final class Source { * @param referenceDistance the reference distance for the source. */ public void setReferenceDistance(final float referenceDistance) { - al.alSourcef(sourceID, ALConstants.AL_REFERENCE_DISTANCE, referenceDistance); + AudioSystem3D.al.alSourcef(sourceID, ALConstants.AL_REFERENCE_DISTANCE, referenceDistance); } /** @@ -208,7 +228,7 @@ public final class Source { */ public float getReferenceDistance() { final float[] result = new float[1]; - al.alGetSourcef(sourceID, ALConstants.AL_REFERENCE_DISTANCE, result, 0); + AudioSystem3D.al.alGetSourcef(sourceID, ALConstants.AL_REFERENCE_DISTANCE, result, 0); return result[0]; } @@ -219,7 +239,7 @@ public final class Source { * @param minGain the minimum gain for this source. */ public void setMinGain(final float minGain) { - al.alSourcef(sourceID, ALConstants.AL_MIN_GAIN, minGain); + AudioSystem3D.al.alSourcef(sourceID, ALConstants.AL_MIN_GAIN, minGain); } /** @@ -229,7 +249,7 @@ public final class Source { */ public float getMinGain() { final float[] result = new float[1]; - al.alGetSourcef(sourceID, ALConstants.AL_MIN_GAIN, result, 0); + AudioSystem3D.al.alGetSourcef(sourceID, ALConstants.AL_MIN_GAIN, result, 0); return result[0]; } @@ -240,7 +260,7 @@ public final class Source { * @param maxGain the maximum gain for this source */ public void setMaxGain(final float maxGain) { - al.alSourcef(sourceID, ALConstants.AL_MAX_GAIN, maxGain); + AudioSystem3D.al.alSourcef(sourceID, ALConstants.AL_MAX_GAIN, maxGain); } /** @@ -250,7 +270,7 @@ public final class Source { */ public float getMaxGain() { final float[] result = new float[1]; - al.alGetSourcef(sourceID, ALConstants.AL_MAX_GAIN, result, 0); + AudioSystem3D.al.alGetSourcef(sourceID, ALConstants.AL_MAX_GAIN, result, 0); return result[0]; } @@ -261,7 +281,7 @@ public final class Source { * @param coneOuterGain the gain when outside the oriented cone. */ public void setConeOuterGain(final float coneOuterGain) { - al.alSourcef(sourceID, ALConstants.AL_CONE_OUTER_GAIN, coneOuterGain); + AudioSystem3D.al.alSourcef(sourceID, ALConstants.AL_CONE_OUTER_GAIN, coneOuterGain); } /** @@ -271,7 +291,7 @@ public final class Source { */ public float getConeOuterGain() { final float[] result = new float[1]; - al.alGetSourcef(sourceID, ALConstants.AL_CONE_OUTER_GAIN, result, 0); + AudioSystem3D.al.alGetSourcef(sourceID, ALConstants.AL_CONE_OUTER_GAIN, result, 0); return result[0]; } @@ -283,7 +303,7 @@ public final class Source { * source. */ public void setPosition(final Vec3f position) { - al.alSource3f( + AudioSystem3D.al.alSource3f( sourceID, ALConstants.AL_POSITION, position.v1, @@ -299,7 +319,7 @@ public final class Source { * @param z the z position of the source. */ public void setPosition(final float x, final float y, final float z) { - al.alSource3f(sourceID, ALConstants.AL_POSITION, x, y, z); + AudioSystem3D.al.alSource3f(sourceID, ALConstants.AL_POSITION, x, y, z); } /** @@ -311,7 +331,7 @@ public final class Source { public Vec3f getPosition() { Vec3f result = null; final float[] pos = new float[3]; - al.alGetSourcefv(sourceID, ALConstants.AL_POSITION, pos, 0); + AudioSystem3D.al.alGetSourcefv(sourceID, ALConstants.AL_POSITION, pos, 0); result = new Vec3f(pos[0], pos[1], pos[2]); return result; @@ -323,7 +343,7 @@ public final class Source { * @param velocity the velocity vector of the source */ public void setVelocity(final Vec3f velocity) { - al.alSource3f( + AudioSystem3D.al.alSource3f( sourceID, ALConstants.AL_VELOCITY, velocity.v1, @@ -339,7 +359,7 @@ public final class Source { * @param z the z velocity of the source. */ public void setVelocity(final float x, final float y, final float z) { - al.alSource3f(sourceID, ALConstants.AL_VELOCITY, x, y, z); + AudioSystem3D.al.alSource3f(sourceID, ALConstants.AL_VELOCITY, x, y, z); } /** @@ -350,7 +370,7 @@ public final class Source { public Vec3f getVelocity() { Vec3f result = null; final float[] vel = new float[3]; - al.alGetSourcefv(sourceID, ALConstants.AL_VELOCITY, vel, 0); + AudioSystem3D.al.alGetSourcefv(sourceID, ALConstants.AL_VELOCITY, vel, 0); result = new Vec3f(vel[0], vel[1], vel[2]); return result; @@ -362,7 +382,7 @@ public final class Source { * @param direction the direction vector of the source. */ public void setDirection(final Vec3f direction) { - al.alSource3f( + AudioSystem3D.al.alSource3f( sourceID, ALConstants.AL_DIRECTION, direction.v1, @@ -378,7 +398,7 @@ public final class Source { * @param z the z direction of the source. */ public void setDirection(final float x, final float y, final float z) { - al.alSource3f(sourceID, ALConstants.AL_DIRECTION, x, y, z); + AudioSystem3D.al.alSource3f(sourceID, ALConstants.AL_DIRECTION, x, y, z); } /** @@ -389,7 +409,7 @@ public final class Source { public Vec3f getDirection() { Vec3f result = null; final float[] dir = new float[3]; - al.alGetSourcefv(sourceID, ALConstants.AL_DIRECTION, dir, 0); + AudioSystem3D.al.alGetSourcefv(sourceID, ALConstants.AL_DIRECTION, dir, 0); result = new Vec3f(dir[0], dir[1], dir[2]); return result; @@ -404,7 +424,7 @@ public final class Source { */ public void setSourceRelative(final boolean isRelative) { final int rel = isRelative ? 1 : 0; - al.alSourcei(sourceID, ALConstants.AL_SOURCE_RELATIVE, rel); + AudioSystem3D.al.alSourcei(sourceID, ALConstants.AL_SOURCE_RELATIVE, rel); } /** @@ -416,7 +436,7 @@ public final class Source { */ public boolean isSourceRelative() { final int[] result = new int[1]; - al.alGetSourcei(sourceID, ALConstants.AL_SOURCE_RELATIVE, result, 0); + AudioSystem3D.al.alGetSourcei(sourceID, ALConstants.AL_SOURCE_RELATIVE, result, 0); return result[0] == 1; } @@ -428,7 +448,7 @@ public final class Source { */ public void setLooping(final boolean isLooping) { final int loop = isLooping ? 1 : 0; - al.alSourcei(sourceID, ALConstants.AL_LOOPING, loop); + AudioSystem3D.al.alSourcei(sourceID, ALConstants.AL_LOOPING, loop); } /** @@ -437,9 +457,8 @@ public final class Source { * @return true-looping is on, false-looping is off */ public boolean getLooping() { - final boolean result = false; final int[] tmp = new int[1]; - al.alGetSourcei(sourceID, ALConstants.AL_LOOPING, tmp, 0); + AudioSystem3D.al.alGetSourcei(sourceID, ALConstants.AL_LOOPING, tmp, 0); return tmp[0] == ALConstants.AL_TRUE; } @@ -450,7 +469,7 @@ public final class Source { */ public int getBuffersQueued() { final int[] result = new int[1]; - al.alGetSourcei(sourceID, ALConstants.AL_BUFFERS_QUEUED, result, 0); + AudioSystem3D.al.alGetSourcei(sourceID, ALConstants.AL_BUFFERS_QUEUED, result, 0); return result[0]; } @@ -461,18 +480,24 @@ public final class Source { */ public int getBuffersProcessed() { final int[] result = new int[1]; - al.alGetSourcei(sourceID, ALConstants.AL_BUFFERS_PROCESSED, result, 0); + AudioSystem3D.al.alGetSourcei(sourceID, ALConstants.AL_BUFFERS_PROCESSED, result, 0); return result[0]; } /** - * Sets the buffer associated with this source. + * Associates the buffer with this source if buffer is not null, + * otherwise disassociates the previously associated buffer from this source. * - * @param buffer the buffer associated with this source + * @param buffer the buffer to be associated with this source if not null. + * If null, disassociates the current buffer from this source. */ public void setBuffer(final Buffer buffer) { - al.alSourcei(sourceID, ALConstants.AL_BUFFER, buffer.bufferID); + if( null != buffer ) { + AudioSystem3D.al.alSourcei(sourceID, ALConstants.AL_BUFFER, buffer.getID()); + } else { + AudioSystem3D.al.alSourcei(sourceID, ALConstants.AL_BUFFER, 0); + } this.buffer = buffer; } @@ -496,10 +521,10 @@ public final class Source { final int[] arr = new int[numBuffers]; for (int i = 0; i < numBuffers; i++) { - arr[i] = buffers[i].bufferID; + arr[i] = buffers[i].getID(); } - al.alSourceQueueBuffers(sourceID, numBuffers, arr, 0); + AudioSystem3D.al.alSourceQueueBuffers(sourceID, numBuffers, arr, 0); } /** @@ -512,9 +537,9 @@ public final class Source { final int[] arr = new int[numBuffers]; for (int i = 0; i < numBuffers; i++) { - arr[i] = buffers[i].bufferID; + arr[i] = buffers[i].getID(); } - al.alSourceUnqueueBuffers(sourceID, numBuffers, arr, 0); + AudioSystem3D.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 index 8112a15..92855b0 100644 --- a/src/java/com/jogamp/openal/sound3d/Vec3f.java +++ b/src/java/com/jogamp/openal/sound3d/Vec3f.java @@ -1,4 +1,5 @@ /** +* Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -34,7 +35,7 @@ package com.jogamp.openal.sound3d; /** - * A onvenience class representing a 3-element float vector + * A convenience class representing a 3-element float vector * * @author Athomas Goldberg */ diff --git a/src/test/com/jogamp/openal/test/manual/Sound3DTest.java b/src/test/com/jogamp/openal/test/manual/Sound3DTest.java index 27a4baa..1a792b6 100644 --- a/src/test/com/jogamp/openal/test/manual/Sound3DTest.java +++ b/src/test/com/jogamp/openal/test/manual/Sound3DTest.java @@ -1,6 +1,5 @@ -package com.jogamp.openal.test.manual; - /** + * Copyright (c) 2010-2023 JogAmp Community. All rights reserved. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -32,6 +31,8 @@ package com.jogamp.openal.test.manual; * 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.test.manual; + import java.io.IOException; import com.jogamp.openal.UnsupportedAudioFileException; @@ -56,12 +57,18 @@ public class Sound3DTest { public static void main(final String[] args) throws IOException, InterruptedException, UnsupportedAudioFileException { - AudioSystem3D.init(); + if( !AudioSystem3D.isAvailable() ) { + System.err.println("AudioSystem3D is not available, static initialization failed"); + return; + } // create the initial context - this can be collapsed into the init. final Device device = AudioSystem3D.openDevice(null); final Context context = AudioSystem3D.createContext(device); - AudioSystem3D.makeContextCurrent(context); + if( !context.makeCurrent() ) { + System.err.println("Context.makeCurrent() failed"); + return; + } // get the listener object final Listener listener = AudioSystem3D.getListener(); |