loopwave.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright (C) 1997-2014 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 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. void
  66. loop()
  67. {
  68. if(done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING))
  69. emscripten_cancel_main_loop();
  70. }
  71. int
  72. main(int argc, char *argv[])
  73. {
  74. int i;
  75. char filename[4096];
  76. /* Enable standard application logging */
  77. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  78. /* Load the SDL library */
  79. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  80. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  81. return (1);
  82. }
  83. if (argc > 1) {
  84. SDL_strlcpy(filename, argv[1], sizeof(filename));
  85. } else {
  86. SDL_strlcpy(filename, "sample.wav", sizeof(filename));
  87. }
  88. /* Load the wave file into memory */
  89. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
  90. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  91. quit(1);
  92. }
  93. wave.spec.callback = fillerup;
  94. #if HAVE_SIGNAL_H
  95. /* Set the signals */
  96. #ifdef SIGHUP
  97. signal(SIGHUP, poked);
  98. #endif
  99. signal(SIGINT, poked);
  100. #ifdef SIGQUIT
  101. signal(SIGQUIT, poked);
  102. #endif
  103. signal(SIGTERM, poked);
  104. #endif /* HAVE_SIGNAL_H */
  105. /* Show the list of available drivers */
  106. SDL_Log("Available audio drivers:");
  107. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  108. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  109. }
  110. /* Initialize fillerup() variables */
  111. if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
  112. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  113. SDL_FreeWAV(wave.sound);
  114. quit(2);
  115. }
  116. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  117. /* Let the audio run */
  118. SDL_PauseAudio(0);
  119. #ifdef __EMSCRIPTEN__
  120. emscripten_set_main_loop(loop, 0, 1);
  121. #else
  122. while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
  123. SDL_Delay(1000);
  124. #endif
  125. /* Clean up on signal */
  126. SDL_CloseAudio();
  127. SDL_FreeWAV(wave.sound);
  128. SDL_Quit();
  129. return (0);
  130. }
  131. /* vi: set ts=4 sw=4 expandtab: */