-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw_sound.cpp
494 lines (384 loc) · 11.6 KB
/
w_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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include "a_cache.h"
#include "pcp.h"
#include "mikmod.h"
#include "mplayer.h"
#include "mwav.h"
#include "mdsfx.h"
#include <direct.h>
#include <vector>
class SongCacheEntry:public CacheEntry
{
public:
UNIMOD *mf;
SongCacheEntry(SongCacheEntry *list, const char *n)
{ name = _strdup(n);
next = list;
prev = NULL;
if(list) list->prev = this;
};
SongCacheEntry *Next()
{ return (SongCacheEntry *)next;
};
};
class SampleCacheEntry:public CacheEntry
{
public:
SAMPLE *handle;
SampleCacheEntry(SampleCacheEntry *list, const char *n)
{ name = _strdup(n);
next = list;
prev = NULL;
if(list) list->prev = this;
};
SampleCacheEntry *Next()
{ return (SampleCacheEntry *)next;
}
SAMPLE *Fetch(const char *res)
{
return NULL;
}
};
typedef struct
{ SongCacheEntry *song;
SampleCacheEntry *sample;
} SNDCACHE;
/*****************************/
// ---> Publics <---
char playingsng[80] = {0};
int numsamples = 0;
bool UseSound = false;
bool music = false;
/*****************************/
// ---> Privates <---
static MDRIVER *md;
static MPLAYER *mp;
static UNIMOD *mf;
static MD_VOICESET *vs_global, *vs_music, *vs_sndfx, *vs_menu;
static SNDCACHE CurCache, NewCache;
struct
{ MPLAYER *mp[8];
char sng[8][80];
int pos;
} fifo;
static bool Session;
std::vector<MD_SAMPLE*> sfx;
// ==============================
// Private Functions
// ==============================
static void ShutdownSound(void) { Mikmod_Exit(md); }
static void errorhandler(int merrnum, const CHAR *merrstr)
{
// the addition of a dialog box on certain error types might be a
// useful 'user-friendly' idea. Something to think about later...
UseSound = false;
return;
}
static void killmodule(void)
// Stops playing and unloads the current module.
{
music = false;
if (mf)
{ // see if our resource is cached -- if not, then unload the song.
if(mp)
{ Player_Stop(mp);
//Player_FreeSong(mp);
Player_Free(mp);
mp = NULL;
}
if(!CurCache.song->CheckFor(playingsng))
{ Unimod_Free(mf);
mf = NULL;
}
}
}
static bool FetchSongCache(SongCacheEntry *list, const char *res)
// See if the resource name 'res exists in the given cache list. If so, it
// sets the mf global to that resource, so that it may be played.
// Returns: 0 if not found. 1 if found.
{
SongCacheEntry *cruise;
cruise = list;
while(cruise)
{ if(!strcmp(cruise->name, res))
{ mf = cruise->mf;
return 1;
}
cruise = cruise->Next();
}
return 0;
}
// ================================
// Initialization and Stuff
// ================================
void InitSound()
{
fifo.pos = 0;
fifo.mp[0] = NULL;
mp = NULL;
mf = NULL;
UseSound = true;
Session = false;
CurCache.song = NULL; CurCache.sample = NULL;
//printlog = Log;
Mikmod_RegisterErrorHandler(errorhandler);
Mikmod_RegisterLoader(load_it);
Mikmod_RegisterLoader(load_xm);
Mikmod_RegisterLoader(load_s3m);
Mikmod_RegisterLoader(load_mod);
Mikmod_RegisterAllDrivers();
// AirChange:
// the new buffer size is set to 60 milliseconds, which is adequate for testing
// purposes. We will want to toy with a 40 ms buffer later to see how stable it
// is on various hardware.
// Notes:
// - We force dynamic sample support. This is mostly a gesture of future expandability.
md = Mikmod_Init(48000, 60, NULL, MD_STEREO, CPU_AUTODETECT, DMODE_16BITS | DMODE_INTERP | DMODE_NOCLICK | DMODE_SAMPLE_DYNAMIC);
// Blackstar voiceset organization:
// Notes:
// - The global voiceset has no voices since everything will be assignd to one
// of its child voicesets.
// - The music voiceset has no voices since those will be allocated by the player.
// - the sound effects are differentiated from the menu system sounds so that we
// can preempt all in-game sound effects when the user enters a menu.
vs_global = Voiceset_Create(md,NULL,64,0);
vs_music = Voiceset_Create(md,vs_global,64,0);
vs_sndfx = Voiceset_Create(md,vs_global,64,0);
vs_menu = Voiceset_Create(md,vs_global,64,0);
// We should load and set configured volume levels for each class
// of sound here... (or somewhere)
AddShutdownProc(ShutdownSound,"Music subsystem");
const char *fname = "MAIN.SFX";
FILE* f;
char i;
int numfx = 0;
f = fopen(fname, "r");
if(!f)
{
//try going into the content dir. i know its crappy
_chdir(".\\content");
f = fopen(fname, "r");
if(!f)
err("Could not open sound effect index file.");
}
fscanf(f, "%s", strbuf);
numfx = atoi(strbuf);
for(int i = 0; i < numfx; i++)
{
fscanf(f, "%s", strbuf);
auto tmpsfx = mdsfx_loadwav(md, strbuf);
if(!tmpsfx)
err("WAV load error.");
sfx.push_back(tmpsfx);
}
fclose(f);
}
void UpdateSound(void)
{
if(music) Mikmod_Update(md);
}
void s_CacheStartSession(void)
{
NewCache.song = NULL; NewCache.sample = NULL;
Session = true;
}
void s_CacheStopSession(void)
{
SongCacheEntry *cruise;
// Compare the two linked lists -- currently cached and 'to be cached.'
// and take appropriate action!
// Step one, look for anything to unload.
cruise = CurCache.song;
while(cruise)
{ SongCacheEntry *tmp = cruise->Next();
if(!NewCache.song->CheckFor(cruise))
{ // this resource isn't in the newcache list so lets unload it.
Unimod_Free(cruise->mf);
}
cruise = tmp;
}
// Step Two, look for anything to load.
cruise = NewCache.song;
while(cruise)
{ SongCacheEntry *tmp = cruise->Next();
if(!CurCache.song->CheckFor(cruise))
{ // this resource isn't in the curcache list so lets load it.
Unimod_Load(md,cruise->name);
}
cruise = tmp;
}
// free up the crcache and move newcache over it.
CurCache.song->FreeList();
CurCache.song = NewCache.song;
Session = false;
}
/* ==========================================================================
*** Music Manipulation Interface
*** Proposed functionality method for the music system:
PlayMusic:
A call to PlayMusic will check the in-memory song cache to see if the re-
quested song is already loaded. If not, it will attempt to load the song
using the vfile packfile interface. If that fails, it will attempt to
load the song from the OS filesystem. If the second param ('cache') is set
to true, then the song will be loaded into the precache area and will not be
unloaded when stopped. This allows us to optionally distribute some of the
loading process.
CacheMusic:
Caches the given song. Loads it using the same search order as PlayMusic.
Cached songs are only unloaded when the engine called FreeAllMusic();
However, it is usually better to let the sound system play it smart. --> See
'StartCacheSession'
s_CacheStartSession:
This applies to both music and sound effects. When the precaching session
is started, all calls to CacheMusic are tracked. Afterward, the user calls
s_CacheEndSession, which will compare the requested precache setup to the
current one - unloading anything not in the new precache and loading anything
that isn't already. This allows us to utilize pre-loaded data with maximum
efficiency.
FreeAllMusic()
Unloads all music in the cache, and whatever might be playing at the moment.
Generally speaking, this shouldn't be used in favor of the much smarter music
manamgement system offered via Cache Sessions. But, you never know. :)
===========================================
*/
static bool loadmodule(const char *sng)
{
// We don't know how long it could take to load the song,
// so lets clear out the mixing buffer.
Mikmod_WipeBuffers(md);
// Load the song and add it to the list of loaded resources.
mf = Unimod_Load(md,sng);
if (!mf)
{ err("Could not load module %s", sng);
return 1;
}
return 0;
}
void CacheMusic(const char *sng)
{
if(Session)
{ // Put this song onto the 'NewCache' songlist to be loaded later.
NewCache.song = new SongCacheEntry(NewCache.song, sng);
} else
{ loadmodule(sng);
CurCache.song = new SongCacheEntry(CurCache.song, sng);
CurCache.song->mf = mf;
}
}
void PlayMusic(char *sng, bool cache)
{
if (!UseSound) return;
if (!vstrlen(sng)) return;
if (!vstrcmp(sng, playingsng)) return;
killmodule();
vstrcpy(playingsng, sng);
if(!FetchSongCache(CurCache.song,sng))
{ // song is not cached, so load it up ...
if(cache) CacheMusic(sng); else loadmodule(sng);
}
if(mf)
{ mp = Player_InitSong(mf, vs_music, PF_LOOP, 24);
Player_Start(mp);
music = true;
}
}
void StopMusic(void)
{
if (!UseSound) return;
killmodule();
playingsng[0]=0;
}
// ---> Music Suspension <---
void SuspendMusic()
// Nested high-level music suspension stuff. This sucker suspends the currently
// active song and puts it on the suspended-songs FIFO. A call to ResumeMusic
// wipes it.
{
if(mf && mp)
{ Player_Pause(mp,0); // pause with complete silence
fifo.mp[fifo.pos] = mp;
vstrcpy(fifo.sng[fifo.pos],playingsng);
fifo.pos++;
mp = NULL;
mf = NULL;
playingsng[0] = 0;
}
}
void ResumeMusic()
// Pick the first voiceset off the FIFO and resume it. If the fifo is empty or
// the pointer is null, it'll be ok. We're not stupid! ;)
// If a song is currently playing, we stop and unload it.
{
killmodule();
if(fifo.pos)
{ fifo.pos--;
if(fifo.mp[fifo.pos]) Player_Start(mp=fifo.mp[fifo.pos]);
mf = mp->mf;
fifo.mp[fifo.pos] = NULL;
vstrcpy(playingsng, fifo.sng[fifo.pos]);
music = true;
}
}
void FreeAllMusic()
{
SongCacheEntry *cruise;
if (!UseSound) return;
cruise = CurCache.song;
while(cruise)
{ SongCacheEntry *tmp = cruise->Next();
Unimod_Free(cruise->mf);
delete cruise;
cruise = tmp;
}
}
int PlaySample(int i, int v, int p)
// returns the voice the sample has been played in.
{
// int voice;
if (!UseSound) return 0;
return 0;
}
void playeffect(int efc)
{
if (!UseSound) return;
mdsfx_playeffect(sfx[efc], vs_sndfx, 0, 0);
//chanl=md_numchn-curch;
//if (curch==1) curch=2; else curch=1;
//
//MD_VoiceSetVolume(chanl,64);
//MD_VoiceSetPanning(chanl,128);
//MD_VoiceSetFrequency(chanl,sfx[efc]->loopend);
//MD_VoicePlay(chanl,sfx[efc]->handle,0,sfx[efc]->length,0,0,sfx[efc]->flags);
}
// ===========================================================================
// Volume set/retrive!!
//
// I assume you want all volumes range from 0 to 100, since you were returning
// 100 as the default music volume. I made a macro anyway, so we can change
// thevolume scale with ease!
#define VOLSCALE 100
#define bs_setvol(x,a) Voiceset_SetVolume(x, (a * 128) / VOLSCALE)
#define bs_getvol(x) ((x->volume * VOLSCALE) / 128)
int s_getglobalvolume()
{
return bs_getvol(vs_global);
}
void s_setglobalvolume(int v)
{
bs_setvol(vs_global, v);
}
int s_getmusicvolume()
{
return bs_getvol(vs_music);
}
void s_setmusicvolume(int v)
{
bs_setvol(vs_music, v);
}
int s_getsndfxvolume()
{
return bs_getvol(vs_sndfx);
}
void s_setsndfxvolume(int v)
{
bs_setvol(vs_sndfx, v);
}