Debian Codegraph @

src/audio/castlesoundengine_internalsoundsource.inc

raw
1 {%MainUnit castlesoundengine.pas}
2 {
3 Copyright 2010-2022 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 TInternalSoundSourceEvent = procedure (Sender: TInternalSoundSource) of object;
22
23 { Sound source that is allocated (on the sound backend) and can be immediately played.
24 One instance of TInternalSoundSource is is not permanently connected
25 to a particular use (like a particular TCastleSound), it is only temporarily allocated
26 for given use (e.g. play a particular TCastleSound), and then it becomes "unused"
27 (see @link(Used), @link(OnRelease)) and can be used for other purposes.
28
29 This is internal now. Use TCastleSound and TCastleSoundSource
30 to manage playback of a sound.
31
32 @exclude
33 }
34 TInternalSoundSource = class
35 private
36 FUsed: boolean;
37 FOnRelease: TInternalSoundSourceEvent;
38 FPriority: Single;
39 FUserData: TObject;
40 FPosition, FVelocity: TVector3;
41 FLoop, FSpatial: boolean;
42 FVolume, FMinGain, FMaxGain, FPitch: Single;
43 FBuffer: TInternalSoundBuffer;
44 FReferenceDistance, FMaxDistance: Single;
45 Backend: TSoundSourceBackend;
46 BackendIsOpen: Boolean;
47 procedure SetPosition(const Value: TVector3);
48 procedure SetVelocity(const Value: TVector3);
49 procedure SetLoop(const Value: boolean);
50 procedure SetSpatial(const Value: boolean);
51 procedure SetVolume(const Value: Single);
52 procedure SetMinGain(const Value: Single);
53 procedure SetMaxGain(const Value: Single);
54 procedure SetBuffer(const Value: TInternalSoundBuffer);
55 procedure SetPitch(const Value: Single);
56 procedure SetReferenceDistance(const Value: Single);
57 procedure SetMaxDistance(const Value: Single);
58 procedure SetPriority(const Value: Single);
59 function GetOffset: Single;
60 procedure SetOffset(const Value: Single);
61
62 { Call backend Update on sound source,
63 and checks whether the source is still playing (or is paused).
64 If not, it calls @link(TInternalSoundSource.Release) (thus setting
65 TInternalSoundSource.Used to @false and triggering TSound.OnRelease) for this source. }
66 procedure Update(const SecondsPassed: TFloatTime);
67 public
68 { Create sound.
69 This allocates sound source using the sound backend (like OpenAL source).
70 @raises(ENoMoreSources If no more sources available.
71 It should be caught and silenced by TSoundAllocator.AllocateSound.) }
72 constructor Create(const SoundEngineBackend: TSoundEngineBackend);
73 destructor Destroy; override;
74
75 { Do we play something.
76 Sources that are not Used are still allocated on the sound backend (like
77 OpenAL), and will be used when we will need them. }
78 property Used: boolean read FUsed default false;
79
80 { The priority of keeping this source, relevant only when @link(Used).
81 See TCastleSound.Priority for the exact meaning and range. }
82 property Priority: Single read FPriority write SetPriority {$ifdef FPC}default 0.0{$endif};
83
84 { Any data comfortable to keep here by the caller of
85 TSoundAllocator.AllocateSound. It should be initialized
86 after calling TSoundAllocator.AllocateSound, and should
87 be finalized in OnRelease. }
88 property UserData: TObject read FUserData write FUserData;
89
90 { Called when this sound will no longer be used.
91 This may happen because it simply finished playing,
92 or when there are more demanding
93 sounds (see @link(Priority) and to keep MaxAllocatedSources)
94 and we must use this sound source for something else.
95
96 When this event occurs, you should forget (e.g. set to @nil) all
97 your references to this sound instance. That's because this TInternalSoundSource instance
98 may be freed (or reused for other sounds) after calling OnRelease.
99 For the same reason, right after calling this event, we always clear it
100 (set OnRelease to @nil).
101
102 It's guaranteed that when this will be called,
103 @link(Used) will be @false and @link(PlayingOrPaused) will be @false.
104
105 Note that we do not guarantee that sources that
106 stopped playing will be immediately reported to OnRelease.
107 A source may have Used = @true state
108 for a short time when it stopped playing (when PlayingOrPaused
109 is already @false). }
110 property OnRelease: TInternalSoundSourceEvent read FOnRelease write FOnRelease;
111
112 { Stops playing the source,
113 sets Used to @false, and calls OnRelease (if assigned).
114
115 You can call this if you want to stop playing the sound.
116 This will also immediately set Used property
117 to @false and will call OnRelease.
118
119 You can call this only when Used = @true. }
120 procedure Release; virtual;
121
122 property Position: TVector3 read FPosition write SetPosition;
123 property Velocity: TVector3 read FVelocity write SetVelocity;
124 property Loop: boolean read FLoop write SetLoop;
125 property Spatial: boolean read FSpatial write SetSpatial;
126 property Volume: Single read FVolume write SetVolume;
127 property MinGain: Single read FMinGain write SetMinGain;
128 property MaxGain: Single read FMaxGain write SetMaxGain;
129 property Buffer: TInternalSoundBuffer read FBuffer write SetBuffer;
130 property Pitch: Single read FPitch write SetPitch;
131 property ReferenceDistance: Single read FReferenceDistance write SetReferenceDistance;
132 property MaxDistance: Single read FMaxDistance write SetMaxDistance;
133
134 { Playback time of this sound, expressed in seconds.
135
136 This value will loop back to zero for looping sound sources.
137 Setting this to something larger than the @italic(sound buffer duration)
138 is ignored.
139
140 This offset refers to the sound like it had a @link(Pitch) equal 1.0
141 (when the sound is not slowed down or sped up).
142 So this offset will vary from 0 to the @italic(sound buffer duration),
143 regardless of the current @link(Pitch) value.
144 The @italic(actual) seconds passed since the sound started
145 playing may be different, if you will change the @link(Pitch)
146 to something else than 1.0.
147
148 Setting this on a not-yet playing sound source
149 (this is done by @link(TSoundEngine.PlaySound))
150 causes the sound to start playing from that offset. }
151 property Offset: Single read GetOffset write SetOffset;
152
153 { Is the sound playing or paused. This is almost always @true for sounds
154 returned by TSoundAllocator.AllocateSound, when it stops being @true
155 --- the sound engine will realize it (soon), which will cause @link(Release)
156 and OnRelease being automatically called, and this TInternalSoundSource may then
157 be reused for playing other sounds. }
158 function PlayingOrPaused: boolean;
159
160 { Make sure that the sound keeps playing, in case it stopped playing.
161
162 This is an alternative approach to play a sound many times,
163 like in a loop, but without using the @link(Loop) property.
164 The idea is that you leave @link(Loop) set to @false,
165 and you keep calling this method from some "update" event
166 (like some @link(TCastleUserInterface.Update) implementation).
167 Once you stop calling this method, the sound will automatically stop
168 (once it finishes the current cycle).
169
170 Note that you still (as always when using TInternalSoundSource) must observe
171 the @link(TInternalSoundSource.OnRelease). When it's called, it means that the sound
172 engine (TSoundEngine) decided that this sound should be used for other purposes
173 (there's also a very small chance that the sound engine "caught"
174 the sound as unused, in a short time when it stopped playing but you didn't
175 yet call this method).
176 In such case, you must stop doing anything with this TInternalSoundSource instance
177 (including calling this method, @name, on it).
178 You have to start playing the sound again by @link(TSoundEngine.PlaySound)
179 instead.
180
181 Note that calling this method is better than observing @link(TInternalSoundSource.OnRelease),
182 to start playing a new sound when the previous one stopped.
183 That's because @link(TInternalSoundSource.OnRelease) may be called with some small delay
184 after the sound actually stopped, and it may be noticeable (e.g. in case
185 of using this for a short rhytmic sound, like footsteps). }
186 procedure KeepPlaying; deprecated 'use Loop to play looping sound. This method is a hack -- as you shall not operate on stopped TInternalSoundSource, as it may be repurposed.';
187 end;
188
189 TSound = TInternalSoundSource deprecated 'play sounds using TCastleSoundSource + TCastleSound';
190
191 TInternalSoundSourceList = class({$ifdef FPC}specialize{$endif} TObjectList<TInternalSoundSource>)
192 public
193 { Sort sounds by Used + Priority, descending.
194 First all sounds with Used = @true are placed,
195 starting from the sound with largest Priority, and so on
196 until the sound with smallest Priority.
197 Then all sounds with Used = @false are placed (in any, arbitrary order).
198
199 List must not contain nil values when calling this. }
200 procedure SortByPriority;
201 end;
202
203 {$endif read_interface}
204
205 {$ifdef read_implementation}
206
207 { TInternalSoundSource ---------------------------------------------------------- }
208
209 constructor TInternalSoundSource.Create(const SoundEngineBackend: TSoundEngineBackend);
210 begin
211 inherited Create;
212
213 Backend := SoundEngineBackend.CreateSource;
214 { For now, TInternalSoundSource always refers to an open (on backend side) sound source. }
215 Backend.ContextOpen;
216 { This way, TInternalSoundSource.Destroy will not call Backend.ContextClose
217 if Backend.ContextOpen failed.
218 (So e.g. OpenAL Backend.ContextClose doesn't try to call
219 alDeleteSources on invalid sound source.) }
220 BackendIsOpen := true;
221 end;
222
223 destructor TInternalSoundSource.Destroy;
224 begin
225 if (Backend <> nil) and BackendIsOpen then
226 Backend.ContextClose;
227 FreeAndNil(Backend);
228 inherited;
229 end;
230
231 procedure TInternalSoundSource.Release;
232 var
233 SavedOnRelease: TInternalSoundSourceEvent;
234 begin
235 FUsed := false;
236
237 Backend.Stop;
238
239 { Detach the buffer from source. Otherwise we couldn't free the buffer
240 while it's associated with the source. Also, this would be a problem
241 once we implement streaming on some sources: you have to reset
242 buffer to 0 before queing buffers on source. }
243 Buffer := nil;
244
245 if Assigned(OnRelease) then
246 begin
247 { Note that calling OnRelease may do anything, it may even reuse this sound
248 and set the OnRelease again. This is possible when using TCastlePlayingSound:
249 if in OnStop of one TCastlePlayingSound, you will start playing another
250 TCastlePlayingSound, it may reuse the same TInternalSoundSource.
251
252 So *do not* do "OnRelease := nil" after calling OnRelease, do it before.
253 That's why we need to keep it in SavedOnRelease.
254
255 Testcase: https://github.com/castle-engine/castle-engine/issues/344 }
256 SavedOnRelease := OnRelease;
257 OnRelease := nil;
258 SavedOnRelease(Self);
259 end;
260 end;
261
262 procedure TInternalSoundSource.SetPosition(const Value: TVector3);
263 begin
264 FPosition := Value;
265 Backend.SetPosition(Value);
266 end;
267
268 procedure TInternalSoundSource.SetVelocity(const Value: TVector3);
269 begin
270 FVelocity := Value;
271 Backend.SetVelocity(Value);
272 end;
273
274 procedure TInternalSoundSource.SetLoop(const Value: boolean);
275 begin
276 FLoop := Value;
277 Backend.SetLoop(Value);
278 end;
279
280 procedure TInternalSoundSource.SetSpatial(const Value: boolean);
281 begin
282 FSpatial := Value;
283 Backend.SetSpatial(Value);
284 end;
285
286 procedure TInternalSoundSource.SetVolume(const Value: Single);
287 begin
288 FVolume := Value;
289 Backend.SetVolume(Value);
290 end;
291
292 procedure TInternalSoundSource.SetMinGain(const Value: Single);
293 begin
294 FMinGain := Value;
295 Backend.SetMinGain(Value);
296 end;
297
298 procedure TInternalSoundSource.SetMaxGain(const Value: Single);
299 begin
300 FMaxGain := Value;
301 Backend.SetMaxGain(Value);
302 end;
303
304 procedure TInternalSoundSource.SetBuffer(const Value: TInternalSoundBuffer);
305 begin
306 FBuffer := Value;
307 if (Value <> nil) and Value.BackendIsOpen then
308 Backend.SetBuffer(Value.Backend)
309 else
310 Backend.SetBuffer(nil);
311 end;
312
313 procedure TInternalSoundSource.SetPitch(const Value: Single);
314 begin
315 FPitch := Value;
316 Backend.SetPitch(Value);
317 end;
318
319 procedure TInternalSoundSource.SetReferenceDistance(const Value: Single);
320 begin
321 FReferenceDistance := Value;
322 Backend.SetReferenceDistance(Value);
323 end;
324
325 procedure TInternalSoundSource.SetMaxDistance(const Value: Single);
326 begin
327 FMaxDistance := Value;
328 Backend.SetMaxDistance(Value);
329 end;
330
331 procedure TInternalSoundSource.SetPriority(const Value: Single);
332 begin
333 FPriority := Value;
334 Backend.SetPriority(Value);
335 end;
336
337 function TInternalSoundSource.GetOffset: Single;
338 begin
339 Result := Backend.Offset;
340 end;
341
342 procedure TInternalSoundSource.SetOffset(const Value: Single);
343 begin
344 Backend.Offset := Value;
345 end;
346
347 function TInternalSoundSource.PlayingOrPaused: boolean;
348 begin
349 Result := Backend.PlayingOrPaused;
350 end;
351
352 procedure TInternalSoundSource.KeepPlaying;
353 begin
354 if not PlayingOrPaused then
355 Backend.Play(false);
356 end;
357
358 procedure TInternalSoundSource.Update(const SecondsPassed: TFloatTime);
359 begin
360 if Used then
361 begin
362 if not BackendIsOpen then
363 Release
364 else
365 begin
366 Backend.Update(SecondsPassed);
367 if not PlayingOrPaused then
368 Release;
369 end;
370 end;
371 end;
372
373 { TInternalSoundSourceList ----------------------------------------------------- }
374
375 function IsSmallerByPriority({$ifdef GENERICS_CONSTREF}constref{$else}const{$endif} AA, BB: TInternalSoundSource): Integer;
376 begin
377 if (AA.Used and (not BB.Used)) or
378 (AA.Used and BB.Used and (AA.Priority > BB.Priority)) then
379 Result := -1 else
380 if (BB.Used and (not AA.Used)) or
381 (BB.Used and AA.Used and (BB.Priority > AA.Priority)) then
382 Result := 1 else
383 Result := 0;
384 end;
385
386 procedure TInternalSoundSourceList.SortByPriority;
387 type
388 TInternalSoundSourceComparer = {$ifdef FPC}specialize{$endif} TComparer<TInternalSoundSource>;
389 begin
390 Sort(TInternalSoundSourceComparer.Construct(
391 {$ifdef FPC}@{$endif} IsSmallerByPriority));
392 end;
393
394 {$endif read_implementation}