loopwavequeue.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 sound queueing */
  11. #include <stdlib.h>
  12. #ifdef __EMSCRIPTEN__
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. #include <SDL3/SDL.h>
  16. #include <SDL3/SDL_main.h>
  17. #include <SDL3/SDL_test.h>
  18. #ifdef HAVE_SIGNAL_H
  19. #include <signal.h>
  20. #endif
  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. } wave;
  28. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  29. static void
  30. quit(int rc)
  31. {
  32. SDL_Quit();
  33. /* Let 'main()' return normally */
  34. if (rc != 0) {
  35. exit(rc);
  36. }
  37. }
  38. static int done = 0;
  39. static void poked(int sig)
  40. {
  41. done = 1;
  42. }
  43. static SDL_AudioDeviceID g_audio_id = 0;
  44. static void loop(void)
  45. {
  46. #ifdef __EMSCRIPTEN__
  47. if (done || (SDL_GetAudioDeviceStatus(g_audio_id) != SDL_AUDIO_PLAYING)) {
  48. emscripten_cancel_main_loop();
  49. } else
  50. #endif
  51. {
  52. /* The device from SDL_OpenAudio() is always device #1. */
  53. const Uint32 queued = SDL_GetQueuedAudioSize(g_audio_id);
  54. SDL_Log("Device has %u bytes queued.\n", (unsigned int)queued);
  55. if (queued <= 8192) { /* time to requeue the whole thing? */
  56. if (SDL_QueueAudio(g_audio_id, wave.sound, wave.soundlen) == 0) {
  57. SDL_Log("Device queued %u more bytes.\n", (unsigned int)wave.soundlen);
  58. } else {
  59. SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int)wave.soundlen, SDL_GetError());
  60. }
  61. }
  62. }
  63. }
  64. int main(int argc, char *argv[])
  65. {
  66. int i;
  67. char *filename = NULL;
  68. SDLTest_CommonState *state;
  69. /* Initialize test framework */
  70. state = SDLTest_CommonCreateState(argv, 0);
  71. if (state == NULL) {
  72. return 1;
  73. }
  74. /* Enable standard application logging */
  75. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  76. /* Parse commandline */
  77. for (i = 1; i < argc;) {
  78. int consumed;
  79. consumed = SDLTest_CommonArg(state, i);
  80. if (!consumed) {
  81. if (!filename) {
  82. filename = argv[i];
  83. consumed = 1;
  84. }
  85. }
  86. if (consumed <= 0) {
  87. static const char *options[] = { "[sample.wav]", NULL };
  88. SDLTest_CommonLogUsage(state, argv[0], options);
  89. exit(1);
  90. }
  91. i += consumed;
  92. }
  93. /* Load the SDL library */
  94. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  95. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  96. return 1;
  97. }
  98. filename = GetResourceFilename(filename, "sample.wav");
  99. if (filename == NULL) {
  100. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  101. quit(1);
  102. }
  103. /* Load the wave file into memory */
  104. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
  105. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  106. quit(1);
  107. }
  108. wave.spec.callback = NULL; /* we'll push audio. */
  109. #ifdef HAVE_SIGNAL_H
  110. /* Set the signals */
  111. #ifdef SIGHUP
  112. (void)signal(SIGHUP, poked);
  113. #endif
  114. (void)signal(SIGINT, poked);
  115. #ifdef SIGQUIT
  116. (void)signal(SIGQUIT, poked);
  117. #endif
  118. (void)signal(SIGTERM, poked);
  119. #endif /* HAVE_SIGNAL_H */
  120. /* Initialize fillerup() variables */
  121. g_audio_id = SDL_OpenAudioDevice(NULL, 0, &wave.spec, NULL, 0);
  122. if (!g_audio_id) {
  123. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  124. SDL_free(wave.sound);
  125. quit(2);
  126. }
  127. /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
  128. /* Let the audio run */
  129. SDL_PlayAudioDevice(g_audio_id);
  130. done = 0;
  131. /* Note that we stuff the entire audio buffer into the queue in one
  132. shot. Most apps would want to feed it a little at a time, as it
  133. plays, but we're going for simplicity here. */
  134. #ifdef __EMSCRIPTEN__
  135. emscripten_set_main_loop(loop, 0, 1);
  136. #else
  137. while (!done && (SDL_GetAudioDeviceStatus(g_audio_id) == SDL_AUDIO_PLAYING)) {
  138. loop();
  139. SDL_Delay(100); /* let it play for a while. */
  140. }
  141. #endif
  142. /* Clean up on signal */
  143. SDL_CloseAudioDevice(g_audio_id);
  144. SDL_free(wave.sound);
  145. SDL_free(filename);
  146. SDL_Quit();
  147. SDLTest_CommonDestroyState(state);
  148. return 0;
  149. }