loopwave.c 4.3 KB

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