diff AudioPlayer.cpp @ 764:90bcfb56c8f9

sound cleaning
author Gloval
date Sat, 23 Mar 2013 21:39:21 +0400
parents a9c1fb7483c2
children ceef50611567
line wrap: on
line diff
--- a/AudioPlayer.cpp	Sat Mar 23 17:07:08 2013 +0400
+++ b/AudioPlayer.cpp	Sat Mar 23 21:39:21 2013 +0400
@@ -78,7 +78,7 @@
 
   pSoundList = this;
   a2 = 1;
-  if ( (signed int)this->uNumSounds > 1 )
+  if ( sNumSounds > 1 )
   {
     v2 = 1;
     //while ( 1 )
@@ -100,7 +100,7 @@
       if ( !pType )
         //goto LABEL_15;
 	  {
-		LOBYTE(pSoundList->pSounds[v2].bDecompressed) = 0;
+		pSoundList->pSounds[v2].bDecompressed = false;
 		goto LABEL_16;
 	  }
       v8 = pType - 1;
@@ -123,23 +123,23 @@
           if ( !AIL_decompress_ADPCM(&pInfo, &pSoundList->pSounds[v2].p3DSound, &v12) )
           {
             pSoundList->pSounds[v2].p3DSound = 0;
-            LOBYTE(pSoundList->pSounds[v2].bDecompressed) = 1;
+            pSoundList->pSounds[v2].bDecompressed = true;
           }
         }
         goto LABEL_16;
       }
 //LABEL_15:
-      LOBYTE(pSoundList->pSounds[v2].bDecompressed) = 0;
+      pSoundList->pSounds[v2].bDecompressed = false;
       goto LABEL_16;
     }
 	pSoundList->pSounds[v2].p3DSound = pSoundList->pSounds[v2].pSoundData[0];
 LABEL_16:
-    pSoundList->_4A9DCD(a2, 1);
+    pSoundList->UnloadSound(a2, 1);
 LABEL_17:
       ++a2;
       ++v2;
 	}
-    while ( a2 < (signed int)pSoundList->uNumSounds );
+    while ( a2 < pSoundList->sNumSounds );
   }
 }
 
@@ -148,12 +148,12 @@
 {
   AILSOUNDINFO v24; // [sp+84h] [bp-28h]@23
 
-  if (bNoSound || !uNumSounds)
+  if (bNoSound || !sNumSounds)
     return 0;
 
   uint       uSoundIdx = 0;
   SoundDesc *pSound = nullptr;
-  for (uint i = 1; i < uNumSounds; ++i)
+  for (uint i = 1; i < sNumSounds; ++i)
     if (pSounds[i].uSoundID == a1)
     {
       uSoundIdx = i;
@@ -202,7 +202,7 @@
         {
           pSound->p3DSound = nullptr;
           pSound->bDecompressed = true;
-          _4A9DCD(uSoundIdx, 0);
+          UnloadSound(uSoundIdx, 0);
         }
       }
       return uSoundIdx;
@@ -240,7 +240,7 @@
   //v8 = this->uNumSounds == 0;
   //v9 = (this->uNumSounds & 0x80000000u) != 0;
   v26 = 0;
-  if (!uNumSounds)
+  if (!sNumSounds)
     return 0;
 
 
@@ -262,7 +262,7 @@
       ++v26;
       v25 += 120;
       v24 += 120;
-      if ( v26 >= (signed int)uNumSounds )
+      if ( v26 >= (signed int)sNumSounds )
         return 0;
     }
     v12 = 0;
@@ -322,7 +322,7 @@
 
   v1 = this;
   v2 = 0;
-  if ( (signed int)this->uNumSounds > 0 )
+  if ( (signed int)this->sNumSounds > 0 )
   {
     v3 = 0;
     do
@@ -339,7 +339,7 @@
       ++v2;
       ++v3;
     }
-    while ( v2 < (signed int)v1->uNumSounds );
+    while ( v2 < (signed int)v1->sNumSounds );
   }
   return result;
 }
@@ -356,7 +356,7 @@
 
   v2 = this;
   v3 = 0;
-  for ( i = 0; i < (signed int)v2->uNumSounds; ++v3 )
+  for ( i = 0; i < (signed int)v2->sNumSounds; ++v3 )
   {
     v5 = &v2->pSounds[v3];
     v6 = v5->eType;
@@ -375,31 +375,22 @@
 }
 
 //----- (004A9DCD) --------------------------------------------------------
