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