diff --git a/src/java/org/lwjgl/openal/AL.java b/src/java/org/lwjgl/openal/AL.java index 1dfdd7e4..20de9477 100644 --- a/src/java/org/lwjgl/openal/AL.java +++ b/src/java/org/lwjgl/openal/AL.java @@ -31,6 +31,8 @@ */ package org.lwjgl.openal; +import java.util.Vector; + import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLUtil; import org.lwjgl.Sys; @@ -52,7 +54,7 @@ public final class AL { /** Have we been created? */ private static boolean created; - + static { Sys.initialize(); } @@ -96,8 +98,17 @@ public final class AL { * @param contextFrequency Frequency for mixing output buffer, in units of Hz (Common values include 11025, 22050, and 44100). * @param contextRefresh Refresh intervalls, in units of Hz. * @param contextSynchronized Flag, indicating a synchronous context.* + */ + public static void create(String deviceArguments, int contextFrequency, int contextRefresh, boolean contextSynchronized) + throws LWJGLException { + create(deviceArguments, contextFrequency, contextRefresh, contextSynchronized, true); + } + + /** + * @param openDevice Whether to automatically open the device + * @see #create(String, int, int, boolean) */ - public static void create(String deviceArguments, int contextFrequency, int contextRefresh, boolean contextSynchronized) + static void create(String deviceArguments, int contextFrequency, int contextRefresh, boolean contextSynchronized, boolean openDevice) throws LWJGLException { if (created) @@ -128,18 +139,20 @@ public final class AL { AL10.initNativeStubs(); ALC.initNativeStubs(); - device = ALC.alcOpenDevice(deviceArguments); - if (device == null) - throw new LWJGLException("Could not open ALC device"); - - if (contextFrequency == -1) { - context = ALC.alcCreateContext(device.device, null); - } else { - context = ALC.alcCreateContext(device.device, - ALCcontext.createAttributeList(contextFrequency, contextRefresh, - contextSynchronized ? ALC.ALC_TRUE : ALC.ALC_FALSE)); + if(openDevice) { + device = ALC.alcOpenDevice(deviceArguments); + if (device == null) + throw new LWJGLException("Could not open ALC device"); + + if (contextFrequency == -1) { + context = ALC.alcCreateContext(device.device, null); + } else { + context = ALC.alcCreateContext(device.device, + ALCcontext.createAttributeList(contextFrequency, contextRefresh, + contextSynchronized ? ALC.ALC_TRUE : ALC.ALC_FALSE)); + } + ALC.alcMakeContextCurrent(context.context); } - ALC.alcMakeContextCurrent(context.context); } catch (LWJGLException e) { destroy(); throw e; @@ -189,4 +202,41 @@ public final class AL { public static Object getContext() { return context; } + + /** + * + * @return + */ + public static String[] getImplementations() throws LWJGLException, OpenALException { + if(AL.isCreated()) { + throw new OpenALException("Cannot enumerate devices if AL has already been created"); + } + + Vector availableDevices = new Vector(); + + try { + // init + create(null, 44100, 60, false, false); + + // check for extension + if(!ALC.alcIsExtensionPresent("ALC_ENUMERATION_EXT")) { + throw new OpenALException("ALC_ENUMERATION_EXT extension not available"); + } + + // get list of published devices + String[] publishedDevices = ALC.ngetImplementations(); + + // run through them and verify + for (int i = 0; i < publishedDevices.length; i++) { + availableDevices.add(publishedDevices[i]); + } + } finally { + destroy(); + } + + String[] available = new String[availableDevices.size()]; + availableDevices.copyInto(available); + + return available; + } } diff --git a/src/java/org/lwjgl/openal/ALC.java b/src/java/org/lwjgl/openal/ALC.java index 4df0b2f7..b5e72034 100644 --- a/src/java/org/lwjgl/openal/ALC.java +++ b/src/java/org/lwjgl/openal/ALC.java @@ -148,6 +148,13 @@ public final class ALC { } static native void initNativeStubs() throws LWJGLException; + + static long getDevice() { + if(AL.device != null) { + return AL.device.device; + } + return 0L; + } /** * The application can obtain certain strings from ALC. @@ -164,7 +171,8 @@ public final class ALC { * @return String property from device */ public static String alcGetString(int pname) { - String result = nalcGetString(AL.device.device, pname); + String result; + result = nalcGetString(getDevice(), pname); Util.checkALCError(); return result; } @@ -192,7 +200,7 @@ public final class ALC { */ public static void alcGetInteger(int pname, IntBuffer integerdata) { BufferChecks.checkDirect(integerdata); - nalcGetIntegerv(AL.device.device, pname, integerdata.remaining(), integerdata, integerdata.position()); + nalcGetIntegerv(getDevice(), pname, integerdata.remaining(), integerdata, integerdata.position()); Util.checkALCError(); } private native static void nalcGetIntegerv(long device, int pname, int size, Buffer integerdata, int offset); @@ -333,7 +341,7 @@ public final class ALC { * @return Errorcode from ALC statemachine */ public static int alcGetError() { - return nalcGetError(AL.device.device); + return nalcGetError(getDevice()); } private native static int nalcGetError(long device); @@ -347,7 +355,7 @@ public final class ALC { * @return true if extension is available, false if not */ public static boolean alcIsExtensionPresent(String extName) { - boolean result = nalcIsExtensionPresent(AL.device.device, extName); + boolean result = nalcIsExtensionPresent(getDevice(), extName); Util.checkALCError(); return result; } @@ -364,9 +372,11 @@ public final class ALC { * @return value of enumeration */ public static int alcGetEnumValue(String enumName) { - int result = nalcGetEnumValue(AL.device.device, enumName); + int result = nalcGetEnumValue(getDevice(), enumName); Util.checkALCError(); return result; } private native static int nalcGetEnumValue(long device, String enumName); + + static native String[] ngetImplementations(); } diff --git a/src/java/org/lwjgl/test/openal/BasicTest.java b/src/java/org/lwjgl/test/openal/BasicTest.java index f4450e97..89a901dc 100644 --- a/src/java/org/lwjgl/test/openal/BasicTest.java +++ b/src/java/org/lwjgl/test/openal/BasicTest.java @@ -52,6 +52,18 @@ public abstract class BasicTest { * Creates an instance of PlayTest */ public BasicTest() { + try { + String[] imps = AL.getImplementations(); + if(imps.length > 0) { + System.out.println("Available devices: "); + for(int i=0; iFindClass(env, "java/lang/String"); + strarr = (*env)->NewObjectArray(env, numDevices+1, strcls, 0); + + // add default + str = NewStringNative(env, "NULL Device (Default)"); + (*env)->SetObjectArrayElement(env, strarr, 0, str); + (*env)->DeleteLocalRef(env, str); + + for (i = 0; i < numDevices; i++) { + str = NewStringNative(env, devices[i]); + (*env)->SetObjectArrayElement(env, strarr, i+1, str); + (*env)->DeleteLocalRef(env, str); + } + + } + return strarr; +} + /** * Loads the context OpenAL functions *