lwjgl/src/java/org/lwjgl/openal/AL.java

251 lines
7.9 KiB
Java
Raw Normal View History

/*
2008-04-07 14:36:09 -04:00
* Copyright (c) 2002-2008 LWJGL Project
2003-08-17 12:38:57 -04:00
* All rights reserved.
*
2003-08-17 12:38:57 -04:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
2003-08-17 12:38:57 -04:00
* met:
*
* * Redistributions of source code must retain the above copyright
2003-08-17 12:38:57 -04:00
* 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.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
2003-08-17 12:38:57 -04:00
* from this software without specific prior written permission.
*
2003-08-17 12:38:57 -04:00
* 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
2003-08-17 12:38:57 -04:00
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2003-08-17 12:38:57 -04:00
* 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;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
2005-05-04 16:59:44 -04:00
import org.lwjgl.Sys;
2004-02-26 16:51:58 -05:00
2022-11-19 14:08:48 -05:00
import java.util.Arrays;
2003-08-17 12:38:57 -04:00
/**
* <p>
* The AL class implements the actual creation code for linking to the native library
* OpenAL.
* </p>
2003-08-17 12:38:57 -04:00
*
* @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. */
static ALCdevice device;
2003-08-17 12:38:57 -04:00
/** Current ALCcontext. */
static ALCcontext context;
2003-08-17 12:38:57 -04:00
/** Have we been created? */
private static boolean created;
static {
2004-03-27 08:48:58 -05:00
Sys.initialize();
}
private AL() {
}
/**
* Native method to create AL instance
*
* @param oalPath Path to search for OpenAL library
*/
private static native void nCreate(String oalPath) throws LWJGLException;
/**
* 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.
*/
private static native void nCreateDefault() throws LWJGLException;
/**
* Native method the destroy the AL
*/
private 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
/**
* 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.*
*/
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
*/
public static void create(String deviceArguments, int contextFrequency, int contextRefresh, boolean contextSynchronized, boolean openDevice)
throws LWJGLException {
if (created)
throw new IllegalStateException("Only one OpenAL context may be instantiated at any one time.");
String libname;
String[] library_names;
switch (LWJGLUtil.getPlatform()) {
case LWJGLUtil.PLATFORM_WINDOWS:
if ( Sys.is64Bit() ) {
libname = "OpenAL64";
library_names = new String[]{"OpenAL64.dll"};
} else {
libname = "OpenAL32";
library_names = new String[]{"OpenAL32.dll"};
}
break;
case LWJGLUtil.PLATFORM_LINUX:
libname = "openal";
library_names = new String[]{"libopenal64.so", "libopenal.so", "libopenal.so.0"};
break;
case LWJGLUtil.PLATFORM_MACOSX:
libname = "openal";
library_names = new String[]{"openal.dylib"};
break;
default:
throw new LWJGLException("Unknown platform: " + LWJGLUtil.getPlatform());
}
String[] oalPaths = LWJGLUtil.getLibraryPaths(libname, library_names, AL.class.getClassLoader());
2022-11-19 14:08:48 -05:00
LWJGLUtil.logger().log(() -> "Found " + oalPaths.length + " OpenAL paths: " + Arrays.toString(oalPaths));
for ( String oalPath : oalPaths ) {
try {
nCreate(oalPath);
created = true;
init(deviceArguments, contextFrequency, contextRefresh, contextSynchronized, openDevice);
break;
} catch (LWJGLException e) {
LWJGLUtil.log("Failed to load " + oalPath + ": " + e.getMessage());
}
}
if (!created && LWJGLUtil.getPlatform() == LWJGLUtil.PLATFORM_MACOSX) {
// Try to load OpenAL from the framework instead
nCreateDefault();
created = true;
init(deviceArguments, contextFrequency, contextRefresh, contextSynchronized, openDevice);
}
if (!created)
throw new LWJGLException("Could not locate OpenAL library.");
}
private static void init(String deviceArguments, int contextFrequency, int contextRefresh, boolean contextSynchronized, boolean openDevice) throws LWJGLException {
try {
AL10.initNativeStubs();
ALC10.initNativeStubs();
if(openDevice) {
device = ALC10.alcOpenDevice(deviceArguments);
2010-05-24 17:50:26 -04:00
if (device == null) {
throw new LWJGLException("Could not open ALC device");
2010-05-24 17:50:26 -04:00
}
if (contextFrequency == -1) {
context = ALC10.alcCreateContext(device, null);
} else {
context = ALC10.alcCreateContext(device,
ALCcontext.createAttributeList(contextFrequency, contextRefresh,
contextSynchronized ? ALC10.ALC_TRUE : ALC10.ALC_FALSE));
}
ALC10.alcMakeContextCurrent(context);
}
} catch (LWJGLException e) {
destroy();
throw e;
}
2010-05-24 17:50:26 -04:00
ALC11.initialize();
// Load EFX10 native stubs if ALC_EXT_EFX is supported.
// Is there any situation where the current device supports ALC_EXT_EFX and one
// later created by the user does not?
// Do we have to call resetNativeStubs(EFX10.class); somewhere? Not done for AL11
// either.
// This can either be here or in ALC11, since ALC_EXT_EFX indirectly requires AL 1.1
// for functions like alSource3i.
if (ALC10.alcIsExtensionPresent(device, EFX10.ALC_EXT_EFX_NAME)){
EFX10.initNativeStubs();
}
}
/**
* 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() {
if (context != null) {
ALC10.alcMakeContextCurrent(null);
ALC10.alcDestroyContext(context);
context = null;
}
if (device != null) {
boolean result = ALC10.alcCloseDevice(device);
device = null;
}
resetNativeStubs(AL10.class);
resetNativeStubs(AL11.class);
resetNativeStubs(ALC10.class);
resetNativeStubs(ALC11.class);
resetNativeStubs(EFX10.class);
2003-08-17 12:38:57 -04:00
if (created)
nDestroy();
created = false;
2003-08-17 12:38:57 -04:00
}
private static native void resetNativeStubs(Class clazz);
2004-08-10 06:31:52 -04:00
/**
* @return handle to the default AL context.
2004-08-10 06:31:52 -04:00
*/
public static ALCcontext getContext() {
2004-08-10 06:31:52 -04:00
return context;
}
/**
* @return handle to the default AL device.
*/
public static ALCdevice getDevice() {
return device;
}
}