1 /*
2  * Copyright 2012 The Emscripten Authors.  All rights reserved.
3  * Emscripten is available under two separate licenses, the MIT license and the
4  * University of Illinois/NCSA Open Source License.  Both these licenses can be
5  * found in the LICENSE file.
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <SDL/SDL.h>
11 #include <SDL/SDL_mixer.h>
12 #include <assert.h>
13 #include <emscripten.h>
14 #include <sys/stat.h>
15 
16 Mix_Chunk *sound, *sound2, *sound3;
17 Mix_Music * music;
18 int play2();
19 
play()20 int play() {
21   int channel = Mix_PlayChannel(-1, sound, 0);
22   assert(channel == 0);
23 
24   emscripten_run_script("setTimeout(Module['_play2'], 500)");
25   return channel;
26 }
27 
done(int channel)28 void done(int channel) {
29   assert(channel == 1);
30 
31   REPORT_RESULT(1);
32 }
33 
play2()34 int play2() {
35   Mix_ChannelFinished(done);
36 
37   int channel2 = Mix_PlayChannel(-1, sound2, 0);
38   assert(channel2 == 1);
39   int channel3 = Mix_PlayChannel(-1, sound3, 0);
40   assert(channel3 == 2);
41   assert(Mix_PlayMusic(music, 1) == 0);
42   return channel2;
43 }
44 
main(int argc,char ** argv)45 int main(int argc, char **argv) {
46   SDL_Init(SDL_INIT_AUDIO);
47 
48   int ret = Mix_OpenAudio(0, 0, 0, 0); // we ignore all these..
49   assert(ret == 0);
50 
51   {
52       SDL_RWops * ops = SDL_RWFromFile("sound.ogg", "r");
53       sound = Mix_LoadWAV_RW(ops, 0);
54       SDL_FreeRW(ops);
55       assert(sound);
56   }
57 
58   {
59       struct stat info;
60       int result = stat("noise.ogg", &info);
61       char * bytes = malloc( info.st_size );
62       FILE * f = fopen( "noise.ogg", "rb" );
63       fread( bytes, 1, info.st_size, f  );
64       fclose(f);
65 
66       SDL_RWops * ops = SDL_RWFromConstMem(bytes, info.st_size);
67       sound3 = Mix_LoadWAV_RW(ops, 0);
68       SDL_FreeRW(ops);
69       free(bytes);
70   }
71 
72   {
73       music = Mix_LoadMUS("the_entertainer.ogg");
74   }
75 
76 
77   sound2 = Mix_LoadWAV("sound2.wav");
78   assert(sound2);
79 
80   int channel = play();
81   printf( "Pausing Channel %d", channel );
82   Mix_Pause(channel);
83   int paused = Mix_Paused(channel);
84   printf( "Channel %d %s", channel, paused ? "is paused" : "is NOT paused" );
85   assert(paused);
86   Mix_Resume(channel);
87   paused = Mix_Paused(channel);
88   printf( "Channel %d %s", channel, paused ? "is paused" : "is NOT paused" );
89   assert(paused == 0);
90 
91   if (argc == 12121) play2(); // keep it alive
92 
93   emscripten_run_script("element = document.createElement('input');"
94                         "element.setAttribute('type', 'button');"
95                         "element.setAttribute('value', 'replay!');"
96                         "element.setAttribute('onclick', 'Module[\"_play\"]()');"
97                         "document.body.appendChild(element);");
98 
99   printf("you should hear two sounds. press the button to replay!\n");
100 
101   return 0;
102 }
103