🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.8 KiB

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