Copy editing
[meritous_recharged.git] / src / audio.c
blobafb603d655a8ba59ddd7a7d4565fcf69650a05f1
1 //
2 // audio.c
3 //
4 // Copyright 2007, 2008 Lancer-X/ASCEAI
5 //
6 // This file is part of Meritous.
7 //
8 // Meritous is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
13 // Meritous is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with Meritous. If not, see <http://www.gnu.org/licenses/>.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #include <SDL.h>
26 #include <SDL_image.h>
27 #include <SDL_mixer.h>
28 #include <string.h>
30 #include "levelblit.h"
31 #include "mapgen.h"
32 #include "boss.h"
34 char *tracks[13] = {"dat/m/ICEFRONT.S3M",
35 "dat/m/cavern.xm",
36 "dat/m/cave.xm",
37 "dat/m/cave06.s3m",
38 "dat/m/Wood.s3m",
39 "dat/m/iller_knarkloader_final.xm",
40 "dat/m/fear2.mod",
41 "dat/m/Cv_boss.mod",
42 "dat/m/Fr_boss.mod",
43 "dat/m/CT_BOSS.MOD",
44 "dat/m/rpg_bat1.xm",
45 "dat/m/amblight.xm",
46 "dat/m/FINALBAT.s3m"};
48 Mix_Music *bgm_music = NULL;
49 int bgm_track = -1;
51 Mix_Chunk *c_sample[16] = {NULL};
53 void InitAudio()
55 Mix_OpenAudio(44100, AUDIO_S16, 2, 4096);
56 Mix_VolumeMusic(112);
57 Mix_AllocateChannels(16);
60 void BackgroundMusic();
61 void CircuitHum();
62 void StopMusic();
64 void MusicUpdate()
66 BackgroundMusic();
67 CircuitHum();
70 void CircuitHum()
72 int hum_vol = 0;
73 static int hum_play = 0;
74 if (!play_sounds) {
75 if (hum_play != 0) {
76 Mix_HaltChannel(0);
77 hum_play = 0;
79 return;
82 if (magic_circuit == 0) {
83 hum_vol = 0;
84 if (hum_play != 0) {
85 Mix_HaltChannel(0);
86 hum_play = 0;
89 if (magic_circuit > 0) {
90 hum_vol = (circuit_size * 24 / 3000)+12;
91 hum_vol = (magic_circuit * 96 / circuit_size) * hum_vol / 36 + 32;
93 if (hum_play != 1) {
94 if (hum_play == 2) {
95 Mix_HaltChannel(0);
96 Mix_FreeChunk(c_sample[0]);
98 c_sample[0] = Mix_LoadWAV("dat/a/circuitcharge.wav");
99 Mix_PlayChannel(0, c_sample[0], -1);
100 hum_play = 1;
103 if (magic_circuit < 0) {
104 hum_vol = (circuit_size - (magic_circuit + circuit_size)) * 80 / 3000;
105 if (hum_play != 2) {
106 if (hum_play == 1) {
107 Mix_HaltChannel(0);
108 Mix_FreeChunk(c_sample[0]);
110 c_sample[0] = Mix_LoadWAV("dat/a/circuitrecover.wav");
111 Mix_PlayChannel(0, c_sample[0], -1);
112 hum_play = 2;
116 Mix_Volume(0, hum_vol);
119 void TitleScreenMusic()
121 if (!play_music) {
122 StopMusic();
123 return;
125 int new_track = 5;
126 if (bgm_track != -1) {
127 Mix_HaltMusic();
128 Mix_FreeMusic(bgm_music);
129 bgm_music = NULL;
132 bgm_music = Mix_LoadMUS(tracks[new_track]);
133 Mix_PlayMusic(bgm_music, -1);
134 bgm_track = new_track;
137 void BackgroundMusic()
139 if (!play_music) {
140 StopMusic();
141 return;
144 int new_track = -1;
145 Mix_VolumeMusic(96);
147 if (rooms[player_room].s_dist <= 15) {
148 new_track = 0;
151 if (bgm_track == 1) {
152 if ((rooms[player_room].s_dist <= 30) && (rooms[player_room].s_dist >= 10)) {
153 new_track = 1;
156 if (bgm_track == 2) {
157 if ((rooms[player_room].s_dist <= 45) && (rooms[player_room].s_dist >= 25)) {
158 new_track = 2;
162 if (new_track == -1) {
163 if ((rooms[player_room].s_dist <= 30) && (rooms[player_room].s_dist >= 16)) {
164 new_track = 1;
166 if ((rooms[player_room].s_dist <= 39) && (rooms[player_room].s_dist >= 31)) {
167 new_track = 2;
169 if (rooms[player_room].s_dist >= 40) {
170 new_track = 3;
174 if (rooms[player_room].room_type == 3) {
175 new_track = 4;
178 if (artifacts[11]) {
179 new_track = 6;
182 if (rooms[player_room].room_type == 2) {
183 if (boss_fight_mode == 2) {
184 if ( (current_boss == 3) && (player_shield == 30) ) {
185 if (boss_lives <= 1) {
186 new_track = 7;
187 } else {
188 new_track = 12;
190 } else {
191 new_track = 7 + current_boss;
193 Mix_VolumeMusic(128);
194 } else {
195 new_track = -1;
199 if ( (player_room == 0) && (current_boss == 3) && (boss_fight_mode >= 3) ) {
200 new_track = 11;
203 if (new_track == bgm_track) return;
205 if (bgm_track != -1) {
206 Mix_HaltMusic();
207 Mix_FreeMusic(bgm_music);
208 bgm_music = NULL;
210 if (new_track != -1) {
211 bgm_music = Mix_LoadMUS(tracks[new_track]);
212 Mix_PlayMusic(bgm_music, -1);
214 bgm_track = new_track;
217 void StopMusic()
219 if(bgm_track != -1) {
220 Mix_HaltMusic();
221 Mix_FreeMusic(bgm_music);
222 bgm_music = NULL;
223 bgm_track = -1;
227 int SND_GetChannel()
229 int i;
231 for (i = 8; i < 16; i++) {
232 if (Mix_Playing(i) == 0) {
233 if (c_sample[i] != NULL) {
234 Mix_FreeChunk(c_sample[i]);
235 c_sample[i] = NULL;
237 return i;
240 return -1;
243 void SND_Play(char *filename, int vol)
245 if(!play_sounds) {
246 return;
248 int ch;
250 ch = SND_GetChannel();
251 if (ch != -1) {
252 c_sample[ch] = Mix_LoadWAV(filename);
253 Mix_Volume(ch, vol);
254 Mix_PlayChannel(ch, c_sample[ch], 0);
258 void SND_Pos(char *filename, int vol, int dist)
260 if(!play_sounds) {
261 return;
263 int real_vol;
264 if (dist > 1600) return;
266 real_vol = vol * (40 - (sqrt(dist))) / 40;
267 SND_Play(filename, real_vol);
270 void SND_CircuitRelease(int str)
272 SND_Play("dat/a/circuitrelease.wav", sqrt(str * 2 + (str * 5300 / circuit_size) + (str > 100 ? 5300 : str*53)));
275 void SND_Stop(int sample_id)
277 Mix_HaltChannel(sample_id);
278 Mix_FreeChunk(c_sample[sample_id]);
279 c_sample[sample_id] = NULL;