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/02-simple-playback-callback/simple-playback-callback.c | |
parent | 8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff) |
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/examples/audio/02-simple-playback-callback/simple-playback-callback.c')
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/audio/02-simple-playback-callback/simple-playback-callback.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/examples/audio/02-simple-playback-callback/simple-playback-callback.c b/src/contrib/SDL-3.2.20/examples/audio/02-simple-playback-callback/simple-playback-callback.c new file mode 100644 index 0000000..c011c72 --- /dev/null +++ b/src/contrib/SDL-3.2.20/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 | |||