CMusicHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include "../stdafx.h"
  2. #include <sstream>
  3. #include <boost/assign/std/vector.hpp>
  4. #include <boost/assign/list_of.hpp>
  5. #include <boost/bimap.hpp>
  6. #include <SDL_mixer.h>
  7. #include "CSndHandler.h"
  8. #include "CMusicHandler.h"
  9. #include "CCreatureHandler.h"
  10. #include "CSpellHandler.h"
  11. #include "../client/CGameInfo.h"
  12. /*
  13. * CMusicHandler.cpp, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. using namespace boost::assign;
  22. static boost::bimap<soundBase::soundID, std::string> sounds;
  23. // Not pretty, but there's only one music handler object in the game.
  24. static void musicFinishedCallbackC(void) {
  25. CGI->musich->musicFinishedCallback();
  26. }
  27. void CAudioBase::init()
  28. {
  29. if (initialized)
  30. return;
  31. if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1)
  32. {
  33. tlog1 << "Mix_OpenAudio error: %s!!!" << Mix_GetError() << std::endl;
  34. return;
  35. }
  36. initialized = true;
  37. }
  38. void CAudioBase::release()
  39. {
  40. if (initialized) {
  41. Mix_CloseAudio();
  42. initialized = false;
  43. }
  44. }
  45. void CAudioBase::setVolume(unsigned int percent)
  46. {
  47. if (percent > 100)
  48. percent = 100;
  49. volume = percent;
  50. }
  51. CSoundHandler::CSoundHandler(): sndh(NULL)
  52. {
  53. // Map sound names
  54. #define VCMI_SOUND_NAME(x) ( soundBase::x,
  55. #define VCMI_SOUND_FILE(y) #y )
  56. sounds = boost::assign::list_of<boost::bimap<soundBase::soundID, std::string>::relation>
  57. VCMI_SOUND_LIST;
  58. #undef VCMI_SOUND_NAME
  59. #undef VCMI_SOUND_FILE
  60. // Vectors for helper(s)
  61. pickupSounds += soundBase::pickup01, soundBase::pickup02, soundBase::pickup03,
  62. soundBase::pickup04, soundBase::pickup05, soundBase::pickup06, soundBase::pickup07;
  63. horseSounds += // must be the same order as terrains (see EtrrainType);
  64. soundBase::horseDirt, soundBase::horseSand, soundBase::horseGrass,
  65. soundBase::horseSnow, soundBase::horseSwamp, soundBase::horseRough,
  66. soundBase::horseSubterranean, soundBase::horseLava,
  67. soundBase::horseWater, soundBase::horseRock;
  68. };
  69. void CSoundHandler::init()
  70. {
  71. CAudioBase::init();
  72. if (initialized)
  73. // Load sounds
  74. sndh = new CSndHandler(std::string(DATA_DIR "/Data/Heroes3.snd"));
  75. }
  76. void CSoundHandler::release()
  77. {
  78. if (initialized) {
  79. Mix_HaltChannel(-1);
  80. delete sndh;
  81. std::map<soundBase::soundID, Mix_Chunk *>::iterator it;
  82. for (it=soundChunks.begin(); it != soundChunks.end(); it++) {
  83. if (it->second)
  84. Mix_FreeChunk(it->second);
  85. }
  86. }
  87. CAudioBase::release();
  88. }
  89. // Allocate an SDL chunk and cache it.
  90. Mix_Chunk *CSoundHandler::GetSoundChunk(soundBase::soundID soundID)
  91. {
  92. // Find its name
  93. boost::bimap<soundBase::soundID, std::string>::left_iterator it;
  94. it = sounds.left.find(soundID);
  95. if (it == sounds.left.end())
  96. return NULL;
  97. // Load and insert
  98. int size;
  99. const char *data = sndh->extract(it->second, size);
  100. if (!data)
  101. return NULL;
  102. SDL_RWops *ops = SDL_RWFromConstMem(data, size);
  103. Mix_Chunk *chunk;
  104. chunk = Mix_LoadWAV_RW(ops, 1); // will free ops
  105. if (!chunk) {
  106. tlog1 << "Unable to mix sound" << it->second << "(" << Mix_GetError() << ")" << std::endl;
  107. return NULL;
  108. }
  109. soundChunks.insert(std::pair<soundBase::soundID, Mix_Chunk *>(soundID, chunk));
  110. return chunk;
  111. }
  112. // Get a soundID given a filename
  113. soundBase::soundID CSoundHandler::getSoundID(std::string &fileName)
  114. {
  115. boost::bimap<soundBase::soundID, std::string>::right_iterator it;
  116. it = sounds.right.find(fileName);
  117. if (it == sounds.right.end())
  118. return soundBase::invalid;
  119. else
  120. return it->second;
  121. }
  122. void CSoundHandler::initCreaturesSounds(std::vector<CCreature> &creatures)
  123. {
  124. tlog5 << "\t\tReading config/cr_sounds.txt" << std::endl;
  125. std::ifstream ifs(DATA_DIR "/config/cr_sounds.txt");
  126. std::string line;
  127. CBattleSounds.resize(creatures.size());
  128. while(getline(ifs, line))
  129. {
  130. std::string cname="", attack="", defend="", killed="", move="",
  131. shoot="", wince="", ext1="", ext2="";
  132. std::istringstream str(line);
  133. str >> cname >> attack >> defend >> killed >> move >> shoot >> wince >> ext1 >> ext2;
  134. if (cname[0] == '#')
  135. // That's a comment. Discard.
  136. continue;
  137. if (str.good() || (str.eof() && wince != ""))
  138. {
  139. int id = -1;
  140. std::map<std::string,int>::iterator i = CGI->creh->nameToID.find(cname);
  141. if(i != CGI->creh->nameToID.end())
  142. id = i->second;
  143. else
  144. {
  145. tlog1 << "Sound info for an unknown creature: " << cname << std::endl;
  146. continue;
  147. }
  148. if (CBattleSounds[id].killed != soundBase::invalid)
  149. tlog1 << "Creature << " << cname << " already has sounds" << std::endl;
  150. CBattleSounds[id].attack = getSoundID(attack);
  151. CBattleSounds[id].defend = getSoundID(defend);
  152. CBattleSounds[id].killed = getSoundID(killed);
  153. CBattleSounds[id].move = getSoundID(move);
  154. CBattleSounds[id].shoot = getSoundID(shoot);
  155. CBattleSounds[id].wince = getSoundID(wince);
  156. CBattleSounds[id].ext1 = getSoundID(ext1);
  157. CBattleSounds[id].ext2 = getSoundID(ext2);
  158. // Special creatures
  159. if (id == 55 || // Archdevil
  160. id == 62 || // Vampire
  161. id == 62) // Vampire Lord
  162. {
  163. CBattleSounds[id].startMoving = CBattleSounds[id].ext1;
  164. CBattleSounds[id].endMoving = CBattleSounds[id].ext2;
  165. }
  166. }
  167. }
  168. ifs.close();
  169. ifs.clear();
  170. //commented to avoid spurious warnings
  171. /*
  172. // Find creatures without sounds
  173. for(unsigned int i=0;i<creatures.size();i++)
  174. {
  175. // Note: this will exclude war machines, but it's better
  176. // than nothing.
  177. if (vstd::contains(CGI->creh->notUsedMonsters, i))
  178. continue;
  179. CCreature &c = creatures[i];
  180. if (c.sounds.killed == soundBase::invalid)
  181. tlog1 << "creature " << c.idNumber << " doesn't have sounds" << std::endl;
  182. }*/
  183. }
  184. void CSoundHandler::initSpellsSounds(std::vector<CSpell> &spells)
  185. {
  186. tlog5 << "\t\tReading config/sp_sounds.txt" << std::endl;
  187. std::ifstream ifs(DATA_DIR "/config/sp_sounds.txt");
  188. std::string line;
  189. while(getline(ifs, line))
  190. {
  191. int spellid;
  192. std::string soundfile="";
  193. std::istringstream str(line);
  194. str >> spellid >> soundfile;
  195. if (str.good() || (str.eof() && soundfile != ""))
  196. {
  197. CSpell &s = CGI->spellh->spells[spellid];
  198. if (s.soundID != soundBase::invalid)
  199. tlog1 << "Spell << " << spellid << " already has a sound" << std::endl;
  200. s.soundID = getSoundID(soundfile);
  201. }
  202. }
  203. ifs.close();
  204. ifs.clear();
  205. }
  206. // Plays a sound, and return its channel so we can fade it out later
  207. int CSoundHandler::playSound(soundBase::soundID soundID, int repeats)
  208. {
  209. if (!initialized)
  210. return -1;
  211. int channel;
  212. Mix_Chunk *chunk = GetSoundChunk(soundID);
  213. if (chunk)
  214. {
  215. channel = Mix_PlayChannel(-1, chunk, repeats);
  216. if (channel == -1)
  217. tlog1 << "Unable to play sound file " << soundID << std::endl;
  218. } else {
  219. channel = -1;
  220. }
  221. return channel;
  222. }
  223. // Helper. Randomly select a sound from an array and play it
  224. int CSoundHandler::playSoundFromSet(std::vector<soundBase::soundID> &sound_vec)
  225. {
  226. return playSound(sound_vec[rand() % sound_vec.size()]);
  227. }
  228. void CSoundHandler::stopSound( int handler )
  229. {
  230. if (initialized && handler != -1)
  231. Mix_HaltChannel(handler);
  232. }
  233. // Sets the sound volume, from 0 (mute) to 100
  234. void CSoundHandler::setVolume(unsigned int percent)
  235. {
  236. CAudioBase::setVolume(percent);
  237. if (initialized)
  238. Mix_Volume(-1, (MIX_MAX_VOLUME * volume)/100);
  239. }
  240. CMusicHandler::CMusicHandler(): currentMusic(NULL), nextMusic(NULL)
  241. {
  242. // Map music IDs
  243. #define VCMI_MUSIC_ID(x) ( musicBase::x ,
  244. #define VCMI_MUSIC_FILE(y) y )
  245. musics = map_list_of
  246. VCMI_MUSIC_LIST;
  247. #undef VCMI_MUSIC_NAME
  248. #undef VCMI_MUSIC_FILE
  249. // Vector for helper
  250. battleMusics += musicBase::combat1, musicBase::combat2,
  251. musicBase::combat3, musicBase::combat4;
  252. }
  253. void CMusicHandler::init()
  254. {
  255. CAudioBase::init();
  256. if (initialized)
  257. Mix_HookMusicFinished(musicFinishedCallbackC);
  258. }
  259. void CMusicHandler::release()
  260. {
  261. if (initialized) {
  262. Mix_HookMusicFinished(NULL);
  263. musicMutex.lock();
  264. if (currentMusic)
  265. {
  266. Mix_HaltMusic();
  267. Mix_FreeMusic(currentMusic);
  268. }
  269. if (nextMusic)
  270. Mix_FreeMusic(nextMusic);
  271. musicMutex.unlock();
  272. }
  273. CAudioBase::release();
  274. }
  275. // Plays a music
  276. // loop: -1 always repeats, 0=do not play, 1+=number of loops
  277. void CMusicHandler::playMusic(musicBase::musicID musicID, int loop)
  278. {
  279. if (!initialized)
  280. return;
  281. std::string filename = DATA_DIR "/Mp3/";
  282. filename += musics[musicID];
  283. musicMutex.lock();
  284. if (nextMusic)
  285. {
  286. // There's already a music queued, so remove it
  287. Mix_FreeMusic(nextMusic);
  288. nextMusic = NULL;
  289. }
  290. if (currentMusic)
  291. {
  292. // A music is already playing. Stop it and the callback will
  293. // start the new one
  294. nextMusic = LoadMUS(filename.c_str());
  295. nextMusicLoop = loop;
  296. Mix_FadeOutMusic(1000);
  297. }
  298. else
  299. {
  300. currentMusic = LoadMUS(filename.c_str());
  301. PlayMusic(currentMusic,loop);
  302. }
  303. musicMutex.unlock();
  304. }
  305. // Helper. Randomly select a music from an array and play it
  306. void CMusicHandler::playMusicFromSet(std::vector<musicBase::musicID> &music_vec, int loop)
  307. {
  308. playMusic(music_vec[rand() % music_vec.size()], loop);
  309. }
  310. // Stop and free the current music
  311. void CMusicHandler::stopMusic(int fade_ms)
  312. {
  313. if (!initialized)
  314. return;
  315. musicMutex.lock();
  316. if (currentMusic)
  317. {
  318. Mix_FadeOutMusic(fade_ms);
  319. }
  320. musicMutex.unlock();
  321. }
  322. // Sets the music volume, from 0 (mute) to 100
  323. void CMusicHandler::setVolume(unsigned int percent)
  324. {
  325. CAudioBase::setVolume(percent);
  326. if (initialized)
  327. Mix_VolumeMusic((MIX_MAX_VOLUME * volume)/100);
  328. }
  329. // Called by SDL when a music finished.
  330. void CMusicHandler::musicFinishedCallback(void)
  331. {
  332. musicMutex.lock();
  333. if (currentMusic)
  334. {
  335. Mix_FreeMusic(currentMusic);
  336. currentMusic = NULL;
  337. }
  338. if (nextMusic)
  339. {
  340. currentMusic = nextMusic;
  341. nextMusic = NULL;
  342. PlayMusic(currentMusic,nextMusicLoop);
  343. }
  344. musicMutex.unlock();
  345. }
  346. Mix_Music * CMusicHandler::LoadMUS(const char *file)
  347. {
  348. Mix_Music *ret = Mix_LoadMUS(file);
  349. if(!ret) //load music and check for error
  350. tlog1 << "Unable to load music file (" << file <<"). Error: " << Mix_GetError() << std::endl;
  351. return ret;
  352. }
  353. int CMusicHandler::PlayMusic(Mix_Music *music, int loops)
  354. {
  355. int ret = Mix_PlayMusic(music, loops);
  356. if(ret == -1)
  357. tlog1 << "Unable to play music (" << Mix_GetError() << ")" << std::endl;
  358. return ret;
  359. }