Updated internal documentation comments.
2 Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Program to load a wave file and loop playing it using SDL sound queueing */
27 Uint8 *sound; /* Pointer to wave data */
28 Uint32 soundlen; /* Length of wave data */
29 int soundpos; /* Current play position */
33 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
49 main(int argc, char *argv[])
53 /* Enable standard application logging */
54 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
56 /* Load the SDL library */
57 if (SDL_Init(SDL_INIT_AUDIO) < 0) {
58 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
63 SDL_strlcpy(filename, argv[1], sizeof(filename));
65 SDL_strlcpy(filename, "sample.wav", sizeof(filename));
67 /* Load the wave file into memory */
68 if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
69 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
73 wave.spec.callback = NULL; /* we'll push audio. */
78 signal(SIGHUP, poked);
80 signal(SIGINT, poked);
82 signal(SIGQUIT, poked);
84 signal(SIGTERM, poked);
85 #endif /* HAVE_SIGNAL_H */
87 /* Initialize fillerup() variables */
88 if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
89 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
90 SDL_FreeWAV(wave.sound);
94 /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
96 /* Let the audio run */
99 /* Note that we stuff the entire audio buffer into the queue in one
100 shot. Most apps would want to feed it a little at a time, as it
101 plays, but we're going for simplicity here. */
103 while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
105 /* The device from SDL_OpenAudio() is always device #1. */
106 const Uint32 queued = SDL_GetQueuedAudioSize(1);
107 SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
108 if (queued <= 8192) { /* time to requeue the whole thing? */
109 if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
110 SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
112 SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
116 SDL_Delay(100); /* let it play for awhile. */
119 /* Clean up on signal */
121 SDL_FreeWAV(wave.sound);
126 /* vi: set ts=4 sw=4 expandtab: */