loopwavequeue.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 queueing */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #if HAVE_SIGNAL_H
  14. #include <signal.h>
  15. #endif
  16. #include "SDL.h"
  17. struct
  18. {
  19. SDL_AudioSpec spec;
  20. Uint8 *sound; /* Pointer to wave data */
  21. Uint32 soundlen; /* Length of wave data */
  22. int soundpos; /* Current play position */
  23. } wave;
  24. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  25. static void
  26. quit(int rc)
  27. {
  28. SDL_Quit();
  29. exit(rc);
  30. }
  31. static int done = 0;
  32. void
  33. poked(int sig)
  34. {
  35. done = 1;
  36. }
  37. int
  38. main(int argc, char *argv[])
  39. {
  40. int i;
  41. char filename[4096];
  42. /* Enable standard application logging */
  43. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  44. /* Load the SDL library */
  45. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  46. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  47. return (1);
  48. }
  49. if (argc > 1) {
  50. SDL_strlcpy(filename, argv[1], sizeof(filename));
  51. } else {
  52. SDL_strlcpy(filename, "sample.wav", sizeof(filename));
  53. }
  54. /* Load the wave file into memory */
  55. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
  56. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  57. quit(1);
  58. }
  59. wave.spec.callback = NULL; /* we'll push audio. */
  60. #if HAVE_SIGNAL_H
  61. /* Set the signals */
  62. #ifdef SIGHUP
  63. signal(SIGHUP, poked);
  64. #endif
  65. signal(SIGINT, poked);
  66. #ifdef SIGQUIT
  67. signal(SIGQUIT, poked);
  68. #endif
  69. signal(SIGTERM, poked);
  70. #endif /* HAVE_SIGNAL_H */
  71. /* Initialize fillerup() variables */
  72. if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
  73. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  74. SDL_FreeWAV(wave.sound);
  75. quit(2);
  76. }
  77. /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
  78. /* Let the audio run */
  79. SDL_PauseAudio(0);
  80. /* Note that we stuff the entire audio buffer into the queue in one
  81. shot. Most apps would want to feed it a little at a time, as it
  82. plays, but we're going for simplicity here. */
  83. while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
  84. {
  85. /* The device from SDL_OpenAudio() is always device #1. */
  86. const Uint32 queued = SDL_GetQueuedAudioSize(1);
  87. SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
  88. if (queued <= 8192) { /* time to requeue the whole thing? */
  89. if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
  90. SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
  91. } else {
  92. SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
  93. }
  94. }
  95. SDL_Delay(100); /* let it play for awhile. */
  96. }
  97. /* Clean up on signal */
  98. SDL_CloseAudio();
  99. SDL_FreeWAV(wave.sound);
  100. SDL_Quit();
  101. return 0;
  102. }
  103. /* vi: set ts=4 sw=4 expandtab: */