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"));
77 for (i = 0; i < MAXSND; i++)
80 Mix_FreeChunk(buff[i]);
89 void audio_bind(int i, int c, const char *filename)
91 strncpy(name[i], filename, MAXSTR);
95 void audio_play(int i, float v)
97 if (audio_state == 1 && buff[i])
99 Mix_VolumeChunk(buff[i], (int) (v * MIX_MAX_VOLUME));
100 Mix_PlayChannel(chan[i], buff[i], 0);
104 /*---------------------------------------------------------------------------*/
106 void audio_music_play(const char *filename)
112 if ((config_get_d(CONFIG_MUSIC_VOLUME) > 0) &&
113 (song = Mix_LoadMUS(config_data(filename))))
115 Mix_PlayMusic(song, -1);
116 strcpy(curr_bgm, filename);
121 void audio_music_queue(const char *filename)
125 if (strlen(curr_bgm) == 0 || strcmp(filename, curr_bgm) != 0)
130 audio_music_play(filename);
131 strcpy(curr_bgm, filename);
138 void audio_music_stop(void)
142 if (Mix_PlayingMusic())
152 /*---------------------------------------------------------------------------*/
154 * SDL_mixer already provides music fading. Unfortunately, it halts playback
155 * at the end of a fade. We need to be able to fade music back in from the
156 * point where it stopped. So, we reinvent this wheel.
159 void audio_timer(float dt)
163 if (fade_rate > 0.0f || fade_rate < 0.0f)
164 fade_volume += dt / fade_rate;
166 if (fade_volume < 0.0f)
170 if (strlen(next_bgm) == 0)
173 if (Mix_PlayingMusic())
178 fade_rate = -fade_rate;
179 audio_music_queue(next_bgm);
183 if (fade_volume > 1.0f)
189 if (Mix_PausedMusic() && fade_rate > 0.0f)
192 if (Mix_PlayingMusic())
193 Mix_VolumeMusic(config_get_d(CONFIG_MUSIC_VOLUME) *
194 (int) (fade_volume * MIX_MAX_VOLUME) / 10);
198 void audio_music_fade_out(float t)
201 strcpy(next_bgm, "");
204 void audio_music_fade_in(float t)
207 strcpy(next_bgm, "");
210 void audio_music_fade_to(float t, const char *filename)
214 if (strlen(curr_bgm) == 0 || strcmp(filename, curr_bgm) != 0)
216 strcpy(next_bgm, filename);
223 audio_music_queue(filename);
224 audio_music_fade_in(t);
228 void audio_volume(int s, int m)
232 Mix_Volume(-1, s * MIX_MAX_VOLUME / 10);
233 Mix_VolumeMusic(m * MIX_MAX_VOLUME / 10);
237 /*---------------------------------------------------------------------------*/