loopwave.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Program to load a wave file and loop playing it using SDL audio */
  11. /* loopwaves.c is much more robust in handling WAVE files --
  12. This is only for simple WAVEs
  13. */
  14. #include <stdlib.h>
  15. #ifdef __EMSCRIPTEN__
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. #include <SDL3/SDL.h>
  19. #include <SDL3/SDL_main.h>
  20. #include <SDL3/SDL_test.h>
  21. #include "testutils.h"
  22. static struct
  23. {
  24. SDL_AudioSpec spec;
  25. Uint8 *sound; /* Pointer to wave data */
  26. Uint32 soundlen; /* Length of wave data */
  27. Uint32 soundpos;
  28. } wave;
  29. static SDL_AudioDeviceID device;
  30. static SDL_AudioStream *stream;
  31. static void SDLCALL
  32. fillerup(SDL_AudioStream *stream, int len, void *unused)
  33. {
  34. Uint8 *waveptr;
  35. int waveleft;
  36. /*SDL_Log("CALLBACK WANTS %d MORE BYTES!", len);*/
  37. /* Set up the pointers */
  38. waveptr = wave.sound + wave.soundpos;
  39. waveleft = wave.soundlen - wave.soundpos;
  40. /* Go! */
  41. while (waveleft <= len) {
  42. SDL_PutAudioStreamData(stream, waveptr, waveleft);
  43. len -= waveleft;
  44. waveptr = wave.sound;
  45. waveleft = wave.soundlen;
  46. wave.soundpos = 0;
  47. }
  48. SDL_PutAudioStreamData(stream, waveptr, len);
  49. wave.soundpos += len;
  50. }
  51. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  52. static void
  53. quit(int rc)
  54. {
  55. SDL_Quit();
  56. /* Let 'main()' return normally */
  57. if (rc != 0) {
  58. exit(rc);
  59. }
  60. }
  61. static void
  62. close_audio(void)
  63. {
  64. if (device != 0) {
  65. SDL_DestroyAudioStream(stream);
  66. stream = NULL;
  67. SDL_CloseAudioDevice(device);
  68. device = 0;
  69. }
  70. }
  71. static void
  72. open_audio(void)
  73. {
  74. device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &wave.spec);
  75. if (!device) {
  76. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  77. SDL_free(wave.sound);
  78. quit(2);
  79. }
  80. stream = SDL_CreateAndBindAudioStream(device, &wave.spec);
  81. if (!stream) {
  82. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create audio stream: %s\n", SDL_GetError());
  83. SDL_CloseAudioDevice(device);
  84. SDL_free(wave.sound);
  85. quit(2);
  86. }
  87. SDL_SetAudioStreamGetCallback(stream, fillerup, NULL);
  88. }
  89. #ifndef __EMSCRIPTEN__
  90. static void reopen_audio(void)
  91. {
  92. close_audio();
  93. open_audio();
  94. }
  95. #endif
  96. static int done = 0;
  97. #ifdef __EMSCRIPTEN__
  98. static void loop(void)
  99. {
  100. if (done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING)) {
  101. emscripten_cancel_main_loop();
  102. } else {
  103. fillerup();
  104. }
  105. }
  106. #endif
  107. int main(int argc, char *argv[])
  108. {
  109. int i;
  110. char *filename = NULL;
  111. SDLTest_CommonState *state;
  112. /* Initialize test framework */
  113. state = SDLTest_CommonCreateState(argv, 0);
  114. if (state == NULL) {
  115. return 1;
  116. }
  117. /* Enable standard application logging */
  118. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  119. /* Parse commandline */
  120. for (i = 1; i < argc;) {
  121. int consumed;
  122. consumed = SDLTest_CommonArg(state, i);
  123. if (!consumed) {
  124. if (!filename) {
  125. filename = argv[i];
  126. consumed = 1;
  127. }
  128. }
  129. if (consumed <= 0) {
  130. static const char *options[] = { "[sample.wav]", NULL };
  131. SDLTest_CommonLogUsage(state, argv[0], options);
  132. exit(1);
  133. }
  134. i += consumed;
  135. }
  136. /* Load the SDL library */
  137. if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_EVENTS) < 0) {
  138. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  139. return 1;
  140. }
  141. filename = GetResourceFilename(filename, "sample.wav");
  142. if (filename == NULL) {
  143. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  144. quit(1);
  145. }
  146. /* Load the wave file into memory */
  147. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == -1) {
  148. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  149. quit(1);
  150. }
  151. /* Show the list of available drivers */
  152. SDL_Log("Available audio drivers:");
  153. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  154. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  155. }
  156. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  157. open_audio();
  158. SDL_FlushEvents(SDL_EVENT_AUDIO_DEVICE_ADDED, SDL_EVENT_AUDIO_DEVICE_REMOVED);
  159. #ifdef __EMSCRIPTEN__
  160. emscripten_set_main_loop(loop, 0, 1);
  161. #else
  162. while (!done) {
  163. SDL_Event event;
  164. while (SDL_PollEvent(&event) > 0) {
  165. if (event.type == SDL_EVENT_QUIT) {
  166. done = 1;
  167. }
  168. if ((event.type == SDL_EVENT_AUDIO_DEVICE_ADDED && !event.adevice.iscapture) ||
  169. (event.type == SDL_EVENT_AUDIO_DEVICE_REMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
  170. reopen_audio();
  171. }
  172. }
  173. SDL_Delay(100);
  174. }
  175. #endif
  176. /* Clean up on signal */
  177. close_audio();
  178. SDL_free(wave.sound);
  179. SDL_free(filename);
  180. SDL_Quit();
  181. SDLTest_CommonDestroyState(state);
  182. return 0;
  183. }