8 #define SET_MUS_VOL(vol) \
10 int mv = (vol) * vol_master >> 10; \
11 MIDASsetMusicVolume(modplay, mv ? mv + 1 : 0); \
14 extern int force_snd_config;
16 static MIDASmodulePlayHandle modplay;
17 static struct au_module *curmod;
19 static int vol_master, vol_mus, vol_sfx;
25 vol_master = vol_mus = vol_sfx = 255;
29 if(force_snd_config || (!MIDASloadConfig("sound.cfg") && !MIDASdetectSoundCard())) {
31 if(!MIDASsaveConfig("sound.cfg")) {
32 fprintf(stderr, "failed to save sound card configuration\n");
39 MIDASstartBackgroundPlay(0);
43 void au_shutdown(void)
46 au_stop_module(curmod);
48 MIDASstopBackgroundPlay();
52 struct au_module *au_load_module(const char *fname)
54 static MIDASmoduleInfo info;
55 struct au_module *mod;
58 if(!(mod = malloc(sizeof *mod))) {
59 fprintf(stderr, "au_load_module: failed to allocate module\n");
63 if(!(mod->impl = MIDASloadModule((char*)fname))) {
64 fprintf(stderr, "au_load_module: failed to load module: %s\n", fname);
70 if(MIDASgetModuleInfo(mod->impl, &info)) {
72 end = name + strlen(name) - 1;
73 while(end >= name && isspace(*end)) {
80 /* fallback to using the filename */
81 if((name = strrchr(fname, '/')) || (name = strrchr(fname, '\\'))) {
88 if(!(mod->name = malloc(strlen(name) + 1))) {
89 fprintf(stderr, "au_load_module: mod->name malloc failed\n");
90 MIDASfreeModule(mod->impl);
93 strcpy(mod->name, name);
95 printf("loaded module \"%s\" (%s)\n", name, fname);
99 void au_free_module(struct au_module *mod)
104 au_stop_module(curmod);
106 MIDASfreeModule(mod->impl);
111 int au_play_module(struct au_module *mod)
114 if(curmod == mod) return 0;
115 au_stop_module(curmod);
118 if(!(modplay = MIDASplayModule(mod->impl, 1))) {
119 fprintf(stderr, "au_play_module: failed to play module: %s\n", mod->name);
122 SET_MUS_VOL(vol_mus);
131 int au_stop_module(struct au_module *mod)
133 if(mod && curmod != mod) return -1;
134 if(!curmod) return -1;
136 MIDASstopModule(modplay);
141 int au_module_state(struct au_module *mod)
144 return curmod == mod ? AU_PLAYING : AU_STOPPED;
146 return curmod ? AU_PLAYING : AU_STOPPED;
149 int au_volume(int vol)
151 AU_VOLADJ(vol_master, vol);
152 if(vol != vol_master) {
155 au_sfx_volume(vol_sfx);
156 au_music_volume(vol_mus);
161 int au_sfx_volume(int vol)
163 AU_VOLADJ(vol_sfx, vol);
166 /* TODO set sfx volume */
171 int au_music_volume(int vol)
173 AU_VOLADJ(vol_mus, vol);
182 /* when using MIDAS, we can't access the PIT directly, so we don't build timer.c
183 * and implement all the timer functions here, using MIDAS callbacks
185 static volatile unsigned long ticks;
186 static unsigned long tick_interval;
188 static void MIDAS_CALL midas_timer(void)
193 /* macro to divide and round to the nearest integer */
194 #define DIV_ROUND(a, b) \
195 ((a) / (b) + ((a) % (b)) / ((b) / 2))
197 void init_timer(int res_hz)
199 MIDASsetTimerCallbacks(res_hz * 1000, 0, midas_timer, 0, 0);
200 tick_interval = DIV_ROUND(1000, res_hz);
203 void reset_timer(void)
208 unsigned long get_msec(void)
210 return ticks * tick_interval;