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, version 3 of the License ONLY.
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.
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/>.
15 {$INCLUDE ../shared/a_modes.inc}
16 unit e_soundfile_modplug
;
20 uses e_soundfile
, modplug
;
23 // a module loader that uses libmodplug
25 TModPlugLoader
= class (TSoundLoader
)
27 function Load(Data
: Pointer; Len
: LongWord; Loop
: Boolean): Boolean; override; overload
;
28 function Load(FName
: string; Loop
: Boolean): Boolean; override; overload
;
29 function Finished(): Boolean; override;
30 function Restart(): Boolean; override;
31 function FillBuffer(Buf
: Pointer; Len
: LongWord): LongWord; override;
32 procedure Free(); override;
39 TModPlugLoaderFactory
= class (TSoundLoaderFactory
)
41 function MatchHeader(Data
: Pointer; Len
: LongWord): Boolean; override;
42 function MatchExtension(FName
: string): Boolean; override;
43 function GetLoader(): TSoundLoader
; override;
48 uses sysutils
, utils
, e_sound
, e_log
, classes
;
51 Settings
: ModPlug_Settings
= (
52 mFlags
: MODPLUG_ENABLE_OVERSAMPLING
or MODPLUG_ENABLE_NOISE_REDUCTION
;
56 mResamplingMode
: MODPLUG_RESAMPLE_LINEAR
;
57 mStereoSeparation
: 128;
68 (* TModPlugLoaderFactory *)
70 function TModPlugLoaderFactory
.MatchHeader(Data
: Pointer; Len
: LongWord): Boolean;
74 // HACK: there's no "test" function in modplug, so just try to load that shit
77 Mpf
:= ModPlug_Load(Data
, Len
);
78 if Mpf
= nil then Exit
;
84 function TModPlugLoaderFactory
.MatchExtension(FName
: string): Boolean;
88 Ext
:= GetFilenameExt(FName
);
89 Result
:= (Ext
= '.it') or (Ext
= '.xm') or (Ext
= '.mod') or (Ext
= '.s3m');
92 function TModPlugLoaderFactory
.GetLoader(): TSoundLoader
;
94 // update interpolation setting
96 Settings
.mResamplingMode
:= MODPLUG_RESAMPLE_LINEAR
98 Settings
.mResamplingMode
:= MODPLUG_RESAMPLE_NEAREST
;
99 ModPlug_SetSettings(@Settings
);
100 Result
:= TModPlugLoader
.Create();
105 function TModPlugLoader
.Load(Data
: Pointer; Len
: LongWord; Loop
: Boolean): Boolean;
109 FFile
:= ModPlug_Load(Data
, Len
);
112 e_LogWriteln('ModPlug: ERROR: ModPlug_Load failed');
116 FFormat
.SampleRate
:= 44100;
117 FFormat
.SampleBits
:= 16;
118 FFormat
.Channels
:= 2;
119 FStreaming
:= True; // modules are always streaming
126 function TModPlugLoader
.Load(FName
: string; Loop
: Boolean): Boolean;
135 S
:= openDiskFileRO(FName
);
136 // ayy just read the entire file
137 Data
:= GetMem(S
.Size
);
139 raise Exception
.Create('out of memory');
140 Len
:= S
.Read(Data
^, S
.Size
);
142 raise Exception
.Create('what the fuck');
143 Result
:= Load(Data
, Len
, Loop
);
146 e_LogWritefln('ModPlug: ERROR: could not read file `%s`: %s', [FName
, E
.Message]);
149 if Data
<> nil then FreeMem(Data
);
150 if S
<> nil then S
.Free();
153 function TModPlugLoader
.Finished(): Boolean;
158 function TModPlugLoader
.Restart(): Boolean;
161 if FFile
= nil then Exit
;
162 ModPlug_Seek(FFile
, 0);
167 function TModPlugLoader
.FillBuffer(Buf
: Pointer; Len
: LongWord): LongWord;
172 if FFile
= nil then Exit
;
174 Cnt
:= ModPlug_Read(FFile
, Buf
, Len
);
175 if Cnt
< 0 then Exit
;
183 // assume it just ended and restart, because modplug only loops if the
184 // module tells it to
185 ModPlug_Seek(FFile
, 0);
186 // this used to be Result := Cnt + Read(FFile, Buf + Cnt, Len - Cnt)
187 // but the difference appears to be negligible
188 Result
:= ModPlug_Read(FFile
, Buf
, Len
);
195 procedure TModPlugLoader
.Free();
199 ModPlug_Unload(FFile
);
207 e_AddSoundLoader(TModPlugLoaderFactory
.Create());