support for ALC_ENUMERATION_EXT using LWJGL specific method: AL.getImplementations

This commit is contained in:
Brian Matzon 2006-02-02 22:34:28 +00:00
parent bf84c8b361
commit ac3a70926b
4 changed files with 137 additions and 18 deletions

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -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; i<imps.length; i++) {
System.out.println(" " + i + ": " + imps[i]);
}
}
} catch (Exception e) {
System.out.println("Unable to query available devices: " + e.getMessage());
}
try {
AL.create();
} catch (Exception e) {

View File

@ -343,6 +343,53 @@ static jint JNICALL Java_org_lwjgl_openal_ALC_nalcGetEnumValue (JNIEnv *env, jcl
return result;
}
JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_openal_ALC_ngetImplementations(JNIEnv *env, jclass clazz) {
jclass strcls;
jarray strarr;
jstring str;
char *deviceList;
char *devices[12];
int numDevices, i;
// get device list
deviceList = (char *)alcGetString(NULL, 4100L);
// build list
for (numDevices = 0; numDevices < 12; numDevices++) {devices[numDevices] = NULL;}
for (numDevices = 0; numDevices < 12; numDevices++) {
devices[numDevices] = deviceList;
deviceList += strlen(deviceList);
if (deviceList[0] == 0) {
if (deviceList[1] == 0) {
break;
} else {
deviceList += 1;
}
}
}
// iterate found devices and add to java string array
if (devices[numDevices] != NULL) {
numDevices++;
strcls = (*env)->FindClass(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
*