loopwave.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (C) 1997-2014 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 */
  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. #include "SDL_audio.h"
  25. struct
  26. {
  27. SDL_AudioSpec spec;
  28. Uint8 *sound; /* Pointer to wave data */
  29. Uint32 soundlen; /* Length of wave data */
  30. int soundpos; /* Current play position */
  31. } wave;
  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. void SDLCALL
  40. fillerup(void *unused, Uint8 * stream, int len)
  41. {
  42. Uint8 *waveptr;
  43. int waveleft;
  44. /* Set up the pointers */
  45. waveptr = wave.sound + wave.soundpos;
  46. waveleft = wave.soundlen - wave.soundpos;
  47. /* Go! */
  48. while (waveleft <= len) {
  49. SDL_memcpy(stream, waveptr, waveleft);
  50. stream += waveleft;
  51. len -= waveleft;
  52. waveptr = wave.sound;
  53. waveleft = wave.soundlen;
  54. wave.soundpos = 0;
  55. }
  56. SDL_memcpy(stream, waveptr, len);
  57. wave.soundpos += len;
  58. }
  59. static int done = 0;
  60. void
  61. poked(int sig)
  62. {
  63. done = 1;
  64. }
  65. #ifdef __EMSCRIPTEN__
  66. void
  67. loop()
  68. {
  69. if(done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING))
  70. emscripten_cancel_main_loop();
  71. }
  72. #endif
  73. int
  74. main(int argc, char *argv[])
  75. {
  76. int i;
  77. char filename[4096];
  78. /* Enable standard application logging */
  79. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  80. /* Load the SDL library */
  81. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  82. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  83. return (1);
  84. }
  85. if (argc > 1) {
  86. SDL_strlcpy(filename, argv[1], sizeof(filename));
  87. } else {
  88. SDL_strlcpy(filename, "sample.wav", sizeof(filename));
  89. }
  90. /* Load the wave file into memory */
  91. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
  92. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  93. quit(1);
  94. }
  95. wave.spec.callback = fillerup;
  96. #if HAVE_SIGNAL_H
  97. /* Set the signals */
  98. #ifdef SIGHUP
  99. signal(SIGHUP, poked);
  100. #endif
  101. signal(SIGINT, poked);
  102. #ifdef SIGQUIT
  103. signal(SIGQUIT, poked);
  104. #endif
  105. signal(SIGTERM, poked);
  106. #endif /* HAVE_SIGNAL_H */
  107. /* Show the list of available drivers */
  108. SDL_Log("Available audio drivers:");
  109. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  110. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  111. }
  112. /* Initialize fillerup() variables */
  113. if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
  114. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  115. SDL_FreeWAV(wave.sound);
  116. quit(2);
  117. }
  118. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  119. /* Let the audio run */
  120. SDL_PauseAudio(0);
  121. #ifdef __EMSCRIPTEN__
  122. emscripten_set_main_loop(loop, 0, 1);
  123. #else
  124. while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
  125. SDL_Delay(1000);
  126. #endif
  127. /* Clean up on signal */
  128. SDL_CloseAudio();
  129. SDL_FreeWAV(wave.sound);
  130. SDL_Quit();
  131. return (0);
  132. }
  133. /* vi: set ts=4 sw=4 expandtab: */