loopwavequeue.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright (C) 1997-2019 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 <stdio.h>
  12. #include <stdlib.h>
  13. #ifdef __EMSCRIPTEN__
  14. #include <emscripten/emscripten.h>
  15. #endif
  16. #include "SDL.h"
  17. #if HAVE_SIGNAL_H
  18. #include <signal.h>
  19. #endif
  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
  35. poked(int sig)
  36. {
  37. done = 1;
  38. }
  39. void
  40. loop()
  41. {
  42. #ifdef __EMSCRIPTEN__
  43. if (done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)) {
  44. emscripten_cancel_main_loop();
  45. }
  46. else
  47. #endif
  48. {
  49. /* The device from SDL_OpenAudio() is always device #1. */
  50. const Uint32 queued = SDL_GetQueuedAudioSize(1);
  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(1, 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
  62. main(int argc, char *argv[])
  63. {
  64. char filename[4096];
  65. /* Enable standard application logging */
  66. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  67. /* Load the SDL library */
  68. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  69. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  70. return (1);
  71. }
  72. if (argc > 1) {
  73. SDL_strlcpy(filename, argv[1], sizeof(filename));
  74. } else {
  75. SDL_strlcpy(filename, "sample.wav", sizeof(filename));
  76. }
  77. /* Load the wave file into memory */
  78. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
  79. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  80. quit(1);
  81. }
  82. wave.spec.callback = NULL; /* we'll push audio. */
  83. #if HAVE_SIGNAL_H
  84. /* Set the signals */
  85. #ifdef SIGHUP
  86. signal(SIGHUP, poked);
  87. #endif
  88. signal(SIGINT, poked);
  89. #ifdef SIGQUIT
  90. signal(SIGQUIT, poked);
  91. #endif
  92. signal(SIGTERM, poked);
  93. #endif /* HAVE_SIGNAL_H */
  94. /* Initialize fillerup() variables */
  95. if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
  96. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  97. SDL_FreeWAV(wave.sound);
  98. quit(2);
  99. }
  100. /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
  101. /* Let the audio run */
  102. SDL_PauseAudio(0);
  103. done = 0;
  104. /* Note that we stuff the entire audio buffer into the queue in one
  105. shot. Most apps would want to feed it a little at a time, as it
  106. plays, but we're going for simplicity here. */
  107. #ifdef __EMSCRIPTEN__
  108. emscripten_set_main_loop(loop, 0, 1);
  109. #else
  110. while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
  111. {
  112. loop();
  113. SDL_Delay(100); /* let it play for awhile. */
  114. }
  115. #endif
  116. /* Clean up on signal */
  117. SDL_CloseAudio();
  118. SDL_FreeWAV(wave.sound);
  119. SDL_Quit();
  120. return 0;
  121. }
  122. /* vi: set ts=4 sw=4 expandtab: */