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}
24 // a WAV loader that just uses SDL_LoadWAV
26 TWAVLoader
= 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;
39 TWAVLoaderFactory
= 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 sdl2
, utils
, e_log
;
49 (* TWAVLoaderFactory *)
51 function TWAVLoaderFactory
.MatchHeader(Data
: Pointer; Len
: LongWord): Boolean;
61 Result
:= ((P
+0)^ = Ord('R')) and ((P
+1)^ = Ord('I')) and ((P
+2)^ = Ord('F')) and ((P
+3)^ = Ord('F'));
64 function TWAVLoaderFactory
.MatchExtension(FName
: string): Boolean;
67 Result
:= GetFilenameExt(FName
) = '.wav';
70 function TWAVLoaderFactory
.GetLoader(): TSoundLoader
;
72 Result
:= TWAVLoader
.Create();
77 function TWAVLoader
.Load(Data
: Pointer; Len
: LongWord; SStreaming
: Boolean): Boolean;
86 RW
:= SDL_RWFromConstMem(Data
, Len
);
88 if SDL_LoadWAV_RW(RW
, 0, @Spec
, @TmpBuf
, @TmpLen
) = nil then
90 e_LogWriteln('Could not load WAV: ' + SDL_GetError());
94 FFormat
.SampleRate
:= Spec
.freq
;
95 FFormat
.SampleBits
:= SDL_AUDIO_BITSIZE(Spec
.format
);
96 FFormat
.Channels
:= Spec
.channels
;
97 FStreaming
:= False; // never stream wavs
106 function TWAVLoader
.Load(FName
: string; SStreaming
: Boolean): Boolean;
108 Spec
: TSDL_AudioSpec
;
115 RW
:= SDL_RWFromFile(PChar(FName
), 'rb');
119 e_LogWritefln('Could not open WAV file `%s`: %s', [FName
, SDL_GetError()]);
123 if SDL_LoadWAV_RW(RW
, 0, @Spec
, @TmpBuf
, @TmpLen
) = nil then
125 e_LogWritefln('Could not load WAV file `%s`: %s', [FName
, SDL_GetError()]);
129 FFormat
.SampleRate
:= Spec
.freq
;
130 FFormat
.SampleBits
:= SDL_AUDIO_BITSIZE(Spec
.format
);
131 FFormat
.Channels
:= Spec
.channels
;
132 FStreaming
:= False; // never stream wavs
141 function TWAVLoader
.SetPosition(Pos
: LongWord): Boolean;
143 Result
:= False; // makes no sense when not streaming
146 function TWAVLoader
.FillBuffer(Buf
: Pointer; Len
: LongWord): LongWord;
148 if FDataLen
< Len
then
152 Move(FData
^, Buf
^, Len
);
159 function TWAVLoader
.GetAll(var OutPtr
: Pointer): LongWord;
165 procedure TWAVLoader
.Free();
168 SDL_FreeWAV(FData
); // SDL allocates inside the DLL, so we need this
172 e_AddSoundLoader(TWAVLoaderFactory
.Create());