#include <mmsystem.h>
//-lwinmm

bool meloInited=false, meloTurnOn=false, meloTurnOff=false,
     meloBlurOn=false, meloBlurOff=false, meloQuit=false;
int meloS=0, meloL, meloP, meloVolume=0;
int meloFc=0;
char meloF [100] [32];
static HANDLE gWThreadM=NULL;
DWORD wThreadIdM;

void melo ()
{
 if (meloFc==0)
 {
  WIN32_FIND_DATA FindFileData;
  HANDLE hf;

  char midiPath [_MAX_PATH];
  memset (midiPath, 0, _MAX_PATH);
  sprintf (midiPath, "%s%s", AppDir, "data\\melody\\*.mp3");
  hf=FindFirstFile (midiPath, &FindFileData);

  int c=0;
  if (hf!=INVALID_HANDLE_VALUE)
  {
   while (true)
   {
    if (FindFileData.dwFileAttributes!=FILE_ATTRIBUTE_DIRECTORY)
    {
     sprintf (meloF [c], "%s", FindFileData.cFileName);
     c++;
    }

    if (FindNextFile(hf, &FindFileData)==0 || meloFc>=100)
     break;
   }
  }

  meloFc=c;

  // initialize random seed:
  srand (time (NULL));
 }

 if (meloFc>0)
 {
  int rndM=rand ()%meloFc;
  char fileOpen [_MAX_PATH], t [32];
  memset (t, 0, 32);
  sprintf (fileOpen, "%s%s%s%s%s", "open \"", AppDir, "data\\melody\\", meloF [rndM], "\" alias file");
  mciSendString (fileOpen, NULL, 0, 0);
  mciSendString("status file length", t, 32, NULL);
  meloL=atoi (t);
  sprintf (fileOpen, "setaudio file volume to %d", meloVol);
  mciSendString (fileOpen, NULL, 0, 0);
  meloVolume=meloVol;
  mciSendString ("play file", NULL, 0, 0);
 }
 else
 {
  meloIs=false;
  meloS=0;
 }
}

void meloOff ()
{
 char t [32];
 memset (t, 0, 32);
 mciSendString ("status file mode", t, 32, NULL);

 if (!strcmp (t, "stopped"))
  mciSendString ("close file", NULL, 0, 0);
 else if (!strcmp (t, "playing"))
  mciSendString ("pause file", NULL, 0, 0);
}

void meloOn ()
{
 if (meloS==0)
 {
  meloS=1;
  melo ();
 }
 else
 {
  char t [32];
  memset (t, 0, 32);
  mciSendString ("status file mode", t, 32, NULL);

  if (!strcmp (t, "paused"))
   mciSendString ("play file", NULL, 0, 0);
  else if (strcmp (t, "playing"))
   melo ();
 }
}

void meloCheck ()
{
 char t [32];
 memset (t, 0, 32);
 mciSendString ("status file mode", t, 32, NULL);

 if (strcmp (t, "playing"))
 {
  memset (t, 0, 32);
  mciSendString("status file position", t, 32, NULL);
  meloP=atoi (t);

  if (meloL==meloP)
  {
   mciSendString ("close file", NULL, 0, 0);
   melo ();
  }
 }
}

void meloOut ()
{
 meloOff ();

 if (meloS==1)
  meloS=2;
}

DWORD WINAPI meloThread (LPVOID)
{
 int mTc=0;

 while (true)
 {
  mTc++;

  if (!meloInited && (meloIs || meloTurnOn))
  {
   audioOnce=true;

   meloInited=true;
   meloTurnOn=true;
   meloBlurOn=false;
   meloBlurOff=false;
  }
  else if (meloTurnOn)
  {
   meloOn ();
   meloTurnOn=false;
   meloIs=true;
  }
  else if (meloTurnOff)
  {
   meloOff ();
   meloTurnOff=false;
   meloIs=false;
  }
  else if (meloBlurOff)
  {
   if (meloIs && meloS==1)
    meloOn ();
   meloBlurOff=false;
  }
  else if (meloBlurOn)
  {
   if (meloIs && meloS==1)
    meloOff ();
   meloBlurOn=false;
  }
  else if (meloQuit)
  {
   meloOut ();
   break;
  }
  if (mTc==100)
  {
   if (meloIs)
    meloCheck ();
   mTc=0;
  }

  if (meloVolume!=meloVol)
  {
   meloVolume=meloVol;
   char fileOpen [_MAX_PATH];
   sprintf (fileOpen, "setaudio file volume to %d", meloVol);
   mciSendString (fileOpen, NULL, 0, 0);
  }

  Sleep (25);
 }

 meloQuit=false;
}

void meloInit ()
{
 if (gWThreadM==NULL)
 {
  gWThreadM=CreateThread (NULL, 0, meloThread, (void *)"meloThread", 0, &wThreadIdM);
  Sleep (10);
 }
}

void meloClose ()
{
 if (meloInited)
 {
  meloQuit=true;

  for (int i=0; i<10 && meloQuit; i++)
   Sleep (10);

  CloseHandle (gWThreadM);
 }
}