DEADSOFTWARE

simplify TSoundLoader interface
[d2df-sdl.git] / src / engine / e_soundfile.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit e_soundfile;
18 interface
20 type
21 TSoundLoader = class;
23 TSoundFormat = record
24 SampleBits: Integer;
25 SampleRate: Integer;
26 Channels: Integer;
27 end;
29 // each sound file format has its own loader factory and loader class,
30 // each sound has its own loader instance for streaming purposes
32 TSoundLoader = class
33 protected
34 FFormat: TSoundFormat;
35 FStreaming: Boolean;
37 public
38 function Load(Data: Pointer; Len: LongWord; Loop: Boolean): Boolean; virtual; abstract; overload;
39 function Load(FName: string; Loop: Boolean): Boolean; virtual; abstract; overload;
41 function Finished(): Boolean; virtual; abstract;
42 function Restart(): Boolean; virtual; abstract;
43 function FillBuffer(Buf: Pointer; Len: LongWord): LongWord; virtual; abstract;
45 procedure Free(); virtual; abstract;
47 property Format: TSoundFormat read FFormat;
48 property Streaming: Boolean read FStreaming;
49 end;
51 TSoundLoaderFactory = class
52 public
53 function MatchHeader(Data: Pointer; Len: LongWord): Boolean; virtual; abstract;
54 function MatchExtension(FName: string): Boolean; virtual; abstract;
55 function GetLoader(): TSoundLoader; virtual; abstract;
56 end;
58 function e_GetSoundLoader(Data: Pointer; Len: LongWord): TSoundLoader; overload;
59 function e_GetSoundLoader(FName: string): TSoundLoader; overload;
61 procedure e_AddSoundLoader(Loader: TSoundLoaderFactory);
63 implementation
65 var
66 e_SoundLoaders: array of TSoundLoaderFactory;
68 function e_GetSoundLoader(FName: string): TSoundLoader; overload;
69 var
70 I: Integer;
71 begin
72 Result := nil;
73 for I := Low(e_SoundLoaders) to High(e_SoundLoaders) do
74 if e_SoundLoaders[I].MatchExtension(FName) then
75 begin
76 Result := e_SoundLoaders[I].GetLoader();
77 break;
78 end;
79 end;
81 function e_GetSoundLoader(Data: Pointer; Len: LongWord): TSoundLoader; overload;
82 var
83 I: Integer;
84 begin
85 Result := nil;
86 for I := Low(e_SoundLoaders) to High(e_SoundLoaders) do
87 if e_SoundLoaders[I].MatchHeader(Data, Len) then
88 begin
89 Result := e_SoundLoaders[I].GetLoader();
90 break;
91 end;
92 end;
94 procedure e_AddSoundLoader(Loader: TSoundLoaderFactory);
95 begin
96 SetLength(e_SoundLoaders, Length(e_SoundLoaders) + 1);
97 e_SoundLoaders[High(e_SoundLoaders)] := Loader;
98 end;
100 end.