X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fengine%2Fe_sound_al.inc;h=ac64ad41d7f8e2d6d83ce7a5c92070270c80d671;hb=367241f47cc2babef77538b6a58c7c500add44e7;hp=04305b61ec0b4ce522dd4dfd99f9f594267d44a4;hpb=8b6cdec88173bd86233865660860f65ee387f6f2;p=d2df-sdl.git diff --git a/src/engine/e_sound_al.inc b/src/engine/e_sound_al.inc index 04305b6..ac64ad4 100644 --- a/src/engine/e_sound_al.inc +++ b/src/engine/e_sound_al.inc @@ -2,8 +2,7 @@ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * the Free Software Foundation, version 3 of the License ONLY. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -99,7 +98,7 @@ var implementation uses - g_window, g_options, utils; + g_options, utils; const NUM_SOURCES = 255; // + 1 stereo @@ -121,6 +120,28 @@ var alStreamData: array [0..STREAM_BUFSIZE-1] of Byte; alStreamAvail: Integer = NUM_STREAM_BUFFERS; +{$IFNDEF OPENAL_SINGLETHREADED} +var + StreamThread: TThreadID = NilThreadId; + StreamThreadRunning: Boolean = False; + StreamLock: TRTLCriticalSection; + StreamBufTime: Integer = 100; // time to sleep between buffer checks + +procedure UpdateStreamSource(Src: Integer); forward; + +function StreamThreadProc(Param: Pointer): PtrInt; +begin + while StreamThreadRunning do + begin + EnterCriticalSection(StreamLock); + UpdateStreamSource(MUSIC_SOURCE); + LeaveCriticalSection(StreamLock); + Sleep(StreamBufTime); + end; + Result := 0; +end; +{$ENDIF} + function CheckALError(): Boolean; begin e_ALError := alGetError(); @@ -215,6 +236,12 @@ begin else alStreamAvail := NUM_STREAM_BUFFERS; + {$IFNDEF OPENAL_SINGLETHREADED} + InitCriticalSection(StreamLock); + StreamThreadRunning := True; + StreamThread := BeginThread(Addr(StreamThreadProc)); + {$ENDIF} + Result := True; end; @@ -266,12 +293,70 @@ begin alGetSourcei(S, AL_SOURCE_STATE, Result); end; +function LoadEntireSound(var Snd: TSoundRec; Loader: TSoundLoader): Boolean; +var + Frame: Pointer; + Data: Pointer; + Rx: LongWord; + DataLen, OldLen: LongWord; +const + CHUNK_SIZE = 65536 * 2 * 2; +begin + Result := False; + + Frame := GetMem(CHUNK_SIZE); + if Frame = nil then exit; + + Data := nil; + DataLen := 0; + + repeat + Rx := Loader.FillBuffer(Frame, CHUNK_SIZE); + if Rx = 0 then break; + + OldLen := DataLen; + DataLen := DataLen + Rx; + Data := ReAllocMem(Data, DataLen); + if Data = nil then begin FreeMem(Frame); exit; end; + + Move(Frame^, (Data + OldLen)^, Rx); + until Loader.Finished(); + + FreeMem(Frame); + + alGenBuffers(1, Addr(Snd.alBuffer)); + if CheckALError() then + begin + e_LogWritefln('AL: Could not create AL buffer: %s', [GetALError()]); + FreeMem(Data); + exit; + end; + + alBufferData( + Snd.alBuffer, + GetALSoundFormat(Loader.Format), + Data, + DataLen, + Loader.Format.SampleRate + ); + + FreeMem(Data); + + if CheckALError() then + begin + e_LogWriteln('AL: Could not fill AL buffer: ' + GetALError()); + alDeleteBuffers(1, Addr(Snd.alBuffer)); + Snd.alBuffer := 0; + exit; + end; + + Result := True; +end; + function e_LoadSound(FileName: String; var ID: DWORD; isMusic: Boolean; ForceNoLoop: Boolean = False): Boolean; var find_id: DWORD; Loader: TSoundLoader; - OutData: Pointer; - OutLen: LongWord; begin ID := NO_SOUND_ID; Result := False; @@ -290,9 +375,7 @@ begin exit; end; - Loader.Looping := e_SoundsArray[find_id].Loops; - - if not Loader.Load(FileName, e_SoundsArray[find_id].isMusic) then + if not Loader.Load(FileName, e_SoundsArray[find_id].Loops) then begin e_LogWritefln('Could not load sound `%s`', [FileName]); exit; @@ -300,36 +383,13 @@ begin alGetError(); // reset error state, god damn it - if not Loader.Streaming then + if not isMusic then begin - alGenBuffers(1, Addr(e_SoundsArray[find_id].alBuffer)); - if CheckALError() then - begin - e_LogWritefln('Could not create AL buffer for `%s`: %s', [FileName, GetALError()]); - Loader.Free(); - exit; - end; - - OutLen := Loader.GetAll(OutData); - alBufferData( - e_SoundsArray[find_id].alBuffer, - GetALSoundFormat(Loader.Format), - OutData, - OutLen, - Loader.Format.SampleRate - ); - + if not LoadEntireSound(e_SoundsArray[find_id], Loader) then + e_LogWritefln('AL: Could not buffer sound effect `%s`', [FileName]); // don't need this anymore Loader.Free(); Loader := nil; - - if CheckALError() then - begin - e_LogWriteln('AL: what the fuck: ' + GetALError()); - alDeleteBuffers(1, Addr(e_SoundsArray[find_id].alBuffer)); - e_SoundsArray[find_id].alBuffer := 0; - exit; - end; end else begin @@ -345,8 +405,6 @@ function e_LoadSoundMem(pData: Pointer; Length: Integer; var ID: DWORD; isMusic: var find_id: DWORD; Loader: TSoundLoader; - OutData: Pointer; - OutLen: LongWord; begin ID := NO_SOUND_ID; Result := False; @@ -365,9 +423,7 @@ begin exit; end; - Loader.Looping := e_SoundsArray[find_id].Loops; - - if not Loader.Load(pData, LongWord(Length), e_SoundsArray[find_id].isMusic) then + if not Loader.Load(pData, LongWord(Length), e_SoundsArray[find_id].Loops) then begin e_LogWritefln('Could not load sound `%p`', [pData]); exit; @@ -375,36 +431,13 @@ begin alGetError(); // reset error state, god damn it - if not Loader.Streaming then + if not isMusic then begin - alGenBuffers(1, Addr(e_SoundsArray[find_id].alBuffer)); - if CheckALError() then - begin - e_LogWritefln('Could not create AL buffer for `%p`: %s', [pData, GetALError()]); - Loader.Free(); - exit; - end; - - OutLen := Loader.GetAll(OutData); - alBufferData( - e_SoundsArray[find_id].alBuffer, - GetALSoundFormat(Loader.Format), - OutData, - OutLen, - Loader.Format.SampleRate - ); - + if not LoadEntireSound(e_SoundsArray[find_id], Loader) then + e_LogWritefln('AL: Could not buffer sound effect `%p`', [pData]); // don't need this anymore Loader.Free(); Loader := nil; - - if CheckALError() then - begin - e_LogWriteln('AL: what the fuck: ' + GetALError()); - alDeleteBuffers(1, Addr(e_SoundsArray[find_id].alBuffer)); - e_SoundsArray[find_id].alBuffer := 0; - exit; - end; end else begin @@ -463,8 +496,19 @@ begin if e_SoundsArray[ID].Loader <> nil then begin // this is a stream + {$IFNDEF OPENAL_SINGLETHREADED} + // lock the stream so the stream thread doesn't shit itself + EnterCriticalSection(StreamLock); + // number of stereo samples / samplerate = + // time until buffer runs out + StreamBufTime := + (STREAM_BUFSIZE div (2 * e_SoundsArray[ID].Loader.Format.SampleBits div 8)) div + (e_SoundsArray[ID].Loader.Format.SampleRate div 1000) - 1; + if StreamBufTime < 1 then StreamBufTime := 1; + e_LogWritefln('sleep time = %d', [StreamBufTime]); + {$ENDIF} // reset position - e_SoundsArray[ID].Loader.SetPosition(0); + e_SoundsArray[ID].Loader.Restart(); if CurStream <> ID then // changing streams begin alSourceStop(Src); // this should mark all buffers as processed @@ -478,6 +522,10 @@ begin end; // this shit is playing now CurStream := ID; + {$IFNDEF OPENAL_SINGLETHREADED} + // unlock the stream + LeaveCriticalSection(StreamLock); + {$ENDIF} end else begin @@ -629,6 +677,16 @@ end; procedure e_ReleaseSoundSystem(); begin + {$IFNDEF OPENAL_SINGLETHREADED} + if StreamThread <> NilThreadId then + begin + StreamThreadRunning := False; + WaitForThreadTerminate(StreamThread, 66666); + StreamThread := NilThreadId; + DoneCriticalSection(StreamLock); + end; + {$ENDIF} + e_RemoveAllSounds(); alcMakeContextCurrent(nil); @@ -698,8 +756,10 @@ begin alOwners[S] := nil; end; + {$IFDEF OPENAL_SINGLETHREADED} // update the stream sources UpdateStreamSource(MUSIC_SOURCE); + {$ENDIF} end; { TBasicSound: }