| 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 |
{ Internal low-level utilities for working with OpenAL. |
| 17 |
Everything based on OpenAL bindings in unit CastleInternalOpenAL. |
| 18 |
For a higher-level class that takes care of initializing OpenAL |
| 19 |
and loading and playing sounds, see CastleSoundEngine. |
| 20 |
|
| 21 |
You shouldn't use any alc* functions or alutInit/alutExit |
| 22 |
functions from CastleInternalOpenAL yourself. |
| 23 |
This unit and CastleSoundEngine take care |
| 24 |
about everything needed there. |
| 25 |
} |
| 26 |
|
| 27 |
unit CastleInternalALUtils; |
| 28 |
|
| 29 |
{$I castleconf.inc} |
| 30 |
|
| 31 |
interface |
| 32 |
|
| 33 |
{$define read_interface} |
| 34 |
|
| 35 |
uses SysUtils, Classes, |
| 36 |
CastleUtils, CastleInternalOpenAL, CastleInternalSoundFile, CastleTimeUtils; |
| 37 |
|
| 38 |
type |
| 39 |
EOpenALError = class(Exception); |
| 40 |
EOpenALInitError = class(EOpenALError); |
| 41 |
|
| 42 |
{ ---------------------------------------------------------------------------- } |
| 43 |
{ @section(Error checking) } |
| 44 |
|
| 45 |
type |
| 46 |
{ Exception for errors reported by alGetError (using constants AL_xxx). } |
| 47 |
EALError = class(EOpenALError) |
| 48 |
private |
| 49 |
FALErrorNum: TALenum; |
| 50 |
public |
| 51 |
property ALErrorNum: TALenum read FALErrorNum; |
| 52 |
constructor Create(AALErrorNum: TALenum; const AMessage: string); |
| 53 |
end; |
| 54 |
|
| 55 |
{ For alcGetError errors (ALC_xxx constants). } |
| 56 |
EALCError = class(EOpenALError) |
| 57 |
private |
| 58 |
FALCErrorNum: TALenum; |
| 59 |
public |
| 60 |
property ALCErrorNum: TALenum read FALCErrorNum; |
| 61 |
constructor Create(AALCErrorNum: TALenum; const AMessage: string); |
| 62 |
end; |
| 63 |
|
| 64 |
{ Check current OpenAL state, |
| 65 |
if error raise exception (by default) or make warning (if Warning = @true). } |
| 66 |
procedure CheckAL(const Situation: String; const Warning: Boolean = false); |
| 67 |
|
| 68 |
{ ---------------------------------------------------------------------------- } |
| 69 |
{ @section(Query AL state) } |
| 70 |
|
| 71 |
{ Comfortable wrappers for alGet*. |
| 72 |
In many cases these should be more comfortable |
| 73 |
(because they are functions) and safer (no need to pass some pointer) |
| 74 |
than directly using related OpenAL functions. |
| 75 |
|
| 76 |
OpenAL errors are @italic(not) checked by these functions |
| 77 |
(i.e. CheckAL or alGetError is not called). |
| 78 |
|
| 79 |
We don't check does @code(Attribute) really return value of given type. |
| 80 |
This means that if you will request value of the wrong type for |
| 81 |
given @code(Attribute), OpenAL may do some conversion, or may set the error |
| 82 |
state. In some cases you may even get nasty access violation errors |
| 83 |
or accidental writes over some random place in memory --- this may |
| 84 |
happen if for given @code(Attribute) OpenAL likes to return an array |
| 85 |
of some values, and you will use the wrong version |
| 86 |
(e.g. using AL_GAIN with a version that returns TALVector3f, |
| 87 |
or using AL_POSITION with a version that returns single TALfloat). |
| 88 |
So @italic(always check carefully that given @code(Attribute) |
| 89 |
supports the requested output value.) |
| 90 |
|
| 91 |
@groupBegin |
| 92 |
} |
| 93 |
function alGetSource1i(SourceName: TALuint; Attribute: TALenum): TALint; |
| 94 |
function alGetSource1f(SourceName: TALuint; Attribute: TALenum): TALfloat; |
| 95 |
function alGetSource1bool(SourceName: TALuint; Attribute: TALenum): TALboolean; |
| 96 |
function alGetSource1ui(SourceName: TALuint; Attribute: TALenum): TALuint; |
| 97 |
function alGetSource3f(SourceName: TALuint; Attribute: TALenum): TALVector3f; |
| 98 |
|
| 99 |
function alGetBuffer1sizei(BufferName: TALuint; Attribute: TALenum): TALsizei; |
| 100 |
function alGetBuffer1i(BufferName: TALuint; Attribute: TALenum): TALint; |
| 101 |
function alGetBuffer1f(BufferName: TALuint; Attribute: TALenum): TALfloat; |
| 102 |
|
| 103 |
function alGetListener1f(Attribute: TALenum): TALfloat; |
| 104 |
function alGetListener3f(Attribute: TALenum): TALVector3f; |
| 105 |
function alGetListenerOrientation: TALTwoVectors3f; |
| 106 |
|
| 107 |
function alcGetInterger1(deviceHandle:PALCdevice; token:TALenum): TALint; |
| 108 |
|
| 109 |
{ @groupEnd } |
| 110 |
|
| 111 |
{ ---------------------------------------------------------------------------- } |
| 112 |
{ @section(Simple wrappers over OpenAL function to pass TALVector* types) } |
| 113 |
|
| 114 |
{ Comfortable wrappers over OpenAL functions that take vector types. |
| 115 |
These take TALVector* / TALTwoVectors* types. |
| 116 |
|
| 117 |
Just like with alGet* wrappers (above in this unit), |
| 118 |
no error checking is done (no CheckAL etc.) and no |
| 119 |
checking does @code(Param) accept the given type of value is done. |
| 120 |
|
| 121 |
@groupBegin } |
| 122 |
procedure alSourceVector3f(SourceName: TALuint; Param: TALenum; const Value: TALVector3f); |
| 123 |
procedure alListenerVector3f(Param: TALenum; const Value: TALVector3f); |
| 124 |
procedure alListenerOrientation(const Dir, Up: TALVector3f); overload; |
| 125 |
procedure alListenerOrientation(const Orient: TALTwoVectors3f); overload; |
| 126 |
{ @groupEnd } |
| 127 |
|
| 128 |
{ ---------------------------------------------------------------------------- } |
| 129 |
{ @section(State setting for compatibility between various OpenAL implementations) } |
| 130 |
|
| 131 |
{ Allocate OpenAL sources and buffers, making sure their initial state |
| 132 |
conforms to specification. |
| 133 |
|
| 134 |
Unfortunately current Creative OpenAL Windows implementation violates |
| 135 |
OpenAL specification: default source state (i.e. newly generated |
| 136 |
source state) is not as it is specified by OpenAL implementation. |
| 137 |
Attributes MAX_DISTANCE, DIRECTION and CONE_OUTER_GAIN have different |
| 138 |
values. |
| 139 |
|
| 140 |
So alCreateSources calls alGenSources and then makes sure that all sources |
| 141 |
have state consistent with OpenAL specification (under Windows it means |
| 142 |
that it sets MAX_DISTANCE, DIRECTION and CONE_OUTER_GAIN attributes |
| 143 |
to their proper values). alCreateBuffers does the same for alGenBuffers |
| 144 |
(which means, @italic(for now), that it simply calls alGenBuffers.) |
| 145 |
|
| 146 |
To be on the safe side, you should always use |
| 147 |
alCreateSources and alCreateBuffers instead alGenSources and alGenBuffers. |
| 148 |
@groupBegin } |
| 149 |
procedure alCreateSources(n: TALsizei; sources: PALuint); |
| 150 |
procedure alCreateBuffers(n: TALsizei; buffers: PALuint); |
| 151 |
{ @groupEnd } |
| 152 |
|
| 153 |
{ @section(Other utils) --------------------------------------------------- } |
| 154 |
|
| 155 |
const |
| 156 |
{ } |
| 157 |
BoolToAL: array[boolean] of TALint = (AL_FALSE, AL_TRUE); |
| 158 |
|
| 159 |
{ Pass resource to alDeleteSources or alDeleteBuffers, |
| 160 |
checking and setting it to zero. |
| 161 |
|
| 162 |
These are trivial wrappers over @code(alDeleteSources(1, @@Source)), |
| 163 |
@code(alDeleteBuffers(1, @@Buffer)). They first check if resource is non-zero, |
| 164 |
and after freeing set it to zero. This makes calling them many times |
| 165 |
(e.g. on already freed resources) harmless. |
| 166 |
|
| 167 |
alFreeSource also calls alSourceStop first, because we cannot free playing |
| 168 |
sources. |
| 169 |
|
| 170 |
@groupBegin } |
| 171 |
procedure alFreeSource(var Source: TALuint); |
| 172 |
procedure alFreeBuffer(var Buffer: TALuint); |
| 173 |
{ @groupEnd } |
| 174 |
|
| 175 |
{ Check and use OpenAL enumeration extension. |
| 176 |
If OpenAL supports ALC_ENUMERATION_EXT, then we return @true |
| 177 |
and pDeviceList is initialized to the null-separated list of |
| 178 |
possible OpenAL devices. |
| 179 |
@groupBegin } |
| 180 |
function EnumerationExtPresent(out pDeviceList: PChar): boolean; overload; |
| 181 |
function EnumerationExtPresent: boolean; overload; |
| 182 |
{ @groupEnd } |
| 183 |
|
| 184 |
{$undef read_interface} |
| 185 |
|
| 186 |
implementation |
| 187 |
|
| 188 |
uses CastleVectors, CastleStringUtils, CastleLog, CastleUriUtils; |
| 189 |
|
| 190 |
{$define read_implementation} |
| 191 |
|
| 192 |
{ error checking ------------------------------------------------------- } |
| 193 |
|
| 194 |
constructor EALError.Create(AALErrorNum: TALenum; const AMessage: string); |
| 195 |
begin |
| 196 |
FALErrorNum := AALErrorNum; |
| 197 |
inherited Create(AMessage); |
| 198 |
end; |
| 199 |
|
| 200 |
procedure CheckAL(const Situation: String; const Warning: Boolean); |
| 201 |
var |
| 202 |
Err: TALenum; |
| 203 |
ErrorStr: String; |
| 204 |
begin |
| 205 |
Err := alGetError(); |
| 206 |
if Err <> AL_NO_ERROR then |
| 207 |
begin |
| 208 |
ErrorStr := Format('OpenAL error when doing %s: (%d) %s', [ |
| 209 |
Situation, |
| 210 |
Err, |
| 211 |
alGetString(Err) |
| 212 |
]); |
| 213 |
if Warning then |
| 214 |
WritelnWarning(ErrorStr) |
| 215 |
else |
| 216 |
raise EALError.Create(Err, ErrorStr); |
| 217 |
end; |
| 218 |
end; |
| 219 |
|
| 220 |
{ EALCError ------------------------------------------------------------------ } |
| 221 |
|
| 222 |
constructor EALCError.Create(AALCErrorNum: TALenum; const AMessage: string); |
| 223 |
begin |
| 224 |
FALCErrorNum := AALCErrorNum; |
| 225 |
inherited Create(AMessage); |
| 226 |
end; |
| 227 |
|
| 228 |
{ query al state -------------------------------------------------------------- } |
| 229 |
|
| 230 |
{ alGetSource* } |
| 231 |
|
| 232 |
function alGetSource1i(SourceName: TALuint; Attribute: TALenum): TALint; |
| 233 |
begin |
| 234 |
alGetSourcei(SourceName, Attribute, @result); |
| 235 |
end; |
| 236 |
|
| 237 |
function alGetSource1f(SourceName: TALuint; Attribute: TALenum): TALfloat; |
| 238 |
begin |
| 239 |
alGetSourcef(SourceName, Attribute, @result); |
| 240 |
end; |
| 241 |
|
| 242 |
function alGetSource1bool(SourceName: TALuint; Attribute: TALenum): TALboolean; |
| 243 |
begin |
| 244 |
result := alGetSource1i(SourceName, Attribute) <> 0; |
| 245 |
end; |
| 246 |
|
| 247 |
function alGetSource1ui(SourceName: TALuint; Attribute: TALenum): TALuint; |
| 248 |
begin |
| 249 |
{ OpenAL doesn't have a function to get unsigned TALuint. |
| 250 |
You should get signed TALint, and just ignore range checks |
| 251 |
(which is done below, since we just pass a pointer). } |
| 252 |
Assert(SizeOf(TALint) = SizeOf(TALuint)); |
| 253 |
alGetSourcei(SourceName, Attribute, @result); |
| 254 |
end; |
| 255 |
|
| 256 |
function alGetSource3f(SourceName: TALuint; Attribute: TALenum): TALVector3f; |
| 257 |
begin |
| 258 |
alGetSourcefv(SourceName, Attribute, @result); |
| 259 |
end; |
| 260 |
|
| 261 |
{ alGetBuffer* } |
| 262 |
|
| 263 |
function alGetBuffer1sizei(BufferName: TALuint; Attribute: TALenum): TALsizei; |
| 264 |
begin |
| 265 |
{ There's no alGetBufferXxx that asks for TALsizei. |
| 266 |
We use alGetBufferi, that returns TALint, assuming that |
| 267 |
TALsizei and TALint are actually same types. } |
| 268 |
Assert(SizeOf(TALsizei) = SizeOf(TALint)); |
| 269 |
|
| 270 |
alGetBufferi(BufferName, Attribute, @result); |
| 271 |
end; |
| 272 |
|
| 273 |
function alGetBuffer1i(BufferName: TALuint; Attribute: TALenum): TALint; |
| 274 |
begin |
| 275 |
alGetBufferi(BufferName, Attribute, @result); |
| 276 |
end; |
| 277 |
|
| 278 |
function alGetBuffer1f(BufferName: TALuint; Attribute: TALenum): TALfloat; |
| 279 |
begin |
| 280 |
alGetBufferf(BufferName, Attribute, @result); |
| 281 |
end; |
| 282 |
|
| 283 |
{ alGetListener } |
| 284 |
|
| 285 |
function alGetListener1f(Attribute: TALenum): TALfloat; |
| 286 |
begin |
| 287 |
alGetListenerf(Attribute, @result); |
| 288 |
end; |
| 289 |
|
| 290 |
function alGetListener3f(Attribute: TALenum): TALVector3f; |
| 291 |
begin |
| 292 |
alGetListenerfv(Attribute, @result); |
| 293 |
end; |
| 294 |
|
| 295 |
function alGetListenerOrientation: TALTwoVectors3f; |
| 296 |
begin |
| 297 |
alGetListenerfv(AL_ORIENTATION, @result); |
| 298 |
end; |
| 299 |
|
| 300 |
function alcGetInterger1(deviceHandle:PALCdevice; token:TALenum): TALint; |
| 301 |
begin |
| 302 |
alcGetIntegerv(deviceHandle, token, 1, @Result); |
| 303 |
end; |
| 304 |
|
| 305 |
procedure alSourceVector3f(SourceName: TALuint; Param: TALenum; const Value: TALVector3f); |
| 306 |
begin |
| 307 |
alSourcefv(SourceName, Param, @Value); |
| 308 |
end; |
| 309 |
|
| 310 |
procedure alListenerVector3f(Param: TALenum; const Value: TALVector3f); |
| 311 |
begin |
| 312 |
alListenerfv(Param, @Value); |
| 313 |
end; |
| 314 |
|
| 315 |
procedure alListenerOrientation(const Dir, Up: TALVector3f); |
| 316 |
var |
| 317 |
Orient: TALTwoVectors3f; |
| 318 |
begin |
| 319 |
Orient[0] := Dir; |
| 320 |
Orient[1] := Up; |
| 321 |
alListenerfv(AL_ORIENTATION, @Orient); |
| 322 |
end; |
| 323 |
|
| 324 |
procedure alListenerOrientation(const Orient: TALTwoVectors3f); |
| 325 |
begin |
| 326 |
alListenerfv(AL_ORIENTATION, @Orient); |
| 327 |
end; |
| 328 |
|
| 329 |
{ -------------------------------------------------------------------------- |
| 330 |
state setting for compatibility between various OpenAL implementations } |
| 331 |
|
| 332 |
procedure alCreateSources(n: TALsizei; sources: PALuint); |
| 333 |
{$ifdef MSWINDOWS} |
| 334 |
var |
| 335 |
I: Integer; |
| 336 |
{$endif} |
| 337 |
begin |
| 338 |
alGenSources(n, sources); |
| 339 |
|
| 340 |
{$ifdef MSWINDOWS} |
| 341 |
for i := 1 to n do |
| 342 |
begin |
| 343 |
//TODO: alSourcei(sources^, AL_MAX_DISTANCE, ); |
| 344 |
alSourceVector3f(sources^, AL_DIRECTION, TVector3.Zero); |
| 345 |
alSourcef(sources^, AL_CONE_OUTER_GAIN, 0); |
| 346 |
Inc(sources); |
| 347 |
end; |
| 348 |
{$endif} |
| 349 |
end; |
| 350 |
|
| 351 |
procedure alCreateBuffers(n: TALsizei; buffers: PALuint); |
| 352 |
begin |
| 353 |
alGenBuffers(n, buffers); |
| 354 |
end; |
| 355 |
|
| 356 |
{ Other utils ---------------------------------------------------------------- } |
| 357 |
|
| 358 |
procedure alFreeSource(var Source: TALuint); |
| 359 |
begin |
| 360 |
if Source <> 0 then |
| 361 |
begin |
| 362 |
alSourceStop(Source); |
| 363 |
alDeleteSources(1, @Source); |
| 364 |
Source := 0; |
| 365 |
end; |
| 366 |
end; |
| 367 |
|
| 368 |
procedure alFreeBuffer(var Buffer: TALuint); |
| 369 |
begin |
| 370 |
if Buffer <> 0 then |
| 371 |
begin |
| 372 |
alDeleteBuffers(1, @Buffer); |
| 373 |
Buffer := 0; |
| 374 |
end; |
| 375 |
end; |
| 376 |
|
| 377 |
function EnumerationExtPresent(out pDeviceList: PChar): boolean; |
| 378 |
begin |
| 379 |
Result := alcIsExtensionPresent(nil, 'ALC_ENUMERATION_EXT'); |
| 380 |
if Result then |
| 381 |
begin |
| 382 |
pDeviceList := alcGetString(nil, ALC_DEVICE_SPECIFIER); |
| 383 |
Assert(pDeviceList <> nil); |
| 384 |
end; |
| 385 |
end; |
| 386 |
|
| 387 |
function EnumerationExtPresent: boolean; |
| 388 |
begin |
| 389 |
Result := alcIsExtensionPresent(nil, 'ALC_ENUMERATION_EXT'); |
| 390 |
end; |
| 391 |
|
| 392 |
end. |