2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
16 #include <SDL_mixer.h>
22 /*---------------------------------------------------------------------------*/
24 static int audio_state = 0;
26 static char name[MAXSND][MAXSTR];
27 static int chan[MAXSND];
28 static Mix_Chunk *buff[MAXSND];
29 static Mix_Music *song;
31 static char curr_bgm[MAXSTR];
32 static char next_bgm[MAXSTR];
34 static float fade_volume = 1.0f;
35 static float fade_rate = 0.0f;
37 /*---------------------------------------------------------------------------*/
41 int r = config_get_d(CONFIG_AUDIO_RATE);
42 int b = config_get_d(CONFIG_AUDIO_BUFF);
45 memset(curr_bgm, 0, MAXSTR);
46 memset(next_bgm, 0, MAXSTR);
50 if (Mix_OpenAudio(r, MIX_DEFAULT_FORMAT, 1, b) == 0)
52 for (i = 0; i < MAXSND; i++)
54 buff[i] = Mix_LoadWAV(config_data(name[i]));
58 audio_volume(config_get_d(CONFIG_SOUND_VOLUME),
59 config_get_d(CONFIG_MUSIC_VOLUME));
63 fprintf(stderr, _("Sound disabled\n"));
77 for (i = 0; i < MAXSND; i++)
80 Mix_FreeChunk(buff[i]);
90 void audio_bind(int i, int c, const char *filename)
92 strncpy(name[i], filename, MAXSTR);
96 void audio_play(int i, float v)
98 if (audio_state == 1 && buff[i])
100 Mix_VolumeChunk(buff[i], (int) (v * MIX_MAX_VOLUME));
101 Mix_PlayChannel(chan[i], buff[i], 0);
105 /*---------------------------------------------------------------------------*/
107 void audio_music_play(const char *filename)
113 if ((config_get_d(CONFIG_MUSIC_VOLUME) > 0) &&
114 (song = Mix_LoadMUS(config_data(filename))))
116 Mix_PlayMusic(song, -1);
117 strcpy(curr_bgm, filename);
122 void audio_music_queue(const char *filename)
126 if (strlen(curr_bgm) == 0 || strcmp(filename, curr_bgm) != 0)
131 audio_music_play(filename);
132 strcpy(curr_bgm, filename);
139 void audio_music_stop(void)
143 if (Mix_PlayingMusic())
153 /*---------------------------------------------------------------------------*/
155 * SDL_mixer already provides music fading. Unfortunately, it halts playback
156 * at the end of a fade. We need to be able to fade music back in from the
157 * point where it stopped. So, we reinvent this wheel.
160 void audio_timer(float dt)
164 if (fade_rate > 0.0f || fade_rate < 0.0f)
165 fade_volume += dt / fade_rate;
167 if (fade_volume < 0.0f)
171 if (strlen(next_bgm) == 0)
174 if (Mix_PlayingMusic())
179 fade_rate = -fade_rate;
180 audio_music_queue(next_bgm);
184 if (fade_volume > 1.0f)
190 if (Mix_PausedMusic() && fade_rate > 0.0f)
193 if (Mix_PlayingMusic())
194 Mix_VolumeMusic(config_get_d(CONFIG_MUSIC_VOLUME) *
195 (int) (fade_volume * MIX_MAX_VOLUME) / 10);
199 void audio_music_fade_out(float t)
202 strcpy(next_bgm, "");
205 void audio_music_fade_in(float t)
208 strcpy(next_bgm, "");
211 void audio_music_fade_to(float t, const char *filename)
215 if (strlen(curr_bgm) == 0 || strcmp(filename, curr_bgm) != 0)
217 strcpy(next_bgm, filename);
224 audio_music_queue(filename);
225 audio_music_fade_in(t);
229 void audio_volume(int s, int m)
233 Mix_Volume(-1, s * MIX_MAX_VOLUME / 10);
234 Mix_VolumeMusic(m * MIX_MAX_VOLUME / 10);
238 /*---------------------------------------------------------------------------*/