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 | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/examples/audio')
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/01-simple-playback/README.txt | 5 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/01-simple-playback/simple-playback.c | 103 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/02-simple-playback-callback/README.txt | 5 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/02-simple-playback-callback/simple-playback-callback.c | 115 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/03-load-wav/README.txt | 5 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/03-load-wav/load-wav.c | 103 | ||||
| -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 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/onmouseover.webp | bin | 0 -> 176470 bytes | |||
| -rw-r--r-- | contrib/SDL-3.2.8/examples/audio/thumbnail.png | bin | 0 -> 14221 bytes |
10 files changed, 476 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/examples/audio/01-simple-playback/README.txt b/contrib/SDL-3.2.8/examples/audio/01-simple-playback/README.txt new file mode 100644 index 0000000..20ad059 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/01-simple-playback/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 creates a simple audio stream for playing sound, and | ||
| 4 | generates a sine wave sound effect for it to play as time goes on. This is the | ||
| 5 | simplest way to get up and running with procedural sound. | ||
diff --git a/contrib/SDL-3.2.8/examples/audio/01-simple-playback/simple-playback.c b/contrib/SDL-3.2.8/examples/audio/01-simple-playback/simple-playback.c new file mode 100644 index 0000000..15126e5 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/01-simple-playback/simple-playback.c | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | /* | ||
| 2 | * This example code creates a simple audio stream for playing sound, and | ||
| 3 | * generates a sine wave sound effect for it to play as time goes on. This | ||
| 4 | * is the simplest way to get up and running with procedural sound. | ||
| 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_AudioStream *stream = NULL; | ||
| 17 | static int current_sine_sample = 0; | ||
| 18 | |||
| 19 | /* This function runs once at startup. */ | ||
| 20 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
| 21 | { | ||
| 22 | SDL_AudioSpec spec; | ||
| 23 | |||
| 24 | SDL_SetAppMetadata("Example Audio Simple Playback", "1.0", "com.example.audio-simple-playback"); | ||
| 25 | |||
| 26 | if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { | ||
| 27 | SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); | ||
| 28 | return SDL_APP_FAILURE; | ||
| 29 | } | ||
| 30 | |||
| 31 | /* we don't _need_ a window for audio-only things but it's good policy to have one. */ | ||
| 32 | if (!SDL_CreateWindowAndRenderer("examples/audio/simple-playback", 640, 480, 0, &window, &renderer)) { | ||
| 33 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
| 34 | return SDL_APP_FAILURE; | ||
| 35 | } | ||
| 36 | |||
| 37 | /* We're just playing a single thing here, so we'll use the simplified option. | ||
| 38 | We are always going to feed audio in as mono, float32 data at 8000Hz. | ||
| 39 | The stream will convert it to whatever the hardware wants on the other side. */ | ||
| 40 | spec.channels = 1; | ||
| 41 | spec.format = SDL_AUDIO_F32; | ||
| 42 | spec.freq = 8000; | ||
| 43 | stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL); | ||
| 44 | if (!stream) { | ||
| 45 | SDL_Log("Couldn't create audio stream: %s", SDL_GetError()); | ||
| 46 | return SDL_APP_FAILURE; | ||
| 47 | } | ||
| 48 | |||
| 49 | /* SDL_OpenAudioDeviceStream starts the device paused. You have to tell it to start! */ | ||
| 50 | SDL_ResumeAudioStreamDevice(stream); | ||
| 51 | |||
| 52 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 53 | } | ||
| 54 | |||
| 55 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
| 56 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 57 | { | ||
| 58 | if (event->type == SDL_EVENT_QUIT) { | ||
| 59 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
| 60 | } | ||
| 61 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 62 | } | ||
| 63 | |||
| 64 | /* This function runs once per frame, and is the heart of the program. */ | ||
| 65 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 66 | { | ||
| 67 | /* see if we need to feed the audio stream more data yet. | ||
| 68 | We're being lazy here, but if there's less than half a second queued, generate more. | ||
| 69 | A sine wave is unchanging audio--easy to stream--but for video games, you'll want | ||
| 70 | to generate significantly _less_ audio ahead of time! */ | ||
| 71 | const int minimum_audio = (8000 * sizeof (float)) / 2; /* 8000 float samples per second. Half of that. */ | ||
| 72 | if (SDL_GetAudioStreamQueued(stream) < minimum_audio) { | ||
| 73 | static float samples[512]; /* this will feed 512 samples each frame until we get to our maximum. */ | ||
| 74 | int i; | ||
| 75 | |||
| 76 | /* generate a 440Hz pure tone */ | ||
| 77 | for (i = 0; i < SDL_arraysize(samples); i++) { | ||
| 78 | const int freq = 440; | ||
| 79 | const float phase = current_sine_sample * freq / 8000.0f; | ||
| 80 | samples[i] = SDL_sinf(phase * 2 * SDL_PI_F); | ||
| 81 | current_sine_sample++; | ||
| 82 | } | ||
| 83 | |||
| 84 | /* wrapping around to avoid floating-point errors */ | ||
| 85 | current_sine_sample %= 8000; | ||
| 86 | |||
| 87 | /* feed the new data to the stream. It will queue at the end, and trickle out as the hardware needs more data. */ | ||
| 88 | SDL_PutAudioStreamData(stream, samples, sizeof (samples)); | ||
| 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. */ | ||
| 99 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 100 | { | ||
| 101 | /* SDL will clean up the window/renderer for us. */ | ||
| 102 | } | ||
| 103 | |||
diff --git a/contrib/SDL-3.2.8/examples/audio/02-simple-playback-callback/README.txt b/contrib/SDL-3.2.8/examples/audio/02-simple-playback-callback/README.txt new file mode 100644 index 0000000..888ae34 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/02-simple-playback-callback/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 creates a simple audio stream for playing sound, and | ||
| 4 | generates a sine wave sound effect for it to play as time goes on. Unlike | ||
| 5 | the previous example, this uses a callback to generate sound. | ||
diff --git a/contrib/SDL-3.2.8/examples/audio/02-simple-playback-callback/simple-playback-callback.c b/contrib/SDL-3.2.8/examples/audio/02-simple-playback-callback/simple-playback-callback.c new file mode 100644 index 0000000..c011c72 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/02-simple-playback-callback/simple-playback-callback.c | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | /* | ||
| 2 | * This example code creates a simple audio stream for playing sound, and | ||
| 3 | * generates a sine wave sound effect for it to play as time goes on. Unlike | ||
| 4 | * the previous example, this uses a callback to generate sound. | ||
| 5 | * | ||
| 6 | * This might be the path of least resistance if you're moving an SDL2 | ||
| 7 | * program's audio code to SDL3. | ||
| 8 | * | ||
| 9 | * This code is public domain. Feel free to use it for any purpose! | ||
| 10 | */ | ||
| 11 | |||
| 12 | #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ | ||
| 13 | #include <SDL3/SDL.h> | ||
| 14 | #include <SDL3/SDL_main.h> | ||
| 15 | |||
| 16 | /* We will use this renderer to draw into this window every frame. */ | ||
| 17 | static SDL_Window *window = NULL; | ||
| 18 | static SDL_Renderer *renderer = NULL; | ||
| 19 | static SDL_AudioStream *stream = NULL; | ||
| 20 | static int current_sine_sample = 0; | ||
| 21 | |||
| 22 | /* this function will be called (usually in a background thread) when the audio stream is consuming data. */ | ||
| 23 | static void SDLCALL FeedTheAudioStreamMore(void *userdata, SDL_AudioStream *astream, int additional_amount, int total_amount) | ||
| 24 | { | ||
| 25 | /* total_amount is how much data the audio stream is eating right now, additional_amount is how much more it needs | ||
| 26 | than what it currently has queued (which might be zero!). You can supply any amount of data here; it will take what | ||
| 27 | it needs and use the extra later. If you don't give it enough, it will take everything and then feed silence to the | ||
| 28 | hardware for the rest. Ideally, though, we always give it what it needs and no extra, so we aren't buffering more | ||
| 29 | than necessary. */ | ||
| 30 | additional_amount /= sizeof (float); /* convert from bytes to samples */ | ||
| 31 | while (additional_amount > 0) { | ||
| 32 | float samples[128]; /* this will feed 128 samples each iteration until we have enough. */ | ||
| 33 | const int total = SDL_min(additional_amount, SDL_arraysize(samples)); | ||
| 34 | int i; | ||
| 35 | |||
| 36 | /* generate a 440Hz pure tone */ | ||
| 37 | for (i = 0; i < total; i++) { | ||
| 38 | const int freq = 440; | ||
| 39 | const float phase = current_sine_sample * freq / 8000.0f; | ||
| 40 | samples[i] = SDL_sinf(phase * 2 * SDL_PI_F); | ||
| 41 | current_sine_sample++; | ||
| 42 | } | ||
| 43 | |||
| 44 | /* wrapping around to avoid floating-point errors */ | ||
| 45 | current_sine_sample %= 8000; | ||
| 46 | |||
| 47 | /* feed the new data to the stream. It will queue at the end, and trickle out as the hardware needs more data. */ | ||
| 48 | SDL_PutAudioStreamData(astream, samples, total * sizeof (float)); | ||
| 49 | additional_amount -= total; /* subtract what we've just fed the stream. */ | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | /* This function runs once at startup. */ | ||
| 54 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
| 55 | { | ||
| 56 | SDL_AudioSpec spec; | ||
| 57 | |||
| 58 | SDL_SetAppMetadata("Example Simple Audio Playback Callback", "1.0", "com.example.audio-simple-playback-callback"); | ||
| 59 | |||
| 60 | if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { | ||
| 61 | SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); | ||
| 62 | return SDL_APP_FAILURE; | ||
| 63 | } | ||
| 64 | |||
| 65 | /* we don't _need_ a window for audio-only things but it's good policy to have one. */ | ||
| 66 | if (!SDL_CreateWindowAndRenderer("examples/audio/simple-playback-callback", 640, 480, 0, &window, &renderer)) { | ||
| 67 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
| 68 | return SDL_APP_FAILURE; | ||
| 69 | } | ||
| 70 | |||
| 71 | /* We're just playing a single thing here, so we'll use the simplified option. | ||
| 72 | We are always going to feed audio in as mono, float32 data at 8000Hz. | ||
| 73 | The stream will convert it to whatever the hardware wants on the other side. */ | ||
| 74 | spec.channels = 1; | ||
| 75 | spec.format = SDL_AUDIO_F32; | ||
| 76 | spec.freq = 8000; | ||
| 77 | stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, FeedTheAudioStreamMore, NULL); | ||
| 78 | if (!stream) { | ||
| 79 | SDL_Log("Couldn't create audio stream: %s", SDL_GetError()); | ||
| 80 | return SDL_APP_FAILURE; | ||
| 81 | } | ||
| 82 | |||
| 83 | /* SDL_OpenAudioDeviceStream starts the device paused. You have to tell it to start! */ | ||
| 84 | SDL_ResumeAudioStreamDevice(stream); | ||
| 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 | /* we're not doing anything with the renderer, so just blank it out. */ | ||
| 102 | SDL_RenderClear(renderer); | ||
| 103 | SDL_RenderPresent(renderer); | ||
| 104 | |||
| 105 | /* all the work of feeding the audio stream is happening in a callback in a background thread. */ | ||
| 106 | |||
| 107 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 108 | } | ||
| 109 | |||
| 110 | /* This function runs once at shutdown. */ | ||
| 111 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 112 | { | ||
| 113 | /* SDL will clean up the window/renderer for us. */ | ||
| 114 | } | ||
| 115 | |||
diff --git a/contrib/SDL-3.2.8/examples/audio/03-load-wav/README.txt b/contrib/SDL-3.2.8/examples/audio/03-load-wav/README.txt new file mode 100644 index 0000000..57eb90c --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/03-load-wav/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 creates a simple audio stream for playing sound, and | ||
| 4 | loads a .wav file that is pushed through the stream in a loop. | ||
| 5 | |||
diff --git a/contrib/SDL-3.2.8/examples/audio/03-load-wav/load-wav.c b/contrib/SDL-3.2.8/examples/audio/03-load-wav/load-wav.c new file mode 100644 index 0000000..c517e5d --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/03-load-wav/load-wav.c | |||
| @@ -0,0 +1,103 @@ | |||
| 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. */ | ||
| 22 | static SDL_Window *window = NULL; | ||
| 23 | static SDL_Renderer *renderer = NULL; | ||
| 24 | static SDL_AudioStream *stream = NULL; | ||
| 25 | static Uint8 *wav_data = NULL; | ||
| 26 | static Uint32 wav_data_len = 0; | ||
| 27 | |||
| 28 | /* This function runs once at startup. */ | ||
| 29 | SDL_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, 0, &window, &renderer)) { | ||
| 43 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
| 44 | return SDL_APP_FAILURE; | ||
| 45 | } | ||
| 46 | |||
| 47 | /* Load the .wav file from wherever the app is being run from. */ | ||
| 48 | SDL_asprintf(&wav_path, "%ssample.wav", SDL_GetBasePath()); /* allocate a string of the full file path */ | ||
| 49 | if (!SDL_LoadWAV(wav_path, &spec, &wav_data, &wav_data_len)) { | ||
| 50 | SDL_Log("Couldn't load .wav file: %s", SDL_GetError()); | ||
| 51 | return SDL_APP_FAILURE; | ||
| 52 | } | ||
| 53 | |||
| 54 | SDL_free(wav_path); /* done with this string. */ | ||
| 55 | |||
| 56 | /* Create our audio stream in the same format as the .wav file. It'll convert to what the audio hardware wants. */ | ||
| 57 | stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL); | ||
| 58 | if (!stream) { | ||
| 59 | SDL_Log("Couldn't create audio stream: %s", SDL_GetError()); | ||
| 60 | return SDL_APP_FAILURE; | ||
| 61 | } | ||
| 62 | |||
| 63 | /* SDL_OpenAudioDeviceStream starts the device paused. You have to tell it to start! */ | ||
| 64 | SDL_ResumeAudioStreamDevice(stream); | ||
| 65 | |||
| 66 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 67 | } | ||
| 68 | |||
| 69 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
| 70 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 71 | { | ||
| 72 | if (event->type == SDL_EVENT_QUIT) { | ||
| 73 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
| 74 | } | ||
| 75 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 76 | } | ||
| 77 | |||
| 78 | /* This function runs once per frame, and is the heart of the program. */ | ||
| 79 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 80 | { | ||
| 81 | /* see if we need to feed the audio stream more data yet. | ||
| 82 | We're being lazy here, but if there's less than the entire wav file left to play, | ||
| 83 | just shove a whole copy of it into the queue, so we always have _tons_ of | ||
| 84 | data queued for playback. */ | ||
| 85 | if (SDL_GetAudioStreamQueued(stream) < (int)wav_data_len) { | ||
| 86 | /* feed more data to the stream. It will queue at the end, and trickle out as the hardware needs more data. */ | ||
| 87 | SDL_PutAudioStreamData(stream, wav_data, wav_data_len); | ||
| 88 | } | ||
| 89 | |||
| 90 | /* we're not doing anything with the renderer, so just blank it out. */ | ||
| 91 | SDL_RenderClear(renderer); | ||
| 92 | SDL_RenderPresent(renderer); | ||
| 93 | |||
| 94 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 95 | } | ||
| 96 | |||
| 97 | /* This function runs once at shutdown. */ | ||
| 98 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 99 | { | ||
| 100 | SDL_free(wav_data); /* strictly speaking, this isn't necessary because the process is ending, but it's good policy. */ | ||
| 101 | /* SDL will clean up the window/renderer for us. */ | ||
| 102 | } | ||
| 103 | |||
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 | } | ||
diff --git a/contrib/SDL-3.2.8/examples/audio/onmouseover.webp b/contrib/SDL-3.2.8/examples/audio/onmouseover.webp new file mode 100644 index 0000000..1f3ba4b --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/onmouseover.webp | |||
| Binary files differ | |||
diff --git a/contrib/SDL-3.2.8/examples/audio/thumbnail.png b/contrib/SDL-3.2.8/examples/audio/thumbnail.png new file mode 100644 index 0000000..10bc6c8 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/audio/thumbnail.png | |||
| Binary files differ | |||
