1 (* Copyright (C) Doom 2D: Forever Developers
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.
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.
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/>.
16 {$INCLUDE ../shared/a_modes.inc}
17 unit e_soundfile_modplug
;
21 uses e_soundfile
, modplug
;
24 // a module loader that uses libmodplug
26 TModPlugLoader
= class (TSoundLoader
)
28 function Load(Data
: Pointer; Len
: LongWord; SStreaming
: Boolean): Boolean; override; overload
;
29 function Load(FName
: string; SStreaming
: Boolean): Boolean; override; overload
;
30 function SetPosition(Pos
: LongWord): Boolean; override;
31 function FillBuffer(Buf
: Pointer; Len
: LongWord): LongWord; override;
32 function GetAll(var OutPtr
: Pointer): LongWord; override;
33 procedure Free(); override;
38 TModPlugLoaderFactory
= class (TSoundLoaderFactory
)
40 function MatchHeader(Data
: Pointer; Len
: LongWord): Boolean; override;
41 function MatchExtension(FName
: string): Boolean; override;
42 function GetLoader(): TSoundLoader
; override;
47 uses sysutils
, utils
, e_sound
, e_log
, classes
;
50 Settings
: ModPlug_Settings
= (
51 mFlags
: MODPLUG_ENABLE_OVERSAMPLING
or MODPLUG_ENABLE_NOISE_REDUCTION
;
55 mResamplingMode
: MODPLUG_RESAMPLE_LINEAR
;
56 mStereoSeparation
: 128;
67 (* TModPlugLoaderFactory *)
69 function TModPlugLoaderFactory
.MatchHeader(Data
: Pointer; Len
: LongWord): Boolean;
73 // HACK: there's no "test" function in modplug, so just try to load that shit
76 Mpf
:= ModPlug_Load(Data
, Len
);
77 if Mpf
= nil then Exit
;
83 function TModPlugLoaderFactory
.MatchExtension(FName
: string): Boolean;
87 Ext
:= GetFilenameExt(FName
);
88 Result
:= (Ext
= '.it') or (Ext
= '.xm') or (Ext
= '.mod') or (Ext
= '.s3m');
91 function TModPlugLoaderFactory
.GetLoader(): TSoundLoader
;
93 ModPlug_SetSettings(@Settings
); // update settings just in case
94 Result
:= TModPlugLoader
.Create();
99 function TModPlugLoader
.Load(Data
: Pointer; Len
: LongWord; SStreaming
: Boolean): Boolean;
103 FFile
:= ModPlug_Load(Data
, Len
);
106 e_LogWriteln('ModPlug: ERROR: ModPlug_Load failed');
110 FFormat
.SampleRate
:= 44100;
111 FFormat
.SampleBits
:= 16;
112 FFormat
.Channels
:= 2;
113 FStreaming
:= True; // modules are always streaming
118 function TModPlugLoader
.Load(FName
: string; SStreaming
: Boolean): Boolean;
127 S
:= openDiskFileRO(FName
);
128 // ayy just read the entire file
129 Data
:= GetMem(S
.Size
);
131 raise Exception
.Create('out of memory');
132 Len
:= S
.Read(Data
^, S
.Size
);
134 raise Exception
.Create('what the fuck');
135 Result
:= Load(Data
, Len
, SStreaming
)
138 e_LogWritefln('ModPlug: ERROR: could not read file `%s`: %s', [FName
, E
.Message]);
141 if Data
<> nil then FreeMem(Data
);
142 if S
<> nil then S
.Free();
145 function TModPlugLoader
.SetPosition(Pos
: LongWord): Boolean;
148 if FFile
= nil then Exit
;
149 ModPlug_Seek(FFile
, Pos
);
153 function TModPlugLoader
.FillBuffer(Buf
: Pointer; Len
: LongWord): LongWord;
158 if FFile
= nil then Exit
;
160 Cnt
:= ModPlug_Read(FFile
, Buf
, Len
);
161 if Cnt
< 0 then Exit
;
163 if FLooping
and (Cnt
< Len
) then
165 // assume it just ended and restart, because modplug only loops if the
166 // module tells it to
167 ModPlug_Seek(FFile
, 0);
168 // this used to be Result := Cnt + Read(FFile, Buf + Cnt, Len - Cnt)
169 // but the difference appears to be negligible
170 Result
:= ModPlug_Read(FFile
, Buf
, Len
);
176 function TModPlugLoader
.GetAll(var OutPtr
: Pointer): LongWord;
178 Result
:= 0; // modules are always streaming, so this don't make sense
181 procedure TModPlugLoader
.Free();
185 ModPlug_Unload(FFile
);
191 e_AddSoundLoader(TModPlugLoaderFactory
.Create());