view AudioPlayer.cpp @ 528:b1598f110b07

Chest working
author Gloval
date Sun, 03 Mar 2013 13:36:41 +0400
parents cb0ad52d6a26
children 96f48cfdd657
line wrap: on
line source

#include <string>
#include <assert.h>

#include "AudioPlayer.h"
#include "Allocator.h"
#include "FrameTableInc.h"
#include "Indoor.h"
#include "SpriteObject.h"
#include "Party.h"
#include "Actor.h"
#include "Game.h"
#include "DecorationList.h"
#include "Time.h"
#include "OSInfo.h"
#include "Math.h"
#include "MapInfo.h"
#include "Actor.h"
#include "GUIWindow.h"
#include "Log.h"

#include "Bink_Smacker.h"

#include "mm7_data.h"



PCMWAVEFORMAT pcmWaveFormat;

int Aureal3D_SplashScreen;
int Aureal3D_SplashAudio;
int uFindSound_BinSearch_ResultID; // weak
int uLastLoadedSoundID; // weak
int sLastTrackLengthMS;
Sound pSounds[3000];
AudioPlayer *pAudioPlayer;
SoundList *pSoundList;


stru339_spell_sound stru_A750F8[4];
stru339_spell_sound stru_AA1058[4];



unsigned __int8 uSoundVolumeMultiplier = 4;
unsigned __int8 uVoicesVolumeMultiplier = 4;
unsigned __int8 uMusicVolimeMultiplier = 4;
int bWalkSound; // idb

float pSoundVolumeLevels[10] = 
{
  0.0000000f, 0.1099999f, 0.2199999f, 0.3300000f, 0.4399999f,
 0.5500000f, 0.6600000f, 0.7699999f, 0.8799999f, 0.9900000f
};




void ReleaseSoundData(void *_this);




//----- (004A9953) --------------------------------------------------------
void SoundList::Initialize()
{
  SoundList *pSoundList; // esi@1
  signed int v2; // edi@2
  SoundDesc *pSoundDesc; // eax@5
  void *pSoundData; // ebx@7
  unsigned int uSoundSize; // eax@7
  char *pSoundBytes; // ebx@7
  AILFILETYPE pType; // eax@7
  int v8; // eax@8
  char pSoundName[120]; // [sp+4h] [bp-A4h]@4
  AILSOUNDINFO pInfo; // [sp+7Ch] [bp-2Ch]@10
  int v12; // [sp+A0h] [bp-8h]@12
  int a2; // [sp+A4h] [bp-4h]@1

  pSoundList = this;
  a2 = 1;
  if ( (signed int)this->uNumSounds > 1 )
  {
    v2 = 1;
    //while ( 1 )
	do
    {
	  sprintf(pSoundName, "%s", pSounds[v2].pSoundName);
	 // pSoundList->pSounds[v2].pSoundData[0] = ::LoadSound(pSoundName, (SoundData *)0xFFFFFFFF, pSounds[v2].uSoundID); //Ritor1: it's error - result: no sound
	  pSoundDesc = &pSoundList->pSounds[v2];
      if ( pSoundList->pSounds[v2].eType != SOUND_DESC_SYSTEM || (sprintf(pSoundName, "%s", pSounds[v2].pSoundName),
            pSoundList->pSounds[v2].pSoundData[0] = ::LoadSound(pSoundName, (SoundData *)0xFFFFFFFF, pSounds[v2].uSoundID),
            !pAudioPlayer->b3DSoundInitialized)
        || (pSoundDesc = &pSoundList->pSounds[v2], !(pSoundDesc->uFlags & 2))
        || !pSoundDesc->pSoundData[0] ) // нужно перевернуть
        goto LABEL_17;
      pSoundData = pSoundDesc->pSoundData[0];
      uSoundSize = *(int *)pSoundData;
      pSoundBytes = (char *)pSoundData + 4;
      pType = AIL_file_type(pSoundBytes, uSoundSize);
      if ( !pType )
        //goto LABEL_15;
	  {
		LOBYTE(pSoundList->pSounds[v2].bDecompressed) = 0;
		goto LABEL_16;
	  }
      v8 = pType - 1;
      if ( v8 )
	  {
        /*break;
      pSoundList->pSounds[v2].p3DSound = pSoundList->pSounds[v2].pSoundData[0];
LABEL_16:
      pSoundList->_4A9DCD(a2, 1);
LABEL_17:
      ++a2;
      ++v2;
      if ( a2 >= (signed int)pSoundList->uNumSounds )
        return;
    }*/
      if ( v8 == 1 )
      {
        if ( AIL_WAV_info(pSoundBytes, &pInfo) && pInfo.uChannels != 2 )
        {
          if ( !AIL_decompress_ADPCM(&pInfo, &pSoundList->pSounds[v2].p3DSound, &v12) )
          {
            pSoundList->pSounds[v2].p3DSound = 0;
            LOBYTE(pSoundList->pSounds[v2].bDecompressed) = 1;
          }
        }
        goto LABEL_16;
      }
//LABEL_15:
      LOBYTE(pSoundList->pSounds[v2].bDecompressed) = 0;
      goto LABEL_16;
    }
	pSoundList->pSounds[v2].p3DSound = pSoundList->pSounds[v2].pSoundData[0];
LABEL_16:
    pSoundList->_4A9DCD(a2, 1);
LABEL_17:
      ++a2;
      ++v2;
	}
    while ( a2 < (signed int)pSoundList->uNumSounds );
  }
}

//----- (004A9A67) --------------------------------------------------------
__int16 SoundList::LoadSound(int a1, unsigned int a3)
{
  AILSOUNDINFO v24; // [sp+84h] [bp-28h]@23

  if (bNoSound || !uNumSounds)
    return 0;

  uint       uSoundIdx = 0;
  SoundDesc *pSound = nullptr;
  for (uint i = 1; i < uNumSounds; ++i)
    if (pSounds[i].uSoundID == a1)
    {
      uSoundIdx = i;
      pSound = &pSounds[i];
      break;
    }
  if (!pSound)
    return 0;

  if (pSound->uFlags & 2 && pSound->p3DSound ||
      ~pSound->uFlags & 2 && pSound->pSoundData[0])
    return uSoundIdx;

  if (!pSound->pSoundData[0])
    pSound->pSoundData[0] = ::LoadSound(pSound->pSoundName, (SoundData *)0xFFFFFFFF, pSound->uSoundID);

  if (!pSound->pSoundData[0])
    return 0;

  if (a3)
    pSound->uFlags |= 1u;

  if (!pAudioPlayer->b3DSoundInitialized)
    return uSoundIdx;

  if (~pSound->uFlags & 2 || !pSound->pSoundData[0])
    return uSoundIdx;


  auto pSoundData = pSound->pSoundData[0];
  switch (AIL_file_type((void *)pSoundData->pData, pSoundData->uDataSize))
  {
    default:
    case AILFILETYPE_UNKNOWN:
      pSound->bDecompressed = false;
      return 0;

    case AILFILETYPE_PCM_WAV:
      pSound->p3DSound = pSound->pSoundData[0];
      return uSoundIdx;

    case AILFILETYPE_ADPCM_WAV:
      if (AIL_WAV_info((void *)pSoundData->pData, &v24) && v24.uChannels != 2)
      {
        if (!AIL_decompress_ADPCM(&v24, &pSound->p3DSound, &a1) )
        {
          pSound->p3DSound = nullptr;
          pSound->bDecompressed = true;
          _4A9DCD(uSoundIdx, 0);
        }
      }
      return uSoundIdx;
  };
}

//----- (004A9BBD) --------------------------------------------------------
int SoundList::LoadSound(unsigned int a2, LPVOID lpBuffer, int uBufferSizeLeft, int *pOutSoundSize, int a6)
{
  //SoundList *v6; // edi@1
  signed int v7; // esi@1
  //unsigned __int8 v8; // zf@1
  //unsigned __int8 v9; // sf@1
  SoundDesc *v10; // eax@3
  SoundDesc *v11; // ecx@3
  int v12; // ebx@7
  int result; // eax@13
  SoundHeader *v14; // esi@16
  DWORD *v15; // edi@16
  int v16; // ebx@16
  unsigned int v17; // eax@18
  void *v18; // ebx@19
  std::string v19; // [sp-18h] [bp-38h]@22
  const char *v20; // [sp-8h] [bp-28h]@22
  int v21; // [sp-4h] [bp-24h]@22
  //SoundList *v22; // [sp+Ch] [bp-14h]@1
  SoundDesc *Args; // [sp+10h] [bp-10h]@3
  unsigned int v24; // [sp+14h] [bp-Ch]@2
  int v25; // [sp+18h] [bp-8h]@2
  int v26; // [sp+1Ch] [bp-4h]@1

  //v6 = this;
  v7 = 0;
  //v22 = this;
  //v8 = this->uNumSounds == 0;
  //v9 = (this->uNumSounds & 0x80000000u) != 0;
  v26 = 0;
  if (!uNumSounds)
    return 0;


    v24 = 0;
    v25 = 44;
    while ( 1 )
    {
      v10 = pSounds;
      v11 = &v10[v24 / 0x78];
      Args = &v10[v24 / 0x78];
      if ( a2 == v10[v24 / 0x78].uSoundID )
      {
        if ( a6 == v7 && *(int *)&v10->pSoundName[v25] != v7 )
          return v26;
        if ( (signed int)pAudioPlayer->uNumSoundHeaders > v7 )
          break;
      }
LABEL_12:
      ++v26;
      v25 += 120;
      v24 += 120;
      if ( v26 >= (signed int)uNumSounds )
        return 0;
    }
    v12 = 0;
    while ( _strcmpi(pAudioPlayer->pSoundHeaders[v12].pSoundName, v11->pSoundName) )
    {
      ++v7;
      ++v12;
      if ( v7 >= (signed int)pAudioPlayer->uNumSoundHeaders )
      {
        v7 = 0;
        goto LABEL_12;
      }
      v11 = Args;
    }
    v14 = &pAudioPlayer->pSoundHeaders[v7];
    v15 = (DWORD *)&v14->uDecompressedSize;
    a2 = v14->uDecompressedSize;
    v16 = a2;
    if ( (signed int)a2 > uBufferSizeLeft )
      Abortf("Sound %s is size %i bytes, sound buffer size is %i bytes", Args, a2, uBufferSizeLeft);
    SetFilePointer(pAudioPlayer->hAudioSnd, v14->uFileOffset, 0, 0);
    v17 = v14->uCompressedSize;
    if ( (signed int)v17 >= (signed int)*v15 )
    {
      if ( v17 == *v15 )
      {
        ReadFile(pAudioPlayer->hAudioSnd, lpBuffer, *v15, (LPDWORD)&Args, 0);
      }
      else
      {
      MessageBoxW(nullptr, L"Can't load sound file!", L"E:\\WORK\\MSDEV\\MM7\\MM7\\Code\\Sound.cpp:666", 0);
      }
    }
    else
    {
      v18 = malloc(v14->uCompressedSize);
      ReadFile(pAudioPlayer->hAudioSnd, v18, v14->uCompressedSize, (LPDWORD)&Args, 0);
      zlib::MemUnzip(lpBuffer, &v14->uDecompressedSize, v18, v14->uCompressedSize);
      free(v18);
      v16 = a2;
    }
    result = v26;
    pSounds->pSoundData[a6 + 30 * v26] = (SoundData *)lpBuffer;
    *pOutSoundSize = v16;

  return result;
}

//----- (004A9D3E) --------------------------------------------------------
SoundDesc *SoundList::Release()
{
  SoundList *v1; // esi@1
  signed int v2; // ebx@1
  int v3; // edi@2
  SoundDesc *result; // eax@3
  void *v5; // ecx@3

  v1 = this;
  v2 = 0;
  if ( (signed int)this->uNumSounds > 0 )
  {
    v3 = 0;
    do
    {
      result = v1->pSounds;
      v5 = result[v3].pSoundData[0];
      if ( v5 )
      {
        ReleaseSoundData(v5);
        v1->pSounds[v3].pSoundData[0] = 0;
        result = (SoundDesc *)((char *)&v1->pSounds[v3] + 40);
        *(int *)&result->pSoundName[0] &= 0xFFFFFFFEu;
      }
      ++v2;
      ++v3;
    }
    while ( v2 < (signed int)v1->uNumSounds );
  }
  return result;
}

