Simple Directmedia Layer
at main 3.8 kB view raw
1/* 2 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org> 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 (!filename) { 66 filename = argv[i]; 67 consumed = 1; 68 } 69 } 70 if (consumed <= 0) { 71 static const char *options[] = { "[sample.wav]", NULL }; 72 SDLTest_CommonLogUsage(state, argv[0], options); 73 exit(1); 74 } 75 76 i += consumed; 77 } 78 79 /* Load the SDL library */ 80 if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_EVENTS)) { 81 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); 82 return SDL_APP_FAILURE; 83 } 84 85 filename = GetResourceFilename(filename, "sample.wav"); 86 87 if (!filename) { 88 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError()); 89 return SDL_APP_FAILURE; 90 } 91 92 /* Load the wave file into memory */ 93 if (!SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen)) { 94 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError()); 95 SDL_free(filename); 96 return SDL_APP_FAILURE; 97 } 98 99 SDL_free(filename); 100 101 /* Show the list of available drivers */ 102 SDL_Log("Available audio drivers:"); 103 for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) { 104 SDL_Log("%i: %s", i, SDL_GetAudioDriver(i)); 105 } 106 107 SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver()); 108 109 stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &wave.spec, NULL, NULL); 110 if (!stream) { 111 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create audio stream: %s\n", SDL_GetError()); 112 return SDL_APP_FAILURE; 113 } 114 115 SDL_ResumeAudioStreamDevice(stream); 116 117 return SDL_APP_CONTINUE; 118} 119 120SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 121{ 122 return (event->type == SDL_EVENT_QUIT) ? SDL_APP_SUCCESS : SDL_APP_CONTINUE; 123} 124 125SDL_AppResult SDL_AppIterate(void *appstate) 126{ 127 return fillerup(); 128} 129 130void SDL_AppQuit(void *appstate, SDL_AppResult result) 131{ 132 SDL_DestroyAudioStream(stream); 133 SDL_free(wave.sound); 134 SDL_Quit(); 135 SDLTest_CommonDestroyState(state); 136} 137