| 1 |
{%MainUnit castlesoundengine.pas} |
| 2 |
{ |
| 3 |
Copyright 2021-2021 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
//type // allow type declaration to continue |
| 20 |
|
| 21 |
{ Controls a sound playback. |
| 22 |
You can pass an instance of it to |
| 23 |
@link(TSoundEngine.Play) or @link(TCastleSoundSource.Play). |
| 24 |
It allows to control sound playback before you start playing (like @link(OnStop)) |
| 25 |
and after (like @link(Offset)). |
| 26 |
|
| 27 |
You can reuse the same instance of TCastlePlayingSound for playback many times, |
| 28 |
but you can start new playback only if the previous one finished. |
| 29 |
IOW, you can pass to @link(TSoundEngine.Play) or @link(TCastleSoundSource.Play) |
| 30 |
only instances when @link(Playing) is @false. |
| 31 |
If you don't want to wait, just create a new instance of TCastlePlayingSound -- they |
| 32 |
are designed to be lightweight, you can create them during runtime. |
| 33 |
|
| 34 |
Destroying this class always stops the associated sound playback. |
| 35 |
Note that it is a TComponent descendant, so you can make it automatically destroyed |
| 36 |
when some owner TComponent is destroyed. |
| 37 |
|
| 38 |
You can make your own descendants of this class, to associate any additional information |
| 39 |
with the playback of the sound. } |
| 40 |
TCastlePlayingSound = class(TComponent) |
| 41 |
strict private |
| 42 |
FSound: TCastleSound; //< @nil => not playing (when not playing, it can be nil or non-nil) |
| 43 |
FSource: TInternalSoundSource; //< @nil <=> not playing; we only keep FSource with Used=true |
| 44 |
FFreeOnStop: Boolean; |
| 45 |
FOnStop: TNotifyEvent; |
| 46 |
FLoop: Boolean; |
| 47 |
FInitialOffset: Single; |
| 48 |
FVolume: Single; |
| 49 |
FPitch: Single; |
| 50 |
FPriority: Single; |
| 51 |
procedure SetSound(const Value: TCastleSound); |
| 52 |
function GetOffset: Single; |
| 53 |
procedure SetOffset(const Value: Single); |
| 54 |
procedure SetOnStop(const Value: TNotifyEvent); |
| 55 |
procedure SetFreeOnStop(const Value: Boolean); |
| 56 |
procedure SetLoop(const Value: Boolean); |
| 57 |
procedure SourceRelease(Source: TInternalSoundSource); |
| 58 |
procedure SetVolume(const Value: Single); |
| 59 |
procedure SetPitch(const Value: Single); |
| 60 |
procedure SetPriority(const Value: Single); |
| 61 |
{ React to sound parameters changes during playback (when FSource <> nil). } |
| 62 |
procedure SoundChangeDuringPlayback(const Sender: TCastleSound; const Change: TSoundChange); |
| 63 |
private |
| 64 |
{ Used internally by TSoundEngine to notify we started playing this, using given ASource. } |
| 65 |
procedure Start(const ASource: TInternalSoundSource); |
| 66 |
protected |
| 67 |
{ Do the necessary job after source stopped, due to any reason. |
| 68 |
In this class it handles OnStop and FreeOnStop. |
| 69 |
|
| 70 |
If you override this: Since FreeOnStop may free the instance, |
| 71 |
you should add your code @italic(before) calling inherited. } |
| 72 |
procedure DoStop; virtual; |
| 73 |
public |
| 74 |
constructor Create(AOwner: TComponent); override; |
| 75 |
destructor Destroy; override; |
| 76 |
|
| 77 |
{ Associated sound used for playing. |
| 78 |
You have to set it before playing. |
| 79 |
Cannot be changed while playing. } |
| 80 |
property Sound: TCastleSound read FSound write SetSound; |
| 81 |
|
| 82 |
{ Volume, a multiplication of @link(TCastleSound.Volume), |
| 83 |
@link(TCastlePlayingSound.Volume) and @link(TCastleSoundSource.Volume) |
| 84 |
(if the sound is played through @link(TCastleSoundSource)). |
| 85 |
@exclude } |
| 86 |
function InternalFinalVolume: Single; virtual; |
| 87 |
|
| 88 |
{ Pitch, a multiplication of @link(TCastleSound.Pitch), |
| 89 |
@link(TCastlePlayingSound.Pitch) and @link(TCastleSoundSource.Pitch) |
| 90 |
(if the sound is played through @link(TCastleSoundSource)). |
| 91 |
@exclude } |
| 92 |
function InternalFinalPitch: Single; virtual; |
| 93 |
|
| 94 |
{ Priority, a multiplication of @link(TCastleSound.Priority), |
| 95 |
@link(TCastlePlayingSound.Priority) and @link(TCastleSoundSource.Priority) |
| 96 |
(if the sound is played through @link(TCastleSoundSource)). |
| 97 |
@exclude } |
| 98 |
function InternalFinalPriority: Single; virtual; |
| 99 |
|
| 100 |
{ If provided, we will call this event when sound stops playing. |
| 101 |
|
| 102 |
Note that in some cases the sound cannot be played at all. |
| 103 |
For example when TCastleSound is not loaded (no TCastleSound.URL provided, |
| 104 |
or invalid TCastleSound.URL provided) or when there are no free sound sources |
| 105 |
(these are limited in sound backends; the new sound may "steal" some sound source |
| 106 |
but only if the @link(TCastleSound.Priority) is high enough). |
| 107 |
Then @link(TSoundEngine.Play) or @link(TCastleSoundSource.Play) |
| 108 |
call the OnStop event immediately, from within Play call. |
| 109 |
|
| 110 |
If the object owning the notification method may be destroyed before |
| 111 |
TCastlePlayingSound is destroyed, be sure to set this callback to @nil. |
| 112 |
Otherwise the callback could try calling method of a non-existent object. |
| 113 |
|
| 114 |
You can only set this when sound is not playing |
| 115 |
(as otherwise it would be an easy source of bugs -- |
| 116 |
sometimes @link(TSoundEngine.Play) wants to immediately stop sound playback). } |
| 117 |
property OnStop: TNotifyEvent read FOnStop write SetOnStop; |
| 118 |
|
| 119 |
{ Automatically free when the playback finishes. |
| 120 |
If the playback is not yet started, it will wait for it to start, and then finish. |
| 121 |
|
| 122 |
You can only set this when sound is not playing |
| 123 |
(as otherwise it would be an easy source of bugs -- |
| 124 |
sometimes @link(TSoundEngine.Play) wants to immediately stop sound playback). } |
| 125 |
property FreeOnStop: Boolean read FFreeOnStop write SetFreeOnStop default false; |
| 126 |
|
| 127 |
{ Is the playback looping. |
| 128 |
You can change this on a playing or stopped sound. } |
| 129 |
property Loop: Boolean read FLoop write SetLoop default false; |
| 130 |
|
| 131 |
{ Is the sound playing now. |
| 132 |
Using @link(TSoundEngine.Play) or @link(TCastleSoundSource.Play) |
| 133 |
makes it @true for some time, then (right before OnStop) it switches back to @false. } |
| 134 |
function Playing: Boolean; |
| 135 |
|
| 136 |
{ Volume (how loud the playing sound is). |
| 137 |
|
| 138 |
The effective sound volume is a multiplication of @link(TCastleSound.Volume), |
| 139 |
@link(TCastlePlayingSound.Volume) and @link(TCastleSoundSource.Volume) |
| 140 |
(if the sound is played through @link(TCastleSoundSource)). |
| 141 |
It is also affected by spatial calculations (if the sound is played |
| 142 |
through @link(TCastleSoundSource) with @link(TCastleSoundSource.Spatial) = @true) |
| 143 |
and finally by a master @link(TSoundEngine.Volume SoundEngine.Volume). |
| 144 |
|
| 145 |
Any value > 0 is allowed. } |
| 146 |
property Volume: Single read FVolume write SetVolume {$ifdef FPC}default 1.0{$endif}; |
| 147 |
|
| 148 |
{ Sound playing speed. |
| 149 |
|
| 150 |
The effective sound pitch is a multiplication of @link(TCastleSound.Pitch), |
| 151 |
@link(TCastlePlayingSound.Pitch) and @link(TCastleSoundSource.Pitch) |
| 152 |
(if the sound is played through @link(TCastleSoundSource)). |
| 153 |
|
| 154 |
Any value > 0 is allowed. } |
| 155 |
property Pitch: Single read FPitch write SetPitch {$ifdef FPC}default 1.0{$endif}; |
| 156 |
|
| 157 |
{ How important is the sound, between 0.0 (least important) to 1.0 (most important). |
| 158 |
|
| 159 |
The effective priority is a multiplication of @link(TCastleSound.Priority), |
| 160 |
@link(TCastlePlayingSound.Priority) and @link(TCastleSoundSource.Priority) |
| 161 |
(if the sound is played through @link(TCastleSoundSource)). } |
| 162 |
property Priority: Single read FPriority write SetPriority {$ifdef FPC}default 1.0{$endif}; |
| 163 |
|
| 164 |
{ Playback time of this sound, expressed in seconds. |
| 165 |
|
| 166 |
This value will loop back to zero for looping sound sources. |
| 167 |
Setting this to something larger than the sound duration |
| 168 |
(which you can read from @link(TCastleSound.Duration)) is ignored. |
| 169 |
|
| 170 |
This offset refers to the sound like it had a @link(TCastleSound.Pitch Pitch) equal 1.0 |
| 171 |
(when the sound is not slowed down or sped up). |
| 172 |
So this offset will vary from 0 to the @link(TCastleSound.Duration), |
| 173 |
regardless of the initial/current @link(TCastleSound.Pitch) value. |
| 174 |
The @italic(actual) seconds passed since the sound started |
| 175 |
playing may be different, if you change the @link(TCastleSound.Pitch) |
| 176 |
to something else than 1.0. |
| 177 |
|
| 178 |
Using this on a sound that is not playing is allowed. |
| 179 |
Getting this value always returns zero in this case, and setting it is ignored. |
| 180 |
|
| 181 |
Note: you can adjust InitialOffset to determine the offset for starting new sound. |
| 182 |
It is deliberately a separate property from current @link(Offset), |
| 183 |
otherwise it would be too easy to accidentally start playing in the middle when |
| 184 |
you wanted to play from the start. } |
| 185 |
property Offset: Single read GetOffset write SetOffset; |
| 186 |
|
| 187 |
{ Initial position, in seconds, within the sound when we start playing it. |
| 188 |
You can get/set this property regardless of whether sound is playing. } |
| 189 |
property InitialOffset: Single read FInitialOffset write FInitialOffset; |
| 190 |
|
| 191 |
{ Associated sound source, to change parameters of this sound at runtime. |
| 192 |
@nil if not playing. |
| 193 |
@exclude } |
| 194 |
property InternalSource: TInternalSoundSource read FSource; |
| 195 |
|
| 196 |
{ Stop playing now. Note that you can also free the TCastlePlayingSound instance, |
| 197 |
it will always automatically stop the playback. |
| 198 |
|
| 199 |
Calling this on a sound that is not playing is allowed, and ignored. } |
| 200 |
procedure Stop; |
| 201 |
end; |
| 202 |
|
| 203 |
{$endif read_interface} |
| 204 |
|
| 205 |
{$ifdef read_implementation} |
| 206 |
|
| 207 |
{ Design note: The TCastlePlayingSound class is somewhat similar in features |
| 208 |
to TInternalSoundSource. |
| 209 |
In fact we used to expose TInternalSoundSource under deprecated TSound name |
| 210 |
for a very similar use-cases that TCastlePlayingSound now handles. |
| 211 |
|
| 212 |
However the lifetime of TInternalSoundSource was less under user control. |
| 213 |
It generally existed for a long time, unless you decreased SoundEngine.MaxAllocatedSources. |
| 214 |
TCastlePlayingSoundXxx has clearer lifetime: |
| 215 |
controlled by user, has FreeOnStop, stops automatically when freed. } |
| 216 |
|
| 217 |
constructor TCastlePlayingSound.Create(AOwner: TComponent); |
| 218 |
begin |
| 219 |
inherited; |
| 220 |
FVolume := 1.0; |
| 221 |
FPitch := 1.0; |
| 222 |
FPriority := 1.0; |
| 223 |
end; |
| 224 |
|
| 225 |
destructor TCastlePlayingSound.Destroy; |
| 226 |
begin |
| 227 |
FFreeOnStop := false; // we are already freeing, don't let DoStop call destructor recursively |
| 228 |
Stop; |
| 229 |
inherited; |
| 230 |
end; |
| 231 |
|
| 232 |
procedure TCastlePlayingSound.SetSound(const Value: TCastleSound); |
| 233 |
begin |
| 234 |
if FSound <> Value then |
| 235 |
begin |
| 236 |
if Playing then |
| 237 |
raise Exception.Create('Cannot change TCastlePlayingSound.Sound during playback'); |
| 238 |
FSound := Value; |
| 239 |
end; |
| 240 |
end; |
| 241 |
|
| 242 |
function TCastlePlayingSound.GetOffset: Single; |
| 243 |
begin |
| 244 |
if FSource <> nil then |
| 245 |
Result := FSource.Offset |
| 246 |
else |
| 247 |
Result := 0; |
| 248 |
end; |
| 249 |
|
| 250 |
procedure TCastlePlayingSound.SetOffset(const Value: Single); |
| 251 |
begin |
| 252 |
if FSource <> nil then |
| 253 |
FSource.Offset := Value; |
| 254 |
end; |
| 255 |
|
| 256 |
function TCastlePlayingSound.Playing: Boolean; |
| 257 |
begin |
| 258 |
Result := FSource <> nil; |
| 259 |
end; |
| 260 |
|
| 261 |
procedure TCastlePlayingSound.SetOnStop(const Value: TNotifyEvent); |
| 262 |
begin |
| 263 |
if not SameMethods(TMethod(FOnStop), TMethod(Value)) then |
| 264 |
begin |
| 265 |
if Playing then |
| 266 |
raise Exception.Create('Cannot change TCastlePlayingSound.OnStop during playback'); |
| 267 |
FOnStop := Value; |
| 268 |
end; |
| 269 |
end; |
| 270 |
|
| 271 |
procedure TCastlePlayingSound.SetFreeOnStop(const Value: Boolean); |
| 272 |
begin |
| 273 |
if FFreeOnStop <> Value then |
| 274 |
begin |
| 275 |
if Playing then |
| 276 |
raise Exception.Create('Cannot change TCastlePlayingSound.FreeOnStop during playback'); |
| 277 |
FFreeOnStop := Value; |
| 278 |
end; |
| 279 |
end; |
| 280 |
|
| 281 |
procedure TCastlePlayingSound.SetLoop(const Value: Boolean); |
| 282 |
begin |
| 283 |
if FLoop <> Value then |
| 284 |
begin |
| 285 |
FLoop := Value; |
| 286 |
if FSource <> nil then |
| 287 |
FSource.Loop := Value; |
| 288 |
end; |
| 289 |
end; |
| 290 |
|
| 291 |
procedure TCastlePlayingSound.Stop; |
| 292 |
begin |
| 293 |
if FSource <> nil then |
| 294 |
begin |
| 295 |
Assert(FSource.Used); // we only keep FSource with Used=true |
| 296 |
FSource.Release; // note: this can destroy our instance (if FreeOnStop) |
| 297 |
{ The SourceRelease should already set FSource to nil |
| 298 |
(but we cannnot make Assert below, as we possibly no longer exist after |
| 299 |
FSource.Release, due to FreeOnStop; testcase: castle1 tower level, let ball missile |
| 300 |
explode on player). } |
| 301 |
// Assert(FSource = nil); |
| 302 |
end; |
| 303 |
end; |
| 304 |
|
| 305 |
procedure TCastlePlayingSound.Start(const ASource: TInternalSoundSource); |
| 306 |
begin |
| 307 |
if FSource <> nil then |
| 308 |
raise Exception.Create('Cannot start new TCastlePlayingSound playback before previous one finished'); |
| 309 |
Assert(FSound <> nil); |
| 310 |
Assert(ASource <> nil); |
| 311 |
Assert(ASource.Buffer = FSound.InternalBuffer); |
| 312 |
FSource := ASource; |
| 313 |
FSource.OnRelease := {$ifdef FPC}@{$endif}SourceRelease; |
| 314 |
|
| 315 |
FSound.InternalAddChangeNotification({$ifdef FPC}@{$endif}SoundChangeDuringPlayback); |
| 316 |
end; |
| 317 |
|
| 318 |
procedure TCastlePlayingSound.SourceRelease(Source: TInternalSoundSource); |
| 319 |
begin |
| 320 |
// FSound must always exist during playback, since we stop playback when FSound is freed. |
| 321 |
FSound.InternalRemoveChangeNotification({$ifdef FPC}@{$endif}SoundChangeDuringPlayback); |
| 322 |
|
| 323 |
Assert(Source = FSource); // we should only ever get callback about current FSource |
| 324 |
Source.OnRelease := nil; |
| 325 |
FSource := nil; |
| 326 |
DoStop; // note: this can destroy our instance (if FreeOnStop) |
| 327 |
end; |
| 328 |
|
| 329 |
procedure TCastlePlayingSound.DoStop; |
| 330 |
begin |
| 331 |
if Assigned(OnStop) then |
| 332 |
OnStop(Self); |
| 333 |
if FreeOnStop then |
| 334 |
Self.Destroy; |
| 335 |
end; |
| 336 |
|
| 337 |
procedure TCastlePlayingSound.SoundChangeDuringPlayback(const Sender: TCastleSound; const Change: TSoundChange); |
| 338 |
begin |
| 339 |
Assert(FSource <> nil); |
| 340 |
Assert(Sender = FSound); |
| 341 |
|
| 342 |
case Change of |
| 343 |
scAfterOther: |
| 344 |
begin |
| 345 |
FSource.Volume := InternalFinalVolume; |
| 346 |
FSource.Pitch := InternalFinalPitch; |
| 347 |
FSource.Priority := InternalFinalPriority; |
| 348 |
{$warnings off} // using unportable symbols knowingly |
| 349 |
FSource.MinGain := FSound.MinGain; |
| 350 |
FSource.MaxGain := FSound.MaxGain; |
| 351 |
{$warnings on} |
| 352 |
FSource.ReferenceDistance := FSound.ReferenceDistance; |
| 353 |
FSource.MaxDistance := FSound.MaxDistance; |
| 354 |
end; |
| 355 |
scBeforeBufferDestroyed: |
| 356 |
Stop; |
| 357 |
else ; |
| 358 |
end; |
| 359 |
end; |
| 360 |
|
| 361 |
function TCastlePlayingSound.InternalFinalVolume: Single; |
| 362 |
begin |
| 363 |
Result := FVolume; |
| 364 |
if Sound <> nil then |
| 365 |
Result := Result * Sound.Volume; |
| 366 |
end; |
| 367 |
|
| 368 |
function TCastlePlayingSound.InternalFinalPitch: Single; |
| 369 |
begin |
| 370 |
Result := FPitch; |
| 371 |
if Sound <> nil then |
| 372 |
Result := Result * Sound.Pitch; |
| 373 |
end; |
| 374 |
|
| 375 |
function TCastlePlayingSound.InternalFinalPriority: Single; |
| 376 |
begin |
| 377 |
Result := FPriority; |
| 378 |
if Sound <> nil then |
| 379 |
Result := Result * Sound.Priority; |
| 380 |
end; |
| 381 |
|
| 382 |
procedure TCastlePlayingSound.SetVolume(const Value: Single); |
| 383 |
begin |
| 384 |
if FVolume <> Value then |
| 385 |
begin |
| 386 |
FVolume := Value; |
| 387 |
// as InternalFinalVolume changed, update playback (InternalSource) properties |
| 388 |
if FSource <> nil then |
| 389 |
SoundChangeDuringPlayback(FSound, scAfterOther); |
| 390 |
end; |
| 391 |
end; |
| 392 |
|
| 393 |
procedure TCastlePlayingSound.SetPitch(const Value: Single); |
| 394 |
begin |
| 395 |
if FPitch <> Value then |
| 396 |
begin |
| 397 |
FPitch := Value; |
| 398 |
// as InternalFinalPitch changed, update playback (InternalSource) properties |
| 399 |
if FSource <> nil then |
| 400 |
SoundChangeDuringPlayback(FSound, scAfterOther); |
| 401 |
end; |
| 402 |
end; |
| 403 |
|
| 404 |
procedure TCastlePlayingSound.SetPriority(const Value: Single); |
| 405 |
begin |
| 406 |
if FPriority <> Value then |
| 407 |
begin |
| 408 |
FPriority := Value; |
| 409 |
// as InternalFinalPriority changed, update playback (InternalSource) properties |
| 410 |
if FSource <> nil then |
| 411 |
SoundChangeDuringPlayback(FSound, scAfterOther); |
| 412 |
end; |
| 413 |
end; |
| 414 |
|
| 415 |
{$endif read_implementation} |