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;
|
|
|
|
|
2006-02-02 17:34:28 -05:00
|
|
|
import java.util.Vector;
|
|
|
|
|
2004-03-27 10:05:24 -05:00
|
|
|
import org.lwjgl.LWJGLException;
|
2005-03-28 07:44:33 -05:00
|
|
|
import org.lwjgl.LWJGLUtil;
|
2005-05-04 16:59:44 -04:00
|
|
|
import org.lwjgl.Sys;
|
2004-02-26 16:51:58 -05:00
|
|
|
|
2003-08-17 12:38:57 -04:00
|
|
|
/**
|
|
|
|
* <br>
|
|
|
|
* This is the OpenAL class. It extends the latest core.
|
|
|
|
*
|
|
|
|
* @author Brian Matzon <brian@matzon.dk>
|
|
|
|
* @version $Revision$
|
2006-03-23 14:32:21 -05:00
|
|
|
* $Id$
|
2003-08-17 12:38:57 -04:00
|
|
|
*/
|
2004-08-01 17:26:25 -04:00
|
|
|
public final class AL {
|
2003-08-17 12:38:57 -04:00
|
|
|
/** ALCdevice instance. */
|
|
|
|
protected static ALCdevice device;
|
|
|
|
|
|
|
|
/** Current ALCcontext. */
|
|
|
|
protected static ALCcontext context;
|
|
|
|
|
2004-03-08 10:21:10 -05:00
|
|
|
/** Have we been created? */
|
2005-09-16 02:43:10 -04:00
|
|
|
private static boolean created;
|
2006-02-02 17:34:28 -05:00
|
|
|
|
2004-03-08 10:21:10 -05:00
|
|
|
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
|
|
|
|
*/
|
2006-01-16 10:54:50 -05:00
|
|
|
private static native void nCreate(String oalPath) throws LWJGLException;
|
2006-01-16 14:35:09 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Native method to create AL instance from the Mac OS X 10.4 OpenAL framework.
|
|
|
|
* It is only defined in the Mac OS X native library.
|
|
|
|
*
|
|
|
|
* @param oalPaths Array of strings containing paths to search for OpenAL library
|
|
|
|
*/
|
|
|
|
private static native void nCreateDefault() throws LWJGLException;
|
2004-03-08 10:21:10 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Native method the destroy the AL
|
|
|
|
*/
|
2005-09-16 02:43:10 -04:00
|
|
|
private static native void nDestroy();
|
2004-03-08 10:21:10 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.*
|
2006-02-02 17:34:28 -05:00
|
|
|
*/
|
|
|
|
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)
|
2003-08-17 12:38:57 -04:00
|
|
|
*/
|
2006-02-02 17:34:28 -05:00
|
|
|
static void create(String deviceArguments, int contextFrequency, int contextRefresh, boolean contextSynchronized, boolean openDevice)
|
2004-03-27 10:05:24 -05:00
|
|
|
throws LWJGLException {
|
2004-03-08 10:21:10 -05:00
|
|
|
|
2005-09-16 02:50:34 -04:00
|
|
|
if (created)
|
|
|
|
throw new IllegalStateException("Only one OpenAL context may be instantiated at any one time.");
|
2005-03-28 07:44:33 -05:00
|
|
|
String[] oalPaths = LWJGLUtil.getLibraryPaths(new String[]{
|
2005-10-20 17:46:20 -04:00
|
|
|
"OpenAL32", "OpenAL32.dll",
|
2005-03-30 03:32:15 -05:00
|
|
|
"openal", "libopenal.so",
|
2005-03-28 07:44:33 -05:00
|
|
|
"openal", "openal.dylib"}, AL.class.getClassLoader());
|
2005-05-12 03:47:07 -04:00
|
|
|
LWJGLUtil.log("Found " + oalPaths.length + " OpenAL paths");
|
2006-01-16 10:54:50 -05:00
|
|
|
for (int i = 0; i < oalPaths.length; i++) {
|
|
|
|
try {
|
|
|
|
nCreate(oalPaths[i]);
|
|
|
|
created = true;
|
|
|
|
break;
|
|
|
|
} catch (LWJGLException e) {
|
|
|
|
LWJGLUtil.log("Failed to load " + oalPaths[i] + ": " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2006-01-16 14:35:09 -05:00
|
|
|
if (!created && LWJGLUtil.getPlatform() == LWJGLUtil.PLATFORM_MACOSX) {
|
|
|
|
// Try to load OpenAL from the framework instead
|
|
|
|
nCreateDefault();
|
|
|
|
created = true;
|
|
|
|
}
|
2006-01-16 10:54:50 -05:00
|
|
|
if (!created)
|
|
|
|
throw new LWJGLException("Could not locate OpenAL library.");
|
2004-07-04 09:58:11 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
AL10.initNativeStubs();
|
|
|
|
ALC.initNativeStubs();
|
|
|
|
|
2006-02-02 17:34:28 -05:00
|
|
|
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);
|
2004-07-04 09:58:11 -04:00
|
|
|
}
|
|
|
|
} catch (LWJGLException e) {
|
|
|
|
destroy();
|
|
|
|
throw e;
|
|
|
|
}
|
2005-09-15 15:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an OpenAL instance. The empty create will cause OpenAL to
|
|
|
|
* open the default device, and create a context using default values.
|
|
|
|
* This method used to use default values that the OpenAL implementation
|
|
|
|
* chose but this produces unexpected results on some systems; so now
|
|
|
|
* it defaults to 44100Hz mixing @ 60Hz refresh.
|
|
|
|
*/
|
|
|
|
public static void create() throws LWJGLException {
|
|
|
|
create(null, 44100, 60, false);
|
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
|
|
|
|
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-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;
|
|
|
|
}
|
2006-02-02 17:34:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2004-03-08 10:21:10 -05:00
|
|
|
}
|