DEADSOFTWARE

Sound: OpenAL: Reclaim stream buffers on stream change
[d2df-sdl.git] / src / engine / e_sound_al.inc
index f6ec993523682ae056ccc574115d1c0ad403cdb8..b73b37f760bdc00f7ea75883425a92b4360ee711 100644 (file)
@@ -288,6 +288,8 @@ begin
     exit;
   end;
 
+  Loader.Looping := e_SoundsArray[find_id].Loops;
+
   if not Loader.Load(FileName, e_SoundsArray[find_id].isMusic) then
   begin
     e_LogWritefln('Could not load sound `%s`', [FileName]);
@@ -329,7 +331,6 @@ begin
   end
   else
   begin
-    Loader.Looping := e_SoundsArray[find_id].Loops;
     e_SoundsArray[find_id].alBuffer := 0;
     e_SoundsArray[find_id].Loader := Loader;
   end;
@@ -362,6 +363,8 @@ begin
     exit;
   end;
 
+  Loader.Looping := e_SoundsArray[find_id].Loops;
+
   if not Loader.Load(pData, LongWord(Length), e_SoundsArray[find_id].isMusic) then
   begin
     e_LogWritefln('Could not load sound `%p`', [pData]);
@@ -403,7 +406,6 @@ begin
   end
   else
   begin
-    Loader.Looping := e_SoundsArray[find_id].Loops;
     e_SoundsArray[find_id].alBuffer := 0;
     e_SoundsArray[find_id].Loader := Loader;
   end;
@@ -449,6 +451,8 @@ begin
 end;
 
 procedure AssignSound(ID: DWORD; Src: ALuint); inline;
+var
+  S: ALint;
 begin
   alGetError(); // reset error state
 
@@ -457,8 +461,17 @@ begin
     // this is a stream
     // reset position
     e_SoundsArray[ID].Loader.SetPosition(0);
-    if CurStream <> ID then // changing streams, stop the thing just in case
-      alSourceStop(Src);
+    if CurStream <> ID then // changing streams
+    begin
+      alSourceStop(Src); // this should mark all buffers as processed
+      alGetSourcei(Src, AL_BUFFERS_PROCESSED, S);
+      // unqueue all buffers
+      if S > 0 then
+      begin
+        alSourceUnqueueBuffers(Src, S, @alStreamBufs[alStreamAvail]);
+        alStreamAvail := NUM_STREAM_BUFFERS;
+      end;
+    end;
     // this shit is playing now
     CurStream := ID;
   end