1
0

loopwave.c 3.7 KB

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