| 1 |
{%MainUnit castlesoundengine.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-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 |
{ Manager of allocated sounds. |
| 22 |
|
| 23 |
For efficiency, the pool of available sound sources (things that are actually |
| 24 |
audible at a given time) is limited. |
| 25 |
This limit comes from the backend limits (like OpenAL or APIs underneath OpenAL), |
| 26 |
it may also come from sound hardware limitations, |
| 27 |
and in general you should not have too many playing sources, |
| 28 |
as mixing many sources is time-consuming. |
| 29 |
So you cannot simply allocate new sound source for each of 100 creatures |
| 30 |
you display in the game. |
| 31 |
|
| 32 |
This class hides this limitation, by managing a pool of sound sources, |
| 33 |
and returning on demand the next unused sound source (or @nil). |
| 34 |
It can prioritize sound sources, |
| 35 |
it can reuse sound sources that finished playing, |
| 36 |
it can interrupt a lower-priority sound when necessary to play |
| 37 |
a higher-priority sound. |
| 38 |
|
| 39 |
This is automatically used by higher-level comfortable methods to play |
| 40 |
sounds like @link(TSoundEngine.Play). } |
| 41 |
TSoundAllocator = class |
| 42 |
strict private |
| 43 |
FAllocatedSources: TInternalSoundSourceList; |
| 44 |
FMinAllocatedSources: Cardinal; |
| 45 |
FMaxAllocatedSources: Cardinal; |
| 46 |
LastSoundRefresh: TTimerResult; |
| 47 |
|
| 48 |
procedure Update(Sender: TObject); |
| 49 |
procedure SetMinAllocatedSources(const Value: Cardinal); |
| 50 |
procedure SetMaxAllocatedSources(const Value: Cardinal); |
| 51 |
private |
| 52 |
Backend: TSoundEngineBackend; |
| 53 |
FIsContextOpenSuccess: boolean; |
| 54 |
procedure ContextOpenCore; virtual; |
| 55 |
procedure ContextCloseCore; virtual; |
| 56 |
|
| 57 |
{ Allocate sound for playing. You should initialize the sound source |
| 58 |
properties and start playing the sound. |
| 59 |
|
| 60 |
Note that if you don't call TInternalSoundSource.Backend.Play immediately, the source may be detected |
| 61 |
as unused (and recycled for another sound) at the next |
| 62 |
sound allocation, play, update etc. |
| 63 |
|
| 64 |
If we can't allocate new sound source, we return nil. |
| 65 |
This may happen if your sound context is not initialized. |
| 66 |
It may also happen if we cannot create more sources (because |
| 67 |
we hit MaxAllocatedSources limit, or backend (like OpenAL) just refuses to create |
| 68 |
more sources) and all existing sounds are used and their |
| 69 |
Priority is > given here Priority. |
| 70 |
|
| 71 |
Note for looping sounds: just like any other sound, looping sound |
| 72 |
may be stopped because the sounds are needed for other sounds. |
| 73 |
If you want to try to restart the looping sound, you will have |
| 74 |
to implement it yourself. Or you can just set Priority of looping |
| 75 |
sounds high enough, and don't use too many looping sounds, |
| 76 |
to never let them be eliminated by other sounds. } |
| 77 |
function AllocateSound(const Priority: Single): TInternalSoundSource; |
| 78 |
|
| 79 |
{ Stop all the sources currently playing. Especially useful since |
| 80 |
you have to stop a source before releasing it's associated buffer. } |
| 81 |
procedure StopAllSources; |
| 82 |
public |
| 83 |
const |
| 84 |
DefaultMinAllocatedSources = 4; |
| 85 |
DefaultMaxAllocatedSources = 16; |
| 86 |
|
| 87 |
constructor Create; |
| 88 |
destructor Destroy; override; |
| 89 |
|
| 90 |
{ Minimum / maximum number of allocated sources. |
| 91 |
Always keep MinAllocatedSources <= MaxAllocatedSources. |
| 92 |
|
| 93 |
For the sake of speed, we always keep allocated at least |
| 94 |
MinAllocatedSources sources. This must be >= 1. |
| 95 |
Setting MinAllocatedSources too large value will raise |
| 96 |
ENoMoreSources. |
| 97 |
|
| 98 |
At most MaxAllocatedSources sources may be simultaneously used (played). |
| 99 |
This prevents us from allocating too many sounds, |
| 100 |
which would be bad for speed (not to mention that it may |
| 101 |
be impossible under some backends, like OpenAL on Windows). |
| 102 |
When all MaxAllocatedSources sources are playing, the only way |
| 103 |
to play another sound is to use appropriately high @code(Priority) |
| 104 |
to AllocateSound. |
| 105 |
|
| 106 |
@groupBegin } |
| 107 |
property MinAllocatedSources: Cardinal |
| 108 |
read FMinAllocatedSources write SetMinAllocatedSources |
| 109 |
default DefaultMinAllocatedSources; |
| 110 |
|
| 111 |
property MaxAllocatedSources: Cardinal |
| 112 |
read FMaxAllocatedSources write SetMaxAllocatedSources |
| 113 |
default DefaultMaxAllocatedSources; |
| 114 |
{ @groupEnd } |
| 115 |
|
| 116 |
{ All allocated (not necessarily used) sources. |
| 117 |
Accessing this is useful only for debugging tasks, |
| 118 |
in normal circumstances this is internal. |
| 119 |
This is @nil when ContextOpen was not yet called. } |
| 120 |
property InternalAllocatedSources: TInternalSoundSourceList read FAllocatedSources; |
| 121 |
|
| 122 |
{ Do we have active sound rendering context. |
| 123 |
This is @true when you successfully |
| 124 |
called @link(TSoundEngine.ContextOpen) and you didn't call |
| 125 |
@link(TSoundEngine.ContextClose) yet. |
| 126 |
|
| 127 |
You should not need this property much. |
| 128 |
The whole CastleSoundEngine API works regardless if the context |
| 129 |
was successfully open or not. |
| 130 |
However, reading this is useful to display to user warning |
| 131 |
e.g. "Sound could not be initialized for some reason" |
| 132 |
(use @link(TSoundEngine.Information) to get the details). |
| 133 |
|
| 134 |
In case of OpenAL backend, this also implies that OpenAL library is loaded. } |
| 135 |
property IsContextOpenSuccess: boolean read FIsContextOpenSuccess; |
| 136 |
end; |
| 137 |
|
| 138 |
{$endif read_interface} |
| 139 |
|
| 140 |
{$ifdef read_implementation} |
| 141 |
|
| 142 |
{ TSoundAllocator ---------------------------------------------------------- } |
| 143 |
|
| 144 |
constructor TSoundAllocator.Create; |
| 145 |
begin |
| 146 |
inherited; |
| 147 |
FMinAllocatedSources := DefaultMinAllocatedSources; |
| 148 |
FMaxAllocatedSources := DefaultMaxAllocatedSources; |
| 149 |
|
| 150 |
{$ifdef CASTLE_SOUND_BACKEND_DEFAULT_OPENAL} UseOpenALSoundBackend; {$endif} |
| 151 |
end; |
| 152 |
|
| 153 |
destructor TSoundAllocator.Destroy; |
| 154 |
begin |
| 155 |
FreeAndNil(Backend); |
| 156 |
inherited; |
| 157 |
end; |
| 158 |
|
| 159 |
procedure TSoundAllocator.ContextOpenCore; |
| 160 |
var |
| 161 |
I: Integer; |
| 162 |
begin |
| 163 |
FAllocatedSources := TInternalSoundSourceList.Create(true); |
| 164 |
FAllocatedSources.Count := MinAllocatedSources; |
| 165 |
for I := 0 to FAllocatedSources.Count - 1 do |
| 166 |
FAllocatedSources[I] := TInternalSoundSource.Create(Backend); |
| 167 |
|
| 168 |
ApplicationProperties.OnUpdate.Add({$ifdef FPC}@{$endif} Update); |
| 169 |
end; |
| 170 |
|
| 171 |
procedure TSoundAllocator.ContextCloseCore; |
| 172 |
var |
| 173 |
I: Integer; |
| 174 |
begin |
| 175 |
if FAllocatedSources <> nil then |
| 176 |
begin |
| 177 |
{ Stop using and free allocated sounds. } |
| 178 |
for I := 0 to FAllocatedSources.Count - 1 do |
| 179 |
{ Although usually we are sure that every FAllocatedSources[I] <> nil, |
| 180 |
in this case we must take into account that maybe our constructor |
| 181 |
raise ENonMoreSources and so some FAllocatedSources[I] were |
| 182 |
not initialized. } |
| 183 |
if FAllocatedSources[I] <> nil then |
| 184 |
begin |
| 185 |
if FAllocatedSources[I].Used then |
| 186 |
FAllocatedSources[I].Release; |
| 187 |
{ This will free FAllocatedSources[I], as FAllocatedSources owns children } |
| 188 |
FAllocatedSources[I] := nil; |
| 189 |
end; |
| 190 |
|
| 191 |
FreeAndNil(FAllocatedSources); |
| 192 |
end; |
| 193 |
|
| 194 |
if ApplicationProperties(false) <> nil then |
| 195 |
ApplicationProperties(false).OnUpdate.Remove({$ifdef FPC}@{$endif} Update); |
| 196 |
end; |
| 197 |
|
| 198 |
function TSoundAllocator.AllocateSound( |
| 199 |
const Priority: Single): TInternalSoundSource; |
| 200 |
var |
| 201 |
I: Integer; |
| 202 |
MinPriorityIndex: Integer; |
| 203 |
begin |
| 204 |
Result := nil; |
| 205 |
|
| 206 |
{ Sound context not initialized yet } |
| 207 |
if FAllocatedSources = nil then Exit; |
| 208 |
|
| 209 |
{ Try: maybe we have already allocated unused sound ? |
| 210 |
If no unused sound will be found, it will calculate |
| 211 |
MinPriorityIndex, this will be useful later. } |
| 212 |
MinPriorityIndex := -1; |
| 213 |
for I := 0 to FAllocatedSources.Count - 1 do |
| 214 |
if not FAllocatedSources[I].Used then |
| 215 |
begin |
| 216 |
Result := FAllocatedSources[I]; |
| 217 |
{ Breaking here means that MinPriorityIndex will not be calculated |
| 218 |
correctly (because we did not iterate to the end of FAllocatedSources |
| 219 |
list). But that's OK, because if Result <> nil here, then we will |
| 220 |
not need MinPriorityIndex later. } |
| 221 |
Break; |
| 222 |
end else |
| 223 |
begin |
| 224 |
{ Update MinPriorityIndex } |
| 225 |
if (MinPriorityIndex = -1) or |
| 226 |
(FAllocatedSources[I].Priority < |
| 227 |
FAllocatedSources[MinPriorityIndex].Priority) then |
| 228 |
MinPriorityIndex := I; |
| 229 |
end; |
| 230 |
|
| 231 |
{ Try: maybe one of the allocated sounds is marked as Used, |
| 232 |
but actually it's not used anymore ? } |
| 233 |
if Result = nil then |
| 234 |
begin |
| 235 |
for I := 0 to FAllocatedSources.Count - 1 do |
| 236 |
if not FAllocatedSources[I].PlayingOrPaused then |
| 237 |
begin |
| 238 |
Result := FAllocatedSources[I]; |
| 239 |
Break; |
| 240 |
end; |
| 241 |
end; |
| 242 |
|
| 243 |
{ Try: maybe we can allocate one more sound ? } |
| 244 |
if (Result = nil) and |
| 245 |
(Cardinal(FAllocatedSources.Count) < MaxAllocatedSources) then |
| 246 |
begin |
| 247 |
try |
| 248 |
Result := TInternalSoundSource.Create(Backend); |
| 249 |
FAllocatedSources.Add(Result); |
| 250 |
except |
| 251 |
{ If TInternalSoundSource.Create raises ENoMoreSources --- |
| 252 |
then silence the exception and leave Result = nil. } |
| 253 |
on ENoMoreSources do ; |
| 254 |
end; |
| 255 |
end; |
| 256 |
|
| 257 |
{ Try: maybe we can remove one more sound ? |
| 258 |
|
| 259 |
If Result = nil then we know that MinPriorityIndex <> -1, because |
| 260 |
all sounds must be used and MinAllocatedSources is always > 0, |
| 261 |
so some sound must be used. |
| 262 |
|
| 263 |
Note that if FAllocatedSources[MinPriorityIndex].Priority |
| 264 |
is equal to Priority, we *do* interrupt already playing sound. |
| 265 |
The assumption is here that the newer sound is more imoportant. } |
| 266 |
if (Result = nil) and |
| 267 |
(FAllocatedSources[MinPriorityIndex].Priority <= Priority) then |
| 268 |
begin |
| 269 |
Result := FAllocatedSources[MinPriorityIndex]; |
| 270 |
end; |
| 271 |
|
| 272 |
if Result <> nil then |
| 273 |
begin |
| 274 |
{ Prepare Result } |
| 275 |
if Result.Used then |
| 276 |
Result.Release; |
| 277 |
Result.FPriority := Priority; |
| 278 |
Result.FUsed := true; |
| 279 |
end; |
| 280 |
end; |
| 281 |
|
| 282 |
procedure TSoundAllocator.SetMinAllocatedSources(const Value: Cardinal); |
| 283 |
var |
| 284 |
I: Integer; |
| 285 |
OldAllocatedSourcesCount: Cardinal; |
| 286 |
begin |
| 287 |
if Value <> FMinAllocatedSources then |
| 288 |
begin |
| 289 |
FMinAllocatedSources := Value; |
| 290 |
if (FAllocatedSources <> nil) and |
| 291 |
(Cardinal(FAllocatedSources.Count) < MinAllocatedSources) then |
| 292 |
begin |
| 293 |
OldAllocatedSourcesCount := FAllocatedSources.Count; |
| 294 |
FAllocatedSources.Count := MinAllocatedSources; |
| 295 |
for I := OldAllocatedSourcesCount to FAllocatedSources.Count - 1 do |
| 296 |
FAllocatedSources[I] := TInternalSoundSource.Create(Backend); |
| 297 |
end; |
| 298 |
end; |
| 299 |
end; |
| 300 |
|
| 301 |
procedure TSoundAllocator.SetMaxAllocatedSources(const Value: Cardinal); |
| 302 |
var |
| 303 |
I: Integer; |
| 304 |
begin |
| 305 |
if Value <> FMaxAllocatedSources then |
| 306 |
begin |
| 307 |
FMaxAllocatedSources := Value; |
| 308 |
if (FAllocatedSources <> nil) and |
| 309 |
(Cardinal(FAllocatedSources.Count) > MaxAllocatedSources) then |
| 310 |
begin |
| 311 |
FAllocatedSources.SortByPriority; |
| 312 |
|
| 313 |
for I := MaxAllocatedSources to FAllocatedSources.Count - 1 do |
| 314 |
begin |
| 315 |
if FAllocatedSources[I].Used then |
| 316 |
FAllocatedSources[I].Release; |
| 317 |
{ This will free FAllocatedSources[I], as FAllocatedSources owns children } |
| 318 |
FAllocatedSources[I] := nil; |
| 319 |
end; |
| 320 |
FAllocatedSources.Count := MaxAllocatedSources; |
| 321 |
end; |
| 322 |
end; |
| 323 |
end; |
| 324 |
|
| 325 |
procedure TSoundAllocator.Update(Sender: TObject); |
| 326 |
|
| 327 |
{ Call Update on all sources, and detect unused sound sources. } |
| 328 |
procedure UpdateSounds(const SecondsPassed: TFloatTime); |
| 329 |
var |
| 330 |
I: Integer; |
| 331 |
begin |
| 332 |
if FAllocatedSources <> nil then |
| 333 |
for I := 0 to FAllocatedSources.Count - 1 do |
| 334 |
FAllocatedSources[I].Update(SecondsPassed); |
| 335 |
end; |
| 336 |
|
| 337 |
const |
| 338 |
{ Delay between calling DetectUnusedSounds, in seconds. } |
| 339 |
SoundRefreshDelay = 0.1; |
| 340 |
var |
| 341 |
TimeNow: TTimerResult; |
| 342 |
SecondsPassed: TFloatTime; |
| 343 |
begin |
| 344 |
if FIsContextOpenSuccess then |
| 345 |
Backend.Update; |
| 346 |
|
| 347 |
{ Calling UpdateSounds relatively often is important, |
| 348 |
to call OnRelease for sound sources that finished playing. } |
| 349 |
if FAllocatedSources <> nil then |
| 350 |
begin |
| 351 |
TimeNow := Timer; |
| 352 |
SecondsPassed := TimerSeconds(TimeNow, LastSoundRefresh); |
| 353 |
if SecondsPassed > SoundRefreshDelay then |
| 354 |
begin |
| 355 |
LastSoundRefresh := TimeNow; |
| 356 |
UpdateSounds(SecondsPassed); |
| 357 |
end; |
| 358 |
end; |
| 359 |
end; |
| 360 |
|
| 361 |
procedure TSoundAllocator.StopAllSources; |
| 362 |
var |
| 363 |
I: Integer; |
| 364 |
begin |
| 365 |
if FAllocatedSources <> nil then |
| 366 |
for I := 0 to FAllocatedSources.Count - 1 do |
| 367 |
if FAllocatedSources[I].Used then |
| 368 |
FAllocatedSources[I].Release; |
| 369 |
end; |
| 370 |
|
| 371 |
{$endif read_implementation} |