X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fengine%2Fe_soundfile_wav.pas;h=fe9a8a26f9c10ded420e82f183cee4c6167f3c77;hb=1408b629ba53aad8cf198b197db1293f6f293c79;hp=ba7bcdf67057a5d65f6e057b6ab4c755838f3b01;hpb=0e101bd452c40da601236aaa2dd4106be47ddce1;p=d2df-sdl.git diff --git a/src/engine/e_soundfile_wav.pas b/src/engine/e_soundfile_wav.pas index ba7bcdf..fe9a8a2 100644 --- a/src/engine/e_soundfile_wav.pas +++ b/src/engine/e_soundfile_wav.pas @@ -50,7 +50,7 @@ uses {$ELSE} SDL2, {$ENDIF} - utils, e_log; + utils, ctypes, e_log; (* TWAVLoaderFactory *) @@ -79,83 +79,97 @@ begin end; (* TWAVLoader *) -function TWAVLoader.Load(Data: Pointer; Len: LongWord; SStreaming: Boolean): Boolean; -var - Spec: TSDL_AudioSpec; - RW: PSDL_RWops; - TmpLen: UInt32; - TmpBuf: PUInt8; +function ConvertSound (var buf: PUInt8; var len: UInt32; var format: UInt16; rate: cint; chan: UInt8): Boolean; + var cvt: TSDL_AudioCVT; tformat: UInt16; begin - Result := False; - - RW := SDL_RWFromConstMem(Data, Len); + result := true; + case format of + 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; + 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 *) + else result := false (* unsupported format *) + end; + if (result = true) and (format <> tformat) then + begin + Result := False; + if SDL_BuildAudioCVT(@cvt, format, chan, rate, tformat, chan, rate) <> -1 then + begin + buf := SDL_realloc(buf, len * cvt.len_mult); + cvt.len := len; + cvt.buf := buf; + result := SDL_ConvertAudio(@cvt) = 0; + len := cvt.len_cvt; + format := tformat + end + end +end; +function LoadWavRW (Loader: TWAVLoader; RW: PSDL_RWops): Boolean; + var + Spec: TSDL_AudioSpec; + Len: UInt32; + Buf: PUInt8; +begin + Result := False; {$IFDEF USE_SDL2} - if SDL_LoadWAV_RW(RW, 0, @Spec, @TmpBuf, @TmpLen) = nil then + if SDL_LoadWAV_RW(RW, 0, @Spec, @Buf, @Len) <> nil then {$ELSE} - if SDL_LoadWAV_RW(RW, 0, @Spec, PUInt8(@TmpBuf), @TmpLen) = nil then + if SDL_LoadWAV_RW(RW, 0, @Spec, PUInt8(@Buf), @Len) <> nil then {$ENDIF} begin - e_LogWriteln('Could not load WAV: ' + SDL_GetError()); + Result := ConvertSound(Buf, Len, Spec.format, Spec.freq, Spec.channels); + if Result = True then + begin + with Loader do + begin + FFormat.SampleRate := Spec.freq; + {$IFDEF USE_SDL2} + FFormat.SampleBits := SDL_AUDIO_BITSIZE(Spec.format); + {$ELSE} + FFormat.SampleBits := Spec.format and $FF; + {$ENDIF} + FFormat.Channels := Spec.channels; + FStreaming := False; // never stream wavs + FDataLen := Len; + FData := Buf; + end + end + else + begin + SDL_FreeWav(Buf) + end end - else - begin - FFormat.SampleRate := Spec.freq; - {$IFDEF USE_SDL2} - FFormat.SampleBits := SDL_AUDIO_BITSIZE(Spec.format); - {$ELSE} - FFormat.SampleBits := Spec.format and $FF; - {$ENDIF} - FFormat.Channels := Spec.channels; - FStreaming := False; // never stream wavs - FDataLen := TmpLen; - FData := TmpBuf; - Result := True; - end; +end; +function TWAVLoader.Load(Data: Pointer; Len: LongWord; SStreaming: Boolean): Boolean; + var + RW: PSDL_RWops; +begin + RW := SDL_RWFromConstMem(Data, Len); + Result := LoadWavRW(Self, RW); + if Result = False then + e_LogWriteln('Could not load WAV: ' + SDL_GetError()); SDL_RWclose(RW); end; function TWAVLoader.Load(FName: string; SStreaming: Boolean): Boolean; -var - Spec: TSDL_AudioSpec; - RW: PSDL_RWops; - TmpLen: UInt32; - TmpBuf: PUInt8; + var + RW: PSDL_RWops; begin - Result := False; - RW := SDL_RWFromFile(PChar(FName), 'rb'); - - if RW = nil then + if RW <> nil then begin - e_LogWritefln('Could not open WAV file `%s`: %s', [FName, SDL_GetError()]); - exit; - end; - -{$IFDEF USE_SDL2} - if SDL_LoadWAV_RW(RW, 0, @Spec, @TmpBuf, @TmpLen) = nil then -{$ELSE} - if SDL_LoadWAV_RW(RW, 0, @Spec, PUInt8(@TmpBuf), @TmpLen) = nil then -{$ENDIF} - begin - e_LogWritefln('Could not load WAV file `%s`: %s', [FName, SDL_GetError()]); + Result := LoadWavRW(Self, RW); + if Result = False then + e_LogWritefln('Could not load WAV file `%s`: %s', [FName, SDL_GetError()]); end else begin - FFormat.SampleRate := Spec.freq; - {$IFDEF USE_SDL2} - FFormat.SampleBits := SDL_AUDIO_BITSIZE(Spec.format); - {$ELSE} - FFormat.SampleBits := Spec.format and $FF; - {$ENDIF} - FFormat.Channels := Spec.channels; - FStreaming := False; // never stream wavs - FDataLen := TmpLen; - FData := TmpBuf; - Result := True; + e_LogWritefln('Could not open WAV file `%s`: %s', [FName, SDL_GetError()]); + Result := False end; - SDL_RWclose(RW); end;