forked from lukaszdk/ps2doom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_sound.c
743 lines (613 loc) · 17.8 KB
/
s_sound.c
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/* Emacs style mode select -*- C++ -*-
*-----------------------------------------------------------------------------
*
* $Id: s_sound.c,v 1.10 1999/10/12 13:01:14 cphipps Exp $
*
* LxDoom, a Doom port for Linux/Unix
* based on BOOM, a modified and improved DOOM engine
* Copyright (C) 1999 by
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
* and Colin Phipps
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* DESCRIPTION: Platform-independent sound code
*
* static const char rcsid[] = "$Id: s_sound.c,v 1.10 1999/10/12 13:01:14 cphipps Exp $";
*-----------------------------------------------------------------------------*/
// killough 3/7/98: modified to allow arbitrary listeners in spy mode
// killough 5/2/98: reindented, removed useless code, beautified
#include "include/doomstat.h"
#include "include/s_sound.h"
//#include "i_sound.h" // cosmito
#include "include/l_sound_sdl.h"
#include "include/r_main.h"
#include "include/m_random.h"
#include "include/w_wad.h"
//#include "lprintf.h" // cosmito
#include <stdlib.h>
// when to clip out sounds
// Does not fit the large outdoor areas.
#define S_CLIPPING_DIST (1200<<FRACBITS)
// Distance tp origin when sounds should be maxed out.
// This should relate to movement clipping resolution
// (see BLOCKMAP handling).
// Originally: (200*0x10000).
#define S_CLOSE_DIST (160<<FRACBITS)
#define S_ATTENUATOR ((S_CLIPPING_DIST-S_CLOSE_DIST)>>FRACBITS)
// Adjustable by menu.
#define NORM_PITCH 128
#define NORM_PRIORITY 64
#define NORM_SEP 128
#define S_STEREO_SWING (96<<FRACBITS)
#define PU_STATIC 1 // static entire execution time
//jff 1/22/98 make sound enabling variables readable here
extern int snd_card, mus_card;
extern boolean nosfxparm, nomusicparm;
//jff end sound enabling variables readable here
// the set of channels available
static channel_t *channels;
// These are not used, but should be (menu).
// Maximum volume of a sound effect.
// Internal default is max out of 0-15.
int snd_SfxVolume = 15;
// Maximum volume of music. Useless so far.
int snd_MusicVolume = 15;
// whether songs are mus_paused
static boolean mus_paused;
// music currently being played
static musicinfo_t *mus_playing;
// following is set
// by the defaults code in M_misc:
// number of channels available
int numChannels;
//jff 3/17/98 to keep track of last IDMUS specified music num
int idmusnum;
//
// Internals.
//
void S_StopChannel(int cnum);
int S_AdjustSoundParams(mobj_t *listener, mobj_t *source,
int *vol, int *sep, int *pitch);
static int S_getChannel(void *origin, sfxinfo_t *sfxinfo, int is_pickup);
// Initializes sound stuff, including volume
// Sets channels, SFX and music volume,
// allocates channel buffer, sets S_sfx lookup.
//
void S_Init(int sfxVolume, int musicVolume)
{
boolean mus_card = 0; // cosmito
boolean nomusicparm = 1; // cosmito
boolean nosfxparm = 0;
boolean snd_card = 1;
//jff 1/22/98 skip sound init if sound not enabled
if (snd_card && !nosfxparm)
{
int i;
//lprintf(LO_CONFIRM, "S_Init: default sfx volume %d\n", sfxVolume);
fprintf( stderr, "S_Init: default sfx volume %d\n", sfxVolume);
// Whatever these did with DMX, these are rather dummies now.
I_SetChannels();
S_SetSfxVolume(sfxVolume);
// Allocating the internal channels for mixing
// (the maximum numer of sounds rendered
// simultaneously) within zone memory.
// CPhipps - calloc
//typedef channel_t channels;
//Z_Malloc(numChannels, sizeof(channels), 0);
// Note that sounds have not been cached (yet).
for (i=1 ; i<NUMSFX ; i++)
S_sfx[i].lumpnum = S_sfx[i].usefulness = -1;
}
// CPhipps - music init reformatted
if (mus_card && !nomusicparm) {
S_SetMusicVolume(musicVolume);
// no sounds are playing, and they are not mus_paused
mus_paused = 0;
}
}
//
// Per level startup code.
// Kills playing sounds at start of level,
// determines music if any, changes music.
//
void S_Start(void)
{
int cnum,mnum;
boolean mus_card = 0; // cosmito
boolean nomusicparm = 1; // cosmito
boolean nosfxparm = 0;
boolean snd_card = 1;
// kill all playing sounds at start of level
// (trust me - a good idea)
//jff 1/22/98 skip sound init if sound not enabled
if (snd_card && !nosfxparm)
for (cnum=0 ; cnum<numChannels ; cnum++)
if (channels[cnum].sfxinfo)
S_StopChannel(cnum);
//jff 1/22/98 return if music is not enabled
if (!mus_card || nomusicparm)
return;
// start new music for the level
mus_paused = 0;
if (idmusnum!=-1)
mnum = idmusnum; //jff 3/17/98 reload IDMUS music if not -1
else
if (gamemode == commercial)
mnum = mus_runnin + gamemap - 1;
else
{
static const int spmus[] = // Song - Who? - Where?
{
mus_e3m4, // American e4m1
mus_e3m2, // Romero e4m2
mus_e3m3, // Shawn e4m3
mus_e1m5, // American e4m4
mus_e2m7, // Tim e4m5
mus_e2m4, // Romero e4m6
mus_e2m6, // J.Anderson e4m7 CHIRON.WAD
mus_e2m5, // Shawn e4m8
mus_e1m9 // Tim e4m9
};
if (gameepisode < 4)
mnum = mus_e1m1 + (gameepisode-1)*9 + gamemap-1;
else
mnum = spmus[gamemap-1];
}
S_ChangeMusic(mnum, true);
}
void S_StartSoundAtVolume(void *origin_p, int sfx_id, int volume)
{
boolean nosfxparm = 0;
boolean snd_card = 1;
int sep, pitch, priority, cnum, is_pickup;
sfxinfo_t *sfx;
mobj_t *origin = (mobj_t *) origin_p;
//jff 1/22/98 return if sound is not enabled
if (!snd_card || nosfxparm)
return;
is_pickup = sfx_id & PICKUP_SOUND || sfx_id == sfx_oof; // killough 4/25/98
sfx_id &= ~PICKUP_SOUND;
// check for bogus sound #
if (sfx_id < 1 || sfx_id > NUMSFX)
// I_Error("Bad sfx #: %d", sfx_id);
sfx = &S_sfx[sfx_id];
// Initialize sound parameters
if (sfx->link)
{
pitch = sfx->pitch;
// priority = sfx->priority;
volume += sfx->volume;
if (volume < 1)
return;
if (volume > snd_SfxVolume)
volume = snd_SfxVolume;
}
else
{
pitch = NORM_PITCH;
priority = NORM_PRIORITY;
}
// Check to see if it is audible, modify the params
// killough 3/7/98, 4/25/98: code rearranged slightly
if (!origin || origin == players[displayplayer].mo)
sep = NORM_SEP;
else
if (!S_AdjustSoundParams(players[displayplayer].mo, origin, &volume,
&sep, &pitch))
return;
else
if ( origin->x == players[displayplayer].mo->x &&
origin->y == players[displayplayer].mo->y)
sep = NORM_SEP;
// hacks to vary the sfx pitches
if (sfx_id >= sfx_sawup && sfx_id <= sfx_sawhit)
pitch += 8 - (M_Random()&15);
else
if (sfx_id != sfx_itemup && sfx_id != sfx_tink)
pitch += 16 - (M_Random()&31);
if (pitch<0)
pitch = 0;
if (pitch>255)
pitch = 255;
// kill old sound
for(cnum=0; cnum<numChannels; cnum++)
{
if (channels[cnum].sfxinfo && channels[cnum].origin == origin && channels[cnum].is_pickup == is_pickup)
{
S_StopChannel(cnum);
break;
}
// try to find a channel
cnum = S_getChannel(origin, sfx, is_pickup);
if (cnum<0)
return;
// get lumpnum if necessary
// killough 2/28/98: make missing sounds non-fatal
if (sfx->lumpnum < 0 && (sfx->lumpnum = I_GetSfxLumpNum(sfx)) < 0)
return;
// increase the usefulness
if (sfx->usefulness++ < 0)
sfx->usefulness = 1;
// Assigns the handle to one of the channels in the mix/output buffer.
channels[cnum].handle = I_StartSound(sfx_id, volume, sep, pitch, priority);
}
}
void S_StartSound(void *origin, int sfx_id)
{
S_StartSoundAtVolume(origin, sfx_id, snd_SfxVolume);
}
void S_StopSound(void *origin)
{
int cnum;
boolean nosfxparm = 0;
boolean snd_card = 1;
//jff 1/22/98 return if sound is not enabled
if (!snd_card || nosfxparm)
return;
for (cnum=0 ; cnum<numChannels ; cnum++)
if (channels[cnum].sfxinfo && channels[cnum].origin == origin)
{
S_StopChannel(cnum);
break;
}
}
//
// Stop and resume music, during game PAUSE.
//
void S_PauseSound(void)
{
boolean mus_card = 0; // cosmito
boolean nomusicparm = 1; // cosmito
//jff 1/22/98 return if music is not enabled
if (!mus_card || nomusicparm)
return;
if (mus_playing && !mus_paused)
{
I_PauseSong(mus_playing->handle);
mus_paused = true;
}
}
void S_ResumeSound(void)
{
boolean mus_card = 0; // cosmito
boolean nomusicparm = 1; // cosmito
//jff 1/22/98 return if music is not enabled
if (!mus_card || nomusicparm)
return;
if (mus_playing && mus_paused)
{
I_ResumeSong(mus_playing->handle);
mus_paused = false;
}
}
//
// Updates music & sounds
//
void S_UpdateSounds(void* listener_p)
{
mobj_t *listener = (mobj_t*) listener_p;
int cnum;
boolean nosfxparm = 0;
boolean snd_card = 1;
//jff 1/22/98 return if sound is not enabled
if (!snd_card || nosfxparm)
return;
for (cnum=0 ; cnum<numChannels ; cnum++)
{
sfxinfo_t *sfx;
channel_t *c = &channels[cnum];
if ((sfx = c->sfxinfo))
{
if (I_SoundIsPlaying(c->handle))
{
// initialize parameters
int volume = snd_SfxVolume;
int pitch = NORM_PITCH;
int sep = NORM_SEP;
if (sfx->link)
{
pitch = sfx->pitch;
volume += sfx->volume;
if (volume < 1)
{
S_StopChannel(cnum);
continue;
}
else
if (volume > snd_SfxVolume)
volume = snd_SfxVolume;
}
// check non-local sounds for distance clipping
// or modify their params
if (c->origin && listener_p != c->origin) { // killough 3/20/98
if (!S_AdjustSoundParams(listener, c->origin,
&volume, &sep, &pitch))
S_StopChannel(cnum);
else
I_UpdateSoundParams(c->handle, volume, sep, pitch);
}
}
else // if channel is allocated but sound has stopped, free it
S_StopChannel(cnum);
}
}
}
void S_SetMusicVolume(int volume)
{
boolean mus_card = 0; // cosmito
boolean nomusicparm = 1; // cosmito
//jff 1/22/98 return if music is not enabled
if (!mus_card || nomusicparm)
return;
if (volume < 0 || volume > 15)
//I_Error(volume, "Attempt to set music volume at %d");
I_SetMusicVolume(volume);
snd_MusicVolume = volume;
}
void S_SetSfxVolume(int volume)
{
boolean nosfxparm = 0;
boolean snd_card = 1;
//jff 1/22/98 return if sound is not enabled
if (!snd_card || nosfxparm)
return;
if (volume < 0 || volume > 127)
// I_Error(volume, "Attempt to set sfx volume at %d");
snd_SfxVolume = volume;
}
//
// Starts some music with the music id found in sounds.h.
//
void S_StartMusic(int m_id)
{
boolean mus_card = 0; // cosmito
boolean nomusicparm = 1; // cosmito
//jff 1/22/98 return if music is not enabled
if (!mus_card || nomusicparm)
return;
S_ChangeMusic(m_id, false);
}
void S_ChangeMusic(int musicnum, int looping)
{
musicinfo_t *music;
boolean mus_card = 0; // cosmito
boolean nomusicparm = 1; // cosmito
//jff 1/22/98 return if music is not enabled
if (!mus_card || nomusicparm)
return;
if (musicnum <= mus_None || musicnum >= NUMMUSIC)
// I_Error(musicnum, "Bad music number %d");
music = &S_music[musicnum];
if (mus_playing == music)
return;
// shutdown old music
S_StopMusic();
// get lumpnum if neccessary
if (!music->lumpnum)
{
char namebuf[9];
sprintf(namebuf, "d_%s", music->name);
music->lumpnum = W_GetNumForName(namebuf);
}
// load & register it
//music->data = W_CacheLumpNum(music->lumpnum);
music->data = W_CacheLumpNum(music->lumpnum, PU_STATIC); // cosmito
music->handle = I_RegisterSong(music->data, W_LumpLength(music->lumpnum));
// play it
I_PlaySong(music->handle, looping);
mus_playing = music;
}
void S_StopMusic(void)
{
boolean mus_card = 0; // cosmito
boolean nomusicparm = 1; // cosmito
//jff 1/22/98 return if music is not enabled
if (!mus_card || nomusicparm)
return;
if (mus_playing)
{
if (mus_paused)
I_ResumeSong(mus_playing->handle);
I_StopSong(mus_playing->handle);
I_UnRegisterSong(mus_playing->handle);
//W_UnlockLumpNum(mus_playing->lumpnum); // cph - release the music data // cosmito : W_UnlockLumpNum needs to be implemented. Since music is currently disabled, I commented this line
mus_playing->data = 0;
mus_playing = 0;
}
}
void S_StopChannel(int cnum)
{
int i;
boolean nosfxparm = 0;
boolean snd_card = 1;
channel_t *c = &channels[cnum];
//jff 1/22/98 return if sound is not enabled
if (!snd_card || nosfxparm)
return;
if (c->sfxinfo)
{
// stop the sound playing
if (I_SoundIsPlaying(c->handle))
I_StopSound(c->handle);
// check to see
// if other channels are playing the sound
for (i=0 ; i<numChannels ; i++)
if (cnum != i && c->sfxinfo == channels[i].sfxinfo)
break;
// degrade usefulness of sound data
c->sfxinfo->usefulness--;
c->sfxinfo = 0;
}
}
//
// Changes volume, stereo-separation, and pitch variables
// from the norm of a sound effect to be played.
// If the sound is not audible, returns a 0.
// Otherwise, modifies parameters and returns 1.
//
int S_AdjustSoundParams(mobj_t *listener, mobj_t *source,
int *vol, int *sep, int *pitch)
{
fixed_t adx, ady,approx_dist;
angle_t angle;
boolean nosfxparm = 0;
boolean snd_card = 1;
//jff 1/22/98 return if sound is not enabled
if (!snd_card || nosfxparm)
return 0;
// calculate the distance to sound origin
// and clip it if necessary
adx = abs(listener->x - source->x);
ady = abs(listener->y - source->y);
// From _GG1_ p.428. Appox. eucledian distance fast.
approx_dist = adx + ady - ((adx < ady ? adx : ady)>>1);
if (!approx_dist) // killough 11/98: handle zero-distance as special case
{
*sep = NORM_SEP;
*vol = snd_SfxVolume;
return *vol > 0;
}
if (approx_dist > S_CLIPPING_DIST)
return 0;
// angle of source to listener
angle = R_PointToAngle2(listener->x, listener->y, source->x, source->y);
if (angle <= listener->angle)
angle += 0xffffffff;
angle -= listener->angle;
angle >>= ANGLETOFINESHIFT;
// stereo separation
*sep = 128 - (FixedMul(S_STEREO_SWING,finesine[angle])>>FRACBITS);
// volume calculation
if (approx_dist < S_CLOSE_DIST)
*vol = snd_SfxVolume;
else
// distance effect
*vol = (snd_SfxVolume * ((S_CLIPPING_DIST-approx_dist)>>FRACBITS))
/ S_ATTENUATOR;
return (*vol > 0);
}
//
// S_getChannel :
// If none available, return -1. Otherwise channel #.
//
// killough 4/25/98: made static, added is_pickup argument
static int S_getChannel(void *origin, sfxinfo_t *sfxinfo, int is_pickup)
{
// channel number to use
int cnum;
channel_t *c;
boolean nosfxparm = 0;
boolean snd_card = 1;
//jff 1/22/98 return if sound is not enabled
if (!snd_card || nosfxparm)
return -1;
// Find an open channel
for (cnum=0; cnum<numChannels && channels[cnum].sfxinfo; cnum++)
if (origin && channels[cnum].origin == origin &&
channels[cnum].is_pickup == is_pickup)
{
S_StopChannel(cnum);
break;
}
// None available
if (cnum == numChannels)
{ // Look for lower priority
for (cnum=0 ; cnum<numChannels ; cnum++)
if (channels[cnum].sfxinfo->priority >= sfxinfo->priority)
break;
if (cnum == numChannels)
return -1; // No lower priority. Sorry, Charlie.
else
S_StopChannel(cnum); // Otherwise, kick out lower priority.
}
c = &channels[cnum]; // channel is decided to be cnum.
c->sfxinfo = sfxinfo;
c->origin = origin;
c->is_pickup = is_pickup; // killough 4/25/98
return cnum;
}
//----------------------------------------------------------------------------
//
// $Log: s_sound.c,v $
// Revision 1.10 1999/10/12 13:01:14 cphipps
// Changed header to GPL
//
// Revision 1.9 1999/03/01 07:51:29 cphipps
// Fix obvious stupid stuff in S_SetMusicVolume, like bad volume range
// and volume being set to 127 before any change. Thanks to Rafael for
// pointing this out
//
// Revision 1.8 1999/01/01 13:03:14 cphipps
// Fix releasing music lump problem
//
// Revision 1.7 1999/01/01 10:30:24 cphipps
// Music no longer retained in memory
// Wad lump handling updated
//
// Revision 1.6 1998/12/24 10:25:59 cphipps
// Minor fixes from MBF
// Reformatted S_Init slightly
//
// Revision 1.5 1998/10/27 19:19:35 cphipps
// Fix typo
//
// Revision 1.4 1998/10/27 18:56:15 cphipps
// Logical output console stuff for Boom v2.02 update patched in
//
// Revision 1.3 1998/10/20 14:45:28 cphipps
// Pass length to I_RegisterSong
//
// Revision 1.2 1998/10/16 22:00:57 cphipps
// Removed hanging else
//
// Revision 1.1 1998/09/13 16:49:50 cphipps
// Initial revision
//
// Revision 1.11 1998/05/03 22:57:06 killough
// beautification, #include fix
//
// Revision 1.10 1998/04/27 01:47:28 killough
// Fix pickups silencing player weapons
//
// Revision 1.9 1998/03/23 03:39:12 killough
// Fix spy-mode sound effects
//
// Revision 1.8 1998/03/17 20:44:25 jim
// fixed idmus non-restore, space bug
//
// Revision 1.7 1998/03/09 07:32:57 killough
// ATTEMPT to support hearing with displayplayer's hears
//
// Revision 1.6 1998/03/04 07:46:10 killough
// Remove full-volume sound hack from MAP08
//
// Revision 1.5 1998/03/02 11:45:02 killough
// Make missing sounds non-fatal
//
// Revision 1.4 1998/02/02 13:18:48 killough
// stop incorrect looping of music (e.g. bunny scroll)
//
// Revision 1.3 1998/01/26 19:24:52 phares
// First rev with no ^Ms
//
// Revision 1.2 1998/01/23 01:50:49 jim
// Added music/sound options, and enables
//
// Revision 1.1.1.1 1998/01/19 14:03:04 rand
// Lee's Jan 19 sources
//
//----------------------------------------------------------------------------