upgrade SDL to version 2.0.8
[platform/upstream/SDL.git] / test / loopwavequeue.c
1 /*
2   Copyright (C) 1997-2018 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 sound queueing */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #ifdef __EMSCRIPTEN__
19 #include <emscripten/emscripten.h>
20 #endif
21
22 #include "SDL.h"
23
24 #if HAVE_SIGNAL_H
25 #include <signal.h>
26 #endif
27
28 static struct
29 {
30     SDL_AudioSpec spec;
31     Uint8 *sound;               /* Pointer to wave data */
32     Uint32 soundlen;            /* Length of wave data */
33 } wave;
34
35
36 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
37 static void
38 quit(int rc)
39 {
40     SDL_Quit();
41     exit(rc);
42 }
43
44 static int done = 0;
45 void
46 poked(int sig)
47 {
48     done = 1;
49 }
50
51 void
52 loop()
53 {
54 #ifdef __EMSCRIPTEN__
55     if (done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)) {
56         emscripten_cancel_main_loop();
57     }
58     else
59 #endif
60     {
61         /* The device from SDL_OpenAudio() is always device #1. */
62         const Uint32 queued = SDL_GetQueuedAudioSize(1);
63         SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
64         if (queued <= 8192) {  /* time to requeue the whole thing? */
65             if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
66                 SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
67             } else {
68                 SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
69             }
70         }
71     }
72 }
73 int
74 main(int argc, char *argv[])
75 {
76     char filename[4096];
77     /* Enable standard application logging */
78     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
79
80     /* Load the SDL library */
81     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
82         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
83         return (1);
84     }
85
86     if (argc > 1) {
87         SDL_strlcpy(filename, argv[1], sizeof(filename));
88     } else {
89         SDL_strlcpy(filename, "./res/sample.wav", sizeof(filename));
90     }
91     /* Load the wave file into memory */
92     if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
93         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
94         quit(1);
95     }
96
97     wave.spec.callback = NULL;  /* we'll push audio. */
98
99 #if HAVE_SIGNAL_H
100     /* Set the signals */
101 #ifdef SIGHUP
102     signal(SIGHUP, poked);
103 #endif
104     signal(SIGINT, poked);
105 #ifdef SIGQUIT
106     signal(SIGQUIT, poked);
107 #endif
108     signal(SIGTERM, poked);
109 #endif /* HAVE_SIGNAL_H */
110
111     /* Initialize fillerup() variables */
112     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
113         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
114         SDL_FreeWAV(wave.sound);
115         quit(2);
116     }
117
118     /*static x[99999]; SDL_QueueAudio(1, x, sizeof (x));*/
119
120     /* Let the audio run */
121     SDL_PauseAudio(0);
122
123     done = 0;
124
125     /* Note that we stuff the entire audio buffer into the queue in one
126        shot. Most apps would want to feed it a little at a time, as it
127        plays, but we're going for simplicity here. */
128
129 #ifdef __EMSCRIPTEN__
130     emscripten_set_main_loop(loop, 0, 1);
131 #else
132     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
133     {
134         loop();
135
136         SDL_Delay(100);  /* let it play for awhile. */
137     }
138 #endif
139
140     /* Clean up on signal */
141     SDL_CloseAudio();
142     SDL_FreeWAV(wave.sound);
143     SDL_Quit();
144     return 0;
145 }
146
147 /* vi: set ts=4 sw=4 expandtab: */