loopwave.c 5.1 KB

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