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}
20 uses e_soundfile
, XMP
;
23 // a module loader that uses libxmp-lite
25 TXMPLoader
= 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;
38 TXMPLoaderFactory
= 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
;
49 (* TXMPLoaderFactory *)
51 function TXMPLoaderFactory
.MatchHeader(Data
: Pointer; Len
: LongWord): Boolean;
56 // HACK: these fine gentlemen didn't provide us with a xmp_test_module_from_memory()
57 // so just load the module and unload it
61 Ctx
:= xmp_create_context();
62 Err
:= xmp_load_module_from_memory(Ctx
, Data
, Len
);
66 xmp_release_module(Ctx
);
70 xmp_free_context(Ctx
);
73 function TXMPLoaderFactory
.MatchExtension(FName
: string): Boolean;
77 Ext
:= GetFilenameExt(FName
);
78 Result
:= (Ext
= '.it') or (Ext
= '.xm') or (Ext
= '.mod') or (Ext
= '.s3m');
81 function TXMPLoaderFactory
.GetLoader(): TSoundLoader
;
83 Result
:= TXMPLoader
.Create();
88 function TXMPLoader
.Load(Data
: Pointer; Len
: LongWord; SStreaming
: Boolean): Boolean;
96 FXMP
:= xmp_create_context();
97 if FXMP
= nil then Exit
;
100 Err
:= xmp_load_module_from_memory(FXMP
, Data
, Len
);
102 raise Exception
.Create('xmp_load_module_from_memory failed');
104 if xmp_start_player(FXMP
, 48000, 0) <> 0 then
105 raise Exception
.Create('xmp_start_player failed');
107 if e_MusicLerp
then Interp
:= XMP_INTERP_LINEAR
108 else Interp
:= XMP_INTERP_NEAREST
;
109 xmp_set_player(FXMP
, XMP_PLAYER_INTERP
, Interp
);
111 FFormat
.SampleRate
:= 48000;
112 FFormat
.SampleBits
:= 16;
113 FFormat
.Channels
:= 2;
115 FStreaming
:= True; // modules are always streaming
121 e_LogWriteln('TXMPLoader.Load() error: ' + E
.Message);
122 if Err
= 0 then xmp_release_module(FXMP
);
123 xmp_free_context(FXMP
);
129 function TXMPLoader
.Load(FName
: string; SStreaming
: Boolean): Boolean;
137 FXMP
:= xmp_create_context();
138 if FXMP
= nil then Exit
;
141 Err
:= xmp_load_module(FXMP
, PChar(FName
));
143 raise Exception
.Create('xmp_load_module failed');
145 if xmp_start_player(FXMP
, 48000, 0) <> 0 then
146 raise Exception
.Create('xmp_start_player failed');
148 if e_MusicLerp
then Interp
:= XMP_INTERP_LINEAR
149 else Interp
:= XMP_INTERP_NEAREST
;
150 xmp_set_player(FXMP
, XMP_PLAYER_INTERP
, Interp
);
152 FFormat
.SampleRate
:= 48000;
153 FFormat
.SampleBits
:= 16;
154 FFormat
.Channels
:= 2;
156 FStreaming
:= True; // modules are always streaming
162 e_LogWritefln('TXMPLoader.Load(%s) error: %s', [FName
, E
.Message]);
163 if Err
= 0 then xmp_release_module(FXMP
);
164 xmp_free_context(FXMP
);
170 function TXMPLoader
.SetPosition(Pos
: LongWord): Boolean;
173 if FXMP
= nil then Exit
;
174 Result
:= xmp_set_position(FXMP
, Pos
) = 0;
177 function TXMPLoader
.FillBuffer(Buf
: Pointer; Len
: LongWord): LongWord;
182 if FXMP
= nil then Exit
;
187 if xmp_play_buffer(FXMP
, Buf
, Len
, LoopN
) = 0 then
191 function TXMPLoader
.GetAll(var OutPtr
: Pointer): LongWord;
193 Result
:= 0; // modules are always streaming, so this don't make sense
196 procedure TXMPLoader
.Free();
202 xmp_end_player(FXMP
);
203 xmp_release_module(FXMP
);
205 xmp_free_context(FXMP
);
212 e_AddSoundLoader(TXMPLoaderFactory
.Create());