Atlas - load-wav.c

Home / ext / SDL / examples / audio / 03-load-wav Lines: 1 | Size: 4185 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
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 17#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 18#include <SDL3/SDL.h> 19#include <SDL3/SDL_main.h> 20 21/* We will use this renderer to draw into this window every frame. */ 22static SDL_Window *window = NULL; 23static SDL_Renderer *renderer = NULL; 24static SDL_AudioStream *stream = NULL; 25static Uint8 *wav_data = NULL; 26static Uint32 wav_data_len = 0; 27 28/* This function runs once at startup. */ 29SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 30{ 31 SDL_AudioSpec spec; 32 char *wav_path = NULL; 33 34 SDL_SetAppMetadata("Example Audio Load Wave", "1.0", "com.example.audio-load-wav"); 35 36 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { 37 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 38 return SDL_APP_FAILURE; 39 } 40 41 /* we don't _need_ a window for audio-only things but it's good policy to have one. */ 42 if (!SDL_CreateWindowAndRenderer("examples/audio/load-wav", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 43 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 44 return SDL_APP_FAILURE; 45 } 46 SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); 47 48 /* Load the .wav file from wherever the app is being run from. */ 49 SDL_asprintf(&wav_path, "%ssample.wav", SDL_GetBasePath()); /* allocate a string of the full file path */ 50 if (!SDL_LoadWAV(wav_path, &spec, &wav_data, &wav_data_len)) { 51 SDL_Log("Couldn't load .wav file: %s", SDL_GetError()); 52 return SDL_APP_FAILURE; 53 } 54 55 SDL_free(wav_path); /* done with this string. */ 56 57 /* Create our audio stream in the same format as the .wav file. It'll convert to what the audio hardware wants. */ 58 stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL); 59 if (!stream) { 60 SDL_Log("Couldn't create audio stream: %s", SDL_GetError()); 61 return SDL_APP_FAILURE; 62 } 63 64 /* SDL_OpenAudioDeviceStream starts the device paused. You have to tell it to start! */ 65 SDL_ResumeAudioStreamDevice(stream); 66 67 return SDL_APP_CONTINUE; /* carry on with the program! */ 68} 69 70/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 71SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 72{ 73 if (event->type == SDL_EVENT_QUIT) { 74 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 75 } 76 return SDL_APP_CONTINUE; /* carry on with the program! */ 77} 78 79/* This function runs once per frame, and is the heart of the program. */ 80SDL_AppResult SDL_AppIterate(void *appstate) 81{ 82 /* see if we need to feed the audio stream more data yet. 83 We're being lazy here, but if there's less than the entire wav file left to play, 84 just shove a whole copy of it into the queue, so we always have _tons_ of 85 data queued for playback. */ 86 if (SDL_GetAudioStreamQueued(stream) < (int)wav_data_len) { 87 /* feed more data to the stream. It will queue at the end, and trickle out as the hardware needs more data. */ 88 SDL_PutAudioStreamData(stream, wav_data, wav_data_len); 89 } 90 91 /* we're not doing anything with the renderer, so just blank it out. */ 92 SDL_RenderClear(renderer); 93 SDL_RenderPresent(renderer); 94 95 return SDL_APP_CONTINUE; /* carry on with the program! */ 96} 97 98/* This function runs once at shutdown. */ 99void SDL_AppQuit(void *appstate, SDL_AppResult result) 100{ 101 SDL_free(wav_data); /* strictly speaking, this isn't necessary because the process is ending, but it's good policy. */ 102 /* SDL will clean up the window/renderer for us. */ 103} 104 105
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.