change SDL 1.2 to SDL 2.0
[platform/upstream/SDL.git] / test / loopwave.c
1 /*
2   Copyright (C) 1997-2016 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 "SDL_config.h"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #if HAVE_SIGNAL_H
24 #include <signal.h>
25 #endif
26
27 #ifdef __EMSCRIPTEN__
28 #include <emscripten/emscripten.h>
29 #endif
30
31 #include "SDL.h"
32
33 struct
34 {
35     SDL_AudioSpec spec;
36     Uint8 *sound;               /* Pointer to wave data */
37     Uint32 soundlen;            /* Length of wave data */
38     int soundpos;               /* Current play position */
39 } wave;
40
41
42 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
43 static void
44 quit(int rc)
45 {
46     SDL_Quit();
47     exit(rc);
48 }
49
50
51 void SDLCALL
52 fillerup(void *unused, Uint8 * stream, int len)
53 {
54     Uint8 *waveptr;
55     int waveleft;
56
57     /* Set up the pointers */
58     waveptr = wave.sound + wave.soundpos;
59     waveleft = wave.soundlen - wave.soundpos;
60
61     /* Go! */
62     while (waveleft <= len) {
63         SDL_memcpy(stream, waveptr, waveleft);
64         stream += waveleft;
65         len -= waveleft;
66         waveptr = wave.sound;
67         waveleft = wave.soundlen;
68         wave.soundpos = 0;
69     }
70     SDL_memcpy(stream, waveptr, len);
71     wave.soundpos += len;
72 }
73
74 static int done = 0;
75 void
76 poked(int sig)
77 {
78     done = 1;
79 }
80
81 #ifdef __EMSCRIPTEN__
82 void
83 loop()
84 {
85     if(done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING))
86         emscripten_cancel_main_loop();
87 }
88 #endif
89
90 int
91 main(int argc, char *argv[])
92 {
93     int i;
94     char filename[4096];
95
96     /* Enable standard application logging */
97     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
98
99     /* Load the SDL library */
100     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
101         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
102         return (1);
103     }
104
105     if (argc > 1) {
106         SDL_strlcpy(filename, argv[1], sizeof(filename));
107     } else {
108         SDL_strlcpy(filename, "sample.wav", sizeof(filename));
109     }
110     /* Load the wave file into memory */
111     if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
112         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
113         quit(1);
114     }
115
116     wave.spec.callback = fillerup;
117 #if HAVE_SIGNAL_H
118     /* Set the signals */
119 #ifdef SIGHUP
120     signal(SIGHUP, poked);
121 #endif
122     signal(SIGINT, poked);
123 #ifdef SIGQUIT
124     signal(SIGQUIT, poked);
125 #endif
126     signal(SIGTERM, poked);
127 #endif /* HAVE_SIGNAL_H */
128
129     /* Show the list of available drivers */
130     SDL_Log("Available audio drivers:");
131     for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
132         SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
133     }
134
135     /* Initialize fillerup() variables */
136     if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
137         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
138         SDL_FreeWAV(wave.sound);
139         quit(2);
140     }
141
142     SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
143
144     /* Let the audio run */
145     SDL_PauseAudio(0);
146
147 #ifdef __EMSCRIPTEN__
148     emscripten_set_main_loop(loop, 0, 1);
149 #else
150     while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
151         SDL_Delay(1000);
152 #endif
153
154     /* Clean up on signal */
155     SDL_CloseAudio();
156     SDL_FreeWAV(wave.sound);
157     SDL_Quit();
158     return (0);
159 }
160
161 /* vi: set ts=4 sw=4 expandtab: */