Guest User

Untitled

a guest
Jun 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include "Mmsystem.h"
  2. #pragma comment(lib,"Winmm.lib")
  3.  
  4. enum cSoundDefintions
  5. {
  6.  
  7.     CSOUND_SUCCESS,
  8.     ERR_INVALID_FILETYPE,
  9.     ERR_PLAY_WAV,
  10.     ERR_INVALID_COMMAND,
  11.     CSOUND_PLAY,
  12.     CSOUND_STOP,
  13.     CSOUND_FILE
  14.  
  15. };
  16.  
  17. class cSound
  18. {
  19.     char PathToFile[MAX_PATH];
  20.     char tFile[MAX_PATH];
  21.     char * s8_Buf;
  22.     public:
  23.     cSound(char*);
  24.     DWORD Command(cSoundDefintions, char*);
  25. };
  26.  
  27.  
  28. cSound::cSound(char * FilePath)
  29. {
  30.     strcpy(PathToFile, FilePath);
  31. }
  32.  
  33. DWORD cSound::Command(cSoundDefintions command, char * FilePath = "placeholder")
  34. {
  35.     switch(command)
  36.     {
  37.  
  38.     case CSOUND_FILE:
  39.         {
  40.         Command(CSOUND_STOP);
  41.         strcpy(PathToFile, FilePath);
  42.         return CSOUND_SUCCESS;
  43.         }
  44.         break;
  45.     case CSOUND_PLAY:
  46.         {
  47.             if(strcmp(FilePath, "placeholder") == 0)
  48.                 strcpy(tFile, PathToFile);
  49.             if(strcmp(FilePath, "placeholder") != 0)
  50.                 strcpy(tFile, FilePath);
  51.  
  52.             DWORD u32_Attr = GetFileAttributes(tFile);
  53.             if (u32_Attr == 0xFFFFFFFF || (u32_Attr & FILE_ATTRIBUTE_DIRECTORY))
  54.                 return ERROR_FILE_NOT_FOUND;
  55.  
  56.             char *p_s8Ext = strrchr(tFile, '.');
  57.             s8_Buf = new char[300];
  58.  
  59.             if (stricmp(p_s8Ext, ".wav") == 0)
  60.             {
  61.                 Command(CSOUND_STOP);
  62.                 if (!PlaySound(tFile, 0, SND_FILENAME | SND_ASYNC))
  63.                     return ERR_PLAY_WAV;
  64.  
  65.                 return CSOUND_SUCCESS;
  66.             }
  67.             DWORD u32_Err;
  68.             if (stricmp(p_s8Ext, ".mp3") == 0)
  69.             {
  70.                 Command(CSOUND_STOP);
  71.                 if (strcmp("", tFile) != 0)
  72.                 {
  73.                     mciSendString("close all", 0, 0, 0);
  74.                     sprintf(s8_Buf, "open \"%s\" type mpegvideo alias MidiDemo", tFile);
  75.                     if (u32_Err = mciSendString(s8_Buf, 0, 0, 0))
  76.                     {
  77.                         delete [] s8_Buf;
  78.                         return u32_Err;
  79.                     }
  80.                     delete [] s8_Buf;
  81.                 }
  82.  
  83.                 if (u32_Err = mciSendString("play MidiDemo from 0", 0, 0, 0))
  84.                 {
  85.                     if (u32_Err == 2)
  86.                         u32_Err = MCIERR_SEQ_NOMIDIPRESENT;
  87.                     return u32_Err;
  88.                 }
  89.  
  90.                 return CSOUND_SUCCESS;
  91.             }
  92.  
  93.             if (!stricmp(p_s8Ext, ".mid") || !stricmp(p_s8Ext, ".midi") || !stricmp(p_s8Ext, ".rmi"))
  94.             {
  95.                 Command(CSOUND_STOP);
  96.                 if (strcmp("", tFile) != 0)
  97.                 {
  98.                     mciSendString("close all", 0, 0, 0);
  99.                     sprintf(s8_Buf, "open \"%s\" type sequencer alias MidiDemo", tFile);
  100.                     if (u32_Err = mciSendString(s8_Buf, 0, 0, 0))
  101.                     {
  102.                         delete [] s8_Buf;
  103.                         return u32_Err;
  104.                     }
  105.                     delete [] s8_Buf;
  106.                 }
  107.  
  108.                 if (u32_Err = mciSendString("play MidiDemo from 0", 0, 0, 0))
  109.                 {
  110.                     if (u32_Err == 2)
  111.                         u32_Err = MCIERR_SEQ_NOMIDIPRESENT;
  112.                     return u32_Err;
  113.                 }
  114.  
  115.                 return CSOUND_SUCCESS;
  116.             }
  117.  
  118.             return ERR_INVALID_FILETYPE;
  119.         }
  120.         break;
  121.  
  122.     case CSOUND_STOP:
  123.         {
  124.             PlaySound(0, 0, SND_PURGE);
  125.             mciSendString("stop MidiDemo", 0, 0, 0);
  126.             return CSOUND_SUCCESS;
  127.         }
  128.         break;
  129.  
  130.     default:
  131.         return ERR_INVALID_COMMAND;
  132.     }
  133.  
  134. }
Add Comment
Please, Sign In to add comment