Atlas - loopwave.c
Home / ext / SDL / test Lines: 1 | Size: 4149 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 3 4 This software is provided 'as-is', without any express or implied 5 warranty. In no event will the authors be held liable for any damages 6 arising from the use of this software. 7 8 Permission is granted to anyone to use this software for any purpose, 9 including commercial applications, and to alter it and redistribute it 10 freely. 11*/ 12 13/* Program to load a wave file and loop playing it using SDL audio */ 14 15/* loopwaves.c is much more robust in handling WAVE files -- 16 This is only for simple WAVEs 17*/ 18#include <stdlib.h> 19 20#define SDL_MAIN_USE_CALLBACKS 1 21#include <SDL3/SDL.h> 22#include <SDL3/SDL_main.h> 23#include <SDL3/SDL_test.h> 24#include "testutils.h" 25 26static struct 27{ 28 SDL_AudioSpec spec; 29 Uint8 *sound; /* Pointer to wave data */ 30 Uint32 soundlen; /* Length of wave data */ 31} wave; 32 33static SDL_AudioStream *stream; 34static SDLTest_CommonState *state; 35 36static int fillerup(void) 37{ 38 const int minimum = (wave.soundlen / SDL_AUDIO_FRAMESIZE(wave.spec)) / 2; 39 if (SDL_GetAudioStreamQueued(stream) < minimum) { 40 SDL_PutAudioStreamData(stream, wave.sound, wave.soundlen); 41 } 42 return SDL_APP_CONTINUE; 43} 44 45SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 46{ 47 int i; 48 char *filename = NULL; 49 50 /* this doesn't have to run very much, so give up tons of CPU time between iterations. */ 51 SDL_SetHint(SDL_HINT_MAIN_CALLBACK_RATE, "5"); 52 53 /* Initialize test framework */ 54 state = SDLTest_CommonCreateState(argv, 0); 55 if (!state) { 56 return SDL_APP_SUCCESS; 57 } 58 59 /* Parse commandline */ 60 for (i = 1; i < argc;) { 61 int consumed; 62 63 consumed = SDLTest_CommonArg(state, i); 64 if (!consumed) { 65 if (SDL_strcmp(argv[i], "--role") == 0 && argv[i + 1]) { 66 SDL_SetHint(SDL_HINT_AUDIO_DEVICE_STREAM_ROLE, argv[i + 1]); 67 ++i; 68 consumed = 1; 69 } else if (!filename) { 70 filename = argv[i]; 71 consumed = 1; 72 } 73 } 74 if (consumed <= 0) { 75 static const char *options[] = { "[--role ROLE]", "[sample.wav]", NULL }; 76 SDLTest_CommonLogUsage(state, argv[0], options); 77 exit(1); 78 } 79 80 i += consumed; 81 } 82 83 /* Load the SDL library */ 84 if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_EVENTS)) { 85 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); 86 return SDL_APP_FAILURE; 87 } 88 89 filename = GetResourceFilename(filename, "sample.wav"); 90 91 if (!filename) { 92 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s", SDL_GetError()); 93 return SDL_APP_FAILURE; 94 } 95 96 /* Load the wave file into memory */ 97 if (!SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen)) { 98 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", filename, SDL_GetError()); 99 SDL_free(filename); 100 return SDL_APP_FAILURE; 101 } 102 103 SDL_free(filename); 104 105 /* Show the list of available drivers */ 106 SDL_Log("Available audio drivers:"); 107 for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) { 108 SDL_Log("%i: %s", i, SDL_GetAudioDriver(i)); 109 } 110 111 SDL_Log("Using audio driver: %s", SDL_GetCurrentAudioDriver()); 112 SDL_Log("Current audio device name: %s", SDL_GetAudioDeviceName(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK)); 113 114 stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &wave.spec, NULL, NULL); 115 if (!stream) { 116 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create audio stream: %s", SDL_GetError()); 117 return SDL_APP_FAILURE; 118 } 119 120 SDL_ResumeAudioStreamDevice(stream); 121 122 return SDL_APP_CONTINUE; 123} 124 125SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 126{ 127 return (event->type == SDL_EVENT_QUIT) ? SDL_APP_SUCCESS : SDL_APP_CONTINUE; 128} 129 130SDL_AppResult SDL_AppIterate(void *appstate) 131{ 132 return fillerup(); 133} 134 135void SDL_AppQuit(void *appstate, SDL_AppResult result) 136{ 137 SDL_DestroyAudioStream(stream); 138 SDL_free(wave.sound); 139 SDL_Quit(); 140 SDLTest_CommonDestroyState(state); 141} 142 143[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.