/usr/src/castle-game-engine-5.2.0/x3d/x3d_sound.inc is in castle-game-engine-src 5.2.0-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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | {
Copyright 2002-2014 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.
----------------------------------------------------------------------------
}
{$ifdef read_interface}
{ }
TAbstractSoundNode = class(TAbstractChildNode)
public
procedure CreateNode; override;
end;
IAbstractSoundSourceNode = interface(IAbstractTimeDependentNode)
['{5E3D46A0-71B5-4561-B6B4-43A5BE5896EF}']
property FdDescription: TSFString { read FFdDescription } { };
property FdPitch: TSFFloat { read FFdPitch } { };
property EventDuration_changed: TX3DEvent { read FEventDuration_changed } { };
end;
TAbstractSoundSourceNode = class(TAbstractTimeDependentNode, IAbstractSoundSourceNode)
public
procedure CreateNode; override;
private FFdDescription: TSFString;
public property FdDescription: TSFString read FFdDescription;
private FFdPitch: TSFFloat;
public property FdPitch: TSFFloat read FFdPitch;
{ Event out } { }
private FEventDuration_changed: TSFTimeEvent;
public property EventDuration_changed: TSFTimeEvent read FEventDuration_changed;
{ Pitch, corrected to always be > 0. }
function Pitch: Single;
end;
TAudioClipNode = class(TAbstractSoundSourceNode, IAbstractUrlObject)
private
FBufferLoaded: boolean;
Buffer: TSoundBuffer;
FDuration: TFloatTime;
Sound: TSound;
InsideLoadBuffer: Cardinal;
procedure SetBufferLoaded(const Value: boolean);
procedure SoundRelease(ASound: TSound);
procedure UpdatePlayingEvent(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
procedure UpdatePlaying;
procedure ALOpenClose(Sender: TObject);
{ Did we attempt loading Buffer. You still have to check Buffer <> 0 to know
if it's successfully loaded. }
property BufferLoaded: boolean read FBufferLoaded write SetBufferLoaded;
protected
procedure ParseAfter(Reader: TX3DReaderNames); override;
procedure BeforeTraverse(StateStack: TX3DGraphTraverseStateStack); override;
public
procedure CreateNode; override;
destructor Destroy; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
function CycleInterval: TFloatTime; override;
private FFdUrl: TMFString;
public property FdUrl: TMFString read FFdUrl;
end;
TSoundNode = class(TAbstractSoundNode)
private
FTransform: TMatrix4Single;
FTransformScale: Single;
procedure UpdateSourcePosition;
procedure EventLocationReceive(
Event: TX3DEvent; Value: TX3DField; const Time: TX3DTime);
protected
procedure BeforeTraverse(StateStack: TX3DGraphTraverseStateStack); override;
procedure DirectEnumerateActive(Func: TEnumerateChildrenFunction); override;
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
private FFdDirection: TSFVec3f;
public property FdDirection: TSFVec3f read FFdDirection;
private FFdIntensity: TSFFloat;
public property FdIntensity: TSFFloat read FFdIntensity;
private FFdLocation: TSFVec3f;
public property FdLocation: TSFVec3f read FFdLocation;
private FFdMaxBack: TSFFloat;
public property FdMaxBack: TSFFloat read FFdMaxBack;
private FFdMaxFront: TSFFloat;
public property FdMaxFront: TSFFloat read FFdMaxFront;
private FFdMinBack: TSFFloat;
public property FdMinBack: TSFFloat read FFdMinBack;
private FFdMinFront: TSFFloat;
public property FdMinFront: TSFFloat read FFdMinFront;
private FFdPriority: TSFFloat;
public property FdPriority: TSFFloat read FFdPriority;
private FFdSource: TSFNode;
public property FdSource: TSFNode read FFdSource;
private FFdSpatialize: TSFBool;
public property FdSpatialize: TSFBool read FFdSpatialize;
end;
{$endif read_interface}
{$ifdef read_implementation}
procedure TAbstractSoundNode.CreateNode;
begin
inherited;
DefaultContainerField := 'children';
end;
procedure TAbstractSoundSourceNode.CreateNode;
begin
inherited;
FFdDescription := TSFString.Create(Self, 'description', '');
Fields.Add(FFdDescription);
FFdPitch := TSFFloat.Create(Self, 'pitch', 1.0);
Fields.Add(FFdPitch);
{ X3D specification comment: (0,Inf) }
FEventDuration_changed := TSFTimeEvent.Create(Self, 'duration_changed', false);
Events.Add(FEventDuration_changed);
DefaultContainerField := 'children';
end;
function TAbstractSoundSourceNode.Pitch: Single;
begin
Result := FdPitch.Value;
if Result <= 0 then
Result := 1; { assign default pitch in case of trouble }
end;
procedure TAudioClipNode.CreateNode;
begin
inherited;
FFdUrl := TMFString.Create(Self, 'url', []);
Fields.Add(FFdUrl);
{ X3D specification comment: [urn] }
EventIsActive.OnReceive.Add(@UpdatePlayingEvent);
EventIsPaused.OnReceive.Add(@UpdatePlayingEvent);
{ Although X3D XML encoding spec says that "children" is default
containerField, common sense says it should be "source".
(Sound.source field is where AudioClip should be placed.)
Testcase: FISH.x3d }
DefaultContainerField := 'source';
FDuration := -1;
SoundEngine.OnOpenClose.Add(@ALOpenClose);
end;
destructor TAudioClipNode.Destroy;
begin
if Sound <> nil then
begin
Sound.Release;
Assert(Sound = nil);
end;
BufferLoaded := false;
SoundEngine.OnOpenClose.Remove(@ALOpenClose);
inherited;
end;
class function TAudioClipNode.ClassNodeTypeName: string;
begin
Result := 'AudioClip';
end;
class function TAudioClipNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNVRML97Nodes + ClassNodeTypeName) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TAudioClipNode.ParseAfter(Reader: TX3DReaderNames);
begin
inherited;
{ We want to load buffer early, not on-demand (inside UpdatePlaying):
- this avoids sudden delay when 1st playing sound,
- it's necessary to make FDuration good, making CycleInterval good,
which is necessary for time-dependent mechanism (in
TTimeDependentNodeHandler) to always work Ok (see e.g. at thunder.x3dv,
when AudioClip.startTime is set by script to future time.
But don't do it if URL field is empty.
This is important in case "url" field value
has an IS clause, and it will be really known only after expanding
the PROTO, see thunder.x3dv. }
if FdUrl.Count <> 0 then
BufferLoaded := true;
end;
procedure TAudioClipNode.BeforeTraverse(StateStack: TX3DGraphTraverseStateStack);
begin
inherited;
{ Reasons for doing this: see ParseAfter }
if FdUrl.Count <> 0 then
BufferLoaded := true;
end;
function TAudioClipNode.CycleInterval: TFloatTime;
begin
{ Secure from invalid FDuration (when sound file is not loaded, or duration
is just unknown). }
if FDuration > 0 then
Result := FDUration / Pitch else
Result := 0;
end;
procedure TAudioClipNode.SoundRelease(ASound: TSound);
begin
Sound := nil;
end;
procedure TAudioClipNode.UpdatePlayingEvent(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
begin
UpdatePlaying;
end;
procedure TAudioClipNode.UpdatePlaying;
function GetSoundNode: TSoundNode;
begin
if (ParentFieldsCount <> 0) and
(ParentFields[0].ParentNode <> nil) and
(ParentFields[0].ParentNode is TSoundNode) then
Result := TSoundNode(ParentFields[0].ParentNode);
end;
var
SoundNode: TSoundNode;
begin
if TimeDependentNodeHandler.IsActive and not TimeDependentNodeHandler.IsPaused then
begin
{ if not playing, try to start playing }
if Sound = nil then
begin
BufferLoaded := true;
SoundNode := GetSoundNode;
if (Buffer <> 0) and (SoundNode <> nil) then
begin
Sound := SoundEngine.PlaySound(Buffer,
SoundNode.FdSpatialize.Value,
FdLoop.Value,
Clamped(Round(SoundNode.FdPriority.Value * 1000), 0, 1000),
SoundNode.FdIntensity.Value, 0, 1,
MatrixMultPoint(SoundNode.FTransform, SoundNode.FdLocation.Value),
Pitch,
{ We pass min/maxFront as OpenAL reference distance/max distance,
as their meanings match for distance model = dmLinearDistanceClamped. }
SoundNode.FTransformScale * SoundNode.FdMinFront.Value,
SoundNode.FTransformScale * SoundNode.FdMaxFront.Value);
if Sound <> nil then
Sound.OnRelease := @SoundRelease;
end;
end;
end else
begin
{ if playing, try to stop playing }
if Sound <> nil then
Sound.Release;
end;
end;
procedure TAudioClipNode.SetBufferLoaded(const Value: boolean);
var
I: Integer;
FullUrl: string;
LoadedDuration: TFloatTime;
begin
if FBufferLoaded <> Value then
begin
FBufferLoaded := Value;
if Value then
begin
for I := 0 to FdUrl.Items.Count - 1 do
begin
FullUrl := PathFromBaseUrl(FdUrl.Items[I]);
try
{ LoadBuffer inside may cause OpenAL open, and we don't want to
do ALOpenClose then.
Doing OnOpenClose.Remove/Add now would be bad, as this code
may be called when inside iterating over OnOpenClose:
(- SetEnable calls ALContextOpen
- that calls OnOpenClose.ExecuteAll
- that calls Self.ALOpenClose
- that calls UpdatePlaying
- that loads the buffer
). Shuffling now OnOpenClose items (Add always adds at the end)
would mean that the same callback is again processed,
so Self.ALOpenClose is called with ALActive = true and Buffer <> 0,
so ALContextOpen releases a buffer for a playing sound source
so OpenAL raises error.
Testcase:
- start view3dscene with sound = on
- click CW on all 4 speakers in sound_final.x3dv
- turn sound off
- turn sound back on
- click on lower-bottom speakers }
Inc(InsideLoadBuffer);
try
Buffer := SoundEngine.LoadBuffer(FullUrl, LoadedDuration);
finally Dec(InsideLoadBuffer) end;
FDuration := LoadedDuration;
EventDuration_Changed.Send(FDuration);
Break;
except
on E: Exception do
{ Remember that OnWarning *may* raise an exception. }
OnWarning(wtMinor, 'Audio', Format(SLoadError,
[E.ClassName, 'sound file', URIDisplay(FullUrl), E.Message]));
end;
end;
end else
begin
Assert(Sound = nil, 'You can unload a buffer only when Sound is not playing');
SoundEngine.FreeBuffer(Buffer);
end;
end;
end;
procedure TAudioClipNode.ALOpenClose(Sender: TObject);
begin
if InsideLoadBuffer <> 0 then Exit;
if (Sender as TSoundEngine).ALActive then
begin
{ Maybe we should start playing? }
BufferLoaded := false; //< force reloading buffer next time
UpdatePlaying;
end else
begin
{ Zero Buffer, since closing of OpenAL already freed it (and stopped the
Sound). }
Buffer := 0;
end;
end;
procedure TSoundNode.CreateNode;
begin
inherited;
FFdDirection := TSFVec3f.Create(Self, 'direction', Vector3Single(0, 0, 1));
Fields.Add(FFdDirection);
{ X3D specification comment: (-Inf,Inf) }
FFdIntensity := TSFFloat.Create(Self, 'intensity', 1);
Fields.Add(FFdIntensity);
{ X3D specification comment: [0,1] }
FFdLocation := TSFVec3f.Create(Self, 'location', Vector3Single(0, 0, 0));
FdLocation.OnReceive.Add(@EventLocationReceive);
Fields.Add(FFdLocation);
{ X3D specification comment: (-Inf,Inf) }
FFdMaxBack := TSFFloat.Create(Self, 'maxBack', 10);
Fields.Add(FFdMaxBack);
{ X3D specification comment: [0,Inf) }
FFdMaxFront := TSFFloat.Create(Self, 'maxFront', 10);
Fields.Add(FFdMaxFront);
{ X3D specification comment: [0,Inf) }
FFdMinBack := TSFFloat.Create(Self, 'minBack', 1);
Fields.Add(FFdMinBack);
{ X3D specification comment: [0,Inf) }
FFdMinFront := TSFFloat.Create(Self, 'minFront', 1);
Fields.Add(FFdMinFront);
{ X3D specification comment: [0,Inf) }
FFdPriority := TSFFloat.Create(Self, 'priority', 0);
Fields.Add(FFdPriority);
{ X3D specification comment: [0,1] }
FFdSource := TSFNode.Create(Self, 'source', IAbstractSoundSourceNode);
Fields.Add(FFdSource);
FFdSpatialize := TSFBool.Create(Self, 'spatialize', true);
FFdSpatialize.Exposed := false;
Fields.Add(FFdSpatialize);
DefaultContainerField := 'children';
FTransform := IdentityMatrix4Single;
FTransformScale := 1;
end;
procedure TSoundNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction);
begin
inherited;
{ Necessary in order to call TAudioClipNode.BeforeTraverse,
testcase: thunder.x3dv. }
FdSource.EnumerateValid(Func);
end;
procedure TSoundNode.UpdateSourcePosition;
var
AudioClip: TAudioClipNode;
begin
if (FdSource.Value <> nil) and
(FdSource.Value is TAudioClipNode) then
begin
AudioClip := TAudioClipNode(FdSource.Value);
if AudioClip.Sound <> nil then
AudioClip.Sound.Position := MatrixMultPoint(FTransform, FdLocation.Value);
end;
end;
procedure TSoundNode.BeforeTraverse(StateStack: TX3DGraphTraverseStateStack);
begin
inherited;
FTransform := StateStack.Top.Transform;
FTransformScale := StateStack.Top.TransformScale;
UpdateSourcePosition;
end;
procedure TSoundNode.EventLocationReceive(
Event: TX3DEvent; Value: TX3DField; const Time: TX3DTime);
begin
UpdateSourcePosition;
end;
class function TSoundNode.ClassNodeTypeName: string;
begin
Result := 'Sound';
end;
class function TSoundNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNVRML97Nodes + ClassNodeTypeName) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure RegisterSoundNodes;
begin
NodesManager.RegisterNodeClasses([
TAudioClipNode,
TSoundNode
]);
end;
{$endif read_implementation}
|