//----- (004A9D79) --------------------------------------------------------
void SoundList::_4A9D79(int a2)
{
  SoundList *v2; // esi@1
  int v3; // edi@1
  signed int i; // ebx@1
  SoundDesc *v5; // eax@2
  SOUND_DESC_TYPE v6; // ecx@2
  void *v7; // eax@5

  v2 = this;
  v3 = 0;
  for ( i = 0; i < (signed int)v2->uNumSounds; ++v3 )
  {
    v5 = &v2->pSounds[v3];
    v6 = v5->eType;
    if ( v6 != SOUND_DESC_SYSTEM && (a2 || v6 != SOUND_DESC_LOCK) )
    {
      v7 = v5->pSoundData[0];
      if ( v7 )
      {
        ReleaseSoundData(v7);
        v2->pSounds[v3].pSoundData[0] = 0;
      }
      v2->pSounds[v3].uFlags &= 0xFFFFFFFEu;
    }
    ++i;
  }
}

//----- (004A9DCD) --------------------------------------------------------
void SoundList::_4A9DCD(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 ( v5->uFlags & 2 && v5->p3DSound && a3 )
    {
      if ( LOBYTE(v5->bDecompressed) )
        AIL_mem_free_lock(v5->p3DSound);
      pSounds[v3].p3DSound = 0;
      pSounds[v3].uFlags &= 0xFFFFFFFEu;
    }
    v6 = pSounds[v3].pSoundData[0];
    if ( v6 )
    {
      ReleaseSoundData(v6);
      pSounds[v3].pSoundData[0] = 0;
      pSounds[v3].uFlags &= 0xFFFFFFFEu;
    }
  }
}


//----- (004A9E3D) --------------------------------------------------------
void SoundList::ToFile()
{
  SoundList *v1; // esi@1
  FILE *v2; // eax@1
  FILE *v3; // edi@1

  v1 = this;
  v2 = fopen("data\\dsounds.bin", "wb");
  v3 = v2;
  if ( !v2 )
    Abortf("Unable to save dsounds.bin!");
  fwrite(v1, 4u, 1u, v2);
  fwrite(v1->pSounds, 0x78u, v1->uNumSounds, 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);
}

//----- (004A9ED0) --------------------------------------------------------
int SoundList::FromFileTxt(const char *Args)
{
  SoundList *v2; // ebx@1
  __int32 v3; // edi@1
  FILE *v4; // eax@1
  unsigned int v5; // esi@3
  void *v6; // eax@9
  FILE *v7; // ST0C_4@11
  char *i; // eax@11
  int v9; // eax@14
  const char *v10; // ST0C_4@14
  int v11; // eax@18
  SoundDesc *v12; // ecx@18
  char v13; // zf@18
  unsigned int v14; // eax@18
  int v15; // eax@23
  char Buf; // [sp+Ch] [bp-2F0h]@3
  FrameTableTxtLine v18; // [sp+200h] [bp-FCh]@4
  FrameTableTxtLine v19; // [sp+27Ch] [bp-80h]@4
  FILE *File; // [sp+2F8h] [bp-4h]@1
  unsigned int Argsa; // [sp+304h] [bp+8h]@3

  v2 = this;
  pAllocator->FreeChunk(this->pSounds);
  v3 = 0;
  v2->pSounds = 0;
  v2->uNumSounds = 0;
  v4 = fopen(Args, "r");
  File = v4;
  if ( !v4 )
    Abortf("SoundListClass::load - Unable to open file: %s.");
  v5 = 0;
  Argsa = 0;
  if ( fgets(&Buf, 490, v4) )
  {
    do
    {
      *strchr(&Buf, 10) = 0;
      memcpy(&v19, texture_frame_table_txt_parser(&Buf, &v18), sizeof(v19));
      if ( v19.field_0 && *v19.pProperties[0] != 47 )
        ++Argsa;
    }
    while ( fgets(&Buf, 490, File) );
    v5 = Argsa;
    v3 = 0;
  }
  v2->uNumSounds = 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);
  v7 = File;
  v2->uNumSounds = v3;
  fseek(v7, v3, v3);
  for ( i = fgets(&Buf, 490, File); i; i = fgets(&Buf, 490, File) )
  {
    *strchr(&Buf, 10) = 0;
    memcpy(&v19, texture_frame_table_txt_parser(&Buf, &v18), sizeof(v19));
    if ( v19.field_0 && *v19.pProperties[0] != 47 )
    {
      sprintf(v2->pSounds[v2->uNumSounds].pSoundName, "%s", v19.pProperties[0]);
      v9 = atoi(v19.pProperties[1]);
      v10 = v19.pProperties[2];
      v2->pSounds[v2->uNumSounds].uSoundID = v9;
      if ( _strcmpi(v10, "system") )
      {
        if ( _strcmpi(v19.pProperties[2], "swap") )
        {
          v11 = _strcmpi(v19.pProperties[2], "lock");
          v12 = v2->pSounds;
          v13 = v11 == 0;
          v14 = v2->uNumSounds;
          if ( v13 )
            v12[v14].eType = SOUND_DESC_LOCK;
          else
            v12[v14].eType = SOUND_DESC_LEVEL;
        }
        else
        {
          v2->pSounds[v2->uNumSounds].eType = SOUND_DESC_SWAP;
        }
      }
      else
      {
        v2->pSounds[v2->uNumSounds].eType = SOUND_DESC_SYSTEM;
      }
      if ( v19.field_0 >= 4 && !_strcmpi(v19.pProperties[3], "3D") )
      {
        v15 = (int)&v2->pSounds[v2->uNumSounds].uFlags;
        *(int *)v15 |= 2u;
      }
      ++v2->uNumSounds;
    }
  }
  fclose(File);
  return 1;
}

//----- (004AA13F) --------------------------------------------------------
void AudioPlayer::PlayMusicTrack(MusicID eTrack)
{
  if (!bNoSound && bPlayerReady && hAILRedbook && uMusicVolimeMultiplier)
  {
    AIL_redbook_stop(hAILRedbook);
    AIL_redbook_set_volume(hAILRedbook, (signed)pSoundVolumeLevels[uMusicVolimeMultiplier] * 64.0f);
    AIL_redbook_track_info(hAILRedbook, eTrack, &uCurrentMusicTrackStartMS, &uCurrentMusicTrackEndMS);
    AIL_redbook_play(hAILRedbook, uCurrentMusicTrackStartMS + 1, uCurrentMusicTrackEndMS);
    uCurrentMusicTrackLength = ((uCurrentMusicTrackEndMS - uCurrentMusicTrackStartMS) * 128) / 1000;
  }
}


//----- (004AA1F3) --------------------------------------------------------
void AudioPlayer::SetMusicVolume(int vol)
{
  if (bPlayerReady)
  {
    if (hAILRedbook)
      AIL_redbook_set_volume(hAILRedbook, vol);
  }
}

//----- (004AA214) --------------------------------------------------------
void AudioPlayer::SetMasterVolume(float fVolume)
{
  if ( bPlayerReady )
  {
    uMasterVolume = fVolume;
    if ( hDigDriver )
      AIL_set_digital_master_volume(hDigDriver, fVolume);
    if ( b3DSoundInitialized )
      s3DSoundVolume = fVolume * 0.5f;
  }
}
// 4D8304: using guessed type int __stdcall AIL_set_digital_master_volume(int, int);

//----- (004AA258) --------------------------------------------------------
void AudioPlayer::_4AA258(int a2)
{
  AudioPlayer *v2; // esi@1
  int v3; // edi@4
  AudioPlayer_3DSample *v4; // ebx@5
  int v5; // ebx@11
  MixerChannel *v6; // edi@12

  v2 = this;
  if ( this->bPlayerReady )
  {
    if ( this->b3DSoundInitialized && a2 && (v3 = 0, this->uNum3DSamples > 0) )
    {
      v4 = this->p3DSamples;
      while ( v4->field_4 != a2 || AIL_3D_sample_status(v4->hSample) != 4 )
      {
        ++v3;
        ++v4;
        if ( v3 >= v2->uNum3DSamples )
          goto LABEL_9;
      }
      AIL_end_3D_sample(v2->p3DSamples[v3].hSample);
    }
    else
    {
LABEL_9:
      if ( v2->hDigDriver )
      {
        if ( a2 )
        {
          v5 = 0;
          if ( v2->uMixerChannels > 0 )
          {
            v6 = v2->pMixerChannels;
            do
            {
              if ( v6->source_pid == a2 )
              {
                if ( AIL_sample_status(v6->hSample) == AIL::Sample::Playing)
                {
                  AIL_end_sample(v6->hSample);
                  _4ABE55(v6);
                }
              }
              ++v5;
              ++v6;
            }
            while ( v5 < v2->uMixerChannels );
          }
        }
      }
    }
  }
}

