summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/examples/audio/04-multiple-streams/multiple-streams.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/examples/audio/04-multiple-streams/multiple-streams.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/examples/audio/04-multiple-streams/multiple-streams.c')
-rw-r--r--src/contrib/SDL-3.2.20/examples/audio/04-multiple-streams/multiple-streams.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/examples/audio/04-multiple-streams/multiple-streams.c b/src/contrib/SDL-3.2.20/examples/audio/04-multiple-streams/multiple-streams.c
new file mode 100644
index 0000000..8d3bfaa
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/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. */
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, 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. */
90SDL_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. */
99SDL_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. */
121void 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}