diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/examples/audio/04-multiple-streams | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/examples/audio/04-multiple-streams')
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/04-multiple-streams/README.txt | 5 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/04-multiple-streams/multiple-streams.c | 135 |
2 files changed, 140 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/examples/audio/04-multiple-streams/README.txt b/contrib/SDL-3.2.8/examples/audio/04-multiple-streams/README.txt new file mode 100644 index 0000000..9ef2275 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/04-multiple-streams/README.txt | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | If you're running this in a web browser, you need to click the window before you'll hear anything! | ||
| 2 | |||
| 3 | This example code loads two .wav files, puts them an audio streams and binds | ||
| 4 | them for playback, repeating both sounds on loop. This shows several streams | ||
| 5 | mixing into a single playback device. | ||
diff --git a/contrib/SDL-3.2.8/examples/audio/04-multiple-streams/multiple-streams.c b/contrib/SDL-3.2.8/examples/audio/04-multiple-streams/multiple-streams.c new file mode 100644 index 0000000..8d3bfaa --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/04-multiple-streams/multiple-streams.c | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | /* | ||
| 2 | * This example code loads two .wav files, puts them an 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. */ | ||
| 14 | static SDL_Window *window = NULL; | ||
| 15 | static SDL_Renderer *renderer = NULL; | ||
| 16 | static 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. */ | ||
| 19 | typedef struct Sound { | ||
| 20 | Uint8 *wav_data; | ||
| 21 | Uint32 wav_data_len; | ||
| 22 | SDL_AudioStream *stream; | ||
| 23 | } Sound; | ||
| 24 | |||
| 25 | static Sound sounds[2]; | ||
| 26 | |||
| 27 | static 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. */ | ||
| 58 | SDL_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, 0, &window, &renderer)) { | ||
| 69 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
| 70 | return SDL_APP_FAILURE; | ||
| 71 | } | ||
| 72 | |||
| 73 | /* open the default audio device in whatever format it prefers; our audio streams will adjust to it. */ | ||
| 74 | audio_device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL); | ||
| 75 | if (audio_device == 0) { | ||
| 76 | SDL_Log("Couldn't open audio device: %s", SDL_GetError()); | ||
| 77 | return SDL_APP_FAILURE; | ||
| 78 | } | ||
| 79 | |||
| 80 | if (!init_sound("sample.wav", &sounds[0])) { | ||
| 81 | return SDL_APP_FAILURE; | ||
| 82 | } else if (!init_sound("sword.wav", &sounds[1])) { | ||
| 83 | return SDL_APP_FAILURE; | ||
| 84 | } | ||
| 85 | |||
| 86 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 87 | } | ||
| 88 | |||
| 89 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
| 90 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 91 | { | ||
| 92 | if (event->type == SDL_EVENT_QUIT) { | ||
| 93 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
| 94 | } | ||
| 95 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 96 | } | ||
| 97 | |||
| 98 | /* This function runs once per frame, and is the heart of the program. */ | ||
| 99 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 100 | { | ||
| 101 | int i; | ||
| 102 | |||
| 103 | for (i = 0; i < SDL_arraysize(sounds); i++) { | ||
| 104 | /* If less than a full copy of the audio is queued for playback, put another copy in there. | ||
| 105 | This is overkill, but easy when lots of RAM is cheap. One could be more careful and | ||
| 106 | queue less at a time, as long as the stream doesn't run dry. */ | ||
| 107 | if (SDL_GetAudioStreamQueued(sounds[i].stream) < ((int) sounds[i].wav_data_len)) { | ||
| 108 | SDL_PutAudioStreamData(sounds[i].stream, sounds[i].wav_data, (int) sounds[i].wav_data_len); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | /* just blank the screen. */ | ||
| 113 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | ||
| 114 | SDL_RenderClear(renderer); | ||
| 115 | SDL_RenderPresent(renderer); | ||
| 116 | |||
| 117 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 118 | } | ||
| 119 | |||
| 120 | /* This function runs once at shutdown. */ | ||
| 121 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 122 | { | ||
| 123 | int i; | ||
| 124 | |||
| 125 | SDL_CloseAudioDevice(audio_device); | ||
| 126 | |||
| 127 | for (i = 0; i < SDL_arraysize(sounds); i++) { | ||
| 128 | if (sounds[i].stream) { | ||
| 129 | SDL_DestroyAudioStream(sounds[i].stream); | ||
| 130 | } | ||
| 131 | SDL_free(sounds[i].wav_data); | ||
| 132 | } | ||
| 133 | |||
| 134 | /* SDL will clean up the window/renderer for us. */ | ||
| 135 | } | ||
