loopwave.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 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. static void reopen_audio()
  59. {
  60. close_audio();
  61. open_audio();
  62. }
  63. void SDLCALL
  64. fillerup(void *unused, Uint8 * stream, int len)
  65. {
  66. Uint8 *waveptr;
  67. int waveleft;
  68. /* Set up the pointers */
  69. waveptr = wave.sound + wave.soundpos;
  70. waveleft = wave.soundlen - wave.soundpos;
  71. /* Go! */
  72. while (waveleft <= len) {
  73. SDL_memcpy(stream, waveptr, waveleft);
  74. stream += waveleft;
  75. len -= waveleft;
  76. waveptr = wave.sound;
  77. waveleft = wave.soundlen;
  78. wave.soundpos = 0;
  79. }
  80. SDL_memcpy(stream, waveptr, len);
  81. wave.soundpos += len;
  82. }
  83. static int done = 0;
  84. #ifdef __EMSCRIPTEN__
  85. void
  86. loop()
  87. {
  88. if(done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING))
  89. emscripten_cancel_main_loop();
  90. }
  91. #endif
  92. int
  93. main(int argc, char *argv[])
  94. {
  95. int i;
  96. char *filename = NULL;
  97. /* Enable standard application logging */
  98. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  99. /* Load the SDL library */
  100. if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
  101. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  102. return (1);
  103. }
  104. filename = GetResourceFilename(argc > 1 ? argv[1] : NULL, "sample.wav");
  105. if (filename == NULL) {
  106. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  107. quit(1);
  108. }
  109. /* Load the wave file into memory */
  110. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
  111. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  112. quit(1);
  113. }
  114. wave.spec.callback = fillerup;
  115. /* Show the list of available drivers */
  116. SDL_Log("Available audio drivers:");
  117. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  118. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  119. }
  120. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  121. open_audio();
  122. SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
  123. #ifdef __EMSCRIPTEN__
  124. emscripten_set_main_loop(loop, 0, 1);
  125. #else
  126. while (!done) {
  127. SDL_Event event;
  128. while (SDL_PollEvent(&event) > 0) {
  129. if (event.type == SDL_QUIT) {
  130. done = 1;
  131. }
  132. if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
  133. (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
  134. reopen_audio();
  135. }
  136. }
  137. SDL_Delay(100);
  138. }
  139. #endif
  140. /* Clean up on signal */
  141. close_audio();
  142. SDL_FreeWAV(wave.sound);
  143. SDL_free(filename);
  144. SDL_Quit();
  145. return (0);
  146. }
  147. /* vi: set ts=4 sw=4 expandtab: */