🛠️🐜 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.

154 lines
3.8 KiB

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