9 #define SET_MUS_VOL(vol) \
11 int mv = (vol) * vol_master >> 10; \
12 MIDASsetMusicVolume(modplay, mv ? mv + 1 : 0); \
15 extern int force_snd_config;
17 static MIDASmodulePlayHandle modplay;
18 static struct au_module *curmod;
20 static int vol_master, vol_mus, vol_sfx;
26 vol_master = vol_mus = vol_sfx = 255;
30 if(force_snd_config || (!MIDASloadConfig("sound.cfg") && !MIDASdetectSoundCard())) {
32 if(!MIDASsaveConfig("sound.cfg")) {
33 fprintf(stderr, "failed to save sound card configuration\n");
40 MIDASstartBackgroundPlay(0);
44 void au_shutdown(void)
47 au_stop_module(curmod);
49 MIDASstopBackgroundPlay();
53 struct au_module *au_load_module(const char *fname)
55 static MIDASmoduleInfo info;
56 struct au_module *mod;
59 if(!(mod = malloc(sizeof *mod))) {
60 fprintf(stderr, "au_load_module: failed to allocate module\n");
64 if(!(mod->impl = MIDASloadModule((char*)fname))) {
65 fprintf(stderr, "au_load_module: failed to load module: %s\n", fname);
71 if(MIDASgetModuleInfo(mod->impl, &info)) {
73 end = name + strlen(name) - 1;
74 while(end >= name && isspace(*end)) {
81 /* fallback to using the filename */
82 if((name = strrchr(fname, '/')) || (name = strrchr(fname, '\\'))) {
89 if(!(mod->name = malloc(strlen(name) + 1))) {
90 fprintf(stderr, "au_load_module: mod->name malloc failed\n");
91 MIDASfreeModule(mod->impl);
94 strcpy(mod->name, name);
96 printf("loaded module \"%s\" (%s)\n", name, fname);
100 void au_free_module(struct au_module *mod)
105 au_stop_module(curmod);
107 MIDASfreeModule(mod->impl);
112 int au_play_module(struct au_module *mod)
115 if(curmod == mod) return 0;
116 au_stop_module(curmod);
119 if(!(modplay = MIDASplayModule(mod->impl, 1))) {
120 fprintf(stderr, "au_play_module: failed to play module: %s\n", mod->name);
123 SET_MUS_VOL(vol_mus);
132 int au_stop_module(struct au_module *mod)
134 if(mod && curmod != mod) return -1;
135 if(!curmod) return -1;
137 MIDASstopModule(modplay);
142 int au_module_state(struct au_module *mod)
145 return curmod == mod ? AU_PLAYING : AU_STOPPED;
147 return curmod ? AU_PLAYING : AU_STOPPED;
150 int au_volume(int vol)
152 AU_VOLADJ(vol_master, vol);
153 if(vol != vol_master) {
156 au_sfx_volume(vol_sfx);
157 au_music_volume(vol_mus);
162 int au_sfx_volume(int vol)
164 AU_VOLADJ(vol_sfx, vol);
167 /* TODO set sfx volume */
172 int au_music_volume(int vol)
174 AU_VOLADJ(vol_mus, vol);
183 /* when using MIDAS, we can't access the PIT directly, so we don't build timer.c
184 * and implement all the timer functions here, using MIDAS callbacks
186 static volatile unsigned long ticks;
187 static unsigned long tick_interval;
189 static void MIDAS_CALL midas_timer(void)
194 /* macro to divide and round to the nearest integer */
195 #define DIV_ROUND(a, b) \
196 ((a) / (b) + ((a) % (b)) / ((b) / 2))
198 void init_timer(int res_hz)
200 MIDASsetTimerCallbacks(res_hz * 1000, 0, midas_timer, 0, 0);
201 tick_interval = DIV_ROUND(1000, res_hz);
204 void reset_timer(void)
209 unsigned long get_msec(void)
211 return ticks * tick_interval;
214 void sleep_msec(unsigned long msec)
216 unsigned long wakeup_time = ticks + msec / tick_interval;
217 while(ticks < wakeup_time) {