loopwavequeue.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright (C) 1997-2022 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 sound queueing */
  11. #include <stdlib.h>
  12. #ifdef __EMSCRIPTEN__
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. #include <SDL3/SDL.h>
  16. #if HAVE_SIGNAL_H
  17. #include <signal.h>
  18. #endif
  19. #include "testutils.h"
  20. static struct
  21. {
  22. SDL_AudioSpec spec;
  23. Uint8 *sound; /* Pointer to wave data */
  24. Uint32 soundlen; /* Length of wave data */
  25. } wave;
  26. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  27. static void
  28. quit(int rc)
  29. {
  30. SDL_Quit();
  31. exit(rc);
  32. }
  33. static int done = 0;
  34. void poked(int sig)
  35. {
  36. done = 1;
  37. }
  38. void loop()
  39. {
  40. #ifdef __EMSCRIPTEN__
  41. if (done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)) {
  42. emscripten_cancel_main_loop();
  43. } else
  44. #endif
  45. {
  46. /* The device from SDL_OpenAudio() is always device #1. */
  47. const Uint32 queued = SDL_GetQueuedAudioSize(1);
  48. SDL_Log("Device has %u bytes queued.\n", (unsigned int)queued);
  49. if (queued <= 8192) { /* time to requeue the whole thing? */
  50. if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
  51. SDL_Log("Device queued %u more bytes.\n", (unsigned int)wave.soundlen);
  52. } else {
  53. SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int)wave.soundlen, SDL_GetError());
  54. }
  55. }
  56. }
  57. }
  58. int main(int argc, char *argv[])
  59. {
  60. char *filename = NULL;
  61. /* Enable standard application logging */
  62. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  63. /* Load the SDL library */
  64. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  65. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  66. return 1;
  67. }
  68. filename = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav");
  69. if (filename == NULL) {
  70. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  71. quit(1);
  72. }
  73. /* Load the wave file into memory */
  74. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
  75. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  76. quit(1);
  77. }
  78. wave.spec.callback = NULL; /* we'll push audio. */
  79. #if HAVE_SIGNAL_H
  80. /* Set the signals */
  81. #ifdef SIGHUP
  82. (void)signal(SIGHUP, poked);
  83. #endif
  84. (void)signal(SIGINT, poked);
  85. #ifdef SIGQUIT
  86. (void)signal(SIGQUIT, poked);
  87. #endif
  88. (void)signal(SIGTERM, poked);
  89. #endif /* HAVE_SIGNAL_H */
  90. /* Initialize fillerup() variables */
  91. if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
  92. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  93. SDL_FreeWAV(wave.sound);
  94. quit(2);
  95. }
  96. /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
  97. /* Let the audio run */
  98. SDL_PauseAudio(0);
  99. done = 0;
  100. /* Note that we stuff the entire audio buffer into the queue in one
  101. shot. Most apps would want to feed it a little at a time, as it
  102. plays, but we're going for simplicity here. */
  103. #ifdef __EMSCRIPTEN__
  104. emscripten_set_main_loop(loop, 0, 1);
  105. #else
  106. while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING)) {
  107. loop();
  108. SDL_Delay(100); /* let it play for awhile. */
  109. }
  110. #endif
  111. /* Clean up on signal */
  112. SDL_CloseAudio();
  113. SDL_FreeWAV(wave.sound);
  114. SDL_free(filename);
  115. SDL_Quit();
  116. return 0;
  117. }
  118. /* vi: set ts=4 sw=4 expandtab: */