-void SoundList::_4A9DCD(unsigned int uSoundID, char a3)
+void SoundList::UnloadSound(unsigned int uSoundID, char a3)
 {
-  unsigned int v3; // esi@1
-  //SoundList *v4; // edi@1
-  SoundDesc *v5; // eax@1
-  void *v6; // ecx@8
-
-  v3 = uSoundID;
-  //v4 = this;
-  v5 = &this->pSounds[uSoundID];
-  if ( v5->eType != SOUND_DESC_SYSTEM )
+  if ( pSounds[uSoundID].eType != SOUND_DESC_SYSTEM )
   {
-    if ( v5->uFlags & SOUND_DESC_SWAP && v5->p3DSound && a3 )
+    if ( (pSounds[uSoundID].uFlags & SOUND_DESC_SWAP) && pSounds[uSoundID].p3DSound && a3 )
     {
-      if ( LOBYTE(v5->bDecompressed) )
-        AIL_mem_free_lock(v5->p3DSound);
-      pSounds[v3].p3DSound = 0;
-      pSounds[v3].uFlags &= ~SOUND_DESC_SYSTEM;
+      if ( pSounds[uSoundID].bDecompressed)
+        AIL_mem_free_lock(pSounds[uSoundID].p3DSound);
+      pSounds[uSoundID].p3DSound = 0;
+      pSounds[uSoundID].uFlags &= ~SOUND_DESC_SYSTEM;
     }
-    v6 = pSounds[v3].pSoundData[0];
-    if ( v6 )
+    if ( pSounds[uSoundID].pSoundData[0] )
     {
-      ReleaseSoundData(v6);
-      pSounds[v3].pSoundData[0] = 0;
-      pSounds[v3].uFlags &= ~SOUND_DESC_SYSTEM;
+      ReleaseSoundData(pSounds[uSoundID].pSoundData[0]);
+      pSounds[uSoundID].pSoundData[0] = NULL;
+      pSounds[uSoundID].uFlags &= ~SOUND_DESC_SYSTEM;
     }
   }
 }
@@ -418,16 +409,16 @@
   if ( !v2 )
     Abortf("Unable to save dsounds.bin!");
   fwrite(v1, 4u, 1u, v2);
-  fwrite(v1->pSounds, 0x78u, v1->uNumSounds, v3);
+  fwrite(v1->pSounds, 0x78u, v1->sNumSounds, v3);
   fclose(v3);
 }
 
 //----- (004A9E89) --------------------------------------------------------
 void *SoundList::FromFile(void *pSerialized)
 {
-  uNumSounds = *(int *)pSerialized;
-  pSounds = (SoundDesc *)pAllocator->AllocNamedChunk(pSounds, 120 * uNumSounds, "Snd Des.");
-  return memcpy(pSounds, (char *)pSerialized + 4, 120 * uNumSounds);
+  sNumSounds = *(int *)pSerialized;
+  pSounds = (SoundDesc *)pAllocator->AllocNamedChunk(pSounds, 120 * sNumSounds, "Snd Des.");
+  return memcpy(pSounds, (char *)pSerialized + 4, 120 * sNumSounds);
 }
 
 //----- (004A9ED0) --------------------------------------------------------
@@ -457,7 +448,7 @@
   pAllocator->FreeChunk(this->pSounds);
   v3 = 0;
   v2->pSounds = 0;
-  v2->uNumSounds = 0;
+  v2->sNumSounds = 0;
   v4 = fopen(Args, "r");
   File = v4;
   if ( !v4 )
@@ -477,14 +468,14 @@
     v5 = Argsa;
     v3 = 0;
   }
-  v2->uNumSounds = v5;
+  v2->sNumSounds = v5;
   v6 = pAllocator->AllocNamedChunk(v2->pSounds, 120 * v5, "Snd Des.");
   v2->pSounds = (SoundDesc *)v6;
   if ( v6 == (void *)v3 )
     Abortf("SoundListClass::load - Out of Memory!");
-  memset(v6, v3, 120 * v2->uNumSounds);
+  memset(v6, v3, 120 * v2->sNumSounds);
   v7 = File;
