diff options
author | 3gg <3gg@shellblade.net> | 2025-08-30 16:53:58 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-08-30 16:53:58 -0700 |
commit | 6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch) | |
tree | 34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/examples/audio/03-load-wav | |
parent | 8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff) |
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/examples/audio/03-load-wav')
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/audio/03-load-wav/README.txt | 5 | ||||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/audio/03-load-wav/load-wav.c | 103 |
2 files changed, 108 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/examples/audio/03-load-wav/README.txt b/src/contrib/SDL-3.2.20/examples/audio/03-load-wav/README.txt new file mode 100644 index 0000000..57eb90c --- /dev/null +++ b/src/contrib/SDL-3.2.20/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/src/contrib/SDL-3.2.20/examples/audio/03-load-wav/load-wav.c b/src/contrib/SDL-3.2.20/examples/audio/03-load-wav/load-wav.c new file mode 100644 index 0000000..c517e5d --- /dev/null +++ b/src/contrib/SDL-3.2.20/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 | |||