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; SStreaming
: Boolean): Boolean; override; overload
;
28 function Load(FName
: string; SStreaming
: Boolean): Boolean; override; overload
;
29 function SetPosition(Pos
: LongWord): Boolean; override;
30 function FillBuffer(Buf
: Pointer; Len
: LongWord): LongWord; override;
31 function GetAll(var OutPtr
: Pointer): LongWord; override;
32 procedure Free(); override;
37 TModPlugLoaderFactory
= class (TSoundLoaderFactory
)
39 function MatchHeader(Data
: Pointer; Len
: LongWord): Boolean; override;
40 function MatchExtension(FName
: string): Boolean; override;
41 function GetLoader(): TSoundLoader
; override;
46 uses sysutils
, utils
, e_sound
, e_log
, classes
;
49 Settings
: ModPlug_Settings
= (
50 mFlags
: MODPLUG_ENABLE_OVERSAMPLING
or MODPLUG_ENABLE_NOISE_REDUCTION
;
54 mResamplingMode
: MODPLUG_RESAMPLE_LINEAR
;
55 mStereoSeparation
: 128;
66 (* TModPlugLoaderFactory *)
68 function TModPlugLoaderFactory
.MatchHeader(Data
: Pointer; Len
: LongWord): Boolean;
72 // HACK: there's no "test" function in modplug, so just try to load that shit
75 Mpf
:= ModPlug_Load(Data
, Len
);
76 if Mpf
= nil then Exit
;
82 function TModPlugLoaderFactory
.MatchExtension(FName
: string): Boolean;
86 Ext
:= GetFilenameExt(FName
);
87 Result
:= (Ext
= '.it') or (Ext
= '.xm') or (Ext
= '.mod') or (Ext
= '.s3m');
90 function TModPlugLoaderFactory
.GetLoader(): TSoundLoader
;
92 // update interpolation setting
94 Settings
.mResamplingMode
:= MODPLUG_RESAMPLE_LINEAR
96 Settings
.mResamplingMode
:= MODPLUG_RESAMPLE_NEAREST
;
97 ModPlug_SetSettings(@Settings
);
98 Result
:= TModPlugLoader
.Create();
103 function TModPlugLoader
.Load(Data
: Pointer; Len
: LongWord; SStreaming
: Boolean): Boolean;
107 FFile
:= ModPlug_Load(Data
, Len
);
110 e_LogWriteln('ModPlug: ERROR: ModPlug_Load failed');
114 FFormat
.SampleRate
:= 44100;
115 FFormat
.SampleBits
:= 16;
116 FFormat
.Channels
:= 2;
117 FStreaming
:= True; // modules are always streaming
122 function TModPlugLoader
.Load(FName
: string; SStreaming
: Boolean): Boolean;
131 S
:= openDiskFileRO(FName
);
132 // ayy just read the entire file
133 Data
:= GetMem(S
.Size
);
135 raise Exception
.Create('out of memory');
136 Len
:= S
.Read(Data
^, S
.Size
);
138 raise Exception
.Create('what the fuck');
139 Result
:= Load(Data
, Len
, SStreaming
)
142 e_LogWritefln('ModPlug: ERROR: could not read file `%s`: %s', [FName
, E
.Message]);
145 if Data
<> nil then FreeMem(Data
);
146 if S
<> nil then S
.Free();
149 function TModPlugLoader
.SetPosition(Pos
: LongWord): Boolean;
152 if FFile
= nil then Exit
;
153 ModPlug_Seek(FFile
, Pos
);
157 function TModPlugLoader
.FillBuffer(Buf
: Pointer; Len
: LongWord): LongWord;
162 if FFile
= nil then Exit
;
164 Cnt
:= ModPlug_Read(FFile
, Buf
, Len
);
165 if Cnt
< 0 then Exit
;
167 if FLooping
and (Cnt
< Len
) then
169 // assume it just ended and restart, because modplug only loops if the
170 // module tells it to
171 ModPlug_Seek(FFile
, 0);
172 // this used to be Result := Cnt + Read(FFile, Buf + Cnt, Len - Cnt)
173 // but the difference appears to be negligible
174 Result
:= ModPlug_Read(FFile
, Buf
, Len
);
180 function TModPlugLoader
.GetAll(var OutPtr
: Pointer): LongWord;
182 Result
:= 0; // modules are always streaming, so this don't make sense
185 procedure TModPlugLoader
.Free();
189 ModPlug_Unload(FFile
);
195 e_AddSoundLoader(TModPlugLoaderFactory
.Create());