-  v2->uNumSounds = v3;
+  v2->sNumSounds = v3;
   fseek(v7, v3, v3);
   for ( i = fgets(&Buf, 490, File); i; i = fgets(&Buf, 490, File) )
   {
@@ -492,10 +483,10 @@
     memcpy(&v19, txt_file_frametable_parser(&Buf, &v18), sizeof(v19));
     if ( v19.uPropCount && *v19.pProperties[0] != 47 )
     {
-      sprintf(v2->pSounds[v2->uNumSounds].pSoundName, "%s", v19.pProperties[0]);
+      sprintf(v2->pSounds[v2->sNumSounds].pSoundName, "%s", v19.pProperties[0]);
       v9 = atoi(v19.pProperties[1]);
       v10 = v19.pProperties[2];
-      v2->pSounds[v2->uNumSounds].uSoundID = v9;
+      v2->pSounds[v2->sNumSounds].uSoundID = v9;
       if ( _strcmpi(v10, "system") )
       {
         if ( _strcmpi(v19.pProperties[2], "swap") )
@@ -503,7 +494,7 @@
           v11 = _strcmpi(v19.pProperties[2], "lock");
           v12 = v2->pSounds;
           v13 = v11 == 0;
-          v14 = v2->uNumSounds;
+          v14 = v2->sNumSounds;
           if ( v13 )
             v12[v14].eType = SOUND_DESC_LOCK;
           else
@@ -511,19 +502,19 @@
         }
         else
         {
-          v2->pSounds[v2->uNumSounds].eType = SOUND_DESC_SWAP;
+          v2->pSounds[v2->sNumSounds].eType = SOUND_DESC_SWAP;
         }
       }
       else
       {
-        v2->pSounds[v2->uNumSounds].eType = SOUND_DESC_SYSTEM;
+        v2->pSounds[v2->sNumSounds].eType = SOUND_DESC_SYSTEM;
       }
       if ( v19.uPropCount >= 4 && !_strcmpi(v19.pProperties[3], "3D") )
       {
-        v15 = (int)&v2->pSounds[v2->uNumSounds].uFlags;
+        v15 = (int)&v2->pSounds[v2->sNumSounds].uFlags;
         *(int *)v15 |= SOUND_DESC_SWAP;
       }
-      ++v2->uNumSounds;
+      ++v2->sNumSounds;
     }
   }
   fclose(File);
@@ -610,7 +601,7 @@
                 if ( AIL_sample_status(v6->hSample) == AIL::Sample::Playing)
                 {
                   AIL_end_sample(v6->hSample);
-                  _4ABE55(v6);
+                  FreeChannel(v6);
                 }
               }
               ++v5;
@@ -740,7 +731,7 @@
 
 
   int sound_id = 0;
-  for (uint i = 0; i < pSoundList->uNumSounds; ++i)
+  for (uint i = 0; i < pSoundList->sNumSounds; ++i)
     if (pSoundList->pSounds[i].uSoundID == eSoundID)
     {
       sound_id = i;
@@ -756,7 +747,7 @@
   int start_channel = 0,
       end_channel = 0;
 
-  assert(sound_id < pSoundList->uNumSounds);
+  assert(sound_id < pSoundList->sNumSounds);
   auto sound_desc = pSoundList->pSounds + sound_id;
   if (!b3DSoundInitialized || sound_desc->Is3D())
   {
@@ -869,7 +860,7 @@
           return;                          // already playing the same sound from the same source - return
 
         AIL_end_sample(channel->hSample);  // requested new sound from the same source - end & switch
-        _4ABE55(channel);
+        FreeChannel(channel);
       }
     }
 
@@ -883,7 +874,7 @@
         {
           AIL_end_sample(channel->hSample);
           if (channel->uSourceTrackIdx)
-            _4ABE55(channel);
+            FreeChannel(channel);
           break;
         }
       }
@@ -947,7 +938,7 @@
 
         auto channel = &pMixerChannels[v62];
         AIL_end_sample(channel->hSample);
-        _4ABE55(channel);
+        FreeChannel(channel);
       }
 
       if (v62 > end_channel)//10!=13
@@ -1001,7 +992,7 @@
         if (!GetSoundStrengthByDistanceFromParty(door->pXOffsets[0], door->pYOffsets[0], door->pZOffsets[0]))
         {
           AIL_end_sample(channel->hSample);
-          _4ABE55(channel);
+          FreeChannel(channel);
           return;
         }
         AIL_set_sample_pan(channel->hSample, sub_4AB66C(door->pXOffsets[0], door->pYOffsets[0]));
@@ -1622,7 +1613,7 @@
     if (AIL_sample_status(channel->hSample) == AIL::Sample::Done)
     {
       AIL_end_sample(channel->hSample);
-      _4ABE55(channel);
+      FreeChannel(channel);
     }
   }
 
@@ -1706,7 +1697,7 @@
     else
     {
       AIL_end_sample(channel->hSample);
-      _4ABE55(channel);
+      FreeChannel(channel);
     }
   }
 
@@ -1882,7 +1873,7 @@
           && pSoundList->pSounds[pChannel->uSourceTrackIdx].eType != SOUND_DESC_SYSTEM)
         {
           AIL_end_sample(pChannel->hSample);
-          _4ABE55(pChannel);
+          FreeChannel(pChannel);
           pChannel->source_pid = 0;
         }
         ++v6;
