Atlas - simple-playback-callback.c

Home / ext / SDL / examples / audio / 02-simple-playback-callback Lines: 1 | Size: 5016 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
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. */ 17static SDL_Window *window = NULL; 18static SDL_Renderer *renderer = NULL; 19static SDL_AudioStream *stream = NULL; 20static int current_sine_sample = 0; 21 22/* this function will be called (usually in a background thread) when the audio stream is consuming data. */ 23static 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. */ 54SDL_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, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 67 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 68 return SDL_APP_FAILURE; 69 } 70 SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); 71 72 /* We're just playing a single thing here, so we'll use the simplified option. 73 We are always going to feed audio in as mono, float32 data at 8000Hz. 74 The stream will convert it to whatever the hardware wants on the other side. */ 75 spec.channels = 1; 76 spec.format = SDL_AUDIO_F32; 77 spec.freq = 8000; 78 stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, FeedTheAudioStreamMore, NULL); 79 if (!stream) { 80 SDL_Log("Couldn't create audio stream: %s", SDL_GetError()); 81 return SDL_APP_FAILURE; 82 } 83 84 /* SDL_OpenAudioDeviceStream starts the device paused. You have to tell it to start! */ 85 SDL_ResumeAudioStreamDevice(stream); 86 87 return SDL_APP_CONTINUE; /* carry on with the program! */ 88} 89 90/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 91SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 92{ 93 if (event->type == SDL_EVENT_QUIT) { 94 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 95 } 96 return SDL_APP_CONTINUE; /* carry on with the program! */ 97} 98 99/* This function runs once per frame, and is the heart of the program. */ 100SDL_AppResult SDL_AppIterate(void *appstate) 101{ 102 /* we're not doing anything with the renderer, so just blank it out. */ 103 SDL_RenderClear(renderer); 104 SDL_RenderPresent(renderer); 105 106 /* all the work of feeding the audio stream is happening in a callback in a background thread. */ 107 108 return SDL_APP_CONTINUE; /* carry on with the program! */ 109} 110 111/* This function runs once at shutdown. */ 112void SDL_AppQuit(void *appstate, SDL_AppResult result) 113{ 114 /* SDL will clean up the window/renderer for us. */ 115} 116 117
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.