summaryrefslogtreecommitdiff
path: root/apps/plugins/doom/s_sound.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/doom/s_sound.c')
-rw-r--r--apps/plugins/doom/s_sound.c608
1 files changed, 608 insertions, 0 deletions
diff --git a/apps/plugins/doom/s_sound.c b/apps/plugins/doom/s_sound.c
new file mode 100644
index 0000000000..c8ed7b2839
--- /dev/null
+++ b/apps/plugins/doom/s_sound.c
@@ -0,0 +1,608 @@
1/* Emacs style mode select -*- C++ -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 *
27 * DESCRIPTION: Platform-independent sound code
28 *
29 *-----------------------------------------------------------------------------*/
30
31#include "i_system.h"
32#include "i_sound.h"
33#include "sounds.h"
34#include "s_sound.h"
35
36#include "z_zone.h"
37#include "m_random.h"
38#include "w_wad.h"
39#include "d_main.h"
40#include "doomdef.h"
41#include "r_main.h"
42
43#include "doomstat.h"
44
45#include "rockmacros.h"
46
47// when to clip out sounds
48// Does not fit the large outdoor areas.
49#define S_CLIPPING_DIST (1200<<FRACBITS)
50
51// Distance tp origin when sounds should be maxed out.
52// This should relate to movement clipping resolution
53// (see BLOCKMAP handling).
54// Originally: (200*0x10000).
55
56#define S_CLOSE_DIST (160*0x10000)
57#define S_ATTENUATOR ((S_CLIPPING_DIST-S_CLOSE_DIST)>>FRACBITS)
58
59// Adjustable by menu.
60#define NORM_PITCH 128
61#define NORM_PRIORITY 64
62#define NORM_SEP 128
63#define S_STEREO_SWING (96<<FRACBITS)
64
65typedef struct
66{
67 sfxinfo_t *sfxinfo; // sound information (if null, channel avail.)
68 void *origin; // origin of sound
69 int handle; // handle of the sound being played
70 int is_pickup; // killough 4/25/98: whether sound is a player's weapon
71} channel_t;
72
73// the set of channels available
74static channel_t* channels;
75
76// These are not used, but should be (menu).
77// Maximum volume of a sound effect.
78// Internal default is max out of 0-15.
79int snd_SfxVolume = 15;
80
81// Maximum volume of music. Useless so far.
82int snd_MusicVolume = 15;
83
84// whether songs are mus_paused
85static boolean mus_paused;
86
87// music currently being played
88static musicinfo_t* mus_playing=0;
89
90// following is set
91// by the defaults code in M_misc:
92// number of channels available
93int default_numChannels;
94int numChannels;
95
96//jff 3/17/98 to keep track of last IDMUS specified music num
97int idmusnum;
98
99//
100// Internals.
101//
102
103void S_StopChannel(int cnum);
104
105int S_AdjustSoundParams(mobj_t *listener, mobj_t *source,
106 int *vol, int *sep, int *pitch);
107
108static int S_getChannel(void *origin, sfxinfo_t *sfxinfo, int is_pickup);
109
110//
111// Initializes sound stuff, including volume
112// Sets channels, SFX and music volume,
113// allocates channel buffer, sets S_sfx lookup.
114//
115
116void S_Init(int sfxVolume,int musicVolume )
117{
118 //jff 1/22/98 skip sound init if sound not enabled
119 numChannels = default_numChannels;
120 if (!nosfxparm)
121 {
122 int i;
123
124 printf("S_Init: default sfx volume %d\n", sfxVolume);
125
126 // Whatever these did with DMX, these are rather dummies now.
127 I_SetChannels();
128
129 S_SetSfxVolume(sfxVolume);
130
131 S_SetMusicVolume(musicVolume);
132
133 // Allocating the internal channels for mixing
134 // (the maximum numer of sounds rendered
135 // simultaneously) within zone memory.
136 // CPhipps - calloc
137 channels = (channel_t *) calloc(numChannels,sizeof(channel_t));
138
139 // no sounds are playing, and they are not mus_paused
140 mus_paused = 0;
141
142 // Note that sounds have not been cached (yet).
143 for (i=1 ; i<NUMSFX ; i++)
144 S_sfx[i].lumpnum = S_sfx[i].usefulness = -1;
145 }
146}
147
148//
149// Per level startup code.
150// Kills playing sounds at start of level,
151// determines music if any, changes music.
152//
153void S_Start(void)
154{
155 int cnum, mnum;
156
157 if (!nosfxparm)
158 {
159 // kill all playing sounds at start of level
160 // (trust me - a good idea)
161 for (cnum=0 ; cnum<numChannels ; cnum++)
162 if (channels[cnum].sfxinfo)
163 S_StopChannel(cnum);
164
165 // start new music for the level
166 mus_paused = 0;
167
168 if (gamemode == commercial)
169 mnum = mus_runnin + gamemap - 1;
170 else
171 {
172 int spmus[]= // Song - Who? - Where?
173 {
174 mus_e3m4, // American e4m1
175 mus_e3m2, // Romero e4m2
176 mus_e3m3, // Shawn e4m3
177 mus_e1m5, // American e4m4
178 mus_e2m7, // Tim e4m5
179 mus_e2m4, // Romero e4m6
180 mus_e2m6, // J.Anderson e4m7 CHIRON.WAD
181 mus_e2m5, // Shawn e4m8
182 mus_e1m9 // Tim e4m9
183 };
184
185 if (gameepisode < 4)
186 mnum = mus_e1m1 + (gameepisode-1)*9 + gamemap-1;
187 else
188 mnum = spmus[gamemap-1];
189 }
190
191 S_ChangeMusic(mnum, true);
192 }
193}
194
195void S_StartSoundAtVolume(void *origin_p, int sfx_id, int volume)
196{
197 int sep, pitch, priority, cnum, is_pickup;
198 sfxinfo_t* sfx;
199 mobj_t* origin = (mobj_t *) origin_p;
200
201 if (nosfxparm)
202 return;
203
204 is_pickup = sfx_id & PICKUP_SOUND || sfx_id == sfx_oof || (compatibility_level >= prboom_2_compatibility && sfx_id == sfx_noway); // killough 4/25/98
205 sfx_id &= ~PICKUP_SOUND;
206
207 // check for bogus sound #
208 if (sfx_id < 1 || sfx_id > NUMSFX)
209 I_Error("S_StartSoundAtVolume: Bad sfx #: %d", sfx_id);
210
211 sfx = &S_sfx[sfx_id];
212
213 // Initialize sound parameters
214 if (sfx->link)
215 {
216 pitch = sfx->pitch;
217 priority = sfx->priority;
218 volume += sfx->volume;
219
220 if (volume < 1)
221 return;
222
223 if (volume > snd_SfxVolume)
224 volume = snd_SfxVolume;
225 }
226 else
227 {
228 pitch = NORM_PITCH;
229 priority = NORM_PRIORITY;
230 }
231
232
233 // Check to see if it is audible,
234 // and if not, modify the params
235
236 if (!origin || origin == players[displayplayer].mo) {
237 sep = NORM_SEP;
238 volume *= 8;
239 } else
240 if (!S_AdjustSoundParams(players[displayplayer].mo, origin, &volume,
241 &sep, &pitch))
242 return;
243 else
244 if ( origin->x == players[displayplayer].mo->x &&
245 origin->y == players[displayplayer].mo->y)
246 sep = NORM_SEP;
247
248 // hacks to vary the sfx pitches
249 if (sfx_id >= sfx_sawup && sfx_id <= sfx_sawhit)
250 pitch += 8 - (M_Random()&15);
251 else
252 if (sfx_id != sfx_itemup && sfx_id != sfx_tink)
253 pitch += 16 - (M_Random()&31);
254
255 if (pitch<0)
256 pitch = 0;
257
258 if (pitch>255)
259 pitch = 255;
260
261 // kill old sound
262 for (cnum=0 ; cnum<numChannels ; cnum++)
263 if (channels[cnum].sfxinfo && channels[cnum].origin == origin &&
264 (comp[comp_sound] || channels[cnum].is_pickup == is_pickup))
265 {
266 S_StopChannel(cnum);
267 break;
268 }
269
270 // try to find a channel
271 cnum = S_getChannel(origin, sfx, is_pickup);
272
273 if (cnum<0)
274 return;
275
276 // get lumpnum if necessary
277 // killough 2/28/98: make missing sounds non-fatal
278 if (sfx->lumpnum < 0 && (sfx->lumpnum = I_GetSfxLumpNum(sfx)) < 0)
279 return;
280
281 // increase the usefulness
282 if (sfx->usefulness++ < 0)
283 sfx->usefulness = 1;
284
285 // Assigns the handle to one of the channels in the mix/output buffer.
286 channels[cnum].handle = I_StartSound(sfx_id, cnum, volume, sep, pitch, priority);
287}
288
289void S_StartSound(void *origin, int sfx_id)
290{
291 S_StartSoundAtVolume(origin, sfx_id, snd_SfxVolume);
292}
293
294void S_StopSound(void *origin)
295{
296 int cnum;
297
298 if (nosfxparm)
299 return;
300
301 for (cnum=0 ; cnum<numChannels ; cnum++)
302 if (channels[cnum].sfxinfo && channels[cnum].origin == origin)
303 {
304 S_StopChannel(cnum);
305 break;
306 }
307}
308
309//
310// Stop and resume music, during game PAUSE.
311//
312void S_PauseSound(void)
313{
314 if (nosfxparm)
315 return;
316
317 if (mus_playing && !mus_paused)
318 {
319 I_PauseSong(mus_playing->handle);
320 mus_paused = true;
321 }
322}
323
324void S_ResumeSound(void)
325{
326 if (nosfxparm)
327 return;
328
329 if (mus_playing && mus_paused)
330 {
331 I_ResumeSong(mus_playing->handle);
332 mus_paused = false;
333 }
334}
335
336//
337// Updates music & sounds
338//
339void S_UpdateSounds(void* listener_p)
340{
341 int cnum;
342
343 mobj_t* listener = (mobj_t*)listener_p;
344
345 if (nosfxparm)
346 return;
347
348 for (cnum=0 ; cnum<numChannels ; cnum++)
349 {
350 sfxinfo_t* sfx;
351 channel_t* c = &channels[cnum];
352
353 if ((sfx = c->sfxinfo))
354 {
355 if (I_SoundIsPlaying(c->handle))
356 {
357 // initialize parameters
358 int volume = snd_SfxVolume;
359 int sep = NORM_PITCH;
360 int pitch = NORM_SEP;
361
362 if (sfx->link)
363 {
364 pitch = sfx->pitch;
365 volume += sfx->volume;
366 if (volume < 1)
367 {
368 S_StopChannel(cnum);
369 continue;
370 }
371 else
372 if (volume > snd_SfxVolume)
373 volume = snd_SfxVolume;
374 }
375
376 // check non-local sounds for distance clipping
377 // or modify their params
378 if (c->origin && listener_p != c->origin) // killough 3/20/98
379 {
380 if (!S_AdjustSoundParams(listener, c->origin,
381 &volume, &sep, &pitch))
382 S_StopChannel(cnum);
383 else
384 I_UpdateSoundParams(c->handle, volume, sep, pitch);
385 }
386 }
387 else
388 S_StopChannel(cnum);
389 }
390 }
391}
392
393void S_SetMusicVolume(int volume)
394{
395 if (nosfxparm)
396 return;
397
398 if (volume < 0 || volume > 15)
399 I_Error("S_SetMusicVolume: Attempt to set music volume at %d", volume);
400 I_SetMusicVolume(volume);
401 snd_MusicVolume = volume;
402}
403
404void S_SetSfxVolume(int volume)
405{
406 if (nosfxparm)
407 return;
408
409 if (volume < 0 || volume > 127)
410 I_Error("S_SetSfxVolume: Attempt to set sfx volume at %d", volume);
411 snd_SfxVolume = volume;
412}
413
414//
415// Starts some music with the music id found in sounds.h.
416//
417void S_StartMusic(int m_id)
418{
419 if (nosfxparm)
420 return;
421 S_ChangeMusic(m_id, false);
422}
423
424void S_ChangeMusic(int musicnum, int looping)
425{
426 musicinfo_t *music;
427
428 if (nosfxparm)
429 return;
430
431 if (musicnum <= mus_None || musicnum >= NUMMUSIC)
432 I_Error("S_ChangeMusic: Bad music number %d", musicnum);
433
434 music = &S_music[musicnum];
435
436 if (mus_playing == music)
437 return;
438
439 // shutdown old music
440 S_StopMusic();
441
442 // get lumpnum if neccessary
443 if (!music->lumpnum)
444 {
445 char namebuf[9];
446 snprintf(namebuf,sizeof(namebuf),"d_%s", music->name);
447 music->lumpnum = W_GetNumForName(namebuf);
448 }
449
450 // load & register it
451 music->data = W_CacheLumpNum(music->lumpnum);
452// music->handle = I_RegisterSong(music->data, W_LumpLength(music->lumpnum));
453
454 // load & register it
455// music->data = (void *) W_CacheLumpNum(music->lumpnum);
456 music->handle = I_RegisterSong(music->data);
457
458 // play it
459 I_PlaySong(music->handle, looping);
460
461 mus_playing = music;
462}
463
464
465void S_StopMusic(void)
466{
467 if (nosfxparm)
468 return;
469
470 if (mus_playing)
471 {
472 if (mus_paused)
473 I_ResumeSong(mus_playing->handle);
474
475 I_StopSong(mus_playing->handle);
476 I_UnRegisterSong(mus_playing->handle);
477 if (mus_playing->lumpnum >= 0)
478 W_UnlockLumpNum(mus_playing->lumpnum); // cph - release the music data
479
480 mus_playing->data = 0;
481 mus_playing = 0;
482 }
483}
484
485void S_StopChannel(int cnum)
486{
487 int i;
488 channel_t* c = &channels[cnum];
489
490 if (nosfxparm)
491 return;
492
493 if (c->sfxinfo)
494 {
495 // stop the sound playing
496 if (I_SoundIsPlaying(c->handle))
497 I_StopSound(c->handle);
498
499 // check to see
500 // if other channels are playing the sound
501 for (i=0 ; i<numChannels ; i++)
502 if (cnum != i && c->sfxinfo == channels[i].sfxinfo)
503 break;
504
505 // degrade usefulness of sound data
506 c->sfxinfo->usefulness--;
507 c->sfxinfo = 0;
508 }
509}
510
511//
512// Changes volume, stereo-separation, and pitch variables
513// from the norm of a sound effect to be played.
514// If the sound is not audible, returns a 0.
515// Otherwise, modifies parameters and returns 1.
516//
517int S_AdjustSoundParams(mobj_t *listener, mobj_t *source,
518 int *vol, int *sep, int *pitch)
519{
520 (void)pitch;
521 fixed_t adx, ady, approx_dist;
522 angle_t angle;
523
524 if (nosfxparm)
525 return 0;
526
527 // calculate the distance to sound origin
528 // and clip it if necessary
529 adx = D_abs(listener->x - source->x);
530 ady = D_abs(listener->y - source->y);
531
532 // From _GG1_ p.428. Appox. eucledian distance fast.
533 approx_dist = adx + ady - ((adx < ady ? adx : ady)>>1);
534
535 if (!approx_dist) // killough 11/98: handle zero-distance as special case
536 {
537 *sep = NORM_SEP;
538 *vol = snd_SfxVolume;
539 return *vol > 0;
540 }
541
542 if (approx_dist > S_CLIPPING_DIST)
543 return 0;
544
545 // angle of source to listener
546 angle = R_PointToAngle2(listener->x, listener->y, source->x, source->y);
547
548 if (angle <= listener->angle)
549 angle += 0xffffffff;
550 angle -= listener->angle;
551 angle >>= ANGLETOFINESHIFT;
552
553 // stereo separation
554 *sep = 128 - (FixedMul(S_STEREO_SWING,finesine[angle])>>FRACBITS);
555
556 // volume calculation
557 if (approx_dist < S_CLOSE_DIST)
558 *vol = snd_SfxVolume*8;
559 else
560 // distance effect
561 *vol = (snd_SfxVolume * ((S_CLIPPING_DIST-approx_dist)>>FRACBITS) * 8)
562 / S_ATTENUATOR;
563
564 return (*vol > 0);
565}
566
567//
568// S_getChannel :
569// If none available, return -1. Otherwise channel #.
570//
571// killough 4/25/98: made static, added is_pickup argument
572
573static int S_getChannel(void *origin, sfxinfo_t *sfxinfo, int is_pickup)
574{
575 // channel number to use
576 int cnum;
577 channel_t* c;
578
579 if (nosfxparm)
580 return -1;
581
582 // Find an open channel
583 for (cnum=0; cnum<numChannels && channels[cnum].sfxinfo; cnum++)
584 if (origin && channels[cnum].origin == origin &&
585 channels[cnum].is_pickup == is_pickup)
586 {
587 S_StopChannel(cnum);
588 break;
589 }
590
591 // None available
592 if (cnum == numChannels)
593 { // Look for lower priority
594 for (cnum=0 ; cnum<numChannels ; cnum++)
595 if (channels[cnum].sfxinfo->priority >= sfxinfo->priority)
596 break;
597 if (cnum == numChannels)
598 return -1; // No lower priority. Sorry, Charlie.
599 else
600 S_StopChannel(cnum); // Otherwise, kick out lower priority.
601 }
602
603 c = &channels[cnum]; // channel is decided to be cnum.
604 c->sfxinfo = sfxinfo;
605 c->origin = origin;
606 c->is_pickup = is_pickup; // killough 4/25/98
607 return cnum;
608}