10 #define SET_MUS_VOL(vol) \
12 int mv = (vol) * vol_master >> 10; \
13 MIDASsetMusicVolume(modplay, mv ? mv + 1 : 0); \
16 extern int force_snd_config;
18 static MIDASmodulePlayHandle modplay;
19 static struct au_module *curmod;
21 static int vol_master, vol_mus, vol_sfx;
27 vol_master = vol_mus = vol_sfx = 255;
31 if(force_snd_config || (!MIDASloadConfig("sound.cfg") && !MIDASdetectSoundCard())) {
33 if(!MIDASsaveConfig("sound.cfg")) {
34 fprintf(stderr, "failed to save sound card configuration\n");
41 MIDASstartBackgroundPlay(0);
45 void au_shutdown(void)
47 printf("au_shutdown\n");
49 au_stop_module(curmod);
51 MIDASstopBackgroundPlay();
55 struct au_module *au_load_module(const char *fname)
57 static MIDASmoduleInfo info;
58 struct au_module *mod;
61 if(!(mod = malloc(sizeof *mod))) {
62 fprintf(stderr, "au_load_module: failed to allocate module\n");
66 if(!(mod->impl = MIDASloadModule((char*)fname))) {
67 fprintf(stderr, "au_load_module: failed to load module: %s\n", fname);
73 if(MIDASgetModuleInfo(mod->impl, &info)) {
75 end = name + strlen(name) - 1;
76 while(end >= name && isspace(*end)) {
83 /* fallback to using the filename */
84 if((name = strrchr(fname, '/')) || (name = strrchr(fname, '\\'))) {
91 if(!(mod->name = malloc(strlen(name) + 1))) {
92 fprintf(stderr, "au_load_module: mod->name malloc failed\n");
93 MIDASfreeModule(mod->impl);
96 strcpy(mod->name, name);
98 printf("loaded module \"%s\" (%s)\n", name, fname);
102 void au_free_module(struct au_module *mod)
107 au_stop_module(curmod);
109 MIDASfreeModule(mod->impl);
114 int au_play_module(struct au_module *mod)
117 if(curmod == mod) return 0;
118 au_stop_module(curmod);
121 if(!(modplay = MIDASplayModule(mod->impl, 1))) {
122 fprintf(stderr, "au_play_module: failed to play module: %s\n", mod->name);
125 SET_MUS_VOL(vol_mus);
134 int au_stop_module(struct au_module *mod)
136 if(mod && curmod != mod) return -1;
137 if(!curmod) return -1;
139 MIDASstopModule(modplay);
144 int au_module_state(struct au_module *mod)
147 return curmod == mod ? AU_PLAYING : AU_STOPPED;
149 return curmod ? AU_PLAYING : AU_STOPPED;
152 int au_volume(int vol)
154 AU_VOLADJ(vol_master, vol);
155 if(vol != vol_master) {
158 au_sfx_volume(vol_sfx);
159 au_music_volume(vol_mus);
164 int au_sfx_volume(int vol)
166 AU_VOLADJ(vol_sfx, vol);
169 /* TODO set sfx volume */
174 int au_music_volume(int vol)
176 AU_VOLADJ(vol_mus, vol);
185 /* when using MIDAS, we can't access the PIT directly, so we don't build timer.c
186 * and implement all the timer functions here, using MIDAS callbacks
188 static volatile unsigned long ticks;
189 static unsigned long tick_interval;
191 static void MIDAS_CALL midas_timer(void)
196 /* macro to divide and round to the nearest integer */
197 #define DIV_ROUND(a, b) \
198 ((a) / (b) + ((a) % (b)) / ((b) / 2))
200 void init_timer(int res_hz)
202 MIDASsetTimerCallbacks(res_hz * 1000, 0, midas_timer, 0, 0);
203 tick_interval = DIV_ROUND(1000, res_hz);
206 void reset_timer(void)
211 unsigned long get_msec(void)
213 return ticks * tick_interval;
216 void sleep_msec(unsigned long msec)
218 unsigned long wakeup_time = ticks + msec / tick_interval;
219 while(ticks < wakeup_time) {
229 static int vol_master, vol_mus, vol_sfx;
233 vol_master = vol_mus = vol_sfx = 255;
237 void au_shutdown(void)
239 printf("au_shutdown\n");
242 struct au_module *au_load_module(const char *fname)
247 void au_free_module(struct au_module *mod)
251 int au_play_module(struct au_module *mod)
260 int au_stop_module(struct au_module *mod)
265 int au_module_state(struct au_module *mod)
270 int au_volume(int vol)
272 AU_VOLADJ(vol_master, vol);
276 int au_sfx_volume(int vol)
278 AU_VOLADJ(vol_sfx, vol);
283 int au_music_volume(int vol)
285 AU_VOLADJ(vol_mus, vol);
288 #endif /* NO_SOUND */