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, 2, 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"));
75 for (i = 0; i < MAXSND; i++)
78 Mix_FreeChunk(buff[i]);
88 void audio_bind(int i, int c, const char *filename)
90 strncpy(name[i], filename, MAXSTR);
94 void audio_play(int i, float v)
96 if (audio_state == 1 && buff[i])
98 Mix_VolumeChunk(buff[i], (int) (v * MIX_MAX_VOLUME));
99 Mix_PlayChannel(chan[i], buff[i], 0);
103 /*---------------------------------------------------------------------------*/
105 void audio_music_play(const char *filename)
111 if ((config_get_d(CONFIG_MUSIC_VOLUME) > 0) &&
112 (song = Mix_LoadMUS(config_data(filename))))
114 Mix_PlayMusic(song, -1);
115 strcpy(curr_bgm, filename);
120 void audio_music_queue(const char *filename)
124 if (strlen(curr_bgm) == 0 || strcmp(filename, curr_bgm) != 0)
129 audio_music_play(filename);
130 strcpy(curr_bgm, filename);
137 void audio_music_stop(void)
141 if (Mix_PlayingMusic())
151 /*---------------------------------------------------------------------------*/
153 * SDL_mixer already provides music fading. Unfortunately, it halts playback
154 * at the end of a fade. We need to be able to fade music back in from the
155 * point where it stopped. So, we reinvent this wheel.
158 void audio_timer(float dt)
162 if (fade_rate > 0.0f || fade_rate < 0.0f)
163 fade_volume += dt / fade_rate;
165 if (fade_volume < 0.0f)
169 if (strlen(next_bgm) == 0)
172 if (Mix_PlayingMusic())
177 fade_rate = -fade_rate;
178 audio_music_queue(next_bgm);
182 if (fade_volume > 1.0f)
188 if (Mix_PausedMusic() && fade_rate > 0.0f)
191 if (Mix_PlayingMusic())
192 Mix_VolumeMusic(config_get_d(CONFIG_MUSIC_VOLUME) *
193 (int) (fade_volume * MIX_MAX_VOLUME) / 10);
197 void audio_music_fade_out(float t)
200 strcpy(next_bgm, "");
203 void audio_music_fade_in(float t)
206 strcpy(next_bgm, "");
209 void audio_music_fade_to(float t, const char *filename)
213 if (strlen(curr_bgm) == 0 || strcmp(filename, curr_bgm) != 0)
215 strcpy(next_bgm, filename);
222 audio_music_queue(filename);
223 audio_music_fade_in(t);
227 void audio_volume(int s, int m)
231 Mix_Volume(-1, s * MIX_MAX_VOLUME / 10);
232 Mix_VolumeMusic(m * MIX_MAX_VOLUME / 10);
236 /*---------------------------------------------------------------------------*/