//----- (004AA306) --------------------------------------------------------
void AudioPlayer::PlaySound(SoundID eSoundID, signed int a3, unsigned int uNumRepeats, signed int PartyX, signed int PartyY, int a7, unsigned int uVolume, int sPlaybackRate)
{
  //AudioPlayer *pAudioPlayer1; // esi@1
  //signed int pNum; // edx@5
  //int *pSoundID; // ecx@6
  int v12; // edi@13
  signed int v13; // ecx@17
  signed int v14; // eax@20
  int v15; // eax@24
  signed int v16; // eax@25
  SpriteObject *pLayingItem; // eax@28
  signed int v18; // eax@29
  Actor *pActor1; // eax@32
  signed int v20; // ecx@32
  double v21; // st7@32
  signed int v22; // ecx@33
  AudioPlayer_3DSample *pAudioPlayer_3DSample; // esi@53
  AudioPlayer_3DSample *pAudioPlayer_3DSample1; // esi@61
  int v25; // esi@67
  double v26; // st7@68
  int v27; // ST18_4@68
  int v28; // ebx@68
  int v29; // eax@68
  AudioPlayer_3DSample *pAudioPlayer_3DSample2; // esi@69
  int v31; // ST18_4@70
  int v32; // ebx@70
  int v33; // eax@70
  int v34; // eax@70
  char v35; // zf@70
  signed int v36; // ebx@74
  //AudioPlayer *pAudioPlayer2; // edi@79
  AudioPlayer_3DSample *pAudioPlayer_3DSample3; // esi@79
  //unsigned int v39; // ebx@80
  int v40; // eax@81
  char *v41; // edi@82
  int v42; // esi@82
  double v43; // st7@91
  SpriteObject *pLayingItem2; // eax@92
  Actor *pActor; // eax@93
  signed int v46; // ecx@93
  double v47; // st7@93
  BLVDoor *pBLVDoor; // eax@97
  double v49; // st7@104
  int v50; // ST18_4@104
  int v51; // ebx@104
  int v52; // eax@104
  float v53; // ST0C_4@106
  float v54; // ST04_4@106
  //AudioPlayer *pAudioPlayer3; // edx@106
  SoundDesc *pSoundDesc; // edx@107
  SpriteObject *pLayingItem3; // eax@114
  //int v58; // edx@115
  //int v59; // ecx@115
  //Actor *pActor2; // eax@118
  //MixerChannel *pMixerChannel; // esi@126
  signed int v62; // esi@133
  //AudioPlayer *pAudioPlayer4; // ebx@133
  //MixerChannel *pMixerChannel2; // edi@134
  //int v65; // ebx@141
  //AudioPlayer *pAudioPlayer5; // ecx@142
  //MixerChannel *pMixerChannel3; // edi@142
  int v68; // eax@143
  //MixerChannel *pMixerChannel4; // edi@149
  //int v70; // ecx@152
  //SoundDesc *pSoundDesc2; // eax@153
  //int v72; // edi@156
  //MixerChannel *pMixerChannel5; // esi@157
  //unsigned int pVolume; // eax@157
  //int pPartyX; // ebx@159
  //int v76; // ebx@160
  //SpriteObject *pLayingItem4; // edi@164
  //unsigned int pPosY; // edx@166
  //unsigned int pPosX; // ecx@166
  //int v80; // eax@167
  //Actor *pActor3; // edi@168
  //BLVDoor *pBLVDoor2; // edi@173
  //int v83; // eax@183
  //int v84; // eax@183
  //_SAMPLE *v85; // ST18_4@186
  unsigned int v86; // [sp+14h] [bp-60h]@84
  //int v87; // [sp+14h] [bp-60h]@115
  RenderVertexSoft pRenderVertexSoft; // [sp+24h] [bp-50h]@1
  //unsigned int v89; // [sp+54h] [bp-20h]@12
  int v90; // [sp+58h] [bp-1Ch]@68
  float v91; // [sp+5Ch] [bp-18h]@68
  //unsigned int v92; // [sp+60h] [bp-14h]@10
  float v93; // [sp+64h] [bp-10h]@1
  signed int varC; // [sp+68h] [bp-Ch]@68
  //AudioPlayer *pAudioPlayer; // [sp+6Ch] [bp-8h]@1
  int v96; // [sp+70h] [bp-4h]@19
  signed int uNumRepeatsb; // [sp+84h] [bp+10h]@93
  float uNumRepeatsa; // [sp+84h] [bp+10h]@104
  float v99; // [sp+8Ch] [bp+18h]@104
  signed int v100; // [sp+90h] [bp+1Ch]@32
  int v101; // [sp+90h] [bp+1Ch]@52
  int v102; // [sp+90h] [bp+1Ch]@60
  int v103; // [sp+90h] [bp+1Ch]@68

  if (!bPlayerReady)
    return;
  if (!uSoundVolumeMultiplier)
    return;
  if (!hDigDriver)
    return;
  if (eSoundID == SOUND_Invalid)
    return;

  //pAudioPlayer = this;
  //v35 = this->bPlayerReady == 0;
  //pAudioPlayer = this;
  //LODWORD(v93) = 10000;
  int sample_volume = 10000;


  int sound_id = 0;
  for (uint i = 0; i < pSoundList->uNumSounds; ++i)
    if (pSoundList->pSounds[i].uSoundID == eSoundID)
    {
      sound_id = i;
      break;
    }

  if (!sound_id)
  {
    Log::Warning(L"SoundID = %u not found", eSoundID);
    return;
  }

  int start_channel = 0,
      end_channel = 0;

  assert(sound_id < pSoundList->uNumSounds);
  auto sound_desc = pSoundList->pSounds + sound_id;
  if (!b3DSoundInitialized || sound_desc->Is3D())
  {
    if (!a3)  // generic sound like from UI
    {
      start_channel = 10;
      end_channel = 12;
      goto LABEL_133;
    }
    else if (a3 == -1)  // exclusive sounds - can override
    {
      start_channel = 13;
      end_channel = 13;
      goto LABEL_133;
    }
    else
    {
      if (a3 < 0)    // exclusive sounds - no override
      {
        start_channel = 14;
        end_channel = 14;
        goto LABEL_123;
      }
      else
      {
        int object_type = a3 & 7,
            object_id = a3 >> 7;
        switch (object_type)
        {
          case OBJECT_BLVDoor:
          case OBJECT_Player:
          {
            start_channel = 10;
            end_channel = 12;
            goto LABEL_133;
          }
          break;

          case OBJECT_Actor:
          {
            start_channel = 0;
            end_channel = 3;

            assert(object_id < uNumActors);
            auto actor = &pActors[object_id];

            sample_volume = GetSoundStrengthByDistanceFromParty(actor->vPosition.x, actor->vPosition.y, actor->vPosition.z);
            if (!sample_volume)
              return;
            goto LABEL_123;
          }

          case OBJECT_Decoration:
          {
            start_channel = 4;
            end_channel = 4;

            assert(object_id < uNumLevelDecorations);
            auto decor = &pLevelDecorations[object_id];

            sample_volume = GetSoundStrengthByDistanceFromParty(decor->vPosition.x, decor->vPosition.y, decor->vPosition.z);
            if (!sample_volume)
              return;
            goto LABEL_123;
          }
          break;

          case OBJECT_Item:
          {
            start_channel = 5;
            end_channel = 7;

            assert(object_id < uNumSpriteObjects);
            auto object = &pSpriteObjects[object_id];

            sample_volume = GetSoundStrengthByDistanceFromParty(object->vPosition.x, object->vPosition.y, object->vPosition.z);
            if (!sample_volume)
              return;
            goto LABEL_123;
          }
          break;

          case OBJECT_BModel:
          {
            start_channel = 8;
            end_channel = 9;
            goto LABEL_123;
          }
          break;

          default:
            assert(false);
        }
      }
    }






LABEL_123:
    for (uint i = 0; i < uMixerChannels; ++i)
    {
      auto channel = pMixerChannels + i;
      if (channel->source_pid == a3 &&
          AIL_sample_status(channel->hSample) == AIL::Sample::Playing)
      {
        if (channel->uSourceTrackIdx == sound_id)
          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);
      }
    }

LABEL_133:
      v62 = start_channel;
      //pAudioPlayer4 = pAudioPlayer;
      for (v62 = start_channel; v62 <= end_channel; ++v62)
      {
        auto channel = pMixerChannels + v62;
        if (AIL_sample_status(channel->hSample) == AIL::Sample::Done)
        {
          AIL_end_sample(channel->hSample);
          if (channel->uSourceTrackIdx)
            _4ABE55(channel);
          break;
        }
      }
      /*if ( start_channel <= end_channel)
      {
        pMixerChannel2 = &pAudioPlayer->pMixerChannels[start_channel];
        while ( AIL_sample_status(pMixerChannel2->hSample) != AIL::Sample::Done)
        {
          ++v62;
          ++pMixerChannel2;
          if ( v62 > end_channel )
            goto LABEL_140;
        }
        AIL_end_sample(pMixerChannel2->hSample);
        if ( pMixerChannel2->uSourceTrackIdx )
          _4ABE55(pMixerChannel2);
      }
LABEL_140:*/

      if (v62 > end_channel)  // no free channel - occupy the quitest one
      {
        v62 = -1;

        int min_volume = sample_volume;
        for (uint i = start_channel; i <= end_channel; ++i)
        {
          auto channel = pMixerChannels + i;

          int volume = AIL_sample_volume(channel->hSample);
          if (volume < min_volume)
          {
            min_volume = volume;
            v62 = i;
          }
        }
        /*v65 = start_channel;
        v91 = v93;
      if (start_channel <= end_channel)
      {
        //pAudioPlayer5 = pAudioPlayer;
        pMixerChannel3 = &pAudioPlayer->pMixerChannels[start_channel];
        do
        {
          v68 = AIL_sample_volume(pMixerChannel3->hSample);
          if ( v68 < SLODWORD(v91) )
          {
            LODWORD(v91) = v68;
            v62 = v65;
          }
          ++v65;
          ++pMixerChannel3;
        }
        while ( v65 <= end_channel );*/

        if (v62 == -1)   // no free channels at all - only channel 13 allows override (a3 == -1)
        {
          if (start_channel != 13)
            return;
          v62 = 13;
        }

        auto channel = &pMixerChannels[v62];
        AIL_end_sample(channel->hSample);
        _4ABE55(channel);
      }

      if (v62 > end_channel)//10!=13
        return;

//LABEL_150:
      if (sample_volume == 10000)
        sample_volume = uMasterVolume;

        //v70 = 0;
      if ( !a7 )
      {
          //pSoundDesc = pSoundList->pSounds;
          //pSoundDesc2 = &pSoundList->pSounds[sound_id];
        if (!sound_desc->pSoundData[0])
        {
          if (sound_desc->eType == SOUND_DESC_SWAP)
            pSoundList->LoadSound(eSoundID, 0);
        }
      }
        //v72 = 4 * (a7 + 30 * sound_id) + 44;
      if (!pSoundList->pSounds[sound_id].pSoundData[a7])
        return;

        //pMixerChannel5 = &pAudioPlayer->pMixerChannels[v62];
      auto channel = &pMixerChannels[v62];
      AIL_init_sample(channel->hSample);
      AIL_set_sample_file(channel->hSample, (char *)pSoundList->pSounds[sound_id].pSoundData[a7] + 4 * (a7 == 0), -1);
      if (uVolume)
        sample_volume = uVolume;
      AIL_set_sample_volume(channel->hSample, sample_volume);

      int object_type = a3 & 7,
          object_id = a3 >> 7;
      if (PartyX != -1)
      {
        if (!PartyX) PartyX = pParty->vPosition.x;
        if (!PartyY) PartyY = pParty->vPosition.y;
        AIL_set_sample_pan(channel->hSample, sub_4AB66C(PartyX, PartyY));
        AIL_set_sample_volume(channel->hSample, GetSoundStrengthByDistanceFromParty(PartyX, PartyY, pParty->vPosition.z));
      }
      else if (object_type == OBJECT_BLVDoor)
      {
        assert(uCurrentlyLoadedLevelType == LEVEL_Indoor);

        assert(object_id < pIndoor->uNumDoors);
        auto door = &pIndoor->pDoors[object_id];
        if (!door->uDoorID)
          return;

        if (!GetSoundStrengthByDistanceFromParty(door->pXOffsets[0], door->pYOffsets[0], door->pZOffsets[0]))
        {
          AIL_end_sample(channel->hSample);
          _4ABE55(channel);
          return;
        }
        AIL_set_sample_pan(channel->hSample, sub_4AB66C(door->pXOffsets[0], door->pYOffsets[0]));
      }
      else if (object_type == OBJECT_Item)
      {
        assert(object_id < uNumSpriteObjects);
        auto object = &pSpriteObjects[object_id];
        if (!GetSoundStrengthByDistanceFromParty(object->vPosition.x, object->vPosition.y, object->vPosition.z) )
            return;
        AIL_set_sample_pan(channel->hSample, sub_4AB66C(object->vPosition.x, object->vPosition.y));
      }
      else if (object_type == OBJECT_Actor)
      {
        assert(object_id < uNumActors);
        auto actor = &pActors[object_id];
        if (!GetSoundStrengthByDistanceFromParty(actor->vPosition.x, actor->vPosition.y, actor->vPosition.z))
          return;
        AIL_set_sample_pan(channel->hSample, sub_4AB66C(actor->vPosition.x, actor->vPosition.y));
      }
      else if (object_type == OBJECT_Decoration)
      {
        assert(object_id < uNumLevelDecorations);
        auto decor = &pLevelDecorations[object_id];
        if (!GetSoundStrengthByDistanceFromParty(decor->vPosition.x, decor->vPosition.y, decor->vPosition.z))
          return;
        AIL_set_sample_pan(channel->hSample, sub_4AB66C(decor->vPosition.x, decor->vPosition.y));
      }

//LABEL_184:
      if (uNumRepeats)
        AIL_set_sample_loop_count(channel->hSample, uNumRepeats - 1);
      channel->uSourceTrackIdx = sound_id;
      channel->source_pid = a3;
      channel->uSourceTrackID = eSoundID;
      int rval = AIL_start_sample(channel->hSample);
      if ( sPlaybackRate )
        AIL_set_sample_playback_rate(channel->hSample, sPlaybackRate);
      if (object_type == OBJECT_Player)
        AIL_sample_ms_position(channel->hSample, &sLastTrackLengthMS, 0);
      return;
  }




  __debugbreak(); // 3d sound stuff, refactor
  v12 = 13;
  if ( a3 < 0 )
  {
    v15 = pAudioPlayer->uNum3DSamples;
    if ( a3 == -1 )
    {
      if ( v15 < 16 )
        v12 = v15 - 1;
      v96 = v12;
      //goto LABEL_46;
	  pRenderVertexSoft.vWorldPosition.x = (double)pParty->vPosition.x;
      pRenderVertexSoft.vWorldPosition.y = (double)pParty->vPosition.y;
      v21 = (double)pParty->sEyelevel + (double)pParty->vPosition.z;
      goto LABEL_47;
    }
    if ( v15 >= 16 )
      v15 = 14;
    v12 = v15;
    //goto LABEL_45;
	v96 = v15;
    pRenderVertexSoft.vWorldPosition.x = (double)pParty->vPosition.x;
    pRenderVertexSoft.vWorldPosition.y = (double)pParty->vPosition.y;
    v21 = (double)pParty->sEyelevel + (double)pParty->vPosition.z;
    goto LABEL_47;
  }
  if ( (a3 & 7) == 2 )
  {
    v22 = pAudioPlayer->uNum3DSamples;
    if ( v22 < 16 )
    {
      v12 = 5 * v22 / 16;
      v96 = 7 * v22 / 16;
    }
    else
    {
      v96 = 7;
      v12 = 5;
    }
    pLayingItem = &pSpriteObjects[a3 >> 3];
  }
  else
  {
    if ( (a3 & 7) == 3 )
    {
      v18 = pAudioPlayer->uNum3DSamples;
      v12 = 0;
      if ( v18 < 16 )
        v96 = 3 * v18 / 16;
      else
        v96 = 3;
      pActor1 = &pActors[a3 >> 3];
      v20 = pActor1->vPosition.y;
      pRenderVertexSoft.vWorldPosition.x = (double)pActor1->vPosition.x;
      v100 = pActor1->vPosition.z;
      pRenderVertexSoft.vWorldPosition.y = (double)v20;
      v21 = (double)v100;
      goto LABEL_47;
    }
    if ( (a3 & 7) != 5 )
    {
      v13 = pAudioPlayer->uNum3DSamples;
      if ( (a3 & 7) == 6 )
      {
        if ( v13 >= 16 )
        {
          v96 = 9;
          v12 = 8;
          //goto LABEL_46;
		  pRenderVertexSoft.vWorldPosition.x = (double)pParty->vPosition.x;
          pRenderVertexSoft.vWorldPosition.y = (double)pParty->vPosition.y;
		  v21 = (double)pParty->sEyelevel + (double)pParty->vPosition.z;
		  goto LABEL_47;
        }
        v12 = 8 * v13 / 16;
        v14 = 9 * v13;
      }
      else
      {
        if ( v13 >= 16 )
        {
          v96 = 12;
          v12 = 10;
//LABEL_46:
          pRenderVertexSoft.vWorldPosition.x = (double)pParty->vPosition.x;
          pRenderVertexSoft.vWorldPosition.y = (double)pParty->vPosition.y;
          v21 = (double)pParty->sEyelevel + (double)pParty->vPosition.z;
          goto LABEL_47;
        }
        v12 = 10 * v13 / 16;
        v14 = 12 * v13;
      }
      v15 = v14 / 16;
//LABEL_45:
      v96 = v15;
      //goto LABEL_46;
	  pRenderVertexSoft.vWorldPosition.x = (double)pParty->vPosition.x;
      pRenderVertexSoft.vWorldPosition.y = (double)pParty->vPosition.y;
      v21 = (double)pParty->sEyelevel + (double)pParty->vPosition.z;
      goto LABEL_47;
    }
    v16 = pAudioPlayer->uNum3DSamples;
    if ( v16 < 16 )
    {
      v12 = v16 / 4;
      v96 = v16 / 4;
    }
    else
    {
      v12 = 4;
      v96 = 4;
    }
    pLayingItem = (SpriteObject *)&pLevelDecorations[a3 >> 3];
  }
  pRenderVertexSoft.vWorldPosition.x = (double)pLayingItem->vPosition.x;
  pRenderVertexSoft.vWorldPosition.y = (double)pLayingItem->vPosition.y;
  v21 = (double)pLayingItem->vPosition.z;
