loopwave.c 3.5 KB

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