X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fengine%2Fe_soundfile_wav.pas;h=153cad427f4f1b58c94f60977644c9dedeb882ec;hb=5d01abb1a3795cd4e4769d8d78841372be8fbcf0;hp=fe9a8a26f9c10ded420e82f183cee4c6167f3c77;hpb=1408b629ba53aad8cf198b197db1293f6f293c79;p=d2df-sdl.git diff --git a/src/engine/e_soundfile_wav.pas b/src/engine/e_soundfile_wav.pas index fe9a8a2..153cad4 100644 --- a/src/engine/e_soundfile_wav.pas +++ b/src/engine/e_soundfile_wav.pas @@ -24,15 +24,17 @@ type TWAVLoader = class (TSoundLoader) public - function Load(Data: Pointer; Len: LongWord; SStreaming: Boolean): Boolean; override; overload; - function Load(FName: string; SStreaming: Boolean): Boolean; override; overload; - function SetPosition(Pos: LongWord): Boolean; override; + function Load(Data: Pointer; Len: LongWord; Loop: Boolean): Boolean; override; overload; + function Load(FName: string; Loop: Boolean): Boolean; override; overload; + function Finished(): Boolean; override; + function Restart(): Boolean; override; function FillBuffer(Buf: Pointer; Len: LongWord): LongWord; override; - function GetAll(var OutPtr: Pointer): LongWord; override; procedure Free(); override; private FData: Pointer; FDataLen: LongWord; + FDataPos: LongWord; + FLooping: Boolean; end; TWAVLoaderFactory = class (TSoundLoaderFactory) @@ -46,7 +48,7 @@ implementation uses {$IFDEF USE_SDL} - SDL, + SDL, cmem, {$ELSE} SDL2, {$ENDIF} @@ -87,8 +89,10 @@ begin AUDIO_U8, AUDIO_S8 : tformat := AUDIO_U8; (* yes, unsigned *) AUDIO_U16LSB, AUDIO_U16MSB: tformat := AUDIO_S16SYS; (* and yes, signed *) AUDIO_S16LSB, AUDIO_S16MSB: tformat := AUDIO_S16SYS; + {$IFDEF USE_SDL2} AUDIO_S32LSB, AUDIO_S32MSB: tformat := AUDIO_S16SYS; (* 32bit not supported in al core *) AUDIO_F32LSB, AUDIO_F32MSB: tformat := AUDIO_S16SYS; (* float not supported in al core *) + {$ENDIF} else result := false (* unsupported format *) end; if (result = true) and (format <> tformat) then @@ -96,7 +100,11 @@ begin Result := False; if SDL_BuildAudioCVT(@cvt, format, chan, rate, tformat, chan, rate) <> -1 then begin - buf := SDL_realloc(buf, len * cvt.len_mult); + {$IFDEF USE_SDL2} + buf := SDL_realloc(buf, len * cvt.len_mult); + {$ELSE} + buf := realloc(buf, len * cvt.len_mult); + {$ENDIF} cvt.len := len; cvt.buf := buf; result := SDL_ConvertAudio(@cvt) = 0; @@ -131,7 +139,7 @@ begin FFormat.SampleBits := Spec.format and $FF; {$ENDIF} FFormat.Channels := Spec.channels; - FStreaming := False; // never stream wavs + FDataPos := 0; FDataLen := Len; FData := Buf; end @@ -143,7 +151,7 @@ begin end end; -function TWAVLoader.Load(Data: Pointer; Len: LongWord; SStreaming: Boolean): Boolean; +function TWAVLoader.Load(Data: Pointer; Len: LongWord; Loop: Boolean): Boolean; var RW: PSDL_RWops; begin @@ -152,9 +160,11 @@ begin if Result = False then e_LogWriteln('Could not load WAV: ' + SDL_GetError()); SDL_RWclose(RW); + FStreaming := False; + FLooping := Loop; end; -function TWAVLoader.Load(FName: string; SStreaming: Boolean): Boolean; +function TWAVLoader.Load(FName: string; Loop: Boolean): Boolean; var RW: PSDL_RWops; begin @@ -171,36 +181,50 @@ begin Result := False end; SDL_RWclose(RW); + FStreaming := False; + FLooping := Loop; end; -function TWAVLoader.SetPosition(Pos: LongWord): Boolean; +function TWAVLoader.Finished(): Boolean; begin - Result := False; // makes no sense when not streaming + Result := FDataPos >= FDataLen; end; -function TWAVLoader.FillBuffer(Buf: Pointer; Len: LongWord): LongWord; +function TWAVLoader.Restart(): Boolean; begin - if FDataLen < Len then - Len := FDataLen; - if FData <> nil then - begin - Move(FData^, Buf^, Len); - Result := Len; - end - else - Result := 0; + Result := True; + FDataPos := 0; end; -function TWAVLoader.GetAll(var OutPtr: Pointer): LongWord; +function TWAVLoader.FillBuffer(Buf: Pointer; Len: LongWord): LongWord; +var + OutPos: LongWord; + Tx: LongWord; begin - OutPtr := FData; - Result := FDataLen; + OutPos := 0; + Result := 0; + + while (FDataPos < FDataLen) and (OutPos < Len) do + begin + Tx := nmin(FDataLen - FDataPos, Len - OutPos); + Move((FData + FDataPos)^, (Buf + OutPos)^, Tx); + + FDataPos := FDataPos + Tx; + OutPos := OutPos + Tx; + Result := Result + Tx; + + if (FDataPos >= FDataLen) and FLooping then + FDataPos := 0; + end; end; procedure TWAVLoader.Free(); begin if FData <> nil then SDL_FreeWAV(FData); // SDL allocates inside the DLL, so we need this + FDataPos := 0; + FData := nil; + FDataLen := 0; end; initialization