LABEL_47:
  pRenderVertexSoft.vWorldPosition.z = v21;
  if ( uCurrentlyLoadedLevelType == LEVEL_Indoor )
    sub_4AAEA6_transform(&pRenderVertexSoft);
  else
    pGame->pIndoorCameraD3D->ViewTransform(&pRenderVertexSoft, 1);
  if ( a3 )
  {
    if ( a3 != -1 )
    {
      v101 = 0;
      if ( pAudioPlayer->uNum3DSamples > 0 )
      {
        pAudioPlayer_3DSample = pAudioPlayer->p3DSamples;
        do
        {
          if ( AIL_3D_sample_status(pAudioPlayer_3DSample->hSample) == 4 
			  && pAudioPlayer_3DSample->field_4 == a3 && AIL_3D_sample_status(pAudioPlayer_3DSample->hSample) == 4 )
          {
            if ( pAudioPlayer_3DSample->field_8 == sound_id )
              return;
            AIL_end_3D_sample(pAudioPlayer_3DSample->hSample);
            pAudioPlayer->_4ABF23(pAudioPlayer_3DSample);
          }
          ++v101;
          ++pAudioPlayer_3DSample;
        }
        while ( v101 < pAudioPlayer->uNum3DSamples );
      }
    }
  }
  v102 = v12;
  if ( v12 <= v96 )
  {
    pAudioPlayer_3DSample1 = &pAudioPlayer->p3DSamples[v12];
    while ( AIL_3D_sample_status(pAudioPlayer_3DSample1->hSample) != 2 )
    {
      ++v102;
      ++pAudioPlayer_3DSample1;
      if ( v102 > v96 )
        goto LABEL_67;
    }
    AIL_end_3D_sample(pAudioPlayer_3DSample1->hSample);
    if ( pAudioPlayer_3DSample1->field_8 )
      pAudioPlayer->_4ABF23(pAudioPlayer_3DSample1);
  }
LABEL_67:
  v25 = v96;
  if ( v102 == v96 + 1 )
  {
    LODWORD(v91) = -1;
    v103 = 0;
    *(float *)&varC = pRenderVertexSoft.vWorldViewPosition.y * -0.012207031;
    v93 = 0.0;
    v26 = pRenderVertexSoft.vWorldViewPosition.x * 0.012207031;
    *(float *)&uVolume = v26;
    v27 = abs((signed __int64)v26);
    v28 = abs((signed __int64)v93);
    v29 = abs((signed __int64)*(float *)&varC);
    v90 = int_get_vector_length(v29, v28, v27);
    sPlaybackRate = v12;
    if ( v12 > v25 )
      goto LABEL_192;
    pAudioPlayer_3DSample2 = &pAudioPlayer->p3DSamples[v12];
    do
    {
      AIL_3D_position(pAudioPlayer_3DSample2->hSample, &varC, &v93, (long *)&uVolume);
      v31 = abs((signed __int64)*(float *)&uVolume);
      v32 = abs((signed __int64)v93);
      v33 = abs((signed __int64)*(float *)&varC);
      v34 = int_get_vector_length(v33, v32, v31);
      v35 = v103 == v34;
      if ( v103 < v34 )
      {
        v103 = v34;
        v35 = 1;
      }
      if ( v35 && v90 < v103 )
      {
        v36 = sPlaybackRate;
        LODWORD(v91) = sPlaybackRate;
      }
      else
      {
        v36 = LODWORD(v91);
      }
      ++sPlaybackRate;
      ++pAudioPlayer_3DSample2;
    }
    while ( sPlaybackRate <= v96 );
    if ( v36 == -1 )
    {
LABEL_192:
      v36 = 13;
      if ( v12 != 13 )
        return;
    }
    //pAudioPlayer2 = pAudioPlayer;
    pAudioPlayer_3DSample3 = &pAudioPlayer->p3DSamples[v36];
    AIL_end_3D_sample(pAudioPlayer_3DSample3->hSample);
    pAudioPlayer->_4ABF23(pAudioPlayer_3DSample3);
    v102 = v36;
  }
  //v39 = v89;
  if ( pSoundList->pSounds[sound_id].p3DSound || (LOWORD(v40) = pSoundList->LoadSound(eSoundID, 0), v40) )
  {
    v41 = (char *)pAudioPlayer + 16 * v102;
    v42 = (int)(v41 + 20);
    if ( AIL_set_3D_sample_file(*((int *)v41 + 5), *(void **)((char *)&pSoundList->pSounds->p3DSound + sound_id * sizeof(SoundDesc))) )
    {
      if ( uNumRepeats )
        v86 = uNumRepeats - 1;
      else
        v86 = 1;
      AIL_set_3D_sample_loop_count(*(int *)v42, v86);
      if ( PartyX == -1 )
      {
        if ( (a3 & 7) == 1 )
        {
          if ( uCurrentlyLoadedLevelType == LEVEL_Indoor )
          {
			//goto LABEL_103;
            pBLVDoor = &pIndoor->pDoors[a3 >> 3];
            if ( !pBLVDoor->uDoorID )
             return;
            pRenderVertexSoft.vWorldPosition.x = (double)*pBLVDoor->pXOffsets;
            pRenderVertexSoft.vWorldPosition.y = (double)*pBLVDoor->pYOffsets;
            v47 = (double)*pBLVDoor->pZOffsets;
LABEL_101:
            pRenderVertexSoft.vWorldPosition.z = v47;
          //if ( uCurrentlyLoadedLevelType == LEVEL_Indoor )
          //{
            sub_4AAEA6_transform(&pRenderVertexSoft);
            //goto LABEL_104;
          //}
		  }
//LABEL_103:
		  if ( uCurrentlyLoadedLevelType != LEVEL_Indoor )
            pGame->pIndoorCameraD3D->ViewTransform(&pRenderVertexSoft, 1);
//LABEL_104:
          AIL_start_3D_sample(*(int *)v42);
          AIL_set_3D_sample_float_distances(*(int **)v42, 100.0, 20.0, 100.0, 20.0);
          AIL_set_3D_sample_volume(*(int **)v42, pAudioPlayer->s3DSoundVolume);
          v99 = pRenderVertexSoft.vWorldViewPosition.y * -0.012207031;
          v49 = pRenderVertexSoft.vWorldViewPosition.x * 0.012207031;
          uNumRepeatsa = v49;
          v50 = abs((signed __int64)v49);
          v51 = abs(0);
          v52 = abs((signed __int64)v99);
          if ( int_get_vector_length(v52, v51, v50) <= 100 )
          {
            AIL_set_3D_position((void *)*(int *)v42, LODWORD(v99), 0.0, LODWORD(uNumRepeatsa));
            v53 = -uNumRepeatsa;
            v54 = -v99;
            AIL_set_3D_orientation((void *)*(int *)v42, LODWORD(v54), 0.0, LODWORD(v53), 0.0, 1.0, 0.0);
            //pAudioPlayer3 = pAudioPlayer;
            *((int *)v41 + 6) = a3;
            *((int *)v41 + 7) = sound_id;
            *(&pAudioPlayer->bEAXSupported + 4 * (v102 + 2)) = eSoundID;
          }
          else
          {
            AIL_end_3D_sample(*(int **)v42);
            pAudioPlayer->_4ABF23((AudioPlayer_3DSample *)(v41 + 20));
          }
          return;
        }
        if ( (a3 & 7) == 2 )
        {
          pLayingItem2 = &pSpriteObjects[a3 >> 3];
        }
        else
        {
          if ( (a3 & 7) == 3 )
          {
            pActor = &pActors[a3 >> 3];
            v46 = pActor->vPosition.y;
            pRenderVertexSoft.vWorldPosition.x = (double)pActor->vPosition.x;
            uNumRepeatsb = pActor->vPosition.z;
            pRenderVertexSoft.vWorldPosition.y = (double)v46;
            v47 = (double)uNumRepeatsb;
            goto LABEL_101;
          }
          if ( (a3 & 7) != 5 )
          {
            pRenderVertexSoft.vWorldPosition.x = (double)pParty->vPosition.x;
            v43 = (double)pParty->vPosition.y;
LABEL_100:
            pRenderVertexSoft.vWorldPosition.y = v43;
            v47 = (double)pParty->sEyelevel + (double)pParty->vPosition.z;
            goto LABEL_101;
          }
          pLayingItem2 = (SpriteObject *)&pLevelDecorations[a3 >> 3];
        }
        pRenderVertexSoft.vWorldPosition.x = (double)pLayingItem2->vPosition.x;
        pRenderVertexSoft.vWorldPosition.y = (double)pLayingItem2->vPosition.y;
        v47 = (double)pLayingItem2->vPosition.z;
        goto LABEL_101;
      }
      pRenderVertexSoft.vWorldPosition.x = (double)PartyX;
      v43 = (double)PartyY;
      goto LABEL_100;
    }
  }
}

