2003-08-17 12:38:57 -04:00
|
|
|
/*
|
2004-06-12 16:28:34 -04:00
|
|
|
* Copyright (c) 2002-2004 LWJGL Project
|
2003-08-17 12:38:57 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions 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.
|
|
|
|
*
|
2004-06-12 16:28:34 -04:00
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
2003-08-17 12:38:57 -04:00
|
|
|
* its contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
package org.lwjgl.openal;
|
|
|
|
|
2004-02-26 16:51:58 -05:00
|
|
|
import java.io.File;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
2005-03-09 08:06:17 -05:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2004-03-27 10:05:24 -05:00
|
|
|
import org.lwjgl.LWJGLException;
|
2004-05-05 10:28:40 -04:00
|
|
|
import org.lwjgl.Sys;
|
2004-02-26 16:51:58 -05:00
|
|
|
|
2003-08-17 12:38:57 -04:00
|
|
|
/**
|
|
|
|
* $Id$
|
|
|
|
* <br>
|
|
|
|
* This is the OpenAL class. It extends the latest core.
|
|
|
|
*
|
|
|
|
* @author Brian Matzon <brian@matzon.dk>
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
2004-08-01 17:26:25 -04:00
|
|
|
public final class AL {
|
2003-08-17 12:38:57 -04:00
|
|
|
|
|
|
|
/** ALC instance. */
|
|
|
|
protected static ALC alc;
|
|
|
|
|
|
|
|
/** ALCdevice instance. */
|
|
|
|
protected static ALCdevice device;
|
|
|
|
|
|
|
|
/** Current ALCcontext. */
|
|
|
|
protected static ALCcontext context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String that requests a certain device or device configuration.
|
|
|
|
* If null is specified, the implementation will provide an
|
2004-02-26 16:51:58 -05:00
|
|
|
* implementation specific default.
|
2004-03-08 10:21:10 -05:00
|
|
|
*/
|
2003-08-17 12:38:57 -04:00
|
|
|
protected static String deviceArguments;
|
|
|
|
|
|
|
|
/** Frequency for mixing output buffer, in units of Hz. */
|
|
|
|
protected static int contextFrequency = -1;
|
|
|
|
|
|
|
|
/** Refresh intervalls, in units of Hz. */
|
|
|
|
protected static int contextRefresh = -1;
|
|
|
|
|
|
|
|
/** Flag, indicating a synchronous context. */
|
|
|
|
protected static int contextSynchronized = ALC.ALC_FALSE;
|
|
|
|
|
2004-03-08 10:21:10 -05:00
|
|
|
/** Have we been created? */
|
|
|
|
protected static boolean created;
|
|
|
|
|
|
|
|
static {
|
2004-03-27 08:48:58 -05:00
|
|
|
Sys.initialize();
|
2004-03-08 10:21:10 -05:00
|
|
|
}
|
|
|
|
|
2004-07-31 05:34:09 -04:00
|
|
|
private AL() {
|
|
|
|
}
|
|
|
|
|
2004-03-08 10:21:10 -05:00
|
|
|
/**
|
|
|
|
* Native method to create AL instance
|
|
|
|
*
|
|
|
|
* @param oalPaths Array of strings containing paths to search for OpenAL library
|
|
|
|
*/
|
2004-03-27 10:05:24 -05:00
|
|
|
protected static native void nCreate(String[] oalPaths) throws LWJGLException;
|
2004-03-08 10:21:10 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Native method the destroy the AL
|
|
|
|
*/
|
|
|
|
protected static native void nDestroy();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if AL has been created
|
|
|
|
*/
|
|
|
|
public static boolean isCreated() {
|
|
|
|
return created;
|
|
|
|
}
|
|
|
|
|
2003-08-17 12:38:57 -04:00
|
|
|
/**
|
2004-03-27 09:42:47 -05:00
|
|
|
* Creates an OpenAL instance. Using this constructor will cause OpenAL to
|
|
|
|
* open the device using supplied device argument, and create a context using the context values
|
|
|
|
* supplied.
|
|
|
|
*
|
|
|
|
* @param deviceArguments Arguments supplied to native device
|
|
|
|
* @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.*
|
2003-08-17 12:38:57 -04:00
|
|
|
*/
|
|
|
|
public static void create(String deviceArguments, int contextFrequency, int contextRefresh, boolean contextSynchronized)
|
2004-03-27 10:05:24 -05:00
|
|
|
throws LWJGLException {
|
2004-03-08 10:21:10 -05:00
|
|
|
|
|
|
|
if (created) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-17 12:38:57 -04:00
|
|
|
AL.deviceArguments = deviceArguments;
|
|
|
|
AL.contextFrequency = contextFrequency;
|
|
|
|
AL.contextRefresh = contextRefresh;
|
|
|
|
AL.contextSynchronized = contextSynchronized ? ALC.ALC_TRUE : ALC.ALC_FALSE;
|
|
|
|
|
|
|
|
create();
|
|
|
|
}
|
|
|
|
|
2004-07-04 09:58:11 -04:00
|
|
|
private static String[] getOALPaths() throws LWJGLException {
|
2004-03-08 10:21:10 -05:00
|
|
|
// need to pass path of possible locations of OAL to native side
|
2005-03-09 08:06:17 -05:00
|
|
|
List possible_paths = new ArrayList();
|
|
|
|
|
2004-05-05 10:28:40 -04:00
|
|
|
String osName = System.getProperty("os.name");
|
2005-03-09 08:06:17 -05:00
|
|
|
|
|
|
|
String libname;
|
2005-03-09 08:28:16 -05:00
|
|
|
String platform_lib_name;
|
2004-05-05 10:28:40 -04:00
|
|
|
if (osName.startsWith("Win")) {
|
2005-03-09 08:06:17 -05:00
|
|
|
libname = "lwjglaudio";
|
2005-03-09 08:28:16 -05:00
|
|
|
platform_lib_name = "lwjglaudio.dll";
|
2004-05-05 10:28:40 -04:00
|
|
|
} else if (osName.startsWith("Lin")) {
|
2005-03-09 08:06:17 -05:00
|
|
|
libname = "openal";
|
2005-03-09 08:28:16 -05:00
|
|
|
platform_lib_name = "libopenal.so";
|
2004-05-05 10:28:40 -04:00
|
|
|
} else if (osName.startsWith("Mac")) {
|
2005-03-09 08:06:17 -05:00
|
|
|
libname = "openal";
|
2005-03-09 08:28:16 -05:00
|
|
|
platform_lib_name = "openal.dylib";
|
2004-05-05 10:28:40 -04:00
|
|
|
} else {
|
|
|
|
throw new LWJGLException("Unknown platform: "+osName);
|
2004-03-08 10:21:10 -05:00
|
|
|
}
|
|
|
|
|
2005-03-09 08:06:17 -05:00
|
|
|
// Add all possible paths from java.library.path
|
|
|
|
String java_library_path = System.getProperty("java.library.path");
|
|
|
|
StringTokenizer st = new StringTokenizer(System.getProperty("java.library.path"), File.pathSeparator);
|
|
|
|
while (st.hasMoreTokens()) {
|
|
|
|
String path = st.nextToken();
|
|
|
|
possible_paths.add(path + File.separator + platform_lib_name);
|
2004-11-22 04:43:52 -05:00
|
|
|
}
|
2005-03-09 08:06:17 -05:00
|
|
|
|
|
|
|
String classloader_path = getPathFromClassLoader(libname);
|
|
|
|
if (classloader_path != null) {
|
|
|
|
Sys.log("getPathFromClassLoader: Path found: " + classloader_path);
|
|
|
|
possible_paths.add(classloader_path);
|
2004-03-08 10:21:10 -05:00
|
|
|
}
|
2005-03-09 08:06:17 -05:00
|
|
|
String lwjgl_classloader_path = getPathFromClassLoader("lwjgl");
|
|
|
|
if (lwjgl_classloader_path != null) {
|
|
|
|
Sys.log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path);
|
|
|
|
possible_paths.add(lwjgl_classloader_path.substring(0, lwjgl_classloader_path.lastIndexOf(File.separator))
|
|
|
|
+ File.separator + platform_lib_name);
|
2004-11-22 04:43:52 -05:00
|
|
|
}
|
2004-03-08 10:21:10 -05:00
|
|
|
|
|
|
|
//add cwd path
|
2005-03-09 08:06:17 -05:00
|
|
|
possible_paths.add(platform_lib_name);
|
|
|
|
//create needed string array
|
|
|
|
String[] oalPaths = new String[possible_paths.size()];
|
|
|
|
possible_paths.toArray(oalPaths);
|
|
|
|
|
2004-07-04 09:58:11 -04:00
|
|
|
return oalPaths;
|
|
|
|
}
|
2003-08-17 12:38:57 -04:00
|
|
|
|
2004-07-04 09:58:11 -04:00
|
|
|
/**
|
|
|
|
* Creates an OpenAL instance. The empty create will cause OpenAL to
|
|
|
|
* open the default device, and create a context using default values.
|
|
|
|
*/
|
|
|
|
public static void create() throws LWJGLException {
|
|
|
|
if (created) {
|
|
|
|
return;
|
2003-08-17 12:38:57 -04:00
|
|
|
}
|
2004-07-04 09:58:11 -04:00
|
|
|
String[] oalPaths = getOALPaths();
|
|
|
|
nCreate(oalPaths);
|
2004-03-08 10:21:10 -05:00
|
|
|
created = true;
|
2004-07-04 09:58:11 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
AL10.initNativeStubs();
|
|
|
|
ALC.initNativeStubs();
|
|
|
|
|
|
|
|
device = ALC.alcOpenDevice(deviceArguments);
|
|
|
|
if (device == null)
|
|
|
|
throw new LWJGLException("Could not open ALC device");
|
|
|
|
//check if doing default values or not
|
|
|
|
if (contextFrequency == -1) {
|
|
|
|
context = ALC.alcCreateContext(device.device, null);
|
|
|
|
} else {
|
|
|
|
context = ALC.alcCreateContext(device.device,
|
|
|
|
ALCcontext.createAttributeList(contextFrequency, contextRefresh, contextSynchronized));
|
|
|
|
}
|
2004-07-04 10:08:53 -04:00
|
|
|
ALC.alcMakeContextCurrent(context.context);
|
2004-07-04 09:58:11 -04:00
|
|
|
created = true;
|
|
|
|
} catch (LWJGLException e) {
|
|
|
|
destroy();
|
|
|
|
throw e;
|
|
|
|
}
|
2003-08-17 12:38:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exit cleanly by calling destroy.
|
|
|
|
*/
|
|
|
|
public static void destroy() {
|
2004-07-04 09:58:11 -04:00
|
|
|
if (context != null) {
|
|
|
|
ALC.alcDestroyContext(context.context);
|
|
|
|
context = null;
|
2004-03-08 10:21:10 -05:00
|
|
|
}
|
2004-07-04 09:58:11 -04:00
|
|
|
if (device != null) {
|
|
|
|
ALC.alcCloseDevice(device.device);
|
|
|
|
device = null;
|
|
|
|
}
|
|
|
|
resetNativeStubs(AL10.class);
|
|
|
|
resetNativeStubs(ALC.class);
|
2003-08-17 12:38:57 -04:00
|
|
|
|
|
|
|
deviceArguments = null;
|
|
|
|
|
|
|
|
contextFrequency = -1;
|
|
|
|
contextRefresh = -1;
|
|
|
|
contextSynchronized = ALC.ALC_FALSE;
|
2004-03-08 10:21:10 -05:00
|
|
|
|
2004-07-04 09:58:11 -04:00
|
|
|
if (created)
|
|
|
|
nDestroy();
|
2004-03-08 10:21:10 -05:00
|
|
|
created = false;
|
2003-08-17 12:38:57 -04:00
|
|
|
}
|
2004-03-08 10:21:10 -05:00
|
|
|
|
|
|
|
/**
|
2005-01-28 10:47:29 -05:00
|
|
|
* Tries to locate OpenAL from the current ClassLoader
|
2004-03-08 10:21:10 -05:00
|
|
|
* This method exists because OpenAL is loaded from native code, and as such
|
2005-01-28 10:47:29 -05:00
|
|
|
* is exempt from ClassLoader library loading rutines. OpenAL therefore always fails.
|
|
|
|
* We therefore invoke the protected method of the ClassLoader to see if it can
|
2004-03-08 10:21:10 -05:00
|
|
|
* locate it.
|
|
|
|
*
|
|
|
|
* @param libname Name of library to search for
|
|
|
|
* @return Absolute path to library if found, otherwise null
|
|
|
|
*/
|
2005-01-28 10:47:29 -05:00
|
|
|
private static String getPathFromClassLoader(String libname) {
|
2004-03-08 10:21:10 -05:00
|
|
|
try {
|
2005-01-28 10:47:29 -05:00
|
|
|
Sys.log("getPathFromClassLoader: searching for: " + libname);
|
2004-03-08 10:21:10 -05:00
|
|
|
Object o = AL.class.getClassLoader();
|
|
|
|
Class c = o.getClass();
|
2005-01-28 10:47:29 -05:00
|
|
|
while (c != null) {
|
|
|
|
try {
|
|
|
|
Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class });
|
2005-02-18 03:17:27 -05:00
|
|
|
findLibrary.setAccessible(true);
|
2005-01-28 10:47:29 -05:00
|
|
|
Object[] arguments = new Object[] { libname };
|
|
|
|
return (String)findLibrary.invoke(o, arguments);
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
c = c.getSuperclass();
|
|
|
|
}
|
|
|
|
}
|
2004-03-08 10:21:10 -05:00
|
|
|
} catch (Exception e) {
|
|
|
|
Sys.log("Failure locating OpenAL using classloader:" + e);
|
|
|
|
}
|
|
|
|
return null;
|
2004-07-04 09:58:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private static native void resetNativeStubs(Class clazz);
|
2004-08-10 06:31:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a handle to the current openAL context. The handle is "opaque" right now
|
|
|
|
* because, realistically, there is nothing you can actually do with it. If it turns
|
|
|
|
* out that there are useful things you can do with it then it'll return an instance
|
|
|
|
* of ALCcontext (which will have to become a public class)
|
|
|
|
* @return an opaque handle to the AL context.
|
|
|
|
*/
|
|
|
|
public static Object getContext() {
|
|
|
|
return context;
|
|
|
|
}
|
2004-03-08 10:21:10 -05:00
|
|
|
}
|