PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/miles/FE_r2/src/ut/play-music.cpp

#
C++ | 247 lines | 126 code | 45 blank | 76 comment | 30 complexity | eed9e1ab4f9579521351bb6ca3958bb5 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. /*******************************************************************************
  2. *
  3. * Audio Test
  4. *
  5. *
  6. ******************************************************************************/
  7. #include "stdafx.h"
  8. #include "CppUnitTest.h"
  9. #include "windows.h"
  10. #include "../common/common.h"
  11. #include <conio.h>
  12. #include "fmod.hpp"
  13. #include "fmod_errors.h"
  14. #ifdef _WIN64
  15. #pragma comment (lib, "fmodex64_vc.lib")
  16. #else
  17. #pragma comment (lib, "fmodex_vc.lib")
  18. #endif
  19. using namespace std;
  20. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  21. namespace ut
  22. {
  23. TEST_CLASS(PlayMusicTest)
  24. {
  25. public:
  26. TEST_METHOD(PlayMusicTestXAudio)
  27. {
  28. GloomEngine engine;
  29. LOG_DEBUG("sizeof(GloomEngine)=%d", sizeof(GloomEngine));
  30. auto field = engine.resource()->get("sound", "field.wav");
  31. auto battle = engine.resource()->get("sound", "battle.wav");
  32. auto field_asp = engine.audio()->create(*field.get());
  33. auto battle_asp = engine.audio()->create(*battle.get());
  34. // play backgound sound async
  35. field_asp->play();
  36. Sleep(2000); // 2
  37. // play sound effect async
  38. battle_asp->play();
  39. Sleep(10000); // 8 + 2
  40. // play sound effect synchronized
  41. battle_asp->play(false);
  42. // stop the sound immediately.
  43. battle_asp->stop();
  44. field_asp->stop();
  45. // release the resource.
  46. field_asp.reset();
  47. battle_asp.reset();
  48. // TODO:
  49. //GloomEngine::instance()->font()->register(L"msyh.ttc");
  50. //GloomEngine::instance()->font()->unregister(L"msyh.ttc");
  51. //GloomEngine::instance()->graphic()->loadfont(L"Microsoft YaHei");
  52. //GloomEngine::instance()->graphic()->begin();
  53. //GloomEngine::instance()->graphic()->end();
  54. //GloomEngine::instance()->graphic()->flip();
  55. //GloomEngine::instance()->graphic()->draw();
  56. }
  57. void ERRCHECK(FMOD_RESULT result)
  58. {
  59. if (result != FMOD_OK)
  60. {
  61. printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  62. exit(-1);
  63. }
  64. }
  65. TEST_METHOD(PlayMusicTestFMod)
  66. {
  67. FMOD::System *system;
  68. FMOD::Sound *sound1, *sound2, *sound3;
  69. FMOD::Channel *channel = 0;
  70. FMOD_RESULT result;
  71. int key;
  72. unsigned int version;
  73. /*
  74. Create a System object and initialize.
  75. */
  76. result = FMOD::System_Create(&system);
  77. ERRCHECK(result);
  78. result = system->getVersion(&version);
  79. ERRCHECK(result);
  80. if (version < FMOD_VERSION)
  81. {
  82. //printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION);
  83. LOG_FATAL("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION);
  84. return;
  85. }
  86. result = system->init(32, FMOD_INIT_NORMAL, 0);
  87. ERRCHECK(result);
  88. auto data = FileHelper::readAll("d:/Lady GaGa - Poker Face.mp3");
  89. FMOD_CREATESOUNDEXINFO si = {0};
  90. {
  91. si.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
  92. si.length = data->size();
  93. }
  94. result = system->createSound((const char*)&(*data.get())[0], FMOD_HARDWARE | FMOD_OPENMEMORY | FMOD_CREATECOMPRESSEDSAMPLE, &si, &sound1);
  95. ERRCHECK(result);
  96. //result = sound1->setMode(FMOD_LOOP_OFF); /* drumloop.wav has embedded loop points which automatically makes looping turn on, */
  97. //ERRCHECK(result); /* so turn it off here. We could have also just put FMOD_LOOP_OFF in the above CreateSound call. */
  98. //result = system->createSound("d:/battle.wav", FMOD_SOFTWARE, 0, &sound2);
  99. //ERRCHECK(result);
  100. //result = system->createSound("d:/intro.wav", FMOD_HARDWARE, 0, &sound3);
  101. //ERRCHECK(result);
  102. //printf("===================================================================\n");
  103. //printf("PlaySound Example. Copyright (c) Firelight Technologies 2004-2011.\n");
  104. //printf("===================================================================\n");
  105. //printf("\n");
  106. //printf("Press '1' to play a mono sound using hardware mixing\n");
  107. //printf("Press '2' to play a mono sound using software mixing\n");
  108. //printf("Press '3' to play a stereo sound using hardware mixing\n");
  109. //printf("Press 'Esc' to quit\n");
  110. //printf("\n");
  111. /*
  112. Main loop.
  113. */
  114. {
  115. result = system->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel);
  116. ERRCHECK(result);
  117. //break;
  118. }
  119. do
  120. {
  121. // if (_kbhit())
  122. // {
  123. // key = _getch();
  124. // switch (key)
  125. // {
  126. // case '1' :
  127. /* {
  128. result = system->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel);
  129. ERRCHECK(result);
  130. break;
  131. }*/
  132. // case '2' :
  133. // {
  134. // result = system->playSound(FMOD_CHANNEL_FREE, sound2, false, &channel);
  135. // ERRCHECK(result);
  136. // break;
  137. // }
  138. // case '3' :
  139. // {
  140. // result = system->playSound(FMOD_CHANNEL_FREE, sound3, false, &channel);
  141. // ERRCHECK(result);
  142. // break;
  143. // }
  144. // }
  145. //}
  146. system->update();
  147. {
  148. unsigned int ms = 0;
  149. unsigned int lenms = 0;
  150. bool playing = 0;
  151. bool paused = 0;
  152. int channelsplaying = 0;
  153. if (channel)
  154. {
  155. FMOD::Sound *currentsound = 0;
  156. result = channel->isPlaying(&playing);
  157. if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  158. {
  159. ERRCHECK(result);
  160. }
  161. result = channel->getPaused(&paused);
  162. if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  163. {
  164. ERRCHECK(result);
  165. }
  166. result = channel->getPosition(&ms, FMOD_TIMEUNIT_MS);
  167. if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  168. {
  169. ERRCHECK(result);
  170. }
  171. channel->getCurrentSound(&currentsound);
  172. if (currentsound)
  173. {
  174. result = currentsound->getLength(&lenms, FMOD_TIMEUNIT_MS);
  175. if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  176. {
  177. ERRCHECK(result);
  178. }
  179. }
  180. }
  181. system->getChannelsPlaying(&channelsplaying);
  182. //LOG_DEBUG("Time %02d:%02d:%02d/%02d:%02d:%02d : %s : Channels Playing %2d\r", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? "Paused " : playing ? "Playing" : "Stopped", channelsplaying);
  183. }
  184. Sleep(10);
  185. } while (true);
  186. //printf("\n");
  187. /*
  188. Shut down
  189. */
  190. result = sound1->release();
  191. ERRCHECK(result);
  192. //result = sound2->release();
  193. //ERRCHECK(result);
  194. //result = sound3->release();
  195. //ERRCHECK(result);
  196. result = system->close();
  197. ERRCHECK(result);
  198. result = system->release();
  199. ERRCHECK(result);
  200. }
  201. };
  202. }