//----- (0040DEA5) --------------------------------------------------------
void __cdecl AudioPlayer::MessWithChannels()
{
  pAudioPlayer->StopChannels(-1, -1);
}



//----- (004AAFCF) --------------------------------------------------------
void AudioPlayer::UpdateSounds()
{
  //AudioPlayer *pAudioPlayer; // edi@1
  int v2; // ebx@1
  //unsigned __int8 v3; // zf@1
  //int *v4; // eax@2
  //unsigned __int8 v5; // sf@4
  AudioPlayer_3DSample *v6; // esi@5
  int v7; // ebx@6
  int v8; // ebx@9
  int v9; // ebx@10
  int v10; // ebx@11
  double v11; // st7@13
  SpriteObject *v12; // eax@14
  Actor *v13; // eax@15
  signed int v14; // edx@15
  BLVDoor *v15; // eax@19
  double v16; // st7@22
  double v17; // st6@22
  double v18; // st5@23
  double v19; // st4@24
  double v20; // st3@24
  double v21; // st6@28
  double v22; // st7@32
  int v23; // ST1C_4@32
  int v24; // ebx@32
  int v25; // eax@32
  float v26; // ST10_4@34
  float v27; // ST08_4@34
  //MixerChannel *v28; // esi@38
  //unsigned __int8 v29; // of@43
  //MixerChannel *v30; // esi@44
  //int v31; // eax@45
  //SpriteObject *v32; // eax@49
  //Actor *v33; // edi@50
  //int v34; // eax@50
  //unsigned int v35; // edx@51
  //unsigned int v36; // ecx@51
  //SpriteObject *v37; // edi@53
  int v38; // eax@53
  //BLVDoor *v39; // edi@56
  //int v40; // eax@57
  //int v41; // eax@60
  //MixerChannel *v42; // edi@65
  //int v43; // ebx@68
  //LevelDecoration *v44; // esi@68
  //int v45; // ST1C_4@68
  //int v46; // edi@68
  //int v47; // eax@68
  //DecorationDesc *v48; // edi@69
  //__int16 v49; // ax@69
  //__int16 v50; // ax@70
  __int16 v51; // ax@71
  //__int16 v52; // ax@73
  signed int v53; // eax@88
  RenderVertexSoft a1; // [sp+24h] [bp-48h]@1
  float v55; // [sp+54h] [bp-18h]@22
  float v56; // [sp+58h] [bp-14h]@22
  int uNumRepeats; // [sp+5Ch] [bp-10h]@15
  float v58; // [sp+60h] [bp-Ch]@23
  int v59; // [sp+64h] [bp-8h]@4
  //AudioPlayer *thisa; // [sp+68h] [bp-4h]@1

  //pAudioPlayer = this;
  v2 = 0;
  //thisa = this;
  //v3 = this->bPlayerReady == 0;
  //a1.flt_2C = 0.0;
  if (!bPlayerReady)
    return;
  
  if (field_2D0_time_left <= pEventTimer->uTimeElapsed)
    field_2D0_time_left = 32;
  else
  {
    field_2D0_time_left -= pEventTimer->uTimeElapsed;
    return;
  }

      //v3 = this->b3DSoundInitialized == 0;
      if (b3DSoundInitialized)
      {
        __debugbreak(); // refactor refactor
        //v3 = this->uNum3DSamples == 0;
        //v5 = this->uNum3DSamples < 0;
        v59 = 0;
        if (uNum3DSamples > 0)
        {
          v6 = this->p3DSamples;
          while ( 1 )
          {
            v7 = v6->field_4 & 7;
            if ( AIL_3D_sample_status(v6->hSample) == 2 )
            {
              AIL_end_3D_sample(v6->hSample);
              pAudioPlayer->_4ABF23(v6);
            }
            if ( AIL_3D_sample_status(v6->hSample) != 4 )
              goto LABEL_35;
            v8 = v7 - 1;
            if ( v8 )
              break;
            if ( uCurrentlyLoadedLevelType != LEVEL_Indoor )
              goto LABEL_31;
            v15 = &pIndoor->pDoors[v6->field_4 >> 3];
            if ( v15->uDoorID )
            {
              uNumRepeats = *v15->pXOffsets;
              a1.vWorldPosition.x = (double)uNumRepeats;
              uNumRepeats = *v15->pYOffsets;
              a1.vWorldPosition.y = (double)uNumRepeats;
              uNumRepeats = *v15->pZOffsets;
              v11 = (double)uNumRepeats;
              goto LABEL_21;
            }
LABEL_35:
            ++v59;
            ++v6;
            if ( v59 >= pAudioPlayer->uNum3DSamples )
            {
              v2 = 0;
              goto LABEL_37;
            }
          }

          v9 = v8 - 1;
          if ( v9 )
          {
            v10 = v9 - 1;
            if ( !v10 )
            {
              v13 = &pActors[v6->field_4 >> 3];
              uNumRepeats = v13->vPosition.x;
              v14 = v13->vPosition.y;
              a1.vWorldPosition.x = (double)uNumRepeats;
              uNumRepeats = v13->vPosition.z;
              a1.vWorldPosition.y = (double)v14;
              v11 = (double)uNumRepeats;
              goto LABEL_21;
            }
            if ( v10 != 2 )
            {
              a1.vWorldPosition.x = (double)pParty->vPosition.x;
              a1.vWorldPosition.y = (double)pParty->vPosition.y;
              v11 = (double)pParty->sEyelevel + (double)pParty->vPosition.z;
              goto LABEL_21;
            }
            v12 = (SpriteObject *)&pLevelDecorations[v6->field_4 >> 3];
          }
          else
          {
            v12 = &pSpriteObjects[v6->field_4 >> 3];
          }
          a1.vWorldPosition.x = (double)v12->vPosition.x;
          a1.vWorldPosition.y = (double)v12->vPosition.y;
          v11 = (double)v12->vPosition.z;
LABEL_21:
          a1.vWorldPosition.z = v11;
          if ( uCurrentlyLoadedLevelType == LEVEL_Indoor )
          {
            v16 = pBLVRenderParams->fCosineNegX;
            v17 = pBLVRenderParams->fSineNegX;
            v55 = pBLVRenderParams->fCosineY;
            v56 = pBLVRenderParams->fSineY;
            if ( pBLVRenderParams->sPartyRotX )
            {
              v58 = a1.vWorldPosition.x - (double)pParty->vPosition.x;
              *(float *)&uNumRepeats = a1.vWorldPosition.y - (double)pParty->vPosition.y;
              v18 = a1.vWorldPosition.z - (double)pParty->vPosition.z;
              if ( pRenderer->pRenderD3D )
              {
                v19 = *(float *)&uNumRepeats * v56 + v58 * v55;
                v20 = v58 * v56 - *(float *)&uNumRepeats * v55;
              }
              else
              {
                v19 = v58 * v55 - *(float *)&uNumRepeats * v56;
                v20 = v58 * v56 + *(float *)&uNumRepeats * v55;
              }
              a1.vWorldViewPosition.x = v19 * v16 - v18 * v17;
              a1.vWorldViewPosition.y = v20;
              a1.vWorldViewPosition.z = v19 * v17 + v18 * v16;
            }
            else
            {
              v58 = a1.vWorldPosition.x - (double)pParty->vPosition.x;
              *(float *)&uNumRepeats = a1.vWorldPosition.y - (double)pParty->vPosition.y;
              if ( pRenderer->pRenderD3D )
              {
                a1.vWorldViewPosition.x = *(float *)&uNumRepeats * v56 + v58 * v55;
                v21 = v58 * v56 - *(float *)&uNumRepeats * v55;
              }
              else
              {
                a1.vWorldViewPosition.x = v58 * v55 - *(float *)&uNumRepeats * v56;
                v21 = v58 * v56 + *(float *)&uNumRepeats * v55;
              }
              a1.vWorldViewPosition.y = v21;
              a1.vWorldViewPosition.z = a1.vWorldPosition.z - (double)pParty->vPosition.z;
            }
          }
          else
          {
LABEL_31:
            pGame->pIndoorCameraD3D->ViewTransform(&a1, 1u);
          }
          v58 = a1.vWorldViewPosition.y * -0.012207031;
          v22 = a1.vWorldViewPosition.x * 0.012207031;
          *(float *)&uNumRepeats = v22;
          v23 = abs((signed __int64)v22);
          v24 = abs(0);
          v25 = abs((signed __int64)v58);
          if ( int_get_vector_length(v25, v24, v23) <= 100 )
          {
            AIL_set_3D_position(v6->hSample, LODWORD(v58), 0.0, uNumRepeats);
            v26 = -*(float *)&uNumRepeats;
            v27 = -v58;
            AIL_set_3D_orientation(v6->hSample, LODWORD(v27), 0.0, LODWORD(v26), 0.0, 1.0, 0.0);
          }
          else
          {
            AIL_end_3D_sample(v6->hSample);
            pAudioPlayer->_4ABF23(v6);
          }
          goto LABEL_35;
        }
      }







LABEL_37:
  for (uint i = 0; i < uMixerChannels; ++i)
  {
    auto channel = pMixerChannels + i;

    if (AIL_sample_status(channel->hSample) == AIL::Sample::Done)
    {
      AIL_end_sample(channel->hSample);
      _4ABE55(channel);
    }
  }

  for (uint i = 0; i < uMixerChannels; ++i)
  {
    auto channel = pMixerChannels + i;
    if (channel->source_pid <= 0)
      continue;

    int source_type = channel->source_pid & 7,
        source_id = channel->source_pid >> 3;
    int source_x,
        source_y,
        source_z;

    switch (source_type)
    {
      case 0:
      case OBJECT_Player:
      case OBJECT_BModel:
        continue;

      case OBJECT_BLVDoor:
      {
        assert(uCurrentlyLoadedLevelType == LEVEL_Indoor);

        assert(source_id < pIndoor->uNumDoors);
        auto door = pIndoor->pDoors + source_id;
        if (!door->uDoorID)
          continue;

        source_x = door->pXOffsets[0];
        source_y = door->pYOffsets[0];
        source_z = door->pZOffsets[0];
      }
      break;

      case OBJECT_Item:
      {
        assert(source_id < uNumSpriteObjects);
        auto object = &pSpriteObjects[source_id];

        source_x = object->vPosition.x;
        source_y = object->vPosition.y;
        source_z = object->vPosition.z;
      }
      break;

      case OBJECT_Decoration:
      {
        assert(source_id < uNumLevelDecorations);
        auto object = (SpriteObject *)&pLevelDecorations[source_id];

        source_x = object->vPosition.x;
        source_y = object->vPosition.y;
        source_z = object->vPosition.z;
      }
      break;

      case OBJECT_Actor:
      {
        assert(source_id < uNumActors);
        auto actor = pActors + source_id;

        source_x = actor->vPosition.x;
        source_y = actor->vPosition.y;
        source_z = actor->vPosition.z;
      }
      break;

      default:
        assert(false);
        continue;
    }

    if (auto sound_strength = GetSoundStrengthByDistanceFromParty(source_x, source_y, source_z))
    {
      AIL_set_sample_volume(channel->hSample, sound_strength);
      AIL_set_sample_pan(channel->hSample, sub_4AB66C(source_x, source_y));
    }
    else
    {
      AIL_end_sample(channel->hSample);
      _4ABE55(channel);
    }
  }



  if (pCurrentScreen != SCREEN_GAME)
  {
    auto channel = &pMixerChannels[4];
    if (AIL_sample_status(channel->hSample) == AIL::Sample::Playing)
      AIL_end_sample(channel->hSample);
    return;
  }
  if (!_6807E0_num_decorations_with_sounds_6807B8)
    return;

  v55 = 0;
      //v59 = 0;
  for (uint i = 0; i < _6807E0_num_decorations_with_sounds_6807B8; ++i)
  {
      //while ( 1 )
      //{
        LODWORD(v56) = 1;
        //v43 = _6807B8_level_decorations_ids[v59];
        //v44 = &pLevelDecorations[_6807B8_level_decorations_ids[v59]];
        //v45 = abs(v44->vPosition.z - pParty->vPosition.z);
        //v46 = abs(v44->vPosition.y - pParty->vPosition.y);
        //v47 = abs(v44->vPosition.x - pParty->vPosition.x);
    auto decor = &pLevelDecorations[_6807B8_level_decorations_ids[i]];
    if (int_get_vector_length(decor->vPosition.x - pParty->vPosition.x,
                              decor->vPosition.y - pParty->vPosition.y,
                              decor->vPosition.z - pParty->vPosition.z) > 8192)
      continue;

    auto decor_desc = &pDecorationList->pDecorations[decor->uDecorationDescID];
      //v48 = &pDecorationList->pDecorations[decor->uDecorationDescID];
      //v49 = v48->uFlags;
      uNumRepeats = (~(unsigned __int8)decor_desc->uFlags & 0x40) >> 6;
 
    if (decor_desc->SoundOnDawn() || decor_desc->SoundOnDusk())
    {
        //v50 = decor->field_1A;
        v55 = 0.0;
        uNumRepeats = 2;
        if (decor->field_1A)
        {
          v51 = decor->field_1A - 32;
          decor->field_1A = v51;
          if ( v51 < 0 )
            decor->field_1A = 0;
        }
    }

      //v52 = v48->uFlags;
    if (!decor_desc->SoundOnDawn())
    {
      if (!decor_desc->SoundOnDusk())
        goto LABEL_84;
      if ( v55 != 0.0 )
        goto LABEL_85;
    }
    v56 = 0.0;

    if (pParty->uCurrentHour >= 5 && pParty->uCurrentHour < 6 ||
        pParty->uCurrentHour >= 20 && pParty->uCurrentHour < 21)
    {
        if ( !decor->field_1A && rand() % 100 < 100 )
          LODWORD(v56) = 1;
        LODWORD(v55) = 1;
    }
LABEL_84:
      if ( v55 == 0.0 )
      {
LABEL_87:
        if ( v56 != 0.0 )
        {
          v53 = 8 * _6807B8_level_decorations_ids[i];
          LOBYTE(v53) = v53 | OBJECT_Decoration;
          PlaySound((SoundID)decor_desc->uSoundID, v53, uNumRepeats, -1, 0, 0, 0, 0);
        }
        continue;
      }
LABEL_85:
      if ( !decor->field_1A )
        decor->field_1A = (rand() % 15 + 1) << 7;
      goto LABEL_87;
  }
}


//----- (004AB66C) --------------------------------------------------------
int __fastcall sub_4AB66C(int a1, int a2)
{
  signed int v2; // eax@1

  v2 = stru_5C6E00->uDoublePiMask & (stru_5C6E00->Atan2(a1 - pParty->vPosition.x, a2 - pParty->vPosition.y)
                                  - stru_5C6E00->uIntegerHalfPi - pParty->sRotationY);
  if ( v2 > (signed int)stru_5C6E00->uIntegerPi )
    v2 = 2 * stru_5C6E00->uIntegerPi - v2;
  return (v2 >> 3) - (v2 >> 10);
}
// 4AB66C: using guessed type int __fastcall sub_4AB66C(int, int);

//----- (004AB6B1) --------------------------------------------------------
int __fastcall GetSoundStrengthByDistanceFromParty(int a1, int a2, int a3)
{
  int v3; // esi@1
  int v4; // edi@1
  int v5; // ST08_4@1
  int v6; // esi@1
  int v7; // eax@1
  int v9; // [sp+10h] [bp+8h]@1

  v3 = a2;
  v4 = a1;
  v5 = abs(a3 - pParty->vPosition.z);
  v6 = abs(v3 - pParty->vPosition.y);
  v7 = abs(v4 - pParty->vPosition.x);
  v9 = int_get_vector_length(v7, v6, v5);
  if ( v9 <= 8192 )
    return 114 - (unsigned __int64)(signed __int64)((double)v9 * 0.0001220703125 * 100.0);
  else
    return 0;
}



//----- (004AB71F) --------------------------------------------------------
void AudioPlayer::StopChannels(int uStartChannel, int uEndChannel)
{
  //AudioPlayer *v3; // esi@1
  int v4; // ecx@1
  AudioPlayer_3DSample *v5; // edi@4
  int v6; // ebx@12
  MixerChannel *pChannel; // edi@14
  //_STREAM *v8; // esi@23
  int v9; // [sp+4h] [bp-4h]@3

  //v3 = this;
  v4 = 0;
  if ( bPlayerReady )
  {
    if ( b3DSoundInitialized )
    {
      v9 = 0;
      if ( uNum3DSamples > 0 )
      {
        v5 = p3DSamples;//;(char *)&p3DSamples[0].field_8;
        do
        {
          if ( (uStartChannel == -1 || v4 < uStartChannel || v4 > uEndChannel)
			  && v5->field_8
			  && pSoundList->pSounds[v5->field_8].eType != SOUND_DESC_SYSTEM)
          {
			  AIL_end_3D_sample(v5->hSample);
            _4ABF23(v5);
            v5->field_4 = 0;
            v4 = v9;
          }
          ++v4;
          v5 += 16;
          v9 = v4;
        }
        while ( v4 < uNum3DSamples );
      }
    }
    v6 = 0;
    if ( hDigDriver && uMixerChannels > 0 )
    {
      pChannel = pMixerChannels;
      do
      {
        if ( (uStartChannel == -1 || v6 < uStartChannel || v6 > uEndChannel)
          && pSoundList->pSounds[pChannel->uSourceTrackIdx].eType != SOUND_DESC_SYSTEM)
        {
          AIL_end_sample(pChannel->hSample);
          _4ABE55(pChannel);
          pChannel->source_pid = 0;
        }
        ++v6;
        ++pChannel;
      }
      while (v6 < uMixerChannels);
    }
    if (hSequence)
      AIL_end_sequence(hSequence);
    //v8 = hStream;
    if (hStream)
      AIL_pause_stream(hStream, 1);
  }
}


//----- (004AB818) --------------------------------------------------------
void AudioPlayer::LoadAudioSnd()
{
  DWORD NumberOfBytesRead; // [sp+Ch] [bp-4h]@3

  hAudioSnd = CreateFileA("Sounds\\Audio.snd", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0x8000080u, 0);
  if (hAudioSnd == INVALID_HANDLE_VALUE)
  {
    Log::Warning(L"Can't open file: %s", L"Sounds\\Audio.snd");
    return;
  }

  ReadFile(hAudioSnd, &uNumSoundHeaders, 4u, &NumberOfBytesRead, 0);
  pSoundHeaders = nullptr;
  pSoundHeaders = (SoundHeader *)pAllocator->AllocNamedChunk(0, 52 * uNumSoundHeaders + 2, 0);
  ReadFile(hAudioSnd, pSoundHeaders, 52 * uNumSoundHeaders, &NumberOfBytesRead, 0);
}

//----- (004AB8CE) --------------------------------------------------------
void AudioPlayer::Initialize(HWND hWnd)
{
  //AudioPlayer *v2; // esi@1
  int v3; // ebx@1
  //_DIG_DRIVER *v4; // eax@1
  char v5; // dl@5
  _PROVIDER *v6; // eax@9
  HWND v7; // ST00_4@9
  MixerChannel *pChannel; // edi@14
  _SAMPLE *v9; // eax@15
  //_REDBOOK *v10; // eax@19
  //int v11; // ecx@21
  int v12; // [sp+Ch] [bp-Ch]@9
  char *Str1; // [sp+10h] [bp-8h]@6
  int v14; // [sp+14h] [bp-4h]@5

  //v2 = this;
  v3 = 0;
  this->hWindow = hWnd;
  this->hAILRedbook = 0;
  this->hDigDriver = 0;
  this->dword_0002AC = 0;
  this->hSequence = 0;
  this->uMasterVolume = 127;
  this->dword_0002C8 = 64;
  this->dword_0002CC = 2;

  MSS32_DLL_Initialize();
  BINKW32_DLL_Initialize();
  SMACKW32_DLL_Initialize();
  
  AIL_startup();
  if (bCanLoadFromCD)
    hAILRedbook = AIL_redbook_open_drive(cMM7GameCDDriveLetter/*cGameCDDriveLetter*/);
  //else
  //  hAILRedbook = AIL_redbook_open(0);
  //v4 = Audio_GetFirstHardwareDigitalDriver();

  hDigDriver = Audio_GetFirstHardwareDigitalDriver();
  if ( hDigDriver )
    SmackSoundUseMSS(hDigDriver);
  if ( ReadWindowsRegistryInt("Disable3DSound", 0) != 1 && pVersion->pVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT )
  {
    v14 = 0;
    bEAXSupported = 0;
    b3DSoundInitialized = 0;
    ReadWindowsRegistryString("3DSoundProvider", p3DSoundProvider, 128u, "NONE");
    CheckA3DSupport(v5);
    while ( AIL_enumerate_3D_providers(&v14, (HPROVIDER *)&hWnd, &Str1) )
    {
      if ( !strcmp(Str1, p3DSoundProvider) )
      {
        if ( AIL_open_3D_provider((HPROVIDER)hWnd) )
        {
          bEAXSupported = 0;
          b3DSoundInitialized = 0;
          h3DSoundProvider = 0;
        }
        else
        {
          v6 = (_PROVIDER *)hWnd;
          v7 = hWnd;
          b3DSoundInitialized = 1;
          h3DSoundProvider = v6;
          uNum3DSamples = 4;
          AIL_3D_provider_attribute((HPROVIDER)v7, "EAX environment selection", &v12);
          if ( v12 != -1 )
            bEAXSupported = 1;
        }
        pAudioPlayer->_4AC0A2();
        break;
      }
    }
  }
  if ( uMixerChannels > 0 )
  {
    pChannel = pMixerChannels;
    do
    {
      v9 = AIL_allocate_sample_handle(hDigDriver);
      pChannel->hSample = v9;
      if ( !v9 )
        break;
      ++v3;
      ++pChannel;
    }
    while ( v3 < uMixerChannels );
  }
  uMixerChannels = v3;
  if ( bPlayerReady )
    StopChannels(-1, -1);
  //v10 = hAILRedbook;
  bPlayerReady = true;
  if ( hAILRedbook )
  {
    AIL_redbook_stop(hAILRedbook);
    uNumRedbookTracks = AIL_redbook_tracks(hAILRedbook);
  }
  pAudioPlayer->sRedbookVolume = AIL_redbook_volume(hAILRedbook);
  pAudioPlayer->SetMasterVolume(pSoundVolumeLevels[uSoundVolumeMultiplier] * 128.0f);
  if ( bPlayerReady && hAILRedbook )
    AIL_redbook_set_volume(hAILRedbook, (unsigned __int64)(pSoundVolumeLevels[uMusicVolimeMultiplier] * 64.0f) >> 32);
  LoadAudioSnd();
}

//----- (004ABAF7) --------------------------------------------------------
_DIG_DRIVER *Audio_GetFirstHardwareDigitalDriver(void)
{
  int v0; // ecx@1
  unsigned int v1; // ebp@2
  size_t v2; // eax@4
  signed int v3; // kr14_4@9
  char *v5; // [sp+10h] [bp-Ch]@2
  unsigned int v6; // [sp+14h] [bp-8h]@1
  _DIG_DRIVER *hDrv; // [sp+18h] [bp-4h]@3
  
  static int dword_4F00DC = 22050;
  static int dword_4F00E0 = 16;
  static int dword_4F00E4 = 2;

  AIL_set_preference(15, 0);
  AIL_set_preference(33, 1);
  v0 = dword_4F00DC;
  pAudioPlayer->pDeviceNames[0][0] = 0;
  pAudioPlayer->uNumDevices = 0;
  v6 = 0;

  if ( dword_4F00DC < 11025 )
    return 0;
  v1 = 0;
  v5 = (char *)pAudioPlayer->array_000BF0;
  while ( 1 )
  {
    while ( 1 )
    {
      pcmWaveFormat.wf.wFormatTag = 1;
      pcmWaveFormat.wf.nChannels = dword_4F00E4;
      pcmWaveFormat.wf.nSamplesPerSec = v0;
      pcmWaveFormat.wf.nBlockAlign = dword_4F00E4 * dword_4F00E0 / 8;
      pcmWaveFormat.wBitsPerSample = dword_4F00E0;
      pcmWaveFormat.wf.nAvgBytesPerSec = v0 * dword_4F00E4 * dword_4F00E0 / 8;
      if ( !AIL_waveOutOpen(&hDrv, 0, -1, &pcmWaveFormat.wf) )
        break;
      if ( !AIL_get_preference(15) )
        goto LABEL_8;
      v3 = dword_4F00DC;
      v0 = dword_4F00DC / 2;
      dword_4F00DC /= 2;
      if ( v3 / 2 < 11025 )
      {
        if ( dword_4F00E0 == 8 )
        {
          v0 = 22050;
          dword_4F00E0 = 8;
          dword_4F00DC = 22050;
        }
        goto LABEL_12;
      }
    }
    strcpy(pAudioPlayer->pDeviceNames[v1 / 8], "Device: ");
    v2 = strlen(pAudioPlayer->pDeviceNames[v1 / 8]);
    AIL_digital_configuration(hDrv, (int *)v5, (int *)(v5 + 64), (char *)pAudioPlayer->pDeviceNames + v2 + v1 * 16);
    ++v6;
    v1 += 8;
    v5 += 4;
    pAudioPlayer->uNumDevices = v6;
    if ( AIL_get_preference(15) )
      return hDrv;
    if ( !strstr(pAudioPlayer->pDeviceNames[v1 / 8 - 1], "Emulated") )
      return hDrv;
    AIL_waveOutClose(hDrv);
    AIL_set_preference(15, 1);
LABEL_8:
    AIL_set_preference(15, 1);
    v0 = dword_4F00DC;
LABEL_12:
    if ( v0 < 11025 )
      return 0;
  }
}


//----- (004ABC9B) --------------------------------------------------------
LSTATUS AudioPlayer::CheckA3DSupport(char a2)
{
  LSTATUS result; // eax@1
  DWORD cbData; // [sp+8h] [bp-Ch]@1
  int Data; // [sp+Ch] [bp-8h]@1
  HKEY hKey; // [sp+10h] [bp-4h]@1
  char v6; // [sp+1Ch] [bp+8h]@0
  v6=0; //to fix
  hKey = 0;
  cbData = 4;
  Data = 0;
  result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Aureal\\A3D", 0, 0x2001Fu, &hKey);
  if ( !result )
  {
    if ( v6 )
      RegQueryValueExA(hKey, "SplashAudio", 0, 0, (LPBYTE)&Aureal3D_SplashAudio, &cbData);
    else
      Data = Aureal3D_SplashAudio;
    RegSetValueExA(hKey, "SplashAudio", 0, 4u, (const BYTE *)&Data, 4u);
    Data = 0;
    if ( v6 )
      RegQueryValueExA(hKey, "SplashScreen", 0, 0, (LPBYTE)&Aureal3D_SplashScreen, &cbData);
    else
      Data = Aureal3D_SplashScreen;
    RegSetValueExA(hKey, "SplashScreen", 0, 4u, (const BYTE *)&Data, 4u);
    result = RegCloseKey(hKey);
  }
  return result;
}
// 4ABC9B: inconsistent function type and number of purged bytes

//----- (004ABD5B) --------------------------------------------------------
void AudioPlayer::Release() //Освободить
{
  AudioPlayer *pAudioPlayer; // esi@1
  int v2; // edi@1
  MixerChannel *pMixerChannel; // ebx@3
  char v4; // dl@5
  int v5; // ebx@6
  AudioPlayer_3DSample *p3DSample; // edi@7
  //int v7; // edx@14
  int v8; // ecx@14
  void *v9; // ecx@15

  pAudioPlayer = this;
  v2 = 0;
  if ( this->bPlayerReady )
  {
    CloseHandle(pVideoPlayer->hMagicVid);
    CloseHandle(pVideoPlayer->hMightVid);
    pAudioPlayer->StopChannels(-1, -1);
    if ( pAudioPlayer->uMixerChannels > 0 )
    {
      pMixerChannel = pAudioPlayer->pMixerChannels;
      do
      {
        AIL_release_sample_handle(pMixerChannel->hSample);
        ++v2;
        ++pMixerChannel;
      }
      while ( v2 < pAudioPlayer->uMixerChannels );
    }
    if ( ReadWindowsRegistryInt("Disable3DSound", 0) != 1 )
    {
      v5 = 0;
      pAudioPlayer->CheckA3DSupport(0);// pAudioPlayer->CheckA3DSupport(v4);
      if ( pAudioPlayer->uNum3DSamples > 0 )
      {
        p3DSample = pAudioPlayer->p3DSamples;
        do
        {
          if ( p3DSample->hSample )
          {
            AIL_release_3D_sample_handle(p3DSample->hSample);
            p3DSample->hSample = 0;
          }
          ++v5;
          ++p3DSample;
        }
        while ( v5 < pAudioPlayer->uNum3DSamples );
      }
      if ( pAudioPlayer->h3DSoundProvider )
      {
        AIL_close_3D_provider(pAudioPlayer->h3DSoundProvider);
        pAudioPlayer->h3DSoundProvider = 0;
      }
    }
    v8 = (int)&pAudioPlayer->hAILRedbook;
    if ( pAudioPlayer->hAILRedbook )
    {
      AIL_redbook_stop(pAudioPlayer->hAILRedbook);
      AIL_redbook_set_volume((HREDBOOK)v8, pAudioPlayer->sRedbookVolume);
      AIL_redbook_close(pAudioPlayer->hAILRedbook);
    }
    AIL_shutdown();
    pSoundList->Release();
    v9 = *(void **)&pAudioPlayer->field_C78[0];
    if ( v9 )
      ReleaseSoundData(v9);
    CloseHandle(pAudioPlayer->hAudioSnd);
  }
}

//----- (004ABE55) --------------------------------------------------------
void AudioPlayer::_4ABE55(MixerChannel *pChannel)
{
  int v2; // ebx@1
  //AudioPlayer *v3; // esi@1
  SoundDesc *v4; // eax@2
  unsigned __int8 v5; // zf@5
  unsigned __int8 v6; // sf@5
  char *v7; // edi@6
  int v8; // 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 v16; // [sp+50h] [bp-4h]@5

  v2 = 0;
  //v3 = this;
  if (!pSoundList->pSounds)
    return;
 
    v4 = &pSoundList->pSounds[pChannel->uSourceTrackIdx];
    if ( v4->eType == SOUND_DESC_SWAP)
    {
      if ( v4->pSoundData[0] && !(v4->uFlags & 1) )
      {
        v5 = this->uMixerChannels == 0;
        v6 = this->uMixerChannels < 0;
        v15 = 0;
        v16 = 0;
        if ( v6 | v5 )
          goto LABEL_16;
        v7 = (char *)this->pMixerChannels;
        do
        {
          if ( pChannel->uSourceTrackID == *((int *)v7 + 3) )
          {
            v8 = v16;
            v9 = *(int *)v7;
            ++v16;
            v14[v8] = v2;
            if ( AIL_sample_status((HSAMPLE)v9) == AIL::Sample::Playing)
              ++v15;
          }
          ++v2;
          v7 += 16;
        }
        while ( v2 < uMixerChannels );
        if ( !v15 )
        {
LABEL_16:
          pSoundList->_4A9DCD(pChannel->uSourceTrackIdx, 1);
          v10 = 0;
          if ( v16 > 0 )
          {
            do
            {
              v11 = v14[v10];
              v12 = 16 * (v14[v10++] + 47);
              pMixerChannels[v11].uSourceTrackID = 0;
              v13 = __OFSUB__(v10, v16);
              v6 = v10 - v16 < 0;
              *(unsigned int *)((char *)&bEAXSupported + v12) = 0;
            }
            while ( v6 ^ v13 );
          }
        }
      }
    }
}



//----- (004AAEA6) --------------------------------------------------------
int __fastcall sub_4AAEA6_transform(RenderVertexSoft *a1)
{
  double v1; // st7@1
  double v2; // st6@1
  int result; // eax@1
  double v4; // st5@2
  double v5; // st4@3
  double v6; // st3@3
  double v7; // st7@6
  double v8; // st6@7
  float v9; // [sp+0h] [bp-10h]@1
  float v10; // [sp+4h] [bp-Ch]@1
  float v11; // [sp+8h] [bp-8h]@2
  float v12; // [sp+8h] [bp-8h]@6
  float v13; // [sp+Ch] [bp-4h]@2
  float v14; // [sp+Ch] [bp-4h]@6

  v1 = pBLVRenderParams->fCosineNegX;
  v2 = pBLVRenderParams->fSineNegX;
  v9 = pBLVRenderParams->fCosineY;
  v10 = pBLVRenderParams->fSineY;
  result = 0;
  if ( pBLVRenderParams->sPartyRotX )
  {
    v13 = a1->vWorldPosition.x - (double)pParty->vPosition.x;
    v11 = a1->vWorldPosition.y - (double)pParty->vPosition.y;
    v4 = a1->vWorldPosition.z - (double)pParty->vPosition.z;
    if ( pRenderer->pRenderD3D )
    {
      v5 = v11 * pBLVRenderParams->fSineY + v13 * pBLVRenderParams->fCosineY;
      v6 = v13 * pBLVRenderParams->fSineY - v11 * pBLVRenderParams->fCosineY;
    }
    else
    {
      v5 = v13 * pBLVRenderParams->fCosineY - v11 * pBLVRenderParams->fSineY;
      v6 = v13 * pBLVRenderParams->fSineY + v11 * pBLVRenderParams->fCosineY;
    }
    a1->vWorldViewPosition.x = v5 * v1 - v4 * v2;
    a1->vWorldViewPosition.y = v6;
    a1->vWorldViewPosition.z = v5 * v2 + v4 * v1;
  }
  else
  {
    v14 = a1->vWorldPosition.x - (double)pParty->vPosition.x;
    v12 = a1->vWorldPosition.y - (double)pParty->vPosition.y;
    v7 = a1->vWorldPosition.z - (double)pParty->vPosition.z;
    if ( pRenderer->pRenderD3D )
    {
      a1->vWorldViewPosition.x = v12 * pBLVRenderParams->fSineY + v14 * pBLVRenderParams->fCosineY;
      v8 = v14 * v10 - v12 * v9;
    }
    else
    {
      a1->vWorldViewPosition.x = v14 * pBLVRenderParams->fCosineY - v12 * pBLVRenderParams->fSineY;
      v8 = v14 * v10 + v12 * v9;
    }
    a1->vWorldViewPosition.y = v8;
    a1->vWorldViewPosition.z = v7;
  }
  return result;
}




//----- (004ABF23) --------------------------------------------------------
void AudioPlayer::_4ABF23(AudioPlayer_3DSample *a2)
{
  int v2; // ebx@1
  //AudioPlayer *v3; // esi@1
  SoundDesc *v4; // eax@2
  unsigned __int8 v5; // zf@5
  unsigned __int8 v6; // sf@5
  char *v7; // edi@6
  int v8; // eax@8
  int v9; // ST04_4@8
  int v10; // ecx@12
  int v11; // eax@13
  unsigned __int8 v12; // of@13
  int v13[16]; // [sp+Ch] [bp-48h]@8
  int v14; // [sp+4Ch] [bp-8h]@5
  int v15; // [sp+50h] [bp-4h]@5

  v2 = 0;
  //v3 = this;
  if ( pSoundList->pSounds )
  {
    v4 = &pSoundList->pSounds[a2->field_8];
    if ( v4->eType == SOUND_DESC_SWAP)
    {
      if ( v4->p3DSound && !(v4->uFlags & 1) )
      {
        v5 = this->uNum3DSamples == 0;
        v6 = this->uNum3DSamples < 0;
        v14 = 0;
        v15 = 0;
        if ( v6 | v5 )
          goto LABEL_16;
        v7 = (char *)this->p3DSamples;
        do
        {
          if ( a2->field_C == *((int *)v7 + 3) )
          {
            v8 = v15;
            v9 = *(int *)v7;
            ++v15;
            v13[v8] = v2;
            if ( AIL_3D_sample_status((void *)v9) == 4 )
              ++v14;
          }
          ++v2;
          v7 += 16;
        }
        while ( v2 < uNum3DSamples );
        if ( !v14 )
        {
LABEL_16:
          pSoundList->_4A9DCD(a2->field_8, 1);
          v10 = 0;
          if ( v15 > 0 )
          {
            do
            {
              v11 = v13[v10++];
              *(&bEAXSupported + 4 * (v11 + 2)) = 0;
              v12 = __OFSUB__(v10, v15);
              v6 = v10 - v15 < 0;
              p3DSamples[v11].field_8 = 0;
            }
            while ( v6 ^ v12 );
          }
        }
      }
    }
  }
}
// 4D82F4: using guessed type int __stdcall AIL_3D_sample_status(int);
// 4ABF23: using guessed type int var_48[16];

//----- (004ABFDB) --------------------------------------------------------
void __cdecl PlayLevelMusic()
{
  unsigned int v0; // eax@1

  v0 = pMapStats->GetMapInfo(pCurrentMapName);
  if ( v0 )
    pAudioPlayer->PlayMusicTrack((MusicID)pMapStats->pInfos[v0].uRedbookTrackID);
}

//----- (004AC004) --------------------------------------------------------
void AudioPlayer::SetEAXPreferences()
{
  AudioPlayer *v1; // edi@1
  _PROVIDER *v2; // ST00_4@2
  _PROVIDER *v3; // ST00_4@2
  float v4; // [sp+4h] [bp-4h]@2

  v1 = this;
  if ( this->bEAXSupported )
  {
    v2 = this->h3DSoundProvider;
    v4 = 0.0;
    AIL_set_3D_provider_preference(v2, "EAX effect volume", (int *)&v4);
    v3 = v1->h3DSoundProvider;
    v4 = 1.0;
    AIL_set_3D_provider_preference(v3, "EAX damping", (int *)&v4);
  }
}
// 4D82DC: using guessed type int __stdcall AIL_set_3D_provider_preference(int, int, int);

//----- (004AC041) --------------------------------------------------------
void AudioPlayer::SetMapEAX()
{
  AudioPlayer *v1; // esi@1
  unsigned int v2; // eax@1
  int v3; // [sp+4h] [bp-4h]@3

  v1 = this;
  v2 = pMapStats->GetMapInfo(pCurrentMapName);
  if ( v1->b3DSoundInitialized && v1->bEAXSupported )
  {
    v3 = pMapStats->pInfos[v2].uEAXEnv;
    if ( (unsigned int)v3 >= 0x1A )
    {
      SetEAXPreferences();
      v1->field_214 = -1;
    }
    else
    {
      AIL_set_3D_provider_preference(v1->h3DSoundProvider, "EAX environment selection", &v3);
      v1->field_214 = v3;
    }
  }
}
// 4D82DC: using guessed type int __stdcall AIL_set_3D_provider_preference(int, int, int);

//----- (004AC0A2) --------------------------------------------------------
int AudioPlayer::_4AC0A2()
{
  AudioPlayer *v1; // esi@1
  unsigned int v2; // eax@1
  char v3; // zf@1
  int v4; // ebx@1
  int *v5; // edi@2
  int v6; // eax@4
  AudioPlayer_3DSample *v8; // ebx@7
  void *v9; // eax@8
  int v10; // ebx@14
  unsigned int v11; // eax@14
  int v12; // [sp+1Ch] [bp-8h]@1
  int v13; // [sp+20h] [bp-4h]@6

  v1 = this;
  v2 = pMapStats->GetMapInfo(pCurrentMapName);
  v3 = v1->b3DSoundInitialized == 0;
  v4 = v2;
  v12 = v2;
  if ( !v3 )
  {
    v5 = &v1->uNum3DSamples;
    AIL_3D_provider_attribute(v1->h3DSoundProvider, "Maximum supported samples", &v1->uNum3DSamples);
    if ( v1->uNum3DSamples > 32 )
      *v5 = 32;
    v6 = *v5;
    if ( !*v5 )
    {
      v1->b3DSoundInitialized = 0;
      return -1;
    }
    v13 = 0;
    if ( v6 > 0 )
    {
      v8 = v1->p3DSamples;
      while ( 1 )
      {
        v9 = (void *)AIL_allocate_3D_sample_handle(v1->h3DSoundProvider);
        v8->hSample = v9;
        if ( !v9 )
          break;
        AIL_set_3D_sample_float_distances(v9, 4096.0, 256.0, 4096.0, 256.0);
        AIL_set_3D_sample_volume(v8->hSample, v1->s3DSoundVolume);
        ++v13;
        ++v8;
        if ( v13 >= *v5 )
          goto LABEL_12;
      }
      *v5 = v13;
LABEL_12:
      v4 = v12;
    }
    if ( v1->bEAXSupported )
    {
      v10 = v4;
      v11 = pMapStats->pInfos[v10].uEAXEnv;
      v12 = pMapStats->pInfos[v10].uEAXEnv;
      if ( v11 >= 0x1A )
      {
        pAudioPlayer->SetEAXPreferences();
        v1->field_214 = -1;
      }
      else
      {
        AIL_set_3D_provider_preference(v1->h3DSoundProvider, "EAX environment selection", &v12);
        v1->field_214 = v12;
      }
    }
  }
  return 1;
}


//----- (004A96BE) --------------------------------------------------------
void ReleaseSoundData(void *_this)
{
  int pID; // esi@1
  char *v2; // eax@1

  pID = 0;
  v2 = (char *)&pSounds[0].pSoundData;
  while ( *(void **)v2 != _this )
  {
    v2 += 128;
    ++pID;
	if ( v2 > (void *)&pSounds[2999].pSoundData)//(signed int)&pAudioPlayer->p3DSamples[6].field_8 )
      return;
  }
  pAllocator->FreeChunk(_this);
  memset(&pSounds[pID], 0, 0x80u);
}

//----- (004A96FF) --------------------------------------------------------
SoundHeader *__fastcall FindSound_BinSearch(unsigned int uStart, unsigned int uEnd, const char *pName)
{
  unsigned int v3; // ebx@1
  unsigned int v4; // esi@1
  SoundHeader *result; // eax@2
  signed int v6; // ebx@11
  int v7; // edi@13
  unsigned int v8; // esi@14
  unsigned int v9; // esi@20
  unsigned int v10; // [sp+Ch] [bp-4h]@1

  v3 = uEnd;
  v10 = uEnd;
  v4 = uStart;
  while ( 1 )
  {
    v6 = v3 - v4;
    result = &pAudioPlayer->pSoundHeaders[v6 / 2 + v4];
    if ( !result )
      return result;
    result = (SoundHeader *)_strcmpi(pName, result->pSoundName);
    if ( !result )
      uFindSound_BinSearch_ResultID = v6 / 2 + v4;
    if ( v4 == v10 )
      goto LABEL_17;
    if ( (signed int)result < 0 )
      break;
    if ( v6 <= 4 )
    {
      v7 = v4;
      if ( (signed int)v4 < (signed int)v10 )
      {
        v9 = v4;
        do
        {
          result = (SoundHeader *)_strcmpi(pName, pAudioPlayer->pSoundHeaders[v9].pSoundName);
          if ( !result )
            goto LABEL_24;
          ++v7;
          ++v9;
        }
        while ( v7 < (signed int)v10 );
      }
LABEL_17:
      uFindSound_BinSearch_ResultID = -1;
      return result;
    }
    v4 += v6 / 2;
LABEL_10:
    v3 = v10;
  }
  if ( v6 > 4 )
  {
    v10 = v6 / 2 + v4;
    goto LABEL_10;
  }
  v7 = v4;
  if ( (signed int)v4 >= (signed int)v10 )
    goto LABEL_17;
  v8 = v4;
  while ( 1 )
  {
    result = (SoundHeader *)_strcmpi(pName, pAudioPlayer->pSoundHeaders[v8].pSoundName);
    if ( !result )
      break;
    ++v7;
    ++v8;
    if ( v7 >= (signed int)v10 )
      goto LABEL_17;
  }
LABEL_24:
  uFindSound_BinSearch_ResultID = v7;
  return result;
}
// F1B4C8: using guessed type int uFindSound_BinSearch_ResultID;

//----- (004A97C6) --------------------------------------------------------
SoundData *LoadSound(const char *pSoundName, SoundData *pOutBuff, unsigned int uID)
{
  SoundData *v3; // edi@1
  int v4; // ecx@1
  //Sound *v5; // eax@1
  SoundHeader *v6; // esi@5
  unsigned int *pDecompressedSize; // ebx@5
  unsigned int v8; // eax@5
  unsigned int v9; // eax@7
  SoundData *result; // eax@9
  int v11; // esi@15
  int v12; // eax@15
  char *v13; // ecx@16
  int v14; // eax@19
  std::string v15; // [sp-18h] [bp-34h]@12
  const char *v16; // [sp-8h] [bp-24h]@12
  int v17; // [sp-4h] [bp-20h]@12
  char v18; // [sp+Ch] [bp-10h]@12
  int v19; // [sp+10h] [bp-Ch]@5
  DWORD NumberOfBytesRead; // [sp+14h] [bp-8h]@8
  const char *pSoundName_; // [sp+18h] [bp-4h]@1

  pSoundName_ = pSoundName;
  v3 = pOutBuff;
  v4 = 0;
  for (uint i = 0; i < 3000; ++i)
    if (pSounds[i].uID == uID)
      return pSounds[i].pSoundData;

  FindSound_BinSearch(0, pAudioPlayer->uNumSoundHeaders, pSoundName_);
  if ( uFindSound_BinSearch_ResultID == -1 )
  {
    result = 0;
    return result;
  }
  v6 = &pAudioPlayer->pSoundHeaders[uFindSound_BinSearch_ResultID];
  pDecompressedSize = &v6->uDecompressedSize;
  v8 = v6->uDecompressedSize;
  v19 = v6->uDecompressedSize;
  if ( v3 == (SoundData *)-1 )
    v3 = (SoundData *)pAllocator->AllocNamedChunk(0, v8 + 4, pSoundName_);
  SetFilePointer(pAudioPlayer->hAudioSnd, v6->uFileOffset, 0, 0);
  v9 = *pDecompressedSize;
  if ( (signed int)v6->uCompressedSize >= (signed int)*pDecompressedSize )
  {
    v6->uCompressedSize = v9;
    if ( v9 )
    {
      ReadFile(pAudioPlayer->hAudioSnd, (char *)v3 + 4, v9, &NumberOfBytesRead, 0);
    }
    else
    {
      MessageBoxW(nullptr, L"Can't load sound file!", L"E:\\WORK\\MSDEV\\MM7\\MM7\\Code\\Sound.cpp:448", 0);
    }
  }
  else
  {
    uID = (unsigned int)malloc(v6->uCompressedSize);
    ReadFile(pAudioPlayer->hAudioSnd, (LPVOID)uID, v6->uCompressedSize, &NumberOfBytesRead, 0);
    zlib::MemUnzip((char *)v3 + 4, &v6->uDecompressedSize, (const void *)uID, v6->uCompressedSize);
    free((void *)uID);
  }
  if ( v3 )
  {
    v11 = v19;
    v12 = 0;
    *(int *)v3 = v19;
    uLastLoadedSoundID = 0;
    if ( pSounds[0].pSoundData )
    {
      v13 = (char *)&pSounds[0].pSoundData;
      do
      {
        v13 += 128;
        ++v12;
      }
      while ( *(int *)v13 );
      uLastLoadedSoundID = v12;
    }
    v16 = pSounds[v12].SoundName;
    strcpy((char *)v16, pSoundName_);
    v14 = uLastLoadedSoundID++ << 7;
    pSoundList->uTotalLoadedSoundSize += v11;
    pSounds[uLastLoadedSoundID].pSoundData = v3;
    result = v3;
  }
  else
  {
    result = 0;
  }
  return result;
}