Atlas - multiple-streams.c

Home / ext / SDL / examples / audio / 04-multiple-streams Lines: 1 | Size: 4929 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 * This example code loads two .wav files, puts them in audio streams and 3 * binds them for playback, repeating both sounds on loop. This shows several 4 * streams mixing into a single playback device. 5 * 6 * This code is public domain. Feel free to use it for any purpose! 7 */ 8 9#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 10#include <SDL3/SDL.h> 11#include <SDL3/SDL_main.h> 12 13/* We will use this renderer to draw into this window every frame. */ 14static SDL_Window *window = NULL; 15static SDL_Renderer *renderer = NULL; 16static SDL_AudioDeviceID audio_device = 0; 17 18/* things that are playing sound (the audiostream itself, plus the original data, so we can refill to loop. */ 19typedef struct Sound { 20 Uint8 *wav_data; 21 Uint32 wav_data_len; 22 SDL_AudioStream *stream; 23} Sound; 24 25static Sound sounds[2]; 26 27static bool init_sound(const char *fname, Sound *sound) 28{ 29 bool retval = false; 30 SDL_AudioSpec spec; 31 char *wav_path = NULL; 32 33 /* Load the .wav files from wherever the app is being run from. */ 34 SDL_asprintf(&wav_path, "%s%s", SDL_GetBasePath(), fname); /* allocate a string of the full file path */ 35 if (!SDL_LoadWAV(wav_path, &spec, &sound->wav_data, &sound->wav_data_len)) { 36 SDL_Log("Couldn't load .wav file: %s", SDL_GetError()); 37 return false; 38 } 39 40 /* Create an audio stream. Set the source format to the wav's format (what 41 we'll input), leave the dest format NULL here (it'll change to what the 42 device wants once we bind it). */ 43 sound->stream = SDL_CreateAudioStream(&spec, NULL); 44 if (!sound->stream) { 45 SDL_Log("Couldn't create audio stream: %s", SDL_GetError()); 46 } else if (!SDL_BindAudioStream(audio_device, sound->stream)) { /* once bound, it'll start playing when there is data available! */ 47 SDL_Log("Failed to bind '%s' stream to device: %s", fname, SDL_GetError()); 48 } else { 49 retval = true; /* success! */ 50 } 51 52 SDL_free(wav_path); /* done with this string. */ 53 return retval; 54} 55 56 57/* This function runs once at startup. */ 58SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 59{ 60 61 SDL_SetAppMetadata("Example Audio Multiple Streams", "1.0", "com.example.audio-multiple-streams"); 62 63 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { 64 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 65 return SDL_APP_FAILURE; 66 } 67 68 if (!SDL_CreateWindowAndRenderer("examples/audio/multiple-streams", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 69 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 70 return SDL_APP_FAILURE; 71 } 72 SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); 73 74 /* open the default audio device in whatever format it prefers; our audio streams will adjust to it. */ 75 audio_device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL); 76 if (audio_device == 0) { 77 SDL_Log("Couldn't open audio device: %s", SDL_GetError()); 78 return SDL_APP_FAILURE; 79 } 80 81 if (!init_sound("sample.wav", &sounds[0])) { 82 return SDL_APP_FAILURE; 83 } else if (!init_sound("sword.wav", &sounds[1])) { 84 return SDL_APP_FAILURE; 85 } 86 87 return SDL_APP_CONTINUE; /* carry on with the program! */ 88} 89 90/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 91SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 92{ 93 if (event->type == SDL_EVENT_QUIT) { 94 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 95 } 96 return SDL_APP_CONTINUE; /* carry on with the program! */ 97} 98 99/* This function runs once per frame, and is the heart of the program. */ 100SDL_AppResult SDL_AppIterate(void *appstate) 101{ 102 int i; 103 104 for (i = 0; i < SDL_arraysize(sounds); i++) { 105 /* If less than a full copy of the audio is queued for playback, put another copy in there. 106 This is overkill, but easy when lots of RAM is cheap. One could be more careful and 107 queue less at a time, as long as the stream doesn't run dry. */ 108 if (SDL_GetAudioStreamQueued(sounds[i].stream) < ((int) sounds[i].wav_data_len)) { 109 SDL_PutAudioStreamData(sounds[i].stream, sounds[i].wav_data, (int) sounds[i].wav_data_len); 110 } 111 } 112 113 /* just blank the screen. */ 114 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 115 SDL_RenderClear(renderer); 116 SDL_RenderPresent(renderer); 117 118 return SDL_APP_CONTINUE; /* carry on with the program! */ 119} 120 121/* This function runs once at shutdown. */ 122void SDL_AppQuit(void *appstate, SDL_AppResult result) 123{ 124 int i; 125 126 SDL_CloseAudioDevice(audio_device); 127 128 for (i = 0; i < SDL_arraysize(sounds); i++) { 129 if (sounds[i].stream) { 130 SDL_DestroyAudioStream(sounds[i].stream); 131 } 132 SDL_free(sounds[i].wav_data); 133 } 134 135 /* SDL will clean up the window/renderer for us. */ 136} 137
[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.