summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/examples/audio/01-simple-playback
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/01-simple-playback
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/examples/audio/01-simple-playback')
-rw-r--r--src/contrib/SDL-3.2.20/examples/audio/01-simple-playback/README.txt5
-rw-r--r--src/contrib/SDL-3.2.20/examples/audio/01-simple-playback/simple-playback.c103
2 files changed, 108 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/examples/audio/01-simple-playback/README.txt b/src/contrib/SDL-3.2.20/examples/audio/01-simple-playback/README.txt
new file mode 100644
index 0000000..20ad059
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/examples/audio/01-simple-playback/README.txt
@@ -0,0 +1,5 @@
1If you're running this in a web browser, you need to click the window before you'll hear anything!
2
3This example code creates a simple audio stream for playing sound, and
4generates a sine wave sound effect for it to play as time goes on. This is the
5simplest way to get up and running with procedural sound.
diff --git a/src/contrib/SDL-3.2.20/examples/audio/01-simple-playback/simple-playback.c b/src/contrib/SDL-3.2.20/examples/audio/01-simple-playback/simple-playback.c
new file mode 100644
index 0000000..15126e5
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/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. */
14static SDL_Window *window = NULL;
15static SDL_Renderer *renderer = NULL;
16static SDL_AudioStream *stream = NULL;
17static int current_sine_sample = 0;
18
19/* This function runs once at startup. */
20SDL_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. */
56SDL_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. */
65SDL_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. */
99void SDL_AppQuit(void *appstate, SDL_AppResult result)
100{
101 /* SDL will clean up the window/renderer for us. */
102}
103