modify ReleaseSound so that it performs a partial release of unused sounds, useful for responding to memory warnings
authorkoda
Sun, 13 Nov 2011 19:32:42 +0100
changeset 6362 ceacd1b61833
parent 6361 bc157c3dac43
child 6363 d812246272e8
modify ReleaseSound so that it performs a partial release of unused sounds, useful for responding to memory warnings
hedgewars/PascalExports.pas
hedgewars/SDLh.pas
hedgewars/uSound.pas
project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m
project_files/HedgewarsMobile/Classes/PascalImports.h
--- a/hedgewars/PascalExports.pas	Sun Nov 13 13:27:04 2011 -0500
+++ b/hedgewars/PascalExports.pas	Sun Nov 13 19:32:42 2011 +0100
@@ -355,6 +355,12 @@
 begin
     exit(cMaxTeams);
 end;
+
+procedure HW_memoryWarningCallback; cdecl; export;
+begin
+    ReleaseSound(false);
+end;
+
 {$ENDIF}
 
 end.
--- a/hedgewars/SDLh.pas	Sun Nov 13 13:27:04 2011 -0500
+++ b/hedgewars/SDLh.pas	Sun Nov 13 19:32:42 2011 +0100
@@ -956,6 +956,7 @@
 function  Mix_PauseMusic(music: PMixMusic): LongInt; cdecl; external SDL_MixerLibName;
 function  Mix_ResumeMusic(music: PMixMusic): LongInt; cdecl; external SDL_MixerLibName;
 function  Mix_HaltChannel(channel: LongInt): LongInt; cdecl; external SDL_MixerLibName;
+function  Mix_HaltMusic: LongInt; cdecl; external SDL_MixerLibName;
 
 function  Mix_FadeInChannelTimed(channel: LongInt; chunk: PMixChunk; loops: LongInt; fadems: LongInt; ticks: LongInt): LongInt; cdecl; external SDL_MixerLibName;
 function  Mix_FadeOutChannel(channel: LongInt; fadems: LongInt): LongInt; cdecl; external SDL_MixerLibName;
--- a/hedgewars/uSound.pas	Sun Nov 13 13:27:04 2011 -0500
+++ b/hedgewars/uSound.pas	Sun Nov 13 19:32:42 2011 +0100
@@ -41,7 +41,7 @@
 procedure freeModule;
 
 procedure InitSound; // Initiates sound-system if isSoundEnabled.
-procedure ReleaseSound; // Releases sound-system and used resources.
+procedure ReleaseSound(complete: boolean); // Releases sound-system and used resources.
 procedure SoundLoad; // Preloads some sounds for performance reasons.
 
 
@@ -176,24 +176,41 @@
     ChangeVolume(cInitVolume)
 end;
 
-procedure ReleaseSound;
+// when complete is false, this procedure just releases some of the chucks on inactive channels
+// this way music is not stopped, nor are chucks currently being plauyed
+procedure ReleaseSound(complete: boolean);
 var i: TSound;
     t: Longword;
 begin
+    // release and nil all sounds
     for t:= 0 to cMaxTeams do
         if voicepacks[t].name <> '' then
             for i:= Low(TSound) to High(TSound) do
                 if voicepacks[t].chunks[i] <> nil then
-                    Mix_FreeChunk(voicepacks[t].chunks[i]);
-
-    if Mus <> nil then
-        Mix_FreeMusic(Mus);
+                    if complete or (Mix_Playing(lastChan[i]) = 0) then
+                        begin
+                        Mix_HaltChannel(lastChan[i]);
+                        lastChan[i]:= -1;
+                        Mix_FreeChunk(voicepacks[t].chunks[i]);
+                        voicepacks[t].chunks[i]:= nil;
+                        end;
 
-    // make sure all instances of sdl_mixer are unloaded before continuing
-    while Mix_Init(0) <> 0 do
-        Mix_Quit();
+    // stop music
+    if complete then
+        begin
+        if Mus <> nil then
+            begin
+            Mix_HaltMusic();
+            Mix_FreeMusic(Mus);
+            Mus:= nil;
+            end;
 
-    Mix_CloseAudio();
+        // make sure all instances of sdl_mixer are unloaded before continuing
+        while Mix_Init(0) <> 0 do
+            Mix_Quit();
+
+        Mix_CloseAudio();
+        end;
 end;
 
 procedure SoundLoad;
@@ -205,15 +222,16 @@
 
     defVoicepack:= AskForVoicepack('Default');
 
+    // initialize all voices to nil so that they can be loaded when needed
     for t:= 0 to cMaxTeams do
         if voicepacks[t].name <> '' then
             for i:= Low(TSound) to High(TSound) do
                 voicepacks[t].chunks[i]:= nil;
 
+    // preload all the big sound files (>32k) that would otherwise lockup the game
     for i:= Low(TSound) to High(TSound) do
     begin
         defVoicepack^.chunks[i]:= nil;
-        // preload all the big sound files (>32k) that would otherwise lockup the game
         if (i in [sndBeeWater, sndBee, sndCake, sndHellishImpact1, sndHellish, sndHomerun,
                   sndMolotov, sndMortar, sndRideOfTheValkyries, sndYoohoo])
             and (Soundz[i].Path <> ptVoices) and (Soundz[i].FileName <> '') then
@@ -480,7 +498,7 @@
 procedure freeModule;
 begin
     if isSoundEnabled then
-        ReleaseSound();
+        ReleaseSound(true);
 end;
 
 end.
--- a/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m	Sun Nov 13 13:27:04 2011 -0500
+++ b/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m	Sun Nov 13 19:32:42 2011 +0100
@@ -88,6 +88,7 @@
     // don't stop music if it is playing
     if ([HWUtils isGameLaunched]) {
         [AudioManagerController releaseCache];
+        HW_memoryWarningCallback();
     }
     MSG_MEMCLEAN();
     // don't clean mainMenuViewController here!!!
--- a/project_files/HedgewarsMobile/Classes/PascalImports.h	Sun Nov 13 13:27:04 2011 -0500
+++ b/project_files/HedgewarsMobile/Classes/PascalImports.h	Sun Nov 13 19:32:42 2011 +0100
@@ -98,6 +98,8 @@
     int  HW_getTurnsForCurrentTeam(void);
     int  HW_getMaxNumberOfHogs(void);
     int  HW_getMaxNumberOfTeams(void);
+
+    void HW_memoryWarningCallback(void);
     
 #ifdef __cplusplus
 }