| 1 |
{ |
| 2 |
Copyright 2003-2018 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ OpenAL library bindings. |
| 17 |
This is a translation of OpenAL C headers: @code(AL/al.h, AL/alc.h) |
| 18 |
(and included headers: @code(AL/altypes.h, AL/alctypes.h)). |
| 19 |
|
| 20 |
The library is loaded in unit initialization, and if not found |
| 21 |
we will merely set global ALLibraryAvailable variable to @false. |
| 22 |
This allows you to check at runtime and gracefully work |
| 23 |
if OpenAL is not installed (e.g. you can turn off game sounds.) |
| 24 |
|
| 25 |
Renamed to CastleInternalOpenAL (from OpenAL) to avoid clash with FPC's OpenAL |
| 26 |
unit. FPC unit doesn't provide above ALLibraryAvailable feature, and it doesn't |
| 27 |
integrate with CastleVectors. |
| 28 |
|
| 29 |
@exclude (Unit not really ready for PasDoc, with many comments from |
| 30 |
original C headers.) } |
| 31 |
unit CastleInternalOpenAL; |
| 32 |
|
| 33 |
{ C->Pascal translation comments: |
| 34 |
|
| 35 |
@unorderedList( |
| 36 |
@item(We use CTypes.) |
| 37 |
@item(All types start with T, except pointers starting with P. |
| 38 |
This is normal for Pascal.) |
| 39 |
@item(In most cases function parameters and return values |
| 40 |
PALubyte were changed into PChar, as their intention is to handle |
| 41 |
C strings. ABI is the same, |
| 42 |
but using PChar allows easier usage under Pascal compilers.) |
| 43 |
@item(I didn't use h2pas. OpenGL headers are clean enough to handle |
| 44 |
most stuff with some regexps and Emacs macros.) |
| 45 |
@item(All original comments are preserved, sometimes they are useful.) |
| 46 |
) |
| 47 |
} |
| 48 |
|
| 49 |
{ If OPENAL_DEPRECATED, we will define and load some functions |
| 50 |
considered deprecated. |
| 51 |
See http://opensource.creative.com/pipermail/openal-devel/2005-April/002985.html . } |
| 52 |
{ $define OPENAL_DEPRECATED} |
| 53 |
|
| 54 |
{$i castleconf.inc} |
| 55 |
|
| 56 |
interface |
| 57 |
|
| 58 |
uses SysUtils, Math, CastleVectors, CTypes; |
| 59 |
|
| 60 |
{$ifdef FREEBSD} |
| 61 |
{$linklib pthread} |
| 62 |
{$endif} |
| 63 |
|
| 64 |
{$ifndef PASDOC} |
| 65 |
{$I castleinternalopenal_al.inc} |
| 66 |
{$I castleinternalopenal_alc.inc} |
| 67 |
{$endif} |
| 68 |
|
| 69 |
{ ---------------------------------------------------------------------------- |
| 70 |
Types to make this unit cooperate nicely with CastleVectors. |
| 71 |
|
| 72 |
Definitions of types below are connected with definitions in openal_altypes.inc |
| 73 |
--- if you change some type in openal_altypes.inc you have to adjust definitions |
| 74 |
below as well. That's because we use constructions like |
| 75 |
TALVector3f = TVector3 |
| 76 |
instead of |
| 77 |
TALVector3f = array [0..2] of TALSingle |
| 78 |
This way we can use all TVector3 functions from CastleVectors |
| 79 |
to operate on OpenAL vectors. |
| 80 |
} |
| 81 |
|
| 82 |
type |
| 83 |
{ } |
| 84 |
TALVector3f = TVector3; |
| 85 |
TALVector3d = TVector3Double; |
| 86 |
{ TwoVectors type is useful for OpenAL's listener ORIENTATION property. |
| 87 |
@groupBegin } |
| 88 |
TALTwoVectors3f = array [0..1] of TALVector3f; |
| 89 |
TALTwoVectors3d = array [0..1] of TALVector3d; |
| 90 |
{ @groupEnd } |
| 91 |
|
| 92 |
{ ---------------------------------------------------------------------------- } |
| 93 |
|
| 94 |
{ Is OpenAL library loaded. |
| 95 |
This means that functions alXxx and alcXxx are loaded (not @nil). |
| 96 |
When OpenAL is not available, alXxx and alcXxx are @nil, |
| 97 |
and this variable is @false, and you can gracefully handle it in your programs |
| 98 |
(for example, continue normal game, just with sound turned off). } |
| 99 |
function ALLibraryAvailable: boolean; |
| 100 |
|
| 101 |
{ Reset OpenAL library. |
| 102 |
|
| 103 |
In this unit's initialization, we link to OpenAL library, load all symbols, |
| 104 |
and initialize ALLibraryAvailable. In this unit's finalization, |
| 105 |
we release library handles and set ALLibraryAvailable back to @false. |
| 106 |
|
| 107 |
What this procedure does? |
| 108 |
It behaves like finalizing this unit (releasing OpenAL library handles), |
| 109 |
then sleeping for some short amount, and initializing this unit again |
| 110 |
(loading OpenAL library symbols). |
| 111 |
|
| 112 |
When is it needed? |
| 113 |
|
| 114 |
Unix OpenAL implementation seems to have a problem. |
| 115 |
It's reproduceable in "The Castle" code: |
| 116 |
When I want to switch from one OpenAL device to the other, |
| 117 |
I would like to close OpenAL context and device, |
| 118 |
change my device name, and create OpenAL context and device again. |
| 119 |
But this causes problems under Linux: |
| 120 |
when user selects some non-working device (e.g. when I select |
| 121 |
esd device while Esound daemon is not running), OpenAL (correctly) |
| 122 |
doesn't let me initialize the device (returns nil). |
| 123 |
But then, after such failure, when I try to initialize *any* other device |
| 124 |
(that worked before, like alsa or waveout), the initialization of |
| 125 |
them also fails. I have to restart my program (to restart whole |
| 126 |
OpenAL library state) to be able to initialize any other backend. |
| 127 |
|
| 128 |
Workaround that I found is to do such OpenALRestart. |
| 129 |
Release OpenAL library, wait some short time |
| 130 |
(I guess that otherwise some resources will still occupy the sound |
| 131 |
device? In any case, this short sleep is needed...), then do |
| 132 |
OpenAL library initialization again. And things work correctly. |
| 133 |
|
| 134 |
Note that this was possibly only necessary for old Loki implementation? } |
| 135 |
procedure OpenALRestart; |
| 136 |
|
| 137 |
{ Detect OpenAL SI (Sample Implementation) from Loki. |
| 138 |
This was the most common implementation on most Unixes, |
| 139 |
until OpenAL-soft came. |
| 140 |
|
| 141 |
Use this only when ALLibraryAvailable, since this is just a call to some alcGetString. } |
| 142 |
function OpenALSampleImplementation: boolean; |
| 143 |
|
| 144 |
procedure OpenALInitialization; |
| 145 |
|
| 146 |
implementation |
| 147 |
|
| 148 |
uses CastleUtils, CastleLog, CastleDynLib; |
| 149 |
|
| 150 |
function OpenALSampleImplementation: boolean; |
| 151 |
begin |
| 152 |
Result := alGetString(AL_VENDOR) = 'J. Valenzuela'; |
| 153 |
end; |
| 154 |
|
| 155 |
{ unit init/fini ------------------------------------------------------------ } |
| 156 |
|
| 157 |
procedure OpenALFinalization; forward; |
| 158 |
|
| 159 |
procedure OpenALRestart; |
| 160 |
begin |
| 161 |
OpenALFinalization; |
| 162 |
Sleep(500); |
| 163 |
OpenALInitialization; |
| 164 |
end; |
| 165 |
|
| 166 |
var |
| 167 |
ALLibrary: TDynLib; |
| 168 |
|
| 169 |
function ALLibraryAvailable: boolean; |
| 170 |
begin |
| 171 |
Result := ALLibrary <> nil; |
| 172 |
end; |
| 173 |
|
| 174 |
procedure OpenALInitialization; |
| 175 |
begin |
| 176 |
{ Be sure to start with a "clean" state. } |
| 177 |
OpenALFinalization; |
| 178 |
|
| 179 |
ALLibrary := TDynLib.Load( |
| 180 |
{$ifdef UNIX} |
| 181 |
{$ifdef DARWIN} |
| 182 |
'/System/Library/Frameworks/OpenAL.framework/OpenAL' |
| 183 |
{$else} |
| 184 |
{ We are linking dynamically using dlopen/dlsym. |
| 185 |
Library name should not be just 'libopenal.so', |
| 186 |
as that is only available in -dev package of openal. } |
| 187 |
'libopenal.so.0' |
| 188 |
{$endif} |
| 189 |
{$endif} |
| 190 |
{$ifdef MSWINDOWS} 'OpenAL32.dll' {$endif} |
| 191 |
, false); |
| 192 |
|
| 193 |
{$ifdef UNIX} |
| 194 |
if ALLibrary = nil then |
| 195 |
ALLibrary := TDynLib.Load('libopenal.so.1', false); |
| 196 |
{ esp. good for Android with our version in ../../../external_libraries/arm-android/ } |
| 197 |
if ALLibrary = nil then |
| 198 |
ALLibrary := TDynLib.Load('libopenal.so', false); |
| 199 |
{$endif} |
| 200 |
|
| 201 |
if ALLibrary <> nil then |
| 202 |
begin |
| 203 |
{ alXxx functions -------------------------------------------------------- } |
| 204 |
Pointer({$ifndef FPC}@{$endif} alEnable) := ALLibrary.Symbol('alEnable'); |
| 205 |
Pointer({$ifndef FPC}@{$endif} alDisable) := ALLibrary.Symbol('alDisable'); |
| 206 |
Pointer({$ifndef FPC}@{$endif} alIsEnabled) := ALLibrary.Symbol('alIsEnabled'); |
| 207 |
Pointer({$ifndef FPC}@{$endif} alGetBooleanv) := ALLibrary.Symbol('alGetBooleanv'); |
| 208 |
Pointer({$ifndef FPC}@{$endif} alGetIntegerv) := ALLibrary.Symbol('alGetIntegerv'); |
| 209 |
Pointer({$ifndef FPC}@{$endif} alGetFloatv) := ALLibrary.Symbol('alGetFloatv'); |
| 210 |
Pointer({$ifndef FPC}@{$endif} alGetDoublev) := ALLibrary.Symbol('alGetDoublev'); |
| 211 |
Pointer({$ifndef FPC}@{$endif} alGetString) := ALLibrary.Symbol('alGetString'); |
| 212 |
Pointer({$ifndef FPC}@{$endif} alGetBoolean) := ALLibrary.Symbol('alGetBoolean'); |
| 213 |
Pointer({$ifndef FPC}@{$endif} alGetInteger) := ALLibrary.Symbol('alGetInteger'); |
| 214 |
Pointer({$ifndef FPC}@{$endif} alGetFloat) := ALLibrary.Symbol('alGetFloat'); |
| 215 |
Pointer({$ifndef FPC}@{$endif} alGetDouble) := ALLibrary.Symbol('alGetDouble'); |
| 216 |
Pointer({$ifndef FPC}@{$endif} alGetError) := ALLibrary.Symbol('alGetError'); |
| 217 |
Pointer({$ifndef FPC}@{$endif} alIsExtensionPresent) := ALLibrary.Symbol('alIsExtensionPresent'); |
| 218 |
Pointer({$ifndef FPC}@{$endif} alGetProcAddress) := ALLibrary.Symbol('alGetProcAddress'); |
| 219 |
Pointer({$ifndef FPC}@{$endif} alGetEnumValue) := ALLibrary.Symbol('alGetEnumValue'); |
| 220 |
Pointer({$ifndef FPC}@{$endif} alListenerf) := ALLibrary.Symbol('alListenerf'); |
| 221 |
Pointer({$ifndef FPC}@{$endif} alListeneri) := ALLibrary.Symbol('alListeneri'); |
| 222 |
Pointer({$ifndef FPC}@{$endif} alListener3f) := ALLibrary.Symbol('alListener3f'); |
| 223 |
Pointer({$ifndef FPC}@{$endif} alListenerfv) := ALLibrary.Symbol('alListenerfv'); |
| 224 |
Pointer({$ifndef FPC}@{$endif} alGetListeneri) := ALLibrary.Symbol('alGetListeneri'); |
| 225 |
Pointer({$ifndef FPC}@{$endif} alGetListenerf) := ALLibrary.Symbol('alGetListenerf'); |
| 226 |
Pointer({$ifndef FPC}@{$endif} alGetListenerfv) := ALLibrary.Symbol('alGetListenerfv'); |
| 227 |
Pointer({$ifndef FPC}@{$endif} alGetListener3f) := ALLibrary.Symbol('alGetListener3f'); |
| 228 |
Pointer({$ifndef FPC}@{$endif} alGenSources) := ALLibrary.Symbol('alGenSources'); |
| 229 |
Pointer({$ifndef FPC}@{$endif} alDeleteSources) := ALLibrary.Symbol('alDeleteSources'); |
| 230 |
Pointer({$ifndef FPC}@{$endif} alIsSource) := ALLibrary.Symbol('alIsSource'); |
| 231 |
Pointer({$ifndef FPC}@{$endif} alSourcei) := ALLibrary.Symbol('alSourcei'); |
| 232 |
|
| 233 |
{ alSource3i possibily not present in older < 1.1 OpenAL implementations, |
| 234 |
in particular in Loki versions. Handle gracefully: set to @nil, allowing |
| 235 |
programs that don't use alSource3i to still work. } |
| 236 |
try |
| 237 |
Pointer({$ifndef FPC}@{$endif} alSource3i) := ALLibrary.Symbol('alSource3i'); |
| 238 |
except |
| 239 |
on EDynLibError do alSource3i := nil; |
| 240 |
end; |
| 241 |
|
| 242 |
Pointer({$ifndef FPC}@{$endif} alSourcef) := ALLibrary.Symbol('alSourcef'); |
| 243 |
Pointer({$ifndef FPC}@{$endif} alSource3f) := ALLibrary.Symbol('alSource3f'); |
| 244 |
Pointer({$ifndef FPC}@{$endif} alSourcefv) := ALLibrary.Symbol('alSourcefv'); |
| 245 |
Pointer({$ifndef FPC}@{$endif} alGetSourcei) := ALLibrary.Symbol('alGetSourcei'); |
| 246 |
Pointer({$ifndef FPC}@{$endif} alGetSourcef) := ALLibrary.Symbol('alGetSourcef'); |
| 247 |
Pointer({$ifndef FPC}@{$endif} alGetSourcefv) := ALLibrary.Symbol('alGetSourcefv'); |
| 248 |
Pointer({$ifndef FPC}@{$endif} alSourcePlayv) := ALLibrary.Symbol('alSourcePlayv'); |
| 249 |
Pointer({$ifndef FPC}@{$endif} alSourceStopv) := ALLibrary.Symbol('alSourceStopv'); |
| 250 |
Pointer({$ifndef FPC}@{$endif} alSourceRewindv) := ALLibrary.Symbol('alSourceRewindv'); |
| 251 |
Pointer({$ifndef FPC}@{$endif} alSourcePausev) := ALLibrary.Symbol('alSourcePausev'); |
| 252 |
Pointer({$ifndef FPC}@{$endif} alSourcePlay) := ALLibrary.Symbol('alSourcePlay'); |
| 253 |
Pointer({$ifndef FPC}@{$endif} alSourcePause) := ALLibrary.Symbol('alSourcePause'); |
| 254 |
Pointer({$ifndef FPC}@{$endif} alSourceRewind) := ALLibrary.Symbol('alSourceRewind'); |
| 255 |
Pointer({$ifndef FPC}@{$endif} alSourceStop) := ALLibrary.Symbol('alSourceStop'); |
| 256 |
Pointer({$ifndef FPC}@{$endif} alGenBuffers) := ALLibrary.Symbol('alGenBuffers'); |
| 257 |
Pointer({$ifndef FPC}@{$endif} alDeleteBuffers) := ALLibrary.Symbol('alDeleteBuffers'); |
| 258 |
Pointer({$ifndef FPC}@{$endif} alIsBuffer) := ALLibrary.Symbol('alIsBuffer'); |
| 259 |
Pointer({$ifndef FPC}@{$endif} alBufferData) := ALLibrary.Symbol('alBufferData'); |
| 260 |
Pointer({$ifndef FPC}@{$endif} alGetBufferi) := ALLibrary.Symbol('alGetBufferi'); |
| 261 |
Pointer({$ifndef FPC}@{$endif} alGetBufferf) := ALLibrary.Symbol('alGetBufferf'); |
| 262 |
Pointer({$ifndef FPC}@{$endif} alSourceQueueBuffers) := ALLibrary.Symbol('alSourceQueueBuffers'); |
| 263 |
Pointer({$ifndef FPC}@{$endif} alSourceUnqueueBuffers) := ALLibrary.Symbol('alSourceUnqueueBuffers'); |
| 264 |
Pointer({$ifndef FPC}@{$endif} alDopplerFactor) := ALLibrary.Symbol('alDopplerFactor'); |
| 265 |
Pointer({$ifndef FPC}@{$endif} alDopplerVelocity) := ALLibrary.Symbol('alDopplerVelocity'); |
| 266 |
Pointer({$ifndef FPC}@{$endif} alDistanceModel) := ALLibrary.Symbol('alDistanceModel'); |
| 267 |
|
| 268 |
{$ifdef OPENAL_DEPRECATED} |
| 269 |
Pointer({$ifndef FPC}@{$endif} alGetBufferiv) := ALLibrary.Symbol('alGetBufferiv'); |
| 270 |
Pointer({$ifndef FPC}@{$endif} alGetBufferfv) := ALLibrary.Symbol('alGetBufferfv'); |
| 271 |
Pointer({$ifndef FPC}@{$endif} alGetSourceiv) := ALLibrary.Symbol('alGetSourceiv'); |
| 272 |
Pointer({$ifndef FPC}@{$endif} alGetListeneriv) := ALLibrary.Symbol('alGetListeneriv'); |
| 273 |
{$endif OPENAL_DEPRECATED} |
| 274 |
|
| 275 |
{ alcXxx functions ---------------------------------------- } |
| 276 |
Pointer({$ifndef FPC}@{$endif} alcCreateContext) := ALLibrary.Symbol('alcCreateContext'); |
| 277 |
Pointer({$ifndef FPC}@{$endif} alcMakeContextCurrent) := ALLibrary.Symbol('alcMakeContextCurrent'); |
| 278 |
Pointer({$ifndef FPC}@{$endif} alcProcessContext) := ALLibrary.Symbol('alcProcessContext'); |
| 279 |
Pointer({$ifndef FPC}@{$endif} alcSuspendContext) := ALLibrary.Symbol('alcSuspendContext'); |
| 280 |
Pointer({$ifndef FPC}@{$endif} alcDestroyContext) := ALLibrary.Symbol('alcDestroyContext'); |
| 281 |
Pointer({$ifndef FPC}@{$endif} alcGetError) := ALLibrary.Symbol('alcGetError'); |
| 282 |
Pointer({$ifndef FPC}@{$endif} alcGetCurrentContext) := ALLibrary.Symbol('alcGetCurrentContext'); |
| 283 |
Pointer({$ifndef FPC}@{$endif} alcOpenDevice) := ALLibrary.Symbol('alcOpenDevice'); |
| 284 |
Pointer({$ifndef FPC}@{$endif} alcCloseDevice) := ALLibrary.Symbol('alcCloseDevice'); |
| 285 |
Pointer({$ifndef FPC}@{$endif} alcIsExtensionPresent) := ALLibrary.Symbol('alcIsExtensionPresent'); |
| 286 |
Pointer({$ifndef FPC}@{$endif} alcGetProcAddress) := ALLibrary.Symbol('alcGetProcAddress'); |
| 287 |
Pointer({$ifndef FPC}@{$endif} alcGetEnumValue) := ALLibrary.Symbol('alcGetEnumValue'); |
| 288 |
Pointer({$ifndef FPC}@{$endif} alcGetContextsDevice) := ALLibrary.Symbol('alcGetContextsDevice'); |
| 289 |
Pointer({$ifndef FPC}@{$endif} alcGetString) := ALLibrary.Symbol('alcGetString'); |
| 290 |
Pointer({$ifndef FPC}@{$endif} alcGetIntegerv) := ALLibrary.Symbol('alcGetIntegerv'); |
| 291 |
|
| 292 |
{ -------------------------------------------------------------------------- |
| 293 |
ALC_xxx constants depending on Sample Implementation or not } |
| 294 |
if OpenALSampleImplementation then |
| 295 |
begin |
| 296 |
ALC_FREQUENCY := $100; |
| 297 |
ALC_REFRESH := $101; |
| 298 |
ALC_SYNC := $102; |
| 299 |
|
| 300 |
ALC_DEFAULT_DEVICE_SPECIFIER := $300; |
| 301 |
ALC_DEVICE_SPECIFIER := $301; |
| 302 |
ALC_EXTENSIONS := $302; |
| 303 |
|
| 304 |
ALC_MAJOR_VERSION := $303; |
| 305 |
ALC_MINOR_VERSION := $304; |
| 306 |
ALC_ATTRIBUTES_SIZE := $305; |
| 307 |
ALC_ALL_ATTRIBUTES := $306; |
| 308 |
end else |
| 309 |
begin |
| 310 |
ALC_MAJOR_VERSION := $1000; |
| 311 |
ALC_MINOR_VERSION := $1001; |
| 312 |
ALC_ATTRIBUTES_SIZE := $1002; |
| 313 |
ALC_ALL_ATTRIBUTES := $1003; |
| 314 |
|
| 315 |
ALC_DEFAULT_DEVICE_SPECIFIER := $1004; |
| 316 |
ALC_DEVICE_SPECIFIER := $1005; |
| 317 |
ALC_EXTENSIONS := $1006; |
| 318 |
|
| 319 |
ALC_FREQUENCY := $1007; |
| 320 |
ALC_REFRESH := $1008; |
| 321 |
ALC_SYNC := $1009; |
| 322 |
end; |
| 323 |
end else |
| 324 |
WritelnLog('OpenAL library not found'); |
| 325 |
end; |
| 326 |
|
| 327 |
procedure OpenALFinalization; |
| 328 |
begin |
| 329 |
FreeAndNil(ALLibrary); |
| 330 |
end; |
| 331 |
|
| 332 |
initialization |
| 333 |
{ SetExceptionMask needs to be done for |
| 334 |
|
| 335 |
- OpenAL |
| 336 |
|
| 337 |
Testcase are applications that use OpenAL but not OpenGL: |
| 338 |
examples/audio/simplest_play_sound/ |
| 339 |
examples/audio/audio_player/ |
| 340 |
|
| 341 |
Otherwse simplest_play_sound on Linux/x86_64 always exits with: |
| 342 |
An unhandled exception occurred at $00007FC4771DF69B: |
| 343 |
EInvalidOp: Invalid floating point operation |
| 344 |
$00007FC4771DF69B |
| 345 |
|
| 346 |
- OpenGL |
| 347 |
|
| 348 |
OpenGL implementations are known to cause floating-point exceptions |
| 349 |
that just have to be ignored. |
| 350 |
Though the GL unit in FPC already do this, |
| 351 |
and dglOpenGL already does this. |
| 352 |
|
| 353 |
- CastleScript (tested by autotests). |
| 354 |
|
| 355 |
Confirmed by tests to be needed when |
| 356 |
- defined(CPUI386) |
| 357 |
- or defined(CPUX86_64) |
| 358 |
- or defined(CPUAARCH64) |
| 359 |
Actually FPC Math now defines SetExceptionMask for all platforms, |
| 360 |
so we just use it always. |
| 361 |
} |
| 362 |
SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); |
| 363 |
|
| 364 |
{$ifdef ALLOW_DLOPEN_FROM_UNIT_INITIALIZATION} |
| 365 |
OpenALInitialization; |
| 366 |
{$endif} |
| 367 |
finalization |
| 368 |
OpenALFinalization; |
| 369 |
end. |