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

184 lines
4.3 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 audio */
  11. /* loopwaves.c is much more robust in handling WAVE files --
  12. This is only for simple WAVEs
  13. */
  14. #include "SDL_config.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #ifdef __EMSCRIPTEN__
  18. #include <emscripten/emscripten.h>
  19. #endif
  20. #include "SDL.h"
  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. int soundpos; /* Current play position */
  28. } wave;
  29. static SDL_AudioDeviceID device;
  30. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  31. static void
  32. quit(int rc)
  33. {
  34. SDL_Quit();
  35. exit(rc);
  36. }
  37. static void
  38. close_audio()
  39. {
  40. if (device != 0) {
  41. SDL_CloseAudioDevice(device);
  42. device = 0;
  43. }
  44. }
  45. static void
  46. open_audio()
  47. {
  48. /* Initialize fillerup() variables */
  49. device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wave.spec, NULL, 0);
  50. if (!device) {
  51. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  52. SDL_FreeWAV(wave.sound);
  53. quit(2);
  54. }
  55. /* Let the audio run */
  56. SDL_PauseAudioDevice(device, SDL_FALSE);
  57. }
  58. #ifndef __EMSCRIPTEN__
  59. static void reopen_audio()
  60. {
  61. close_audio();
  62. open_audio();
  63. }
  64. #endif
  65. void SDLCALL
  66. fillerup(void *unused, Uint8 * stream, int len)
  67. {
  68. Uint8 *waveptr;
  69. int waveleft;
  70. /* Set up the pointers */
  71. waveptr = wave.sound + wave.soundpos;
  72. waveleft = wave.soundlen - wave.soundpos;
  73. /* Go! */
  74. while (waveleft <= len) {
  75. SDL_memcpy(stream, waveptr, waveleft);
  76. stream += waveleft;
  77. len -= waveleft;
  78. waveptr = wave.sound;
  79. waveleft = wave.soundlen;
  80. wave.soundpos = 0;
  81. }
  82. SDL_memcpy(stream, waveptr, len);
  83. wave.soundpos += len;
  84. }
  85. static int done = 0;
  86. #ifdef __EMSCRIPTEN__
  87. void
  88. loop()
  89. {
  90. if(done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING))
  91. emscripten_cancel_main_loop();
  92. }
  93. #endif
  94. int
  95. main(int argc, char *argv[])
  96. {
  97. int i;
  98. char *filename = NULL;
  99. /* Enable standard application logging */
  100. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  101. /* Load the SDL library */
  102. if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
  103. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  104. return (1);
  105. }
  106. filename = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav");
  107. if (filename == NULL) {
  108. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  109. quit(1);
  110. }
  111. /* Load the wave file into memory */
  112. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
  113. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  114. quit(1);
  115. }
  116. wave.spec.callback = fillerup;
  117. /* Show the list of available drivers */
  118. SDL_Log("Available audio drivers:");
  119. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  120. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  121. }
  122. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  123. open_audio();
  124. SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
  125. #ifdef __EMSCRIPTEN__
  126. emscripten_set_main_loop(loop, 0, 1);
  127. #else
  128. while (!done) {
  129. SDL_Event event;
  130. while (SDL_PollEvent(&event) > 0) {
  131. if (event.type == SDL_QUIT) {
  132. done = 1;
  133. }
  134. if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
  135. (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
  136. reopen_audio();
  137. }
  138. }
  139. SDL_Delay(100);
  140. }
  141. #endif
  142. /* Clean up on signal */
  143. close_audio();
  144. SDL_FreeWAV(wave.sound);
  145. SDL_free(filename);
  146. SDL_Quit();
  147. return (0);
  148. }
  149. /* vi: set ts=4 sw=4 expandtab: */