@@ -2205,7 +2196,7 @@
 }
 
 //----- (004ABE55) --------------------------------------------------------
-void AudioPlayer::_4ABE55(MixerChannel *pChannel)
+void AudioPlayer::FreeChannel(MixerChannel *pChannel)
 {
   int v2; // ebx@1
   //AudioPlayer *v3; // esi@1
@@ -2213,14 +2204,14 @@
   unsigned __int8 v5; // zf@5
   unsigned __int8 v6; // sf@5
   char *v7; // edi@6
-  int v8; // eax@8
+  int num_same_sound_on_channels; // eax@8
   int v9; // ST04_4@8
   int v10; // ecx@12
   int v11; // edi@13
   int v12; // eax@13
   unsigned __int8 v13; // of@13
   int v14[16]; // [sp+Ch] [bp-48h]@8
-  int v15; // [sp+4Ch] [bp-8h]@5
+  int num_playing_channels; // [sp+4Ch] [bp-8h]@5
   int v16; // [sp+50h] [bp-4h]@5
 
   v2 = 0;
@@ -2229,49 +2220,41 @@
     return;
  
     v4 = &pSoundList->pSounds[pChannel->uSourceTrackIdx];
-    if ( v4->eType == SOUND_DESC_SWAP)
+    if ( pSoundList->pSounds[pChannel->uSourceTrackIdx].eType == SOUND_DESC_SWAP)
     {
-      if ( v4->pSoundData[0] && !(v4->uFlags & SOUND_DESC_SYSTEM) )
+      if ( pSoundList->pSounds[pChannel->uSourceTrackIdx].pSoundData[0] && 
+          !(pSoundList->pSounds[pChannel->uSourceTrackIdx].uFlags & SOUND_DESC_SYSTEM) )
       {
-        v5 = this->uMixerChannels == 0;
-        v6 = this->uMixerChannels < 0;
-        v15 = 0;
-        v16 = 0;
-        if ( v6 | v5 )
+        num_playing_channels = 0;
+        num_same_sound_on_channels = 0;
+        if ( this->uMixerChannels <=0 )
           goto LABEL_16;
-        v7 = (char *)this->pMixerChannels;
         do
         {
-          if ( pChannel->uSourceTrackID == *((int *)v7 + 3) )
+          if ( pChannel->uSourceTrackID ==pMixerChannels[v2].uSourceTrackID )
           {
-            v8 = v16;
-            v9 = *(int *)v7;
-            ++v16;
-            v14[v8] = v2;
-            if ( AIL_sample_status((HSAMPLE)v9) == AIL::Sample::Playing)
-              ++v15;
+            v14[num_same_sound_on_channels++] = v2;
+            if ( AIL_sample_status((HSAMPLE)pMixerChannels[v2].hSample) == AIL::Sample::Playing)
+              ++num_playing_channels;
           }
           ++v2;
-          v7 += 16;
         }
         while ( v2 < uMixerChannels );
-        if ( !v15 )
+        if ( !num_playing_channels )
         {
 LABEL_16:
-          pSoundList->_4A9DCD(pChannel->uSourceTrackIdx, 1);
+          pSoundList->UnloadSound(pChannel->uSourceTrackIdx, 1);
           v10 = 0;
-          if ( v16 > 0 )
+          if ( num_same_sound_on_channels  > 0 )
           {
             do
             {
               v11 = v14[v10];
-              v12 = 16 * (v14[v10++] + 47);
+              v10++;
               pMixerChannels[v11].uSourceTrackID = 0;
-              v13 = __OFSUB__(v10, v16);
-              v6 = v10 - v16 < 0;
-              *(unsigned int *)((char *)&bEAXSupported + v12) = 0;
+               pMixerChannels[v11].hSample = NULL;
             }
-            while ( v6 ^ v13 );
+            while (v10<num_same_sound_on_channels);
           }
         }
       }
@@ -2398,7 +2381,7 @@
         if ( !v14 )
         {
 LABEL_16:
-          pSoundList->_4A9DCD(a2->field_8, 1);
+          pSoundList->UnloadSound(a2->field_8, 1);
           v10 = 0;
           if ( v15 > 0 )
           {
@@ -2675,9 +2658,10 @@
   v3 = pOutBuff;
   v4 = 0;
   for (uint i = 0; i < 3000; ++i)
-    if (pSounds[i].uID == uID)
-      return pSounds[i].pSoundData;
-
+      {
+      if (pSounds[i].uID == uID)
+          return pSounds[i].pSoundData;
+      }
   FindSound_BinSearch(0, pAudioPlayer->uNumSoundHeaders, pSoundName_);
   if ( uFindSound_BinSearch_ResultID == -1 )
   {