24effff9709233d5ea9fe5feb7fee47dfb7b5f8d
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
)
41 function MatchHeader(Data
: Pointer; Len
: LongWord): Boolean; override;
42 function MatchExtension(FName
: string): Boolean; override;
43 function GetLoader(): TSoundLoader
; override;
48 uses sdl2
, utils
, e_log
;
50 (* TWAVLoaderFactory *)
52 function TWAVLoaderFactory
.MatchHeader(Data
: Pointer; Len
: LongWord): Boolean;
62 Result
:= ((P
+0)^ = Ord('R')) and ((P
+1)^ = Ord('I')) and ((P
+2)^ = Ord('F')) and ((P
+3)^ = Ord('F'));
65 function TWAVLoaderFactory
.MatchExtension(FName
: string): Boolean;
68 Result
:= GetFilenameExt(FName
) = '.wav';
71 function TWAVLoaderFactory
.GetLoader(): TSoundLoader
;
73 Result
:= TWAVLoader
.Create();
78 function TWAVLoader
.Load(Data
: Pointer; Len
: LongWord; SStreaming
: Boolean): Boolean;
87 RW
:= SDL_RWFromConstMem(Data
, Len
);
89 if SDL_LoadWAV_RW(RW
, 0, @Spec
, @TmpBuf
, @TmpLen
) = nil then
91 e_LogWriteln('Could not load WAV: ' + SDL_GetError());
95 FFormat
.SampleRate
:= Spec
.freq
;
96 FFormat
.SampleBits
:= SDL_AUDIO_BITSIZE(Spec
.format
);
97 FFormat
.Channels
:= Spec
.channels
;
98 FStreaming
:= False; // never stream wavs
107 function TWAVLoader
.Load(FName
: string; SStreaming
: Boolean): Boolean;
109 Spec
: TSDL_AudioSpec
;
116 RW
:= SDL_RWFromFile(PChar(FName
), 'rb');
120 e_LogWritefln('Could not open WAV file `%s`: %s', [FName
, SDL_GetError()]);
124 if SDL_LoadWAV_RW(RW
, 0, @Spec
, @TmpBuf
, @TmpLen
) = nil then
126 e_LogWritefln('Could not load WAV file `%s`: %s', [FName
, SDL_GetError()]);
130 FFormat
.SampleRate
:= Spec
.freq
;
131 FFormat
.SampleBits
:= SDL_AUDIO_BITSIZE(Spec
.format
);
132 FFormat
.Channels
:= Spec
.channels
;
133 FStreaming
:= False; // never stream wavs
142 function TWAVLoader
.SetPosition(Pos
: LongWord): Boolean;
144 Result
:= False; // makes no sense when not streaming
147 function TWAVLoader
.FillBuffer(Buf
: Pointer; Len
: LongWord): LongWord;
149 if FDataLen
< Len
then
153 Move(FData
^, Buf
^, Len
);
160 function TWAVLoader
.GetAll(var OutPtr
: Pointer): LongWord;
166 procedure TWAVLoader
.Free();
169 SDL_FreeWAV(FData
); // SDL allocates inside the DLL, so we need this
173 e_AddSoundLoader(TWAVLoaderFactory
.Create());