/trunk/external/FMOD/examples/loadfrommemory/main.cpp

https://github.com/cpzhang/bud · C++ · 213 lines · 157 code · 40 blank · 16 comment · 33 complexity · dd94c369654cbe6451c8ec168154f0a6 MD5 · raw file

  1. /*===============================================================================================
  2. Load from memory example
  3. Copyright (c), Firelight Technologies Pty, Ltd 2004-2011.
  4. This example is simply a variant of the play sound example, but it loads the data into memory
  5. then uses the 'load from memory' feature of System::createSound.
  6. ===============================================================================================*/
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #include "../../api/inc/fmod.hpp"
  11. #include "../../api/inc/fmod_errors.h"
  12. void ERRCHECK(FMOD_RESULT result)
  13. {
  14. if (result != FMOD_OK)
  15. {
  16. printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  17. exit(-1);
  18. }
  19. }
  20. void LoadFileIntoMemory(const char *name, void **buff, int *length)
  21. {
  22. FILE *fp = fopen(name, "rb");
  23. fseek(fp, 0, SEEK_END);
  24. *length = ftell(fp);
  25. fseek(fp, 0, SEEK_SET);
  26. *buff = malloc(*length);
  27. fread(*buff, *length, 1, fp);
  28. fclose(fp);
  29. }
  30. int main(int argc, char *argv[])
  31. {
  32. FMOD::System *system;
  33. FMOD::Sound *sound1, *sound2, *sound3;
  34. FMOD::Channel *channel = 0;
  35. FMOD_RESULT result;
  36. int key;
  37. unsigned int version;
  38. void *buff = 0;
  39. int length = 0;
  40. FMOD_CREATESOUNDEXINFO exinfo;
  41. /*
  42. Create a System object and initialize.
  43. */
  44. result = FMOD::System_Create(&system);
  45. ERRCHECK(result);
  46. result = system->getVersion(&version);
  47. ERRCHECK(result);
  48. if (version < FMOD_VERSION)
  49. {
  50. printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION);
  51. return 0;
  52. }
  53. result = system->init(32, FMOD_INIT_NORMAL, 0);
  54. ERRCHECK(result);
  55. LoadFileIntoMemory("../media/drumloop.wav", &buff, &length);
  56. memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
  57. exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
  58. exinfo.length = length;
  59. result = system->createSound((const char *)buff, FMOD_HARDWARE | FMOD_OPENMEMORY, &exinfo, &sound1);
  60. ERRCHECK(result);
  61. result = sound1->setMode(FMOD_LOOP_OFF);
  62. ERRCHECK(result);
  63. free(buff); // don't need the original memory any more. Note! If loading as a stream, the memory must stay active so do not free it!
  64. LoadFileIntoMemory("../media/jaguar.wav", &buff, &length);
  65. memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
  66. exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
  67. exinfo.length = length;
  68. result = system->createSound((const char *)buff, FMOD_SOFTWARE | FMOD_OPENMEMORY, &exinfo, &sound2);
  69. ERRCHECK(result);
  70. free(buff); // don't need the original memory any more. Note! If loading as a stream, the memory must stay active so do not free it!
  71. LoadFileIntoMemory("../media/swish.wav", &buff, &length);
  72. memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
  73. exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
  74. exinfo.length = length;
  75. result = system->createSound((const char *)buff, FMOD_HARDWARE | FMOD_OPENMEMORY, &exinfo, &sound3);
  76. ERRCHECK(result);
  77. free(buff); // don't need the original memory any more. Note! If loading as a stream, the memory must stay active so do not free it!
  78. printf("==========================================================================\n");
  79. printf("Load from memory example. Copyright (c) Firelight Technologies 2004-2011.\n");
  80. printf("==========================================================================\n");
  81. printf("\n");
  82. printf("Press '1' to play a mono sound using hardware mixing\n");
  83. printf("Press '2' to play a mono sound using software mixing\n");
  84. printf("Press '3' to play a stereo sound using hardware mixing\n");
  85. printf("Press 'Esc' to quit\n");
  86. printf("\n");
  87. /*
  88. Main loop.
  89. */
  90. do
  91. {
  92. if (_kbhit())
  93. {
  94. key = _getch();
  95. switch (key)
  96. {
  97. case '1' :
  98. {
  99. result = system->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel);
  100. ERRCHECK(result);
  101. break;
  102. }
  103. case '2' :
  104. {
  105. result = system->playSound(FMOD_CHANNEL_FREE, sound2, false, &channel);
  106. ERRCHECK(result);
  107. break;
  108. }
  109. case '3' :
  110. {
  111. result = system->playSound(FMOD_CHANNEL_FREE, sound3, false, &channel);
  112. ERRCHECK(result);
  113. break;
  114. }
  115. }
  116. }
  117. system->update();
  118. {
  119. unsigned int ms = 0;
  120. unsigned int lenms = 0;
  121. bool playing = 0;
  122. bool paused = 0;
  123. int channelsplaying = 0;
  124. if (channel)
  125. {
  126. FMOD::Sound *currentsound = 0;
  127. result = channel->isPlaying(&playing);
  128. if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  129. {
  130. ERRCHECK(result);
  131. }
  132. result = channel->getPaused(&paused);
  133. if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  134. {
  135. ERRCHECK(result);
  136. }
  137. result = channel->getPosition(&ms, FMOD_TIMEUNIT_MS);
  138. if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  139. {
  140. ERRCHECK(result);
  141. }
  142. channel->getCurrentSound(&currentsound);
  143. if (currentsound)
  144. {
  145. result = currentsound->getLength(&lenms, FMOD_TIMEUNIT_MS);
  146. if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
  147. {
  148. ERRCHECK(result);
  149. }
  150. }
  151. }
  152. system->getChannelsPlaying(&channelsplaying);
  153. printf("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);
  154. }
  155. Sleep(10);
  156. } while (key != 27);
  157. printf("\n");
  158. /*
  159. Shut down
  160. */
  161. result = sound1->release();
  162. ERRCHECK(result);
  163. result = sound2->release();
  164. ERRCHECK(result);
  165. result = sound3->release();
  166. ERRCHECK(result);
  167. result = system->close();
  168. ERRCHECK(result);
  169. result = system->release();
  170. ERRCHECK(result);
  171. return 0;
  172. }