load-wav.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This example code creates a simple audio stream for playing sound, and
  3. * loads a .wav file that is pushed through the stream in a loop.
  4. *
  5. * This code is public domain. Feel free to use it for any purpose!
  6. *
  7. * The .wav file is a sample from Will Provost's song, The Living Proof,
  8. * used with permission.
  9. *
  10. * From the album The Living Proof
  11. * Publisher: 5 Guys Named Will
  12. * Copyright 1996 Will Provost
  13. * https://itunes.apple.com/us/album/the-living-proof/id4153978
  14. * http://www.amazon.com/The-Living-Proof-Will-Provost/dp/B00004R8RH
  15. */
  16. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  17. #include <SDL3/SDL.h>
  18. #include <SDL3/SDL_main.h>
  19. /* We will use this renderer to draw into this window every frame. */
  20. static SDL_Window *window = NULL;
  21. static SDL_Renderer *renderer = NULL;
  22. static SDL_AudioStream *stream = NULL;
  23. static Uint8 *wav_data = NULL;
  24. static Uint32 wav_data_len = 0;
  25. /* This function runs once at startup. */
  26. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  27. {
  28. SDL_AudioSpec spec;
  29. char *wav_path = NULL;
  30. SDL_SetAppMetadata("Example Audio Load Wave", "1.0", "com.example.audio-load-wav");
  31. if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
  32. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  33. return SDL_APP_FAILURE;
  34. }
  35. /* we don't _need_ a window for audio-only things but it's good policy to have one. */
  36. if (!SDL_CreateWindowAndRenderer("examples/audio/load-wav", 640, 480, 0, &window, &renderer)) {
  37. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  38. return SDL_APP_FAILURE;
  39. }
  40. /* Load the .wav file from wherever the app is being run from. */
  41. SDL_asprintf(&wav_path, "%ssample.wav", SDL_GetBasePath()); /* allocate a string of the full file path */
  42. if (!SDL_LoadWAV(wav_path, &spec, &wav_data, &wav_data_len)) {
  43. SDL_Log("Couldn't load .wav file: %s", SDL_GetError());
  44. return SDL_APP_FAILURE;
  45. }
  46. SDL_free(wav_path); /* done with this string. */
  47. /* Create our audio stream in the same format as the .wav file. It'll convert to what the audio hardware wants. */
  48. stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL);
  49. if (!stream) {
  50. SDL_Log("Couldn't create audio stream: %s", SDL_GetError());
  51. return SDL_APP_FAILURE;
  52. }
  53. /* SDL_OpenAudioDeviceStream starts the device paused. You have to tell it to start! */
  54. SDL_ResumeAudioStreamDevice(stream);
  55. return SDL_APP_CONTINUE; /* carry on with the program! */
  56. }
  57. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  58. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  59. {
  60. if (event->type == SDL_EVENT_QUIT) {
  61. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  62. }
  63. return SDL_APP_CONTINUE; /* carry on with the program! */
  64. }
  65. /* This function runs once per frame, and is the heart of the program. */
  66. SDL_AppResult SDL_AppIterate(void *appstate)
  67. {
  68. /* see if we need to feed the audio stream more data yet.
  69. We're being lazy here, but if there's less than the entire wav file left to play,
  70. just shove a whole copy of it into the queue, so we always have _tons_ of
  71. data queued for playback. */
  72. if (SDL_GetAudioStreamAvailable(stream) < (int)wav_data_len) {
  73. /* feed more data to the stream. It will queue at the end, and trickle out as the hardware needs more data. */
  74. SDL_PutAudioStreamData(stream, wav_data, wav_data_len);
  75. }
  76. /* we're not doing anything with the renderer, so just blank it out. */
  77. SDL_RenderClear(renderer);
  78. SDL_RenderPresent(renderer);
  79. return SDL_APP_CONTINUE; /* carry on with the program! */
  80. }
  81. /* This function runs once at shutdown. */
  82. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  83. {
  84. SDL_free(wav_data); /* strictly speaking, this isn't necessary because the process is ending, but it's good policy. */
  85. /* SDL will clean up the window/renderer for us. */
  86. }