CFMOD.h
1 #ifndef CFMOD_H 2 #define CFMOD_H 3 4 #include "qfmod.h" 5 6 struct Sound_t 7 { 8 char *pszName; 9 FMOD_SOUND *pSound; 10 FMOD_CHANNEL *pChannel; 11 Sound_t *pNext; 12 }; 13 14 #ifdef PlaySound 15 #undef PlaySound 16 #endif 17 18 class CFmod 19 { 20 public: 21 CFmod(); 22 ~CFmod(); 23 public: 24 void Init(void); 25 void PlaySound(char *pszFileName); 26 void StopSound(char *pszFileName); 27 void Shutdown(void); 28 private: 29 Sound_t* FindSound(char *pszFileName); 30 bool CacheSound(char *pszFileName); 31 private: 32 FMOD_SYSTEM *m_pSystem; 33 Sound_t *m_pBaseSound; 34 }; 35 36 #endif
CFMOD.cpp
1 #include <metahook.h> 2 3 #include "qfmod.h" 4 5 #include "cfmod.h" 6 7 extern IFileSystem *g_pFileSystem; 8 9 CFmod::CFmod() 10 { 11 m_pSystem = NULL; 12 m_pBaseSound = NULL; 13 } 14 15 CFmod::~CFmod() 16 { 17 m_pSystem = NULL; 18 m_pBaseSound = NULL; 19 } 20 21 void CFmod::Init(void) 22 { 23 CFmod(); 24 25 qFMOD_System_Create(&m_pSystem); 26 qFMOD_System_Init(m_pSystem, 32, FMOD_INIT_NORMAL, NULL); 27 } 28 29 void CFmod::PlaySound(char *pszFileName) 30 { 31 if (!m_pSystem) 32 return; 33 34 Sound_t *pSound = FindSound(pszFileName); 35 36 if (!pSound) 37 { 38 if (!CacheSound(pszFileName)) 39 return; 40 } 41 42 pSound = FindSound(pszFileName); 43 44 if (!pSound) 45 return; 46 47 qFMOD_System_PlaySound(m_pSystem, FMOD_CHANNEL_FREE, pSound->pSound, NULL, &pSound->pChannel); 48 } 49 50 void CFmod::StopSound(char *pszFileName) 51 { 52 if (!m_pSystem) 53 return; 54 55 if (pszFileName != NULL) 56 { 57 Sound_t *pSound = FindSound(pszFileName); 58 59 if (!pSound) 60 return; 61 62 if (pSound->pChannel) 63 qFMOD_Channel_Stop(pSound->pChannel); 64 } 65 else 66 { 67 for (Sound_t *p = m_pBaseSound; p; p = p->pNext) 68 { 69 if (p->pChannel) 70 qFMOD_Channel_Stop(p->pChannel); 71 } 72 } 73 } 74 75 void CFmod::Shutdown(void) 76 { 77 if (!m_pSystem) 78 return; 79 80 Sound_t *p = m_pBaseSound; 81 Sound_t *t; 82 83 while (p) 84 { 85 t = p->pNext; 86 87 free(p->pszName); 88 qFMOD_Sound_Release(p->pSound); 89 delete p; 90 91 p = t; 92 } 93 94 qFMOD_System_Close(m_pSystem); 95 qFMOD_System_Release(m_pSystem); 96 } 97 98 Sound_t* CFmod::FindSound(char *pszFileName) 99 { 100 if (!m_pSystem) 101 return NULL; 102 103 for (Sound_t *p = m_pBaseSound; p; p = p->pNext) 104 { 105 if (p->pszName && !strcmp(p->pszName, pszFileName)) 106 return p; 107 } 108 109 return NULL; 110 } 111 112 bool CFmod::CacheSound(char *pszFileName) 113 { 114 if (!m_pSystem) 115 return false; 116 117 FileHandle_t pFile; 118 uint32 iFileLen; 119 BYTE *pBuffer; 120 FMOD_CREATESOUNDEXINFO ExInfo; 121 FMOD_SOUND *pSound; 122 Sound_t *pCache; 123 124 pFile = g_pFileSystem->Open(pszFileName, "rb"); 125 126 if (!pFile) 127 { 128 return false; 129 } 130 131 g_pFileSystem->Seek(pFile, 0, FILESYSTEM_SEEK_TAIL); 132 iFileLen = g_pFileSystem->Tell(pFile); 133 g_pFileSystem->Seek(pFile, 0, FILESYSTEM_SEEK_HEAD); 134 135 pBuffer = (BYTE *)malloc(iFileLen); 136 g_pFileSystem->Read(pBuffer, iFileLen, pFile); 137 138 g_pFileSystem->Close(pFile); 139 140 memset(&ExInfo, 0, sizeof(ExInfo)); 141 ExInfo.cbsize = sizeof(ExInfo); 142 ExInfo.length = iFileLen; 143 144 if (qFMOD_System_CreateSound(m_pSystem, (const char *)pBuffer, FMOD_HARDWARE | FMOD_OPENMEMORY, &ExInfo, &pSound) != FMOD_OK) 145 { 146 free(pBuffer); 147 return false; 148 } 149 150 free(pBuffer); 151 152 pCache = new Sound_t; 153 pCache->pszName = (char *)malloc(strlen(pszFileName) * sizeof(char) + 1); 154 strcpy(pCache->pszName, pszFileName); 155 pCache->pSound = pSound; 156 pCache->pChannel = NULL; 157 pCache->pNext = m_pBaseSound; 158 m_pBaseSound = pCache; 159 160 return true; 161 }