loopwave.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Copyright (C) 1997-2023 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 <stdlib.h>
  15. #ifdef __EMSCRIPTEN__
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. #include <SDL3/SDL.h>
  19. #include <SDL3/SDL_main.h>
  20. #include <SDL3/SDL_test.h>
  21. #include "testutils.h"
  22. static struct
  23. {
  24. SDL_AudioSpec spec;
  25. Uint8 *sound; /* Pointer to wave data */
  26. Uint32 soundlen; /* Length of wave data */
  27. Uint32 soundpos;
  28. } wave;
  29. static SDL_AudioDeviceID device;
  30. static SDL_AudioStream *stream;
  31. static void fillerup(void)
  32. {
  33. if (SDL_GetAudioStreamAvailable(stream) < (wave.soundlen / 2)) {
  34. SDL_PutAudioStreamData(stream, wave.sound, wave.soundlen);
  35. }
  36. }
  37. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  38. static void
  39. quit(int rc)
  40. {
  41. SDL_Quit();
  42. /* Let 'main()' return normally */
  43. if (rc != 0) {
  44. exit(rc);
  45. }
  46. }
  47. static void
  48. close_audio(void)
  49. {
  50. if (device != 0) {
  51. SDL_DestroyAudioStream(stream);
  52. stream = NULL;
  53. SDL_CloseAudioDevice(device);
  54. device = 0;
  55. }
  56. }
  57. static void
  58. open_audio(void)
  59. {
  60. device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &wave.spec);
  61. if (!device) {
  62. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
  63. SDL_free(wave.sound);
  64. quit(2);
  65. }
  66. stream = SDL_CreateAndBindAudioStream(device, &wave.spec);
  67. if (!stream) {
  68. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create audio stream: %s\n", SDL_GetError());
  69. SDL_CloseAudioDevice(device);
  70. SDL_free(wave.sound);
  71. quit(2);
  72. }
  73. }
  74. static int done = 0;
  75. #ifdef __EMSCRIPTEN__
  76. static void loop(void)
  77. {
  78. if (done) {
  79. emscripten_cancel_main_loop();
  80. } else {
  81. fillerup();
  82. }
  83. }
  84. #endif
  85. int main(int argc, char *argv[])
  86. {
  87. int i;
  88. char *filename = NULL;
  89. SDLTest_CommonState *state;
  90. /* Initialize test framework */
  91. state = SDLTest_CommonCreateState(argv, 0);
  92. if (state == NULL) {
  93. return 1;
  94. }
  95. /* Enable standard application logging */
  96. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  97. /* Parse commandline */
  98. for (i = 1; i < argc;) {
  99. int consumed;
  100. consumed = SDLTest_CommonArg(state, i);
  101. if (!consumed) {
  102. if (!filename) {
  103. filename = argv[i];
  104. consumed = 1;
  105. }
  106. }
  107. if (consumed <= 0) {
  108. static const char *options[] = { "[sample.wav]", NULL };
  109. SDLTest_CommonLogUsage(state, argv[0], options);
  110. exit(1);
  111. }
  112. i += consumed;
  113. }
  114. /* Load the SDL library */
  115. if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_EVENTS) < 0) {
  116. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  117. return 1;
  118. }
  119. filename = GetResourceFilename(filename, "sample.wav");
  120. if (filename == NULL) {
  121. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  122. quit(1);
  123. }
  124. /* Load the wave file into memory */
  125. if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == -1) {
  126. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
  127. quit(1);
  128. }
  129. /* Show the list of available drivers */
  130. SDL_Log("Available audio drivers:");
  131. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  132. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  133. }
  134. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  135. open_audio();
  136. #ifdef __EMSCRIPTEN__
  137. emscripten_set_main_loop(loop, 0, 1);
  138. #else
  139. while (!done) {
  140. SDL_Event event;
  141. while (SDL_PollEvent(&event) > 0) {
  142. if (event.type == SDL_EVENT_QUIT) {
  143. done = 1;
  144. }
  145. }
  146. fillerup();
  147. SDL_Delay(100);
  148. }
  149. #endif
  150. /* Clean up on signal */
  151. close_audio();
  152. SDL_free(wave.sound);
  153. SDL_free(filename);
  154. SDL_Quit();
  155. SDLTest_CommonDestroyState(state);
  156. return 0;
  157. }