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