/
SOUND.CPP
309 lines (244 loc) · 6.72 KB
/
SOUND.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
/*-------------------------------------------------------*/
/* High Level Sound Routines. */
/* [c]copyright 1995 by AlphaHelix */
/* */
/* */
/* */
/*-------------------------------------------------------*/
// Master SOUND ENABLE SWITCH (debugging reasons)
//#define NO_SOUND
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include "ERROR.HPP"
#include "MEMORY.HPP"
#include "DISKIO.HPP"
#include "SOUND.HPP"
#include "RAVAGE.HPP"
#include "SOUND_SDL.HPP"
// Mod player variables.
static Mix_Music *mf = NULL; // Music structure for Module file.
static int mod_play; // MOD is playing(1) or stopped(0).
static int mod_vol; // Volume of MOD voices.
// Wave variables.
static int wav_vol; // WAVE volume.
// All other variables.
static int initialized = 0;
//-------------------------------------------------------------------
// WavePool members.
//-------------------------------------------------------------------
void WavePool::load(char *f)
{
myFile myfile;
int i;
SDL_RWops *memSample = NULL;
// read in header
if (!myfile.open(f)) error("WavPool not found: ", f);
myfile.read(&nsamples, sizeof(long));
// Just quit if empty library.
if (nsamples == 0) {
myfile.close();
return;
}
sample = new Sample[nsamples];
myfile.read(sample, nsamples * sizeof(Sample));
fseek(myfile.getFILE(), myfile.getbase(), SEEK_SET);
memSample = SDL_RWFromMem(myfile.getMemory(), myfile.getsize());
// Load WAVES.
for (i = 0; i < nsamples; i++) {
#ifndef NO_SOUND
memSample->seek(memSample, (long)sample[i].sample, RW_SEEK_SET);
if((sample[i].sample = Mix_LoadWAV_RW(memSample, SDL_FALSE)) == NULL) error("Error loading sample. ", (char *)Mix_GetError());
#endif
}
SDL_RWclose(memSample);
myfile.close();
}
void WavePool::unload(void)
{
int i;
if (nsamples == 0) return;
for (i = 0; i < nsamples; i++) {
#ifndef NO_SOUND
Mix_FreeChunk(sample[i].sample);
#endif
}
delete []sample;
}
void WavePool::play(int n)
{
Mix_Chunk *s = sample[n].sample;
//printf("Sound vol %d wave_vol %d\n", sample[n].volume*wav_vol, wav_vol);
if (!wav_vol) return;
#ifndef NO_SOUND
if(!sound_Call(s, n, SND_DUPLICATE, sample[n].volume, 0)) printf("Sound not added\n");
#endif
}
//-------------------------------------------------------------------
// MOD functions.
//-------------------------------------------------------------------
/*---------------------------------------------------------
Function:
Description:
Load a MOD or UNIMOD file into memory and prepare it to
be played.
---------------------------------------------------------*/
void s_loadmod(char *f)
{
#ifndef NO_SOUND
myFile myfile;
SDL_RWops *mod = NULL;
if (mf) s_unloadmod();
if (!myfile.open(f)) error("(s_loadmod): not found: ", f);
mod = SDL_RWFromMem(myfile.getMemory(), myfile.getsize());
if(mod == NULL) error("SDL_RWFromMem: ", SDL_GetError());
#ifdef PANDORA
mf = Mix_LoadMUS_RW(mod); SDL_FreeRW(mod);
#else
mf = Mix_LoadMUS_RW(mod, SDL_TRUE);
#endif
if(!mf) {
printf("Mix_LoadMUS_RW: %s\n", Mix_GetError());
}
myfile.close();
#endif
}
/*---------------------------------------------------------
Function:
Description:
Unload a previously loaded MOD file from memory.
---------------------------------------------------------*/
void s_unloadmod(void)
{
#ifndef NO_SOUND
s_stopmod();
if (mf) Mix_FreeMusic(mf);
mf = NULL;
#endif
}
/*---------------------------------------------------------
Function: startmod & stopmod
Description:
Start and stop playing a previous loaded MUSIC file
respectivly.
---------------------------------------------------------*/
void s_startmod(void)
{
#ifndef NO_SOUND
if (mf) mod_play = 1;
if(Mix_PlayMusic(mf, settings.loop_music ? -1 : 1) == -1) printf("Mix_PlayMusic(): %s\n", Mix_GetError());
#endif
}
void s_stopmod(void)
{
#ifndef NO_SOUND
Mix_HaltMusic();
mod_play = 0;
#endif
}
char *s_modname(void)
{
#ifdef NO_SOUND
return "debug mode";
#else
return "Music Temp";
#endif
}
/*---------------------------------------------------------
Function:
Description:
Set MUSIC volume. Must be in the range (0 to 64).
If 0 is given, all the voices used by the MOD won't be
mixed anymore. So this saves a lot of power on a SoundBlaster.
---------------------------------------------------------*/
void s_setmodvol(int volume)
{
int percentage = (volume * 100)/64;
#ifndef NO_SOUND
sound_SetMusicVolume(percentage);
mod_vol = volume;
#endif
}
int s_getmodvol(void)
{
return mod_vol;
}
/*---------------------------------------------------------
Function:
Description:
Set the volume to play any WAV files.
Must be in the range 0 to 64.
If 0 is given, all the WAV voices are releases saving
a lot of cpu power with a non hardware-mixing SoundCard.
---------------------------------------------------------*/
void s_setwavvol(int volume)
{
int percentage = (volume * 100)/64;
wav_vol = volume;
#ifndef NO_SOUND
sound_SetVolume(percentage);
#endif
}
/*---------------------------------------------------------
Function:
Description:
Returns Sound Card Driver Name.
---------------------------------------------------------*/
char *s_drivername(void)
{
#ifdef NO_SOUND
return "Debug Mode";
#else
return "SDL Audio";
#endif
}
/*---------------------------------------------------------
Function:
Description:
Call this function once in a while to update the
player.
---------------------------------------------------------*/
void s_update(void)
{
#ifndef NO_SOUND
sound_Play();
#endif
}
/*---------------------------------------------------------
Function:
Description:
Initialize sound system.
---------------------------------------------------------*/
void initsound()
{
if(Mix_OpenAudio(8000, AUDIO_S16SYS, 2, 512) == -1)
{
error("Unable to open audio device: ", Mix_GetError());
}
if(!Mix_Init(MIX_INIT_MOD))
{
printf("Unable to support MOD files: %s\n", Mix_GetError());
}
#ifndef NO_SOUND
if (initialized) return;
// Initialize several more or less cool variables.
mf = 0; // No MOD loaded.
mod_play = 0; // No MOD has been loaded so don't play it.
// initialize soundcard
if (sound_Init(128, 8, 100, 1)) {
error("Sound Error: ", Mix_GetError());
} else {
initialized = 1;
}
#endif
}
void shutsound(void)
{
if (!initialized) return;
#ifndef NO_SOUND
s_stopmod();
sound_Clean();
#endif
Mix_Quit();
initialized = 0;
}