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

179 lines
4.2 KiB

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