Debian Codegraph @

src/audio/castleinternalabstractsoundbackend.pas

raw
1 {
2 Copyright 2010-2022 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Abstract sound engine backend types. }
17 unit CastleInternalAbstractSoundBackend;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses CastleTimeUtils, CastleVectors, CastleSoundBase, CastleInternalSoundFile;
24
25 type
26 TSoundEngineBackend = class;
27
28 { Abstract sound engine sound buffer: sound file data, not playing and not placed in 3D space. }
29 TSoundBufferBackend = class
30 strict private
31 { Set by ContextOpen. }
32 FUrl: String;
33 FSoundEngine: TSoundEngineBackend;
34 protected
35 property SoundEngine: TSoundEngineBackend read FSoundEngine;
36 public
37 { Absolute URL.
38 Never empty (do not create TSoundBuffer instances for invalid / empty URL,
39 like the ones that can be created by TRepoSoundEngine for not defined sounds.) }
40 property Url: String read FUrl;
41
42 { Sound buffer information. }
43 function Duration: TFloatTime; virtual; abstract;
44 function DataFormat: TSoundDataFormat; virtual; abstract;
45 function Frequency: Cardinal; virtual; abstract;
46
47 constructor Create(const ASoundEngine: TSoundEngineBackend);
48
49 { Load from @link(Url), set a couple of properties.
50 When overriding, call inherited first.
51 @raises Exception In case sound loading failed for any reason. }
52 procedure ContextOpen(const AUrl: String); virtual;
53
54 { Guaranteed to be called always after ContextOpen that didn't raise exception,
55 and before destructor. }
56 procedure ContextClose; virtual;
57 end;
58
59 { TSoundBufferBackend descendant that loads sound files using TSoundFile.
60 Should be used by sound backends that cannot load sound files themselves,
61 and rely on TSoundFile to do it (right now, this applies to all backends
62 except FMOD). }
63 TSoundBufferBackendFromSoundFile = class(TSoundBufferBackend)
64 strict private
65 FDuration: TFloatTime;
66 FDataFormat: TSoundDataFormat;
67 FFrequency: Cardinal;
68 protected
69 { Optionally change (pre-process in some way) SoundFile contents
70 before loading it. Called by @link(ContextOpen),
71 always before @link(ContextOpenFromSoundFile).
72 E.g. if backend only supports 16-bit sound formats,
73 here you can call SoundFile.ConvertTo16bit. }
74 procedure ContextOpenPreProcess(const SoundFile: TSoundFile); virtual;
75
76 { Load from @link(SoundFile).
77 When overriding, call inherited first.
78 @raises Exception In case sound loading failed for any reason. }
79 procedure ContextOpenFromSoundFile(const SoundFile: TSoundFile); virtual;
80 public
81 procedure ContextOpen(const AUrl: String); override;
82 function Duration: TFloatTime; override;
83 function DataFormat: TSoundDataFormat; override;
84 function Frequency: Cardinal; override;
85 end;
86
87 { TSoundBufferBackend descendant that loads sound files using TStreamedSoundFile.
88
89 Note that this doesn't actually open file immediately in ContextOpen
90 (it does not create TStreamedSoundFile instance at that point).
91 Instead the file will be opened on-demand when needed (when playing this,
92 or accessing Duration / DataFormat / Frequency).
93
94 This is especially useful on Android, where switching from/to the application
95 requires to close/reopen the OpenAL context.
96 The sounds that are streamed do not need to be immediately loaded
97 in this case (only the playing sounds need to be loaded on-demand).
98 So starting and switching back to the application is much faster.
99
100 In contrast, slComplete loads complete data immediately,
101 making it more predictable (no additional work at runtime)
102 but introducing a loading time.
103 }
104 TSoundBufferBackendFromStreamedFile = class(TSoundBufferBackend)
105 strict private
106 FStreamConfigRead: Boolean;
107 // Fields below are valid only if FStreamConfigRead
108 FDuration: TFloatTime;
109 FDataFormat: TSoundDataFormat;
110 FFrequency: Cardinal;
111 procedure ReadStreamConfigFromTemp;
112 protected
113 procedure ReadStreamConfig(const StreamedSoundFile: TStreamedSoundFile);
114 public
115 function Duration: TFloatTime; override;
116 function DataFormat: TSoundDataFormat; override;
117 function Frequency: Cardinal; override;
118 end;
119
120 { Abstract sound engine sound source: something in 3D that plays sound. }
121 TSoundSourceBackend = class
122 strict private
123 FSoundEngine: TSoundEngineBackend;
124 protected
125 property SoundEngine: TSoundEngineBackend read FSoundEngine;
126 public
127 constructor Create(const ASoundEngine: TSoundEngineBackend);
128
129 procedure ContextOpen; virtual; abstract;
130 { Guaranteed to be called always after ContextOpen that didn't raise exception,
131 and before destructor. }
132 procedure ContextClose; virtual; abstract;
133
134 { All the methods below are guaranteed to be called only after ContextOpen. }
135 { }
136
137 { Check whether the sound is still playing. }
138 function PlayingOrPaused: boolean; virtual; abstract;
139 { Play sound from the beginning.
140 May be called on an already playing sound, should then restart playing it from beginning. }
141 procedure Play(const BufferChangedRecently: Boolean); virtual; abstract;
142 { Stop the sound playing.
143 May be called on an already stopped sound, should do nothing then. }
144 procedure Stop; virtual; abstract;
145
146 { Called continuously on a playing sound source. }
147 procedure Update(const SecondsPassed: TFloatTime); virtual;
148
149 procedure SetPosition(const Value: TVector3); virtual; abstract;
150 procedure SetVelocity(const Value: TVector3); virtual; abstract;
151 procedure SetLoop(const Value: boolean); virtual; abstract;
152 procedure SetSpatial(const Value: boolean); virtual; abstract;
153 procedure SetVolume(const Value: Single); virtual; abstract;
154 procedure SetMinGain(const Value: Single); virtual; abstract;
155 procedure SetMaxGain(const Value: Single); virtual; abstract;
156 { We guarantee that SetBuffer is only called on a stopped sound source
157 (when PlayingOrPaused is false, because it finished playing or because Stop
158 was explicitly called). }
159 procedure SetBuffer(const Value: TSoundBufferBackend); virtual; abstract;
160 procedure SetPitch(const Value: Single); virtual; abstract;
161 procedure SetReferenceDistance(const Value: Single); virtual; abstract;
162 procedure SetMaxDistance(const Value: Single); virtual; abstract;
163 procedure SetPriority(const Value: Single); virtual; abstract;
164 function GetOffset: Single; virtual; abstract;
165 procedure SetOffset(const Value: Single); virtual; abstract;
166 property Offset: Single read GetOffset write SetOffset;
167 end;
168
169 { Abstract sound engine backend. }
170 TSoundEngineBackend = class
171 public
172 { Open backend. Returns whether it was success.
173 Regardless whether it returns @true or @false,
174 also fills Information (with reasons of failure,
175 or with details about initialized sound backend). }
176 function ContextOpen(const ADevice: String; out Information, InformationSummary: String): Boolean; virtual; abstract;
177
178 { Close backend.
179 Guaranteed to be called only after successful (returning @true) ContextOpen. }
180 procedure ContextClose; virtual; abstract;
181
182 { Create suitable non-abstract TSoundBufferBackend descendant. }
183 function CreateBuffer(const SoundLoading: TSoundLoading): TSoundBufferBackend; virtual; abstract;
184
185 { Create suitable non-abstract TSoundSourceBackend descendant. }
186 function CreateSource: TSoundSourceBackend; virtual; abstract;
187
188 { Add available sound devices.
189 There is always at least one default device, name '',
190 it is already added to Devices list when calling this method. }
191 procedure DetectDevices(const Devices: TSoundDeviceList); virtual;
192
193 procedure Update; virtual;
194
195 { All the methods below are guaranteed to be called only after ContextOpen. }
196 procedure SetVolume(const Value: Single); virtual; abstract;
197 procedure SetDistanceModel(const Value: TSoundDistanceModel); virtual; abstract;
198 procedure SetDopplerFactor(const Value: Single); virtual; abstract;
199 procedure SetListener(const Position, Direction, Up: TVector3); virtual; abstract;
200 end;
201
202 implementation
203
204 uses SysUtils;
205
206 { TSoundBufferBackend -------------------------------------------------------- }
207
208 constructor TSoundBufferBackend.Create(const ASoundEngine: TSoundEngineBackend);
209 begin
210 inherited Create;
211 FSoundEngine := ASoundEngine;
212 end;
213
214 procedure TSoundBufferBackend.ContextOpen(const AUrl: String);
215 begin
216 FUrl := AUrl;
217 end;
218
219 procedure TSoundBufferBackend.ContextClose;
220 begin
221 end;
222
223 { TSoundBufferBackendFromSoundFile -------------------------------------------}
224
225 function TSoundBufferBackendFromSoundFile.Duration: TFloatTime;
226 begin
227 Result := FDuration;
228 end;
229
230 function TSoundBufferBackendFromSoundFile.DataFormat: TSoundDataFormat;
231 begin
232 Result := FDataFormat;
233 end;
234
235 function TSoundBufferBackendFromSoundFile.Frequency: Cardinal;
236 begin
237 Result := FFrequency;
238 end;
239
240 procedure TSoundBufferBackendFromSoundFile.ContextOpenFromSoundFile(const SoundFile: TSoundFile);
241 begin
242 end;
243
244 procedure TSoundBufferBackendFromSoundFile.ContextOpenPreProcess(const SoundFile: TSoundFile);
245 begin
246 end;
247
248 procedure TSoundBufferBackendFromSoundFile.ContextOpen(const AUrl: String);
249 var
250 F: TSoundFile;
251 begin
252 inherited;
253
254 F := TSoundFile.Create(Url);
255 try
256 ContextOpenPreProcess(F);
257 FDuration := F.Duration;
258 FDataFormat := F.DataFormat;
259 FFrequency := F.Frequency;
260 ContextOpenFromSoundFile(F);
261 finally FreeAndNil(F) end;
262 end;
263
264 { TSoundBufferBackendFromStreamedFile ---------------------------------------- }
265
266 function TSoundBufferBackendFromStreamedFile.Duration: TFloatTime;
267 begin
268 if not FStreamConfigRead then
269 ReadStreamConfigFromTemp;
270 Result := FDuration;
271 end;
272
273 function TSoundBufferBackendFromStreamedFile.DataFormat: TSoundDataFormat;
274 begin
275 if not FStreamConfigRead then
276 ReadStreamConfigFromTemp;
277 Result := FDataFormat;
278 end;
279
280 function TSoundBufferBackendFromStreamedFile.Frequency: Cardinal;
281 begin
282 if not FStreamConfigRead then
283 ReadStreamConfigFromTemp;
284 Result := FFrequency;
285 end;
286
287 procedure TSoundBufferBackendFromStreamedFile.ReadStreamConfig(const StreamedSoundFile: TStreamedSoundFile);
288 begin
289 if not FStreamConfigRead then
290 begin
291 FDuration := StreamedSoundFile.Duration;
292 FDataFormat := StreamedSoundFile.DataFormat;
293 FFrequency := StreamedSoundFile.Frequency;
294 FStreamConfigRead := true;
295 end;
296 end;
297
298 procedure TSoundBufferBackendFromStreamedFile.ReadStreamConfigFromTemp;
299 var
300 F: TStreamedSoundFile;
301 begin
302 F := TStreamedSoundFile.Create(Url);
303 try
304 ReadStreamConfig(F);
305 finally FreeAndNil(F) end;
306 end;
307
308 { TSoundSourceBackend -------------------------------------------------------- }
309
310 constructor TSoundSourceBackend.Create(const ASoundEngine: TSoundEngineBackend);
311 begin
312 inherited Create;
313 FSoundEngine := ASoundEngine;
314 end;
315
316 procedure TSoundSourceBackend.Update(const SecondsPassed: TFloatTime);
317 begin
318 end;
319
320 { TSoundEngineBackend -------------------------------------------------------- }
321
322 procedure TSoundEngineBackend.DetectDevices(const Devices: TSoundDeviceList);
323 begin
324 // do nothing
325 end;
326
327 procedure TSoundEngineBackend.Update;
328 begin
329 // do nothing
330 end;
331
332 end.