Add tests for new sound API
[projects/chimara/chimara.git] / tests / soundtest.c
1 #include <libchimara/glk.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 #define NUM_CHANNELS 2
7
8 void
9 glk_main(void)
10 {
11         if(!glk_gestalt(gestalt_Sound, 0)) {
12                 fprintf(stderr, "Sound not supported.\n");
13                 return;
14         }
15         if(!glk_gestalt(gestalt_SoundVolume, 0)) {
16                 fprintf(stderr, "Sound volume not supported.\n");
17                 return;
18         }
19         if(!glk_gestalt(gestalt_SoundNotify, 0)) {
20                 fprintf(stderr, "Sound notification not supported.\n");
21                 return;
22         }
23         
24         schanid_t sc[NUM_CHANNELS];
25         int count;
26         for(count = 0; count < NUM_CHANNELS; count++) {
27                 sc[count] = glk_schannel_create(count);
28                 if(!sc[count]) {
29                         fprintf(stderr, "Could not create sound channel number %d.\n", count);
30                         return;
31                 }
32         }
33
34         /* Open the main window. */
35     winid_t mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 1);
36     if (!mainwin) {
37         /* It's possible that the main window failed to open. There's
38             nothing we can do without it, so exit. */
39         return;
40     }
41         glk_set_window(mainwin);
42         glk_put_string("Copy a sound file to the current directory and rename it "
43             "to SND3. Supported formats: AIFF, OGG, MOD, S3M, IT, XM. Type 'play' "
44             "to play it.\n\n"
45                 "If you want to test multi-sound playing, copy another sound file and "
46                 "rename it to SND4 as well. You can't stop it, so make it a short "
47                 "sound effect.\n");
48
49         char buffer[1024];
50         int len;
51         int finish = 0;
52         int repeat = 1;
53         int ramp = 0;
54
55         event_t ev;
56         while(!finish) {
57                 glk_put_string("\nprompt> ");
58                 glk_request_line_event(mainwin, buffer, 1024, 0);
59                 glk_select(&ev);
60                 printf("Received event:\n");
61                 printf("Type: %d\n", ev.type);
62                 printf("Win: ");
63                 if(ev.win)
64                         printf( "%d\n", glk_window_get_rock(ev.win) );
65                 else
66                         printf("NULL\n");
67                 printf("Var1: %d\n", ev.val1);
68                 printf("Var2: %d\n", ev.val2);
69                 switch(ev.type) {
70                         case evtype_LineInput:
71                                 // Null-terminate string
72                                 len = ev.val1;
73                                 buffer[len] = '\0';
74
75                                 if(strcmp(buffer, "quit") == 0) {
76                                         glk_put_string("That's all, folks.\n");
77                                         finish = 1;
78                                 } else if(strcmp(buffer, "play") == 0) {
79                                         glk_put_string("Playing sound.\n");
80                                         if(!glk_schannel_play_ext(sc[0], 3, repeat, 1)) {
81                                                 fprintf(stderr, "Could not start sound channel.\n");
82                                                 finish = 1;
83                                         }
84                                 } else if(strcmp(buffer, "stop") == 0) {
85                                         glk_put_string("Stopping sound.\n");
86                                         glk_schannel_stop(sc[0]);
87                                 } else if(strcmp(buffer, "repeat") == 0) {
88                                         glk_put_string("Setting repeat to ");
89                                         if(repeat == 1) {
90                                                 glk_put_string("TWICE.\n");
91                                                 repeat = 2;
92                                         } else if(repeat == 2) {
93                                                 glk_put_string("INFINITE.\n");
94                                                 repeat = -1;
95                                         } else if(repeat == -1) {
96                                                 glk_put_string("DON'T PLAY.\n");
97                                                 repeat = 0;
98                                         } else if(repeat == 0) {
99                                                 glk_put_string("ONCE.\n");
100                                                 repeat = 1;
101                                         }
102                                 } else if(strcmp(buffer, "pause") == 0) {
103                                         glk_put_string("Pausing channel.\n");
104                                         glk_schannel_pause(sc[0]);
105                                 } else if(strcmp(buffer, "unpause") == 0) {
106                                         glk_put_string("Unpausing channel.\n");
107                                         glk_schannel_unpause(sc[0]);
108                                 } else if(strcmp(buffer, "ramp") == 0) {
109                                         glk_put_string("Ramping volume to ");
110                                         if(ramp == 0) {
111                                                 glk_put_string("HALF.\n");
112                                                 glk_schannel_set_volume_ext(sc[0], 0x8000, 1000, 42);
113                                                 ramp = 1;
114                                         } else if(ramp == 1) {
115                                                 glk_put_string("FULL.\n");
116                                                 glk_schannel_set_volume_ext(sc[0], 0x10000, 1000, 69);
117                                                 ramp = 0;
118                                         }
119                                 } else if(strcmp(buffer, "multi") == 0) {
120                                         glk_put_string("Playing two sounds. (These will not repeat.)\n");
121                                         glui32 sounds[NUM_CHANNELS] = { 3, 4 };
122                                         if(glk_schannel_play_multi(sc, NUM_CHANNELS, sounds, NUM_CHANNELS, 1) < 2) {
123                                                 fprintf(stderr, "Tried to start %d sounds, but not all were successful.", NUM_CHANNELS);
124                                                 finish = 1;
125                                         }
126                                 } else if(strcmp(buffer, "help") == 0) {
127                                         glk_put_string("Type PLAY or MULTI or REPEAT or PAUSE or UNPAUSE or RAMP or STOP or QUIT.\n");
128                                 }
129                                 break;
130                         case evtype_SoundNotify:
131                                 glk_cancel_line_event(mainwin, NULL);
132                                 glk_put_string("\nGot sound notify event!\n");
133                                 break;
134                         case evtype_VolumeNotify:
135                                 glk_cancel_line_event(mainwin, NULL);
136                                 glk_put_string("\nGot volume notify event!\n");
137                                 break;
138                         default:
139                                 ;
140                 }
141         }
142
143         for(count = 0; count < NUM_CHANNELS; count++) {
144                 glk_schannel_stop(sc[count]);
145                 glk_schannel_destroy(sc[count]);
146         }
147 }