loopwave.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_AudioFormat fmt;
  25. int channels;
  26. int freq;
  27. Uint8 *sound; /* Pointer to wave data */
  28. Uint32 soundlen; /* Length of wave data */
  29. Uint32 soundpos;
  30. } wave;
  31. static SDL_AudioDeviceID device;
  32. static SDL_AudioStream *stream;
  33. static void SDLCALL
  34. fillerup(SDL_AudioStream *stream, int len, void *unused)
  35. {
  36. Uint8 *waveptr;
  37. int waveleft;
  38. /*SDL_Log("CALLBACK WANTS %d MORE BYTES!", len);*/
  39. /* Set up the pointers */
  40. waveptr = wave.sound + wave.soundpos;
  41. waveleft = wave.soundlen - wave.soundpos;
  42. /* Go! */
  43. while (waveleft <= len) {
  44. SDL_PutAudioStreamData(stream, waveptr, waveleft);
  45. len -= waveleft;
  46. waveptr = wave.sound;
  47. waveleft = wave.soundlen;
  48. wave.soundpos = 0;
  49. }
  50. SDL_PutAudioStreamData(stream, waveptr, len);
  51. wave.soundpos += len;
  52. }
  53. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  54. static void
  55. quit(int rc)
  56. {
  57. SDL_Quit();
  58. /* Let 'main()' return normally */
  59. if (rc != 0) {
  60. exit(rc);
  61. }
  62. }
  63. static void
  64. close_audio(void)
  65. {
  66. if (device != 0) {
  67. SDL_DestroyAudioStream(stream);
  68. stream = NULL;
  69. SDL_CloseAudioDevice(device);
  70. device = 0;
  71. }
  72. }
  73. static void
  74. open_audio(void)
  75. {
  76. SDL_AudioDeviceID *devices = SDL_GetAudioOutputDevices(NULL);
  77. device = devices ? SDL_OpenAudioDevice(devices[0], wave.fmt, wave.channels, wave.freq) : 0;
  78. SDL_free(devices);
  79. if (!device) {
  80. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  81. SDL_free(wave.sound);
  82. quit(2);
  83. }
  84. stream = SDL_CreateAndBindAudioStream(device, wave.fmt, wave.channels, wave.freq);
  85. if (!stream) {
  86. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create audio stream: %s\n", SDL_GetError());
  87. SDL_CloseAudioDevice(device);
  88. SDL_free(wave.sound);
  89. quit(2);
  90. }
  91. SDL_SetAudioStreamGetCallback(stream, fillerup, NULL);
  92. }
  93. #ifndef __EMSCRIPTEN__
  94. static void reopen_audio(void)
  95. {
  96. close_audio();
  97. open_audio();
  98. }
  99. #endif
  100. static int done = 0;
  101. #ifdef __EMSCRIPTEN__
  102. static void loop(void)
  103. {
  104. if (done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING)) {
  105. emscripten_cancel_main_loop();
  106. } else {
  107. fillerup();
  108. }
  109. }
  110. #endif
  111. int main(int argc, char *argv[])
  112. {
  113. int i;
  114. char *filename = NULL;
  115. SDLTest_CommonState *state;
  116. /* Initialize test framework */
  117. state = SDLTest_CommonCreateState(argv, 0);
  118. if (state == NULL) {
  119. return 1;
  120. }
  121. /* Enable standard application logging */
  122. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  123. /* Parse commandline */
  124. for (i = 1; i < argc;) {
  125. int consumed;
  126. consumed = SDLTest_CommonArg(state, i);
  127. if (!consumed) {
  128. if (!filename) {
  129. filename = argv[i];
  130. consumed = 1;
  131. }
  132. }
  133. if (consumed <= 0) {
  134. static const char *options[] = { "[sample.wav]", NULL };
  135. SDLTest_CommonLogUsage(state, argv[0], options);
  136. exit(1);
  137. }
  138. i += consumed;
  139. }
  140. /* Load the SDL library */
  141. if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_EVENTS) < 0) {
  142. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  143. return 1;
  144. }
  145. filename = GetResourceFilename(filename, "sample.wav");
  146. if (filename == NULL) {
  147. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  148. quit(1);
  149. }
  150. /* Load the wave file into memory */
  151. if (SDL_LoadWAV(filename, &wave.fmt, &wave.channels, &wave.freq, &wave.sound, &wave.soundlen) == -1) {
  152. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  153. quit(1);
  154. }
  155. /* Show the list of available drivers */
  156. SDL_Log("Available audio drivers:");
  157. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  158. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  159. }
  160. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  161. open_audio();
  162. SDL_FlushEvents(SDL_EVENT_AUDIO_DEVICE_ADDED, SDL_EVENT_AUDIO_DEVICE_REMOVED);
  163. #ifdef __EMSCRIPTEN__
  164. emscripten_set_main_loop(loop, 0, 1);
  165. #else
  166. while (!done) {
  167. SDL_Event event;
  168. while (SDL_PollEvent(&event) > 0) {
  169. if (event.type == SDL_EVENT_QUIT) {
  170. done = 1;
  171. }
  172. if ((event.type == SDL_EVENT_AUDIO_DEVICE_ADDED && !event.adevice.iscapture) ||
  173. (event.type == SDL_EVENT_AUDIO_DEVICE_REMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
  174. reopen_audio();
  175. }
  176. }
  177. SDL_Delay(100);
  178. }
  179. #endif
  180. /* Clean up on signal */
  181. close_audio();
  182. SDL_free(wave.sound);
  183. SDL_free(filename);
  184. SDL_Quit();
  185. SDLTest_CommonDestroyState(state);
  186. return 0;
  187. }