1 | unit AudioSystem;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TMediaPlayerDriverClass = class of TMediaPlayerDriver;
|
---|
10 | TOutputDriver = (omAlsa, omOSS, omDirectX, omWin32);
|
---|
11 |
|
---|
12 | EOpenOutputFailed = class(Exception);
|
---|
13 |
|
---|
14 | { TAudioSystem }
|
---|
15 |
|
---|
16 | TAudioSystem = class(TComponent)
|
---|
17 | protected
|
---|
18 | FOutputDriver: TOutputDriver;
|
---|
19 | procedure SetOutputMode(AValue: TOutputDriver); virtual;
|
---|
20 | public
|
---|
21 | constructor Create(AOwner: TComponent); override;
|
---|
22 | destructor Destroy; override;
|
---|
23 | function GetMediaPlayerDriverClass: TMediaPlayerDriverClass; virtual;
|
---|
24 | property OutputMode: TOutputDriver read FOutputDriver write SetOutputMode;
|
---|
25 | end;
|
---|
26 |
|
---|
27 | TAudioSystemClass = class of TAudioSystem;
|
---|
28 |
|
---|
29 | { TMediaPlayerDriver }
|
---|
30 |
|
---|
31 | TMediaPlayerDriver = class
|
---|
32 | private
|
---|
33 | protected
|
---|
34 | FActive: Boolean;
|
---|
35 | FFileName: string;
|
---|
36 | FAudioSystem: TAudioSystem;
|
---|
37 | FPlaying: Boolean;
|
---|
38 | procedure SetActive(AValue: Boolean); virtual;
|
---|
39 | procedure SetPlaying(AValue: Boolean); virtual;
|
---|
40 | function GetMuted: Boolean; virtual;
|
---|
41 | procedure SetMuted(AValue: Boolean); virtual;
|
---|
42 | function GetLength: TDateTime; virtual;
|
---|
43 | function GetPosition: TDateTime; virtual;
|
---|
44 | function GetVolume: Real; virtual;
|
---|
45 | procedure SetPosition(AValue: TDateTime); virtual;
|
---|
46 | procedure SetVolume(AValue: Real); virtual;
|
---|
47 | procedure SetFileName(AValue: string); virtual;
|
---|
48 | public
|
---|
49 | procedure Play; virtual;
|
---|
50 | procedure Pause; virtual;
|
---|
51 | procedure Stop; virtual;
|
---|
52 | procedure Open; virtual;
|
---|
53 | procedure Close; virtual;
|
---|
54 | property Position: TDateTime read GetPosition write SetPosition;
|
---|
55 | property Length: TDateTime read GetLength;
|
---|
56 | property Volume: Real read GetVolume write SetVolume; // 0..1
|
---|
57 | property Muted: Boolean read GetMuted write SetMuted;
|
---|
58 | property AudioSystem: TAudioSystem read FAudioSystem write FAudioSystem;
|
---|
59 | property FileName: string read FFileName write SetFileName;
|
---|
60 | property Playing: Boolean read FPlaying write SetPlaying;
|
---|
61 | property Active: Boolean read FActive write SetActive;
|
---|
62 | constructor Create; virtual;
|
---|
63 | destructor Destroy; override;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | TMediaPlayer = class(TComponent)
|
---|
67 | private
|
---|
68 | procedure CheckDriver;
|
---|
69 | function GetActive: Boolean;
|
---|
70 | function GetAudioSystem: TAudioSystem;
|
---|
71 | function GetFileName: string;
|
---|
72 | function GetLength: TDateTime;
|
---|
73 | function GetMuted: Boolean;
|
---|
74 | function GetPlaying: Boolean;
|
---|
75 | function GetPosition: TDateTime;
|
---|
76 | function GetVolume: Real;
|
---|
77 | procedure SetActive(AValue: Boolean);
|
---|
78 | procedure SetAudioSystem(AValue: TAudioSystem);
|
---|
79 | procedure SetFileName(AValue: string);
|
---|
80 | procedure SetMuted(AValue: Boolean);
|
---|
81 | procedure SetPlaying(AValue: Boolean);
|
---|
82 | procedure SetPosition(AValue: TDateTime);
|
---|
83 | procedure SetVolume(AValue: Real);
|
---|
84 | public
|
---|
85 | Driver: TMediaPlayerDriver;
|
---|
86 | constructor Create(AOwner: TComponent); override;
|
---|
87 | destructor Destroy; override;
|
---|
88 | procedure Play;
|
---|
89 | procedure Pause;
|
---|
90 | procedure Stop;
|
---|
91 | procedure Open;
|
---|
92 | procedure Close;
|
---|
93 | property Position: TDateTime read GetPosition write SetPosition;
|
---|
94 | property Length: TDateTime read GetLength;
|
---|
95 | property Playing: Boolean read GetPlaying write SetPlaying;
|
---|
96 | published
|
---|
97 | property Volume: Real read GetVolume write SetVolume; // 0..1
|
---|
98 | property Muted: Boolean read GetMuted write SetMuted;
|
---|
99 | property AudioSystem: TAudioSystem read GetAudioSystem write SetAudioSystem;
|
---|
100 | property FileName: string read GetFileName write SetFileName;
|
---|
101 | property Active: Boolean read GetActive write SetActive;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | TPlayerClass = class of TMediaPlayerDriver;
|
---|
105 |
|
---|
106 | { TAudioSystemManagerItem }
|
---|
107 |
|
---|
108 | TAudioSystemManagerItem = class
|
---|
109 | Name: string;
|
---|
110 | SystemClass: TAudioSystemClass;
|
---|
111 | Supported: Boolean;
|
---|
112 | end;
|
---|
113 |
|
---|
114 | { TAudioSystemManager }
|
---|
115 |
|
---|
116 | TAudioSystemManager = class(TComponent)
|
---|
117 | public
|
---|
118 | Systems: TObjectList<TAudioSystemManagerItem>;
|
---|
119 | procedure Register(Name: string; SystemClass: TAudioSystemClass);
|
---|
120 | procedure FillStringList(StringList: TStrings);
|
---|
121 | function SearchByName(Name: string; SupportedOnly: Boolean = True): TAudioSystemManagerItem;
|
---|
122 | constructor Create(AOwner: TComponent); override;
|
---|
123 | destructor Destroy; override;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | const
|
---|
127 | WavFileExt = '.wav';
|
---|
128 | Mp3FileExt = '.mp3';
|
---|
129 |
|
---|
130 | var
|
---|
131 | DefaultAudioSystem: TAudioSystem;
|
---|
132 |
|
---|
133 | resourcestring
|
---|
134 | SOpenOutputFailed = 'Failed opening audio output';
|
---|
135 | SDefaultAudioSystemNotSet = 'Default audio system not set';
|
---|
136 | SMediaPlayerDriverNotAssigned = 'Media player driver not assigned';
|
---|
137 |
|
---|
138 |
|
---|
139 | implementation
|
---|
140 |
|
---|
141 | { TMediaPlayer }
|
---|
142 |
|
---|
143 | function TMediaPlayer.GetLength: TDateTime;
|
---|
144 | begin
|
---|
145 | CheckDriver;
|
---|
146 | Result := Driver.Length;
|
---|
147 | end;
|
---|
148 |
|
---|
149 | procedure TMediaPlayer.CheckDriver;
|
---|
150 | begin
|
---|
151 | if not Assigned(Driver) then
|
---|
152 | raise Exception.Create(SMediaPlayerDriverNotAssigned);
|
---|
153 | end;
|
---|
154 |
|
---|
155 | function TMediaPlayer.GetActive: Boolean;
|
---|
156 | begin
|
---|
157 | CheckDriver;
|
---|
158 | Result := Driver.Active;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | function TMediaPlayer.GetAudioSystem: TAudioSystem;
|
---|
162 | begin
|
---|
163 | CheckDriver;
|
---|
164 | Result := Driver.AudioSystem;
|
---|
165 | end;
|
---|
166 |
|
---|
167 | function TMediaPlayer.GetFileName: string;
|
---|
168 | begin
|
---|
169 | CheckDriver;
|
---|
170 | Result := Driver.FileName;
|
---|
171 | end;
|
---|
172 |
|
---|
173 | function TMediaPlayer.GetMuted: Boolean;
|
---|
174 | begin
|
---|
175 | CheckDriver;
|
---|
176 | Result := Driver.Muted;
|
---|
177 | end;
|
---|
178 |
|
---|
179 | function TMediaPlayer.GetPlaying: Boolean;
|
---|
180 | begin
|
---|
181 | CheckDriver;
|
---|
182 | Result := Driver.Playing;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | function TMediaPlayer.GetPosition: TDateTime;
|
---|
186 | begin
|
---|
187 | CheckDriver;
|
---|
188 | Result := Driver.Position;
|
---|
189 | end;
|
---|
190 |
|
---|
191 | function TMediaPlayer.GetVolume: Real;
|
---|
192 | begin
|
---|
193 | CheckDriver;
|
---|
194 | Result := Driver.Volume;
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TMediaPlayer.SetActive(AValue: Boolean);
|
---|
198 | begin
|
---|
199 | CheckDriver;
|
---|
200 | Driver.Active := True;
|
---|
201 | end;
|
---|
202 |
|
---|
203 | procedure TMediaPlayer.SetAudioSystem(AValue: TAudioSystem);
|
---|
204 | var
|
---|
205 | DriverClass: TMediaPlayerDriverClass;
|
---|
206 | begin
|
---|
207 | FreeAndNil(Driver);
|
---|
208 | DriverClass := AValue.GetMediaPlayerDriverClass;
|
---|
209 | Driver := DriverClass.Create;
|
---|
210 | Driver.AudioSystem := DefaultAudioSystem;
|
---|
211 | end;
|
---|
212 |
|
---|
213 | procedure TMediaPlayer.SetFileName(AValue: string);
|
---|
214 | begin
|
---|
215 | CheckDriver;
|
---|
216 | Driver.FileName := AValue;
|
---|
217 | end;
|
---|
218 |
|
---|
219 | procedure TMediaPlayer.SetMuted(AValue: Boolean);
|
---|
220 | begin
|
---|
221 | CheckDriver;
|
---|
222 | Driver.Muted := AValue;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | procedure TMediaPlayer.SetPlaying(AValue: Boolean);
|
---|
226 | begin
|
---|
227 | CheckDriver;
|
---|
228 | Driver.Playing := AValue;
|
---|
229 | end;
|
---|
230 |
|
---|
231 | procedure TMediaPlayer.SetPosition(AValue: TDateTime);
|
---|
232 | begin
|
---|
233 | CheckDriver;
|
---|
234 | Driver.Position := AValue;
|
---|
235 | end;
|
---|
236 |
|
---|
237 | procedure TMediaPlayer.SetVolume(AValue: Real);
|
---|
238 | begin
|
---|
239 | CheckDriver;
|
---|
240 | Driver.Volume := AValue;
|
---|
241 | end;
|
---|
242 |
|
---|
243 | procedure TMediaPlayer.Play;
|
---|
244 | begin
|
---|
245 | CheckDriver;
|
---|
246 | Driver.Play;
|
---|
247 | end;
|
---|
248 |
|
---|
249 | procedure TMediaPlayer.Pause;
|
---|
250 | begin
|
---|
251 | CheckDriver;
|
---|
252 | Driver.Pause;
|
---|
253 | end;
|
---|
254 |
|
---|
255 | procedure TMediaPlayer.Stop;
|
---|
256 | begin
|
---|
257 | CheckDriver;
|
---|
258 | Driver.Stop;
|
---|
259 | end;
|
---|
260 |
|
---|
261 | procedure TMediaPlayer.Open;
|
---|
262 | begin
|
---|
263 | CheckDriver;
|
---|
264 | Driver.Open;
|
---|
265 | end;
|
---|
266 |
|
---|
267 | procedure TMediaPlayer.Close;
|
---|
268 | begin
|
---|
269 | CheckDriver;
|
---|
270 | Driver.Close;
|
---|
271 | end;
|
---|
272 |
|
---|
273 | constructor TMediaPlayer.Create(AOwner: TComponent);
|
---|
274 | begin
|
---|
275 | inherited Create(AOwner);
|
---|
276 | if not Assigned(DefaultAudioSystem) then
|
---|
277 | raise Exception.Create(SDefaultAudioSystemNotSet);
|
---|
278 | AudioSystem := DefaultAudioSystem;
|
---|
279 | end;
|
---|
280 |
|
---|
281 | destructor TMediaPlayer.Destroy;
|
---|
282 | begin
|
---|
283 | FreeAndNil(Driver);
|
---|
284 | inherited Destroy;
|
---|
285 | end;
|
---|
286 |
|
---|
287 | { TAudioSystemManagerItem }
|
---|
288 |
|
---|
289 |
|
---|
290 | { TAudioSystemManager }
|
---|
291 |
|
---|
292 | procedure TAudioSystemManager.FillStringList(StringList: TStrings);
|
---|
293 | var
|
---|
294 | I: Integer;
|
---|
295 | begin
|
---|
296 | StringList.Clear;
|
---|
297 | for I := 0 to Systems.Count - 1 do
|
---|
298 | with Systems[I] do
|
---|
299 | StringList.AddObject(Name, Systems[I]);
|
---|
300 | end;
|
---|
301 |
|
---|
302 | function TAudioSystemManager.SearchByName(Name: string; SupportedOnly: Boolean = True):
|
---|
303 | TAudioSystemManagerItem;
|
---|
304 | var
|
---|
305 | I: Integer;
|
---|
306 | begin
|
---|
307 | I := 0;
|
---|
308 | while (I < Systems.Count) and
|
---|
309 | ((Systems[I].Name <> Name) or
|
---|
310 | (not Systems[I].Supported and SupportedOnly)) do Inc(I);
|
---|
311 | if I < Systems.Count then Result := Systems[I]
|
---|
312 | else Result := nil;
|
---|
313 | end;
|
---|
314 |
|
---|
315 | procedure TAudioSystemManager.Register(Name: string;
|
---|
316 | SystemClass: TAudioSystemClass);
|
---|
317 | var
|
---|
318 | NewItem: TAudioSystemManagerItem;
|
---|
319 | begin
|
---|
320 | NewItem := TAudioSystemManagerItem.Create;
|
---|
321 | NewItem.Name := Name;
|
---|
322 | NewItem.SystemClass := SystemClass;
|
---|
323 | NewItem.Supported := True;
|
---|
324 | Systems.Add(NewItem);
|
---|
325 | end;
|
---|
326 |
|
---|
327 | constructor TAudioSystemManager.Create(AOwner: TComponent);
|
---|
328 | begin
|
---|
329 | inherited;
|
---|
330 | Systems := TObjectList<TAudioSystemManagerItem>.Create;
|
---|
331 | end;
|
---|
332 |
|
---|
333 | destructor TAudioSystemManager.Destroy;
|
---|
334 | begin
|
---|
335 | FreeAndNil(Systems);
|
---|
336 | inherited;
|
---|
337 | end;
|
---|
338 |
|
---|
339 | { TMediaPlayerDriver }
|
---|
340 |
|
---|
341 | procedure TMediaPlayerDriver.SetActive(AValue: Boolean);
|
---|
342 | begin
|
---|
343 | if FActive = AValue then Exit;
|
---|
344 | FActive := AValue;
|
---|
345 | end;
|
---|
346 |
|
---|
347 | procedure TMediaPlayerDriver.SetPlaying(AValue: Boolean);
|
---|
348 | begin
|
---|
349 | if FPlaying = AValue then Exit;
|
---|
350 | if AValue then Play else Stop;
|
---|
351 | end;
|
---|
352 |
|
---|
353 | function TMediaPlayerDriver.GetMuted: Boolean;
|
---|
354 | begin
|
---|
355 | Result := False;
|
---|
356 | end;
|
---|
357 |
|
---|
358 | procedure TMediaPlayerDriver.SetMuted(AValue: Boolean);
|
---|
359 | begin
|
---|
360 | end;
|
---|
361 |
|
---|
362 | function TMediaPlayerDriver.GetLength: TDateTime;
|
---|
363 | begin
|
---|
364 | Result := 0;
|
---|
365 | end;
|
---|
366 |
|
---|
367 | function TMediaPlayerDriver.GetPosition: TDateTime;
|
---|
368 | begin
|
---|
369 | Result := 0;
|
---|
370 | end;
|
---|
371 |
|
---|
372 | function TMediaPlayerDriver.GetVolume: Real;
|
---|
373 | begin
|
---|
374 | Result := 0;
|
---|
375 | end;
|
---|
376 |
|
---|
377 | procedure TMediaPlayerDriver.SetPosition(AValue: TDateTime);
|
---|
378 | begin
|
---|
379 | end;
|
---|
380 |
|
---|
381 | procedure TMediaPlayerDriver.SetVolume(AValue: Real);
|
---|
382 | begin
|
---|
383 | end;
|
---|
384 |
|
---|
385 | procedure TMediaPlayerDriver.SetFileName(AValue: string);
|
---|
386 | begin
|
---|
387 | if AValue = FFileName then Exit;
|
---|
388 | FFileName := AValue;
|
---|
389 | Close;
|
---|
390 | Open;
|
---|
391 | end;
|
---|
392 |
|
---|
393 | procedure TMediaPlayerDriver.Play;
|
---|
394 | begin
|
---|
395 | end;
|
---|
396 |
|
---|
397 | procedure TMediaPlayerDriver.Pause;
|
---|
398 | begin
|
---|
399 | end;
|
---|
400 |
|
---|
401 | procedure TMediaPlayerDriver.Stop;
|
---|
402 | begin
|
---|
403 | end;
|
---|
404 |
|
---|
405 | procedure TMediaPlayerDriver.Open;
|
---|
406 | begin
|
---|
407 | Active := True;
|
---|
408 | end;
|
---|
409 |
|
---|
410 | procedure TMediaPlayerDriver.Close;
|
---|
411 | begin
|
---|
412 | Active := False;
|
---|
413 | end;
|
---|
414 |
|
---|
415 | constructor TMediaPlayerDriver.Create;
|
---|
416 | begin
|
---|
417 | inherited;
|
---|
418 | end;
|
---|
419 |
|
---|
420 | destructor TMediaPlayerDriver.Destroy;
|
---|
421 | begin
|
---|
422 | Stop;
|
---|
423 | Active := False;
|
---|
424 | inherited;
|
---|
425 | end;
|
---|
426 |
|
---|
427 | { TAudioSystem }
|
---|
428 |
|
---|
429 | procedure TAudioSystem.SetOutputMode(AValue: TOutputDriver);
|
---|
430 | begin
|
---|
431 | if FOutputDriver = AValue then Exit;
|
---|
432 | FOutputDriver := AValue;
|
---|
433 | end;
|
---|
434 |
|
---|
435 | constructor TAudioSystem.Create(AOwner: TComponent);
|
---|
436 | begin
|
---|
437 | inherited;
|
---|
438 | {$IFDEF Windows}
|
---|
439 | FOutputDriver := omWin32;
|
---|
440 | {$ENDIF}
|
---|
441 | {$IFDEF Linux}
|
---|
442 | FOutputDriver := omAlsa;
|
---|
443 | {$ENDIF}
|
---|
444 | end;
|
---|
445 |
|
---|
446 | destructor TAudioSystem.Destroy;
|
---|
447 | begin
|
---|
448 | inherited;
|
---|
449 | end;
|
---|
450 |
|
---|
451 | function TAudioSystem.GetMediaPlayerDriverClass: TMediaPlayerDriverClass;
|
---|
452 | begin
|
---|
453 | Result := TMediaPlayerDriver;
|
---|
454 | end;
|
---|
455 |
|
---|
456 | initialization
|
---|
457 |
|
---|
458 | DefaultAudioSystem := TAudioSystem.Create(nil);
|
---|
459 |
|
---|
460 | finalization
|
---|
461 |
|
---|
462 | FreeAndNil(DefaultAudioSystem);
|
---|
463 |
|
---|
464 | end.
|
---|
465 |
|
---|