source: trunk/Packages/CoolAudio/Systems/AudioSystem.pas

Last change on this file was 75, checked in by chronos, 8 months ago
  • Modified: Removed U prefix from CoolAudio package unit names.
File size: 9.9 KB
Line 
1unit AudioSystem;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections;
7
8type
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
126const
127 WavFileExt = '.wav';
128 Mp3FileExt = '.mp3';
129
130var
131 DefaultAudioSystem: TAudioSystem;
132
133resourcestring
134 SOpenOutputFailed = 'Failed opening audio output';
135 SDefaultAudioSystemNotSet = 'Default audio system not set';
136 SMediaPlayerDriverNotAssigned = 'Media player driver not assigned';
137
138
139implementation
140
141{ TMediaPlayer }
142
143function TMediaPlayer.GetLength: TDateTime;
144begin
145 CheckDriver;
146 Result := Driver.Length;
147end;
148
149procedure TMediaPlayer.CheckDriver;
150begin
151 if not Assigned(Driver) then
152 raise Exception.Create(SMediaPlayerDriverNotAssigned);
153end;
154
155function TMediaPlayer.GetActive: Boolean;
156begin
157 CheckDriver;
158 Result := Driver.Active;
159end;
160
161function TMediaPlayer.GetAudioSystem: TAudioSystem;
162begin
163 CheckDriver;
164 Result := Driver.AudioSystem;
165end;
166
167function TMediaPlayer.GetFileName: string;
168begin
169 CheckDriver;
170 Result := Driver.FileName;
171end;
172
173function TMediaPlayer.GetMuted: Boolean;
174begin
175 CheckDriver;
176 Result := Driver.Muted;
177end;
178
179function TMediaPlayer.GetPlaying: Boolean;
180begin
181 CheckDriver;
182 Result := Driver.Playing;
183end;
184
185function TMediaPlayer.GetPosition: TDateTime;
186begin
187 CheckDriver;
188 Result := Driver.Position;
189end;
190
191function TMediaPlayer.GetVolume: Real;
192begin
193 CheckDriver;
194 Result := Driver.Volume;
195end;
196
197procedure TMediaPlayer.SetActive(AValue: Boolean);
198begin
199 CheckDriver;
200 Driver.Active := True;
201end;
202
203procedure TMediaPlayer.SetAudioSystem(AValue: TAudioSystem);
204var
205 DriverClass: TMediaPlayerDriverClass;
206begin
207 FreeAndNil(Driver);
208 DriverClass := AValue.GetMediaPlayerDriverClass;
209 Driver := DriverClass.Create;
210 Driver.AudioSystem := DefaultAudioSystem;
211end;
212
213procedure TMediaPlayer.SetFileName(AValue: string);
214begin
215 CheckDriver;
216 Driver.FileName := AValue;
217end;
218
219procedure TMediaPlayer.SetMuted(AValue: Boolean);
220begin
221 CheckDriver;
222 Driver.Muted := AValue;
223end;
224
225procedure TMediaPlayer.SetPlaying(AValue: Boolean);
226begin
227 CheckDriver;
228 Driver.Playing := AValue;
229end;
230
231procedure TMediaPlayer.SetPosition(AValue: TDateTime);
232begin
233 CheckDriver;
234 Driver.Position := AValue;
235end;
236
237procedure TMediaPlayer.SetVolume(AValue: Real);
238begin
239 CheckDriver;
240 Driver.Volume := AValue;
241end;
242
243procedure TMediaPlayer.Play;
244begin
245 CheckDriver;
246 Driver.Play;
247end;
248
249procedure TMediaPlayer.Pause;
250begin
251 CheckDriver;
252 Driver.Pause;
253end;
254
255procedure TMediaPlayer.Stop;
256begin
257 CheckDriver;
258 Driver.Stop;
259end;
260
261procedure TMediaPlayer.Open;
262begin
263 CheckDriver;
264 Driver.Open;
265end;
266
267procedure TMediaPlayer.Close;
268begin
269 CheckDriver;
270 Driver.Close;
271end;
272
273constructor TMediaPlayer.Create(AOwner: TComponent);
274begin
275 inherited Create(AOwner);
276 if not Assigned(DefaultAudioSystem) then
277 raise Exception.Create(SDefaultAudioSystemNotSet);
278 AudioSystem := DefaultAudioSystem;
279end;
280
281destructor TMediaPlayer.Destroy;
282begin
283 FreeAndNil(Driver);
284 inherited Destroy;
285end;
286
287{ TAudioSystemManagerItem }
288
289
290{ TAudioSystemManager }
291
292procedure TAudioSystemManager.FillStringList(StringList: TStrings);
293var
294 I: Integer;
295begin
296 StringList.Clear;
297 for I := 0 to Systems.Count - 1 do
298 with Systems[I] do
299 StringList.AddObject(Name, Systems[I]);
300end;
301
302function TAudioSystemManager.SearchByName(Name: string; SupportedOnly: Boolean = True):
303 TAudioSystemManagerItem;
304var
305 I: Integer;
306begin
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;
313end;
314
315procedure TAudioSystemManager.Register(Name: string;
316 SystemClass: TAudioSystemClass);
317var
318 NewItem: TAudioSystemManagerItem;
319begin
320 NewItem := TAudioSystemManagerItem.Create;
321 NewItem.Name := Name;
322 NewItem.SystemClass := SystemClass;
323 NewItem.Supported := True;
324 Systems.Add(NewItem);
325end;
326
327constructor TAudioSystemManager.Create(AOwner: TComponent);
328begin
329 inherited;
330 Systems := TObjectList<TAudioSystemManagerItem>.Create;
331end;
332
333destructor TAudioSystemManager.Destroy;
334begin
335 FreeAndNil(Systems);
336 inherited;
337end;
338
339{ TMediaPlayerDriver }
340
341procedure TMediaPlayerDriver.SetActive(AValue: Boolean);
342begin
343 if FActive = AValue then Exit;
344 FActive := AValue;
345end;
346
347procedure TMediaPlayerDriver.SetPlaying(AValue: Boolean);
348begin
349 if FPlaying = AValue then Exit;
350 if AValue then Play else Stop;
351end;
352
353function TMediaPlayerDriver.GetMuted: Boolean;
354begin
355 Result := False;
356end;
357
358procedure TMediaPlayerDriver.SetMuted(AValue: Boolean);
359begin
360end;
361
362function TMediaPlayerDriver.GetLength: TDateTime;
363begin
364 Result := 0;
365end;
366
367function TMediaPlayerDriver.GetPosition: TDateTime;
368begin
369 Result := 0;
370end;
371
372function TMediaPlayerDriver.GetVolume: Real;
373begin
374 Result := 0;
375end;
376
377procedure TMediaPlayerDriver.SetPosition(AValue: TDateTime);
378begin
379end;
380
381procedure TMediaPlayerDriver.SetVolume(AValue: Real);
382begin
383end;
384
385procedure TMediaPlayerDriver.SetFileName(AValue: string);
386begin
387 if AValue = FFileName then Exit;
388 FFileName := AValue;
389 Close;
390 Open;
391end;
392
393procedure TMediaPlayerDriver.Play;
394begin
395end;
396
397procedure TMediaPlayerDriver.Pause;
398begin
399end;
400
401procedure TMediaPlayerDriver.Stop;
402begin
403end;
404
405procedure TMediaPlayerDriver.Open;
406begin
407 Active := True;
408end;
409
410procedure TMediaPlayerDriver.Close;
411begin
412 Active := False;
413end;
414
415constructor TMediaPlayerDriver.Create;
416begin
417 inherited;
418end;
419
420destructor TMediaPlayerDriver.Destroy;
421begin
422 Stop;
423 Active := False;
424 inherited;
425end;
426
427{ TAudioSystem }
428
429procedure TAudioSystem.SetOutputMode(AValue: TOutputDriver);
430begin
431 if FOutputDriver = AValue then Exit;
432 FOutputDriver := AValue;
433end;
434
435constructor TAudioSystem.Create(AOwner: TComponent);
436begin
437 inherited;
438 {$IFDEF Windows}
439 FOutputDriver := omWin32;
440 {$ENDIF}
441 {$IFDEF Linux}
442 FOutputDriver := omAlsa;
443 {$ENDIF}
444end;
445
446destructor TAudioSystem.Destroy;
447begin
448 inherited;
449end;
450
451function TAudioSystem.GetMediaPlayerDriverClass: TMediaPlayerDriverClass;
452begin
453 Result := TMediaPlayerDriver;
454end;
455
456initialization
457
458DefaultAudioSystem := TAudioSystem.Create(nil);
459
460finalization
461
462FreeAndNil(DefaultAudioSystem);
463
464end.
465
Note: See TracBrowser for help on using the repository browser.