-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaudio.cpp
387 lines (326 loc) · 10 KB
/
audio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* --------------------------------------------------------------------
EXTREME TUXRACER
Copyright (C) 2010 Extreme Tuxracer Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
---------------------------------------------------------------------*/
#include "audio.h"
#include "spx.h"
// the global instances of the 3 audio classes
CAudio Audio;
CMusic Music;
CSound Sound;
// --------------------------------------------------------------------
// class CAudio
// --------------------------------------------------------------------
CAudio::CAudio () {
IsOpen = false;
}
void CAudio::Open () {
// first initialize audio (SDL, not SDL_Mixer).
if (SDL_Init (SDL_INIT_AUDIO) < 0) {
Message ("Couldn't initialize SDL Audio", SDL_GetError());
return;
}
Uint16 format = AUDIO_S16SYS;
int channels = 2;
if (Mix_OpenAudio (param.audio_freq, format, channels, param.audio_buffer_size) < 0)
Message ("Couldn't open SDL_mixer", Mix_GetError());
IsOpen = CheckOpen ();
Mix_AllocateChannels (8);
}
void CAudio::Close () {
if (IsOpen) {
Music.FreeMusics ();
Sound.FreeSounds ();
Mix_CloseAudio();
IsOpen = false;
}
}
bool CAudio::CheckOpen() {
int freq;
Uint16 format;
int channels;
int ret = Mix_QuerySpec (&freq, &format, &channels);
return (ret > 0);
}
// --------------------------------------------------------------------
// class CSound
// --------------------------------------------------------------------
CSound::CSound () {
for (int i=0; i<MAX_SOUNDS; i++) {
sounds[i].chunk = NULL;
active_arr[i] = false;
}
SoundIndex = "";
numSounds = 0;
}
int CSound::LoadChunk (const char *name, const char *filename) {
if (Audio.IsOpen == false) return -1;
if (numSounds >= MAX_SOUNDS) return -1;
sounds[numSounds].chunk = Mix_LoadWAV (filename);
if (sounds[numSounds].chunk == NULL) return -1;
sounds[numSounds].channel = -1; // default: no channel
sounds[numSounds].loop_count = 0; // default: playing once
Mix_VolumeChunk (sounds[numSounds].chunk, param.sound_volume);
SoundIndex = SoundIndex + "[" + name + "]" + Int_StrN (numSounds);
numSounds++;
return numSounds-1;
}
// Load all soundfiles listed in "/sounds/sounds.lst"
void CSound::LoadSoundList () {
if (!Audio.IsOpen) {
Message ("cannot load music, first open Audio");
return;
}
CSPList list(200);
string name, soundfile, path, line;
// int soundid = 0;
if (list.Load (param.sounds_dir, "sounds.lst")) {
for (int i=0; i<list.Count(); i++) {
line = list.Line(i);
name = SPStrN (line, "name", "");
soundfile = SPStrN (line, "file", "");
path = MakePathStr (param.sounds_dir, soundfile);
LoadChunk (name.c_str(), path.c_str());
}
}
}
void CSound::FreeSounds () {
HaltAll ();
for (int i=0; i<numSounds; i++)
if (sounds[i].chunk != NULL) Mix_FreeChunk (sounds[i].chunk);
for (int i=0; i<MAX_SOUNDS; i++) {
sounds[i].chunk = NULL;
active_arr[i] = false;
}
SoundIndex = "";
numSounds = 0;
}
int CSound::GetSoundIdx (string name) {
if (Audio.IsOpen == false) return -1;
return SPIntN (SoundIndex, name, -1);
}
void CSound::SetVolume (int soundid, int volume) {
if (Audio.IsOpen == false) return;
if (soundid < 0 || soundid >= numSounds) return;
volume = MIN (MIX_MAX_VOLUME, MAX (0, volume));
if (sounds[soundid].chunk == NULL) return;
Mix_VolumeChunk (sounds[soundid].chunk, volume);
}
void CSound::SetVolume (string name, int volume) {
SetVolume (GetSoundIdx (name), volume);
}
// ------------------- play -------------------------------------------
void CSound::Play (int soundid, int loop) {
if (!Audio.IsOpen) return;
if (soundid < 0 || soundid >= numSounds) return;
if (active_arr[soundid] == true) return;
if (sounds[soundid].chunk == NULL) return;
sounds[soundid].channel = Mix_PlayChannel (-1, sounds[soundid].chunk, loop);
sounds[soundid].loop_count = loop;
if (loop < 0) active_arr[soundid] = true;
}
void CSound::Play (string name, int loop) {
Play (GetSoundIdx (name), loop);
}
void CSound::Play (int soundid, int loop, int volume) {
if (!Audio.IsOpen) return;
if (soundid < 0 || soundid >= numSounds) return;
if (active_arr[soundid] == true) return;
if (sounds[soundid].chunk == NULL) return;
volume = MIN (MIX_MAX_VOLUME, MAX (0, volume));
Mix_VolumeChunk (sounds[soundid].chunk, volume);
sounds[soundid].channel = Mix_PlayChannel (-1, sounds[soundid].chunk, loop);
sounds[soundid].loop_count = loop;
if (loop < 0) active_arr[soundid] = true;
}
void CSound::Play (string name, int loop, int volume) {
Play (GetSoundIdx (name), loop, volume);
}
void CSound::Halt (int soundid) {
if (!Audio.IsOpen) return;
if (soundid < 0 || soundid >= numSounds) return;
if (sounds[soundid].chunk == NULL) return;
// loop_count must be -1 (endless loop) for halt
if (sounds[soundid].loop_count < 0) {
Mix_HaltChannel (sounds[soundid].channel);
sounds[soundid].loop_count = 0;
sounds[soundid].channel = -1;
active_arr[soundid] = false;
}
}
void CSound::Halt (string name) {
Halt (GetSoundIdx (name));
}
void CSound::HaltAll () {
if (!Audio.IsOpen) return;
Mix_HaltChannel (-1);
for (int i=0; i<numSounds; i++) {
sounds[i].loop_count = 0;
sounds[i].channel = -1;
active_arr[i] = false;
}
}
// --------------------------------------------------------------------
// class CMusic
// --------------------------------------------------------------------
void Hook () {
Mix_HaltMusic();
PrintString ("halted");
}
CMusic::CMusic () {
for (int i=0; i<MAX_MUSICS; i++) musics[i] = NULL;
MusicIndex = "";
numMusics = 0;
for (int i=0; i<MAX_THEMES; i++) {
for (int j=0; j<3; j++) themes[i][j] = -1;
}
ThemesIndex = "";
numThemes = 0;
curr_musid = -1;
curr_volume = 10;
loop_count = 0;
is_playing = false;
// Mix_HookMusicFinished (Hook);
}
int CMusic::LoadPiece (const char *name, const char *filename) {
if (!Audio.IsOpen) return -1;
if (numMusics >= MAX_MUSICS) return -1;
musics[numMusics] = Mix_LoadMUS (filename);
if (musics[numMusics] == NULL) {
Message ("could not load music", filename);
return -1;
}
MusicIndex = MusicIndex + "[" + name + "]" + Int_StrN (numMusics);
numMusics++;
return numMusics-1;
}
void CMusic::LoadMusicList () {
if (!Audio.IsOpen) {
Message ("cannot load music, first open audio");
return;
}
// --- music ---
CSPList list(200);
string name, musicfile, path, line, item;
// int musid = 0;
if (list.Load (param.music_dir, "music.lst")) {
for (int i=0; i<list.Count(); i++) {
line = list.Line(i);
name = SPStrN (line, "name", "");
musicfile = SPStrN (line, "file", "");
path = MakePathStr (param.music_dir, musicfile);
LoadPiece (name.c_str(), path.c_str());
}
} else {
Message ("could not load music.lst");
return;
}
// --- racing themes ---
list.Clear();
numThemes = 0;
ThemesIndex = "";
if (list.Load (param.music_dir, "racing_themes.lst")) {
for (int i=0; i<list.Count(); i++) {
line = list.Line(i);
name = SPStrN (line, "name", "");
ThemesIndex = ThemesIndex + "[" + name + "]" + Int_StrN (numThemes);
item = SPStrN (line, "race", "race_1");
themes [numThemes][0] = GetMusicIdx (item);
item = SPStrN (line, "wonrace", "wonrace_1");
themes [numThemes][1] = GetMusicIdx (item);
item = SPStrN (line, "lostrace", "lostrace_1");
themes [numThemes][2] = GetMusicIdx (item);
numThemes++;
}
} else Message ("could not load racing_themes.lst");
}
void CMusic::FreeMusics () {
Halt ();
for (int i=0; i<numMusics; i++) if (musics[i] != NULL) Mix_FreeMusic (musics[i]);
for (int i=0; i<MAX_MUSICS; i++) musics[i] = NULL;
MusicIndex = "";
numMusics = 0;
for (int i=0; i<MAX_THEMES; i++) {
for (int j=0; j<3; j++) themes[i][j] = -1;
}
ThemesIndex = "";
numThemes = 0;
curr_musid = -1;
curr_volume = 10;
is_playing = false;
}
int CMusic::GetMusicIdx (string name) {
if (Audio.IsOpen == false) return -1;
return SPIntN (MusicIndex, name, -1);
}
int CMusic::GetThemeIdx (string theme) {
if (Audio.IsOpen == false) return -1;
return SPIntN (ThemesIndex, theme, -1);
}
void CMusic::SetVolume (int volume) {
int vol = MIN (MIX_MAX_VOLUME, MAX (0, volume));
Mix_VolumeMusic (vol);
curr_volume = vol;
}
// If the piece is played in a loop, the volume adjustment gets lost.
// probably a bug in SDL_mixer. Help: we have to refresh the volume
// in each (!) frame.
void CMusic::Update () {
Mix_VolumeMusic (curr_volume);
}
bool CMusic::Play (int musid, int loop) {
if (!Audio.IsOpen) return false;
if (musid < 0 || musid >= numMusics) return false;
Mix_Music *music = musics[musid];
if (music == NULL) return false;
if (musid != curr_musid) {
Halt ();
Mix_PlayMusic (music, loop);
curr_musid = musid;
loop_count = loop;
}
Mix_VolumeMusic (curr_volume);
return true;
}
bool CMusic::Play (string name, int loop) {
return Play (GetMusicIdx (name), loop);
}
bool CMusic::Play (int musid, int loop, int volume) {
if (!Audio.IsOpen) return false;
if (musid < 0 || musid >= numMusics) return false;
Mix_Music *music = musics[musid];
int vol = MIN (MIX_MAX_VOLUME, MAX (0, volume));
if (musid != curr_musid) {
Halt ();
Mix_PlayMusic (music, loop);
Mix_VolumeMusic (vol);
curr_musid = musid;
loop_count = loop;
}
return true;
}
bool CMusic::Play (string name, int loop, int volume) {
return Play (GetMusicIdx (name), loop, volume);
}
bool CMusic::PlayTheme (int theme, int situation) {
if (theme < 0 || theme >= numThemes) return false;
if (situation < 0 || situation >= 3) return false;
int musid = themes [theme][situation];
return Play (musid, -1);
}
void CMusic::Refresh (string name) {
Play (name, -1);
}
void CMusic::Halt () {
if (Mix_PlayingMusic ()) Mix_HaltMusic();
loop_count = -1;
curr_musid = -1;
}