DEADSOFTWARE

6ba268ae389119ce40f812f84cd5e111e4ad4749
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit e_soundfile;
19 interface
21 type
22 TSoundLoader = class;
24 TSoundFormat = record
25 SampleBits: Integer;
26 SampleRate: Integer;
27 Channels: Integer;
28 end;
30 // each sound file format has its own loader factory and loader class,
31 // each sound has its own loader instance for streaming purposes
33 TSoundLoader = class
34 protected
35 FFormat: TSoundFormat;
36 FStreaming: Boolean;
37 FLooping: Boolean;
39 public
40 function Load(Data: Pointer; Len: LongWord; SStreaming: Boolean): Boolean; virtual; abstract; overload;
41 function Load(FName: string; SStreaming: Boolean): Boolean; virtual; abstract; overload;
43 function SetPosition(Pos: LongWord): Boolean; virtual; abstract;
44 function FillBuffer(Buf: Pointer; Len: LongWord): LongWord; virtual; abstract;
46 function GetAll(var OutPtr: Pointer): LongWord; virtual; abstract;
48 procedure Free(); virtual; abstract;
50 property Format: TSoundFormat read FFormat;
51 property Streaming: Boolean read FStreaming;
52 property Looping: Boolean read FLooping write FLooping;
53 end;
55 TSoundLoaderFactory = class
56 public
57 function MatchHeader(Data: Pointer; Len: LongWord): Boolean; virtual; abstract;
58 function MatchExtension(FName: string): Boolean; virtual; abstract;
59 function GetLoader(): TSoundLoader; virtual; abstract;
60 end;
62 function e_GetSoundLoader(Data: Pointer; Len: LongWord): TSoundLoader; overload;
63 function e_GetSoundLoader(FName: string): TSoundLoader; overload;
65 procedure e_AddSoundLoader(Loader: TSoundLoaderFactory);
67 implementation
69 var
70 e_SoundLoaders: array of TSoundLoaderFactory;
72 function e_GetSoundLoader(FName: string): TSoundLoader; overload;
73 var
74 I: Integer;
75 begin
76 Result := nil;
77 for I := Low(e_SoundLoaders) to High(e_SoundLoaders) do
78 if e_SoundLoaders[I].MatchExtension(FName) then
79 begin
80 Result := e_SoundLoaders[I].GetLoader();
81 break;
82 end;
83 end;
85 function e_GetSoundLoader(Data: Pointer; Len: LongWord): TSoundLoader; overload;
86 var
87 I: Integer;
88 begin
89 Result := nil;
90 for I := Low(e_SoundLoaders) to High(e_SoundLoaders) do
91 if e_SoundLoaders[I].MatchHeader(Data, Len) then
92 begin
93 Result := e_SoundLoaders[I].GetLoader();
94 break;
95 end;
96 end;
98 procedure e_AddSoundLoader(Loader: TSoundLoaderFactory);
99 begin
100 SetLength(e_SoundLoaders, Length(e_SoundLoaders) + 1);
101 e_SoundLoaders[High(e_SoundLoaders)] := Loader;
102 end;
104 end.