24 bool audio::audio_initialized =
false;
26 int audio::background_volume;
27 int audio::effects_volume;
31 Mix_Music *audio::music[NUM_MUSIC];
32 string audio::music_file[NUM_MUSIC];
33 Mix_Chunk *audio::sounds[NUM_WAVES];
34 int audio::current_background;
35 int audio::last_background;
36 bool audio::background_paused;
37 int audio::audio_rate;
38 Uint16 audio::buffer_size;
39 Uint16 audio::audio_format;
40 int audio::audio_channels;
44 bool audio::schedule_active = 0;
45 PyObject *audio::schedule_args = NULL;
48 void audio::init (
config *myconfig) {
75 current_background = -1;
77 background_paused =
false;
78 audio_initialized =
false;
79 schedule_active =
false;
82 for (i = 0; i < NUM_WAVES; i++) sounds[i] = NULL;
83 for (i = 0; i < NUM_MUSIC; i++) {
89 i = Mix_OpenAudio(audio_rate, audio_format, audio_channels, buffer_size);
94 fprintf(stderr,
"Unable to open audio: %s\n", SDL_GetError());
95 fprintf(stderr,
"Audio will not be used.\n");
99 audio_initialized =
true;
100 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
101 set_background_volume (background_volume);
105 void audio::cleanup(
void)
109 current_background = -1;
112 for (i = 0; i < NUM_WAVES; i++)
118 for (i = 0; i < NUM_MUSIC; i++)
120 unload_background(i);
129 if (audio_initialized ==
true)
132 audio_initialized =
false;
136 int audio::load_background(
int slot,
char *filename) {
138 if (!audio_initialized)
return (0);
141 if ((slot >= NUM_MUSIC) || (slot < 0)) {
142 fprintf(stderr,
"Error: Tried to put music in invalid slot.\n");
147 FILE *f = fopen (filename,
"r");
149 fprintf(stderr,
"Error: No such file: %s.\n", filename);
155 if (music[slot] != NULL)
156 unload_background (slot);
159 music[slot] = Mix_LoadMUS(filename);
160 music_file[slot] = filename;
172 void audio::unload_background (
int slot)
174 if (music[slot] == NULL)
return;
178 if (current_background == slot)
180 last_background = current_background;
181 current_background = -1;
188 Mix_FreeMusic (music[slot]);
190 music_file[slot] =
"";
197 void audio::pause_music(
void) {
201 void audio::unpause_music(
void) {
207 void audio::set_background_volume(
int volume) {
211 background_volume = 0;
212 }
else if (volume > 100) {
213 background_volume = 100;
215 background_volume = volume;
218 Mix_VolumeMusic(
int(background_volume * 1.28));
223 int audio::load_wave(
int slot,
char *filename) {
225 if (!audio_initialized)
return(1);
228 if ((slot >= NUM_WAVES) || (slot < 0)) {
229 fprintf(stderr,
"Error: Tried to put wave in invalid slot.\n");
233 FILE *f = fopen (filename,
"r");
242 sounds[slot] = Mix_LoadWAV(filename);
247 void audio::unload_wave(
int wave) {
248 if (sounds[wave] != NULL) {
249 Mix_FreeChunk(sounds[wave]);
254 void audio::play_wave(
int channel,
int slot) {
255 if ((slot > -1) && (slot < NUM_CHANNELS))
256 if (sounds[slot] != NULL) Mix_PlayChannel(channel, sounds[slot], 0);
259 void audio::play_background(
int slot) {
260 if (music[slot] != NULL) {
261 current_background = slot;
262 Mix_PlayMusic(music[current_background], 0);
266 void audio::fade_out_background(
int time) {
267 if (Mix_PlayingMusic ())
269 Mix_FadeOutMusic(time);
270 last_background = current_background;
271 current_background = -1;
278 void audio::fade_in_background(
int slot,
int time) {
279 if (music[slot] != NULL) {
280 current_background = slot;
281 Mix_FadeInMusic(music[slot], 0, time);
286 void audio::change_background(
int slot,
int time) {
287 fade_out_background(time);
288 fade_in_background(slot, time);
299 void audio::set_schedule (
string file, PyObject * args)
303 Py_XDECREF (schedule_args);
304 schedule_args = NULL;
309 schedule_args = args;
310 Py_XINCREF (schedule_args);
316 void audio::run_schedule ()
318 PyObject *song = Py_BuildValue (
"(i)", last_background);
319 if (schedule_active) schedule.
call_method (
"music_finished", song);
327 current_background >> file;
330 if (current_background != -1) music_file[current_background] >> file;
340 is_schedule_activated () >> file;
352 last_background << file;
355 if (last_background != -1)
360 if (load_background (last_background, (
char *) song.c_str ()))
361 play_background (last_background);
365 PyObject * args = NULL;
370 set_schedule (script, args);
373 schedule_active << file;