11 #define SET_MUS_VOL(vol) \
13 int mv = (vol) * vol_master >> 10; \
14 MIDASsetMusicVolume(modplay, mv ? mv + 1 : 0); \
17 extern int force_snd_config;
19 static MIDASmodulePlayHandle modplay;
20 static struct au_module *curmod;
22 static int vol_master, vol_mus, vol_sfx;
28 vol_master = vol_mus = vol_sfx = 255;
32 if(force_snd_config || (!MIDASloadConfig("sound.cfg") && !MIDASdetectSoundCard())) {
34 if(!MIDASsaveConfig("sound.cfg")) {
35 fprintf(stderr, "failed to save sound card configuration\n");
42 MIDASstartBackgroundPlay(0);
46 void au_shutdown(void)
48 printf("au_shutdown\n");
50 au_stop_module(curmod);
52 MIDASstopBackgroundPlay();
56 struct au_module *au_load_module(const char *fname)
58 static MIDASmoduleInfo info;
59 struct au_module *mod;
62 if(!(mod = malloc(sizeof *mod))) {
63 fprintf(stderr, "au_load_module: failed to allocate module\n");
67 if(!(mod->impl = MIDASloadModule((char*)fname))) {
68 fprintf(stderr, "au_load_module: failed to load module: %s\n", fname);
74 if(MIDASgetModuleInfo(mod->impl, &info)) {
76 end = name + strlen(name) - 1;
77 while(end >= name && isspace(*end)) {
84 /* fallback to using the filename */
85 if((name = strrchr(fname, '/')) || (name = strrchr(fname, '\\'))) {
92 if(!(mod->name = malloc(strlen(name) + 1))) {
93 fprintf(stderr, "au_load_module: mod->name malloc failed\n");
94 MIDASfreeModule(mod->impl);
97 strcpy(mod->name, name);
99 printf("loaded module \"%s\" (%s)\n", name, fname);
103 void au_free_module(struct au_module *mod)
108 au_stop_module(curmod);
110 MIDASfreeModule(mod->impl);
115 int au_play_module(struct au_module *mod)
118 if(curmod == mod) return 0;
119 au_stop_module(curmod);
122 if(!(modplay = MIDASplayModule(mod->impl, 1))) {
123 fprintf(stderr, "au_play_module: failed to play module: %s\n", mod->name);
126 SET_MUS_VOL(vol_mus);
135 int au_stop_module(struct au_module *mod)
137 if(mod && curmod != mod) return -1;
138 if(!curmod) return -1;
140 MIDASstopModule(modplay);
145 int au_module_state(struct au_module *mod)
148 return curmod == mod ? AU_PLAYING : AU_STOPPED;
150 return curmod ? AU_PLAYING : AU_STOPPED;
153 int au_volume(int vol)
155 AU_VOLADJ(vol_master, vol);
156 if(vol != vol_master) {
159 au_sfx_volume(vol_sfx);
160 au_music_volume(vol_mus);
165 int au_sfx_volume(int vol)
167 AU_VOLADJ(vol_sfx, vol);
170 /* TODO set sfx volume */
175 int au_music_volume(int vol)
177 AU_VOLADJ(vol_mus, vol);
186 /* when using MIDAS, we can't access the PIT directly, so we don't build timer.c
187 * and implement all the timer functions here, using MIDAS callbacks
189 static volatile unsigned long ticks;
190 static unsigned long tick_interval;
192 static void MIDAS_CALL midas_timer(void)
197 /* macro to divide and round to the nearest integer */
198 #define DIV_ROUND(a, b) \
199 ((a) / (b) + ((a) % (b)) / ((b) / 2))
201 void init_timer(int res_hz)
203 MIDASsetTimerCallbacks(res_hz * 1000, 0, midas_timer, 0, 0);
204 tick_interval = DIV_ROUND(1000, res_hz);
207 void reset_timer(void)
212 unsigned long get_msec(void)
214 return ticks * tick_interval;
217 void sleep_msec(unsigned long msec)
219 unsigned long wakeup_time = ticks + msec / tick_interval;
220 while(ticks < wakeup_time) {
230 static int vol_master, vol_mus, vol_sfx;
234 vol_master = vol_mus = vol_sfx = 255;
238 void au_shutdown(void)
240 printf("au_shutdown\n");
243 struct au_module *au_load_module(const char *fname)
248 void au_free_module(struct au_module *mod)
252 int au_play_module(struct au_module *mod)
261 int au_stop_module(struct au_module *mod)
266 int au_module_state(struct au_module *mod)
271 int au_volume(int vol)
273 AU_VOLADJ(vol_master, vol);
277 int au_sfx_volume(int vol)
279 AU_VOLADJ(vol_sfx, vol);
284 int au_music_volume(int vol)
286 AU_VOLADJ(vol_mus, vol);
289 #endif /* NO_SOUND */