loopwavequeue.c 4.6 KB

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