#include <mmsystem.h>
//-lwinmm

static HANDLE gWThread1=NULL, gWThread2=NULL, gWThread3=NULL, gWThread4=NULL, gWThread5=NULL;
DWORD wThreadId1, wThreadId2, wThreadId3, wThreadId4, wThreadId5;
bool soundInited=false, soundGo=false, soundQuit=false;
int soundN=0, soundQ=5;
int psVolume1=100, psVolume2=100, psVolume3=100, psVolume4=100, psVolume5=100;
int pfVolume1=100, pfVolume2=100, pfVolume3=100, pfVolume4=100, pfVolume5=100;
char psChar1 [32], psChar2 [32], psChar3 [32], psChar4 [32], psChar5 [32],
     charExt [4]={"mp3"};

void soundPlay (int psVolume, int pfVolume, char psChar [32])
{
 char soundOpen [_MAX_PATH];
 sprintf (soundOpen, "%s%s%s%s%s%s%s", "open \"", AppDir, "data\\sound\\", psChar, ".", charExt, "\" alias sound");
 mciSendString (soundOpen, NULL, 0, 0);

 sprintf (soundOpen, "setaudio sound volume to %d", (pfVolume>0 ? pfVolume*10 : soundVol)*psVolume/100);
 mciSendString (soundOpen, NULL, 0, 0);

 mciSendString ("play sound wait", NULL, 0, 0);
 mciSendString ("close sound", NULL, 0, 0);
}

DWORD WINAPI soundThread1 (LPVOID)
{
 while (!soundQuit)
 {
  if (soundN==1 && soundGo)
  {
   soundGo=false;
   soundPlay (psVolume1, pfVolume1, psChar1);
  }

  Sleep (10);
 }
}

DWORD WINAPI soundThread2 (LPVOID)
{
 while (!soundQuit)
 {
  if (soundN==2 && soundGo)
  {
   soundGo=false;
   soundPlay (psVolume2, pfVolume2, psChar2);
  }

  Sleep (10);
 }
}

DWORD WINAPI soundThread3 (LPVOID)
{
 while (!soundQuit)
 {
  if (soundN==3 && soundGo)
  {
   soundGo=false;
   soundPlay (psVolume3, pfVolume3, psChar3);
  }

  Sleep (10);
 }
}

DWORD WINAPI soundThread4 (LPVOID)
{
 while (!soundQuit)
 {
  if (soundN==4 && soundGo)
  {
   soundGo=false;
   soundPlay (psVolume4, pfVolume4, psChar4);
  }

  Sleep (10);
 }
}

DWORD WINAPI soundThread5 (LPVOID)
{
 while (!soundQuit)
 {
  if (soundN==5 && soundGo)
  {
   soundGo=false;
   soundPlay (psVolume5, pfVolume5, psChar5);
  }

  Sleep (10);
 }
}

void soundInit ()
{
 if (gWThread1==NULL)
 {
  gWThread1=CreateThread (NULL, 0, soundThread1, (void *)"soundThread1", 0, &wThreadId1);
  Sleep (1);
 }
 if (gWThread2==NULL)
 {
  gWThread2=CreateThread (NULL, 0, soundThread2, (void *)"soundThread2", 0, &wThreadId2);
  Sleep (1);
 }
 if (gWThread3==NULL)
 {
  gWThread3=CreateThread (NULL, 0, soundThread3, (void *)"soundThread3", 0, &wThreadId3);
  Sleep (1);
 }
 if (gWThread4==NULL)
 {
  gWThread4=CreateThread (NULL, 0, soundThread4, (void *)"soundThread4", 0, &wThreadId4);
  Sleep (1);
 }
 if (gWThread5==NULL)
 {
  gWThread5=CreateThread (NULL, 0, soundThread5, (void *)"soundThread5", 0, &wThreadId5);
  Sleep (1);
 }

 soundInited=true;
}

void playSound (LPCSTR sound, int volume=100, int force=0)
{
 if (!soundInited)
  soundInit ();

 if (soundOn==true || force>0)
 {
  audioOnce=true;

  if (soundN<soundQ)
   soundN++;
  else
   soundN=1;

  if (soundN==1)
  {
   psVolume1=volume;
   pfVolume1=force;
   strcpy (psChar1, sound);
  }
  else if (soundN==2)
  {
   psVolume2=volume;
   pfVolume2=force;
   strcpy (psChar2, sound);
  }
  else if (soundN==3)
  {
   psVolume3=volume;
   pfVolume3=force;
   strcpy (psChar3, sound);
  }
  else if (soundN==4)
  {
   psVolume4=volume;
   pfVolume4=force;
   strcpy (psChar4, sound);
  }
  else if (soundN==5)
  {
   psVolume5=volume;
   pfVolume5=force;
   strcpy (psChar5, sound);
  }

  soundGo=true;
 }
}

void soundClose ()
{
 if (soundInited)
 {
  soundQuit=true;

  if (gWThread1!=NULL)
   CloseHandle (gWThread1);
  if (gWThread2!=NULL)
   CloseHandle (gWThread2);
  if (gWThread3!=NULL)
   CloseHandle (gWThread3);
  if (gWThread4!=NULL)
   CloseHandle (gWThread4);
  if (gWThread5!=NULL)
   CloseHandle (gWThread5);
 }
}