Debian Codegraph @

prboom2/src/s_sound.c

raw
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 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION: Platform-independent sound code
30 *
31 *-----------------------------------------------------------------------------*/
32
33 // killough 3/7/98: modified to allow arbitrary listeners in spy mode
34 // killough 5/2/98: reindented, removed useless code, beautified
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "doomstat.h"
41 #include "s_sound.h"
42 #include "s_advsound.h"
43 #include "i_sound.h"
44 #include "i_system.h"
45 #include "d_main.h"
46 #include "r_main.h"
47 #include "m_random.h"
48 #include "w_wad.h"
49 #include "lprintf.h"
50 #include "p_maputl.h"
51 #include "p_setup.h"
52 #include "e6y.h"
53
54 #include "hexen/sn_sonix.h"
55
56 #include "dsda/configuration.h"
57 #include "dsda/map_format.h"
58 #include "dsda/mapinfo.h"
59 #include "dsda/memory.h"
60 #include "dsda/music.h"
61 #include "dsda/settings.h"
62 #include "dsda/sfx.h"
63 #include "dsda/skip.h"
64
65 // Adjustable by menu.
66 #define NORM_PITCH 128
67 #define NORM_PRIORITY 64
68 #define NORM_SEP 128
69 #define S_STEREO_SWING (96<<FRACBITS)
70
71 const int channel_not_found = -1;
72
73 typedef struct
74 {
75 sfxinfo_t *sfxinfo; // sound information (if null, channel avail.)
76 void *origin; // origin of sound
77 int handle; // handle of the sound being played
78 int pitch;
79
80 // heretic
81 int priority;
82
83 // hexen
84 int volume;
85
86 dboolean active;
87 dboolean ambient;
88 float attenuation;
89 float volume_factor;
90 dboolean loop;
91 int loop_timeout;
92 sfx_class_t sfx_class;
93 } channel_t;
94
95 // the set of channels available
96 static channel_t channels[MAX_CHANNELS];
97 static degenmobj_t sobjs[MAX_CHANNELS];
98
99 // Maximum volume of a sound effect.
100 // Internal default is max out of 0-15.
101 int snd_SfxVolume;
102
103 // Derived value (not saved, accounts for muted sfx)
104 static int sfx_volume;
105
106 // Maximum volume of music.
107 int snd_MusicVolume = 15;
108
109 // whether songs are mus_paused
110 static dboolean mus_paused;
111
112 // music currently being played
113 musicinfo_t *mus_playing;
114
115 // music currently should play
116 static int musicnum_current;
117
118 // number of channels available
119 int numChannels;
120
121 //jff 3/17/98 to keep track of last IDMUS specified music num
122 int idmusnum;
123
124 //
125 // Internals.
126 //
127
128 void S_StopChannel(int cnum);
129
130 int S_AdjustSoundParams(mobj_t *listener, mobj_t *source, channel_t *channel, sfx_params_t *params);
131
132 static int S_getChannel(void *origin, sfxinfo_t *sfxinfo, sfx_params_t *params);
133
134
135 // heretic
136 int max_snd_dist = 1600;
137 int dist_adjust = 160;
138
139 static byte* soundCurve;
140 static int AmbChan = -1;
141
142 static mobj_t* GetSoundListener(void);
143 static void Heretic_S_StopSound(void *_origin);
144 static void Raven_S_StartSoundAtVolume(void *_origin, int sound_id, int volume, int loop_timeout);
145
146 void S_ResetSfxVolume(void)
147 {
148 snd_SfxVolume = dsda_IntConfig(dsda_config_sfx_volume);
149
150 if (nosfxparm)
151 return;
152
153 if (dsda_MuteSfx())
154 sfx_volume = 0;
155 else
156 sfx_volume = snd_SfxVolume;
157 }
158
159 void S_ResetVolume(void)
160 {
161 void I_ResetMusicVolume(void);
162
163 S_ResetSfxVolume();
164 I_ResetMusicVolume();
165 }
166
167 // Initializes sound stuff, including volume
168 // Sets channels, SFX and music volume,
169 // allocates channel buffer, sets S_sfx lookup.
170 //
171
172 void S_Init(void)
173 {
174 idmusnum = -1; //jff 3/17/98 insure idmus number is blank
175
176 S_Stop();
177
178 numChannels = dsda_IntConfig(dsda_config_snd_channels);
179
180 //jff 1/22/98 skip sound init if sound not enabled
181 if (!nosfxparm)
182 {
183 static dboolean first_s_init = true;
184
185 // Whatever these did with DMX, these are rather dummies now.
186 I_SetChannels();
187
188 S_ResetSfxVolume();
189
190 // Reset channel memory
191 memset(channels, 0, sizeof(channels));
192 memset(sobjs, 0, sizeof(sobjs));
193
194 if (first_s_init)
195 {
196 int i;
197 int snd_curve_lump;
198
199 first_s_init = false;
200
201 for (i = 1; i < num_sfx; i++)
202 S_sfx[i].lumpnum = -1;
203
204 dsda_CacheSoundLumps();
205
206 lprintf(LO_DEBUG, " Precaching all sound effects... ");
207 I_CacheSounds();
208 lprintf(LO_DEBUG, "done\n");
209
210 // {
211 // int i;
212 // const int snd_curve_length = 1200;
213 // const int flat_curve_length = 160;
214 // byte* buffer = Z_Malloc(snd_curve_length);
215 // for (i = 0; i < snd_curve_length; ++i)
216 // {
217 // if (i < flat_curve_length)
218 // buffer[i] = 127;
219 // else
220 // buffer[i] = 127 * (snd_curve_length - i) / (snd_curve_length - flat_curve_length);
221
222 // if (!buffer[i])
223 // buffer[i] = 1;
224 // }
225 // M_WriteFile("sndcurve.lmp", buffer, snd_curve_length);
226 // }
227
228 snd_curve_lump = W_GetNumForName("SNDCURVE");
229 max_snd_dist = W_LumpLength(snd_curve_lump);
230
231 dist_adjust = max_snd_dist / 10;
232
233 soundCurve = Z_Malloc(max_snd_dist);
234 memcpy(soundCurve, (const byte *) W_LumpByNum(snd_curve_lump), max_snd_dist);
235 }
236 }
237
238 // CPhipps - music init reformatted
239 if (!nomusicparm) {
240 void I_ResetMusicVolume(void);
241
242 I_ResetMusicVolume();
243
244 // no sounds are playing, and they are not mus_paused
245 mus_paused = 0;
246 }
247 }
248
249 void S_Stop(void)
250 {
251 int cnum;
252
253 // heretic
254 AmbChan = -1;
255
256 //jff 1/22/98 skip sound init if sound not enabled
257 if (!nosfxparm)
258 for (cnum=0 ; cnum<numChannels ; cnum++)
259 if (channels[cnum].active)
260 S_StopChannel(cnum);
261 }
262
263 //
264 // Per level startup code.
265 // Kills playing sounds at start of level,
266 // determines music if any, changes music.
267 //
268
269 void S_Start(void)
270 {
271 int mnum;
272 int muslump;
273
274 // kill all playing sounds at start of level
275 // (trust me - a good idea)
276
277 S_Stop();
278
279 // start new music for the level
280 mus_paused = 0;
281
282 dsda_MapMusic(&mnum, &muslump, gameepisode, gamemap);
283
284 if (muslump >= 0)
285 {
286 musinfo.items[0] = muslump;
287 }
288
289 if (musinfo.items[0] != -1)
290 {
291 if (!dsda_StartQueuedMusic())
292 S_ChangeMusInfoMusic(musinfo.items[0], true);
293 }
294 else
295 {
296 if (!dsda_StartQueuedMusic())
297 S_ChangeMusic(mnum, true);
298 }
299 }
300
301 static float adjust_attenuation;
302 static float adjust_volume;
303
304 void S_AdjustAttenuation(float attenuation) {
305 adjust_attenuation = attenuation;
306 }
307
308 void S_AdjustVolume(float volume) {
309 adjust_volume = volume;
310 }
311
312 void S_ResetAdjustments(void) {
313 adjust_attenuation = 0;
314 adjust_volume = 0;
315 }
316
317 void S_StartSoundAtVolume(void *origin_p, int sfx_id, int volume, int loop_timeout)
318 {
319 int cnum;
320 sfx_params_t params;
321 sfxinfo_t *sfx;
322 mobj_t *origin;
323 mobj_t *listener;
324
325 if (raven) return Raven_S_StartSoundAtVolume(origin_p, sfx_id, volume, loop_timeout);
326
327 origin = (mobj_t *) origin_p;
328 listener = GetSoundListener();
329
330 //jff 1/22/98 return if sound is not enabled
331 if (nosfxparm)
332 return;
333
334 // killough 4/25/98
335 if (sfx_id == g_sfx_secret)
336 params.sfx_class = sfx_class_secret;
337 else if (sfx_id & PICKUP_SOUND ||
338 sfx_id == sfx_oof ||
339 (compatibility_level >= prboom_2_compatibility && sfx_id == sfx_noway))
340 params.sfx_class = sfx_class_important;
341 else
342 params.sfx_class = sfx_class_none;
343
344 params.ambient = false;
345 params.attenuation = adjust_attenuation;
346 params.volume_factor = adjust_volume;
347 params.loop = loop_timeout > 0;
348 params.loop_timeout = loop_timeout;
349
350 sfx_id &= ~PICKUP_SOUND;
351
352 if (sfx_id == sfx_None)
353 return;
354
355 // check for bogus sound #
356 if (sfx_id < 1 || sfx_id > num_sfx)
357 I_Error("S_StartSoundAtVolume: Bad sfx #: %d", sfx_id);
358
359 sfx = &S_sfx[sfx_id];
360
361 // Initialize sound parameters
362 params.priority = 128 - sfx->priority;
363 if (params.priority <= 0)
364 params.priority = 1;
365 if (sfx->pitch < 0)
366 params.pitch = NORM_PITCH;
367 else
368 params.pitch = sfx->pitch;
369 params.volume = volume;
370
371 // Check to see if it is audible, modify the params
372 // killough 3/7/98, 4/25/98: code rearranged slightly
373
374 if (!origin || origin == listener) {
375 params.separation = NORM_SEP;
376 params.volume *= 8;
377 params.priority *= 10;
378 } else
379 if (!S_AdjustSoundParams(listener, origin, NULL, &params))
380 return;
381 else if (origin->x == listener->x && origin->y == listener->y)
382 params.separation = NORM_SEP;
383
384 if (dsda_BlockSFX(sfx)) return;
385
386 // hacks to vary the sfx pitches
387 if (sfx_id >= sfx_sawup && sfx_id <= sfx_sawhit)
388 params.pitch += 8 - (M_Random()&15);
389 else
390 if (sfx_id != sfx_itemup && sfx_id != sfx_tink)
391 params.pitch += 16 - (M_Random()&31);
392
393 if (params.pitch < 0)
394 params.pitch = 0;
395
396 if (params.pitch > 255)
397 params.pitch = 255;
398
399 // try to find a channel
400 cnum = S_getChannel(origin, sfx, &params);
401
402 if (cnum == channel_not_found)
403 return;
404
405 // get lumpnum if necessary
406 // killough 2/28/98: make missing sounds non-fatal
407 if (sfx->lumpnum < 0 && (sfx->lumpnum = I_GetSfxLumpNum(sfx)) < 0)
408 return;
409
410 // Assigns the handle to one of the channels in the mix/output buffer.
411 { // e6y: [Fix] Crash with zero-length sounds.
412 int h = I_StartSound(sfx_id, cnum, &params);
413 if (h != -1)
414 {
415 channels[cnum].handle = h;
416 channels[cnum].pitch = params.pitch;
417 channels[cnum].priority = params.priority;
418 channels[cnum].ambient = params.ambient;
419 channels[cnum].attenuation = params.attenuation;
420 channels[cnum].volume_factor = params.volume_factor;
421 channels[cnum].loop = params.loop;
422 channels[cnum].loop_timeout = params.loop_timeout;
423 channels[cnum].active = true;
424 }
425 }
426 }
427
428 void S_StartSectorSound(sector_t *sector, int sfx_id)
429 {
430 if (sector->flags & SECF_SILENT)
431 return;
432
433 S_StartSound((mobj_t *) &sector->soundorg, sfx_id);
434 }
435
436 void S_LoopSectorSound(sector_t *sector, int sfx_id, int timeout)
437 {
438 if (sector->flags & SECF_SILENT)
439 return;
440
441 S_LoopSound((mobj_t *) &sector->soundorg, sfx_id, timeout);
442 }
443
444 void S_StartMobjSound(mobj_t *mobj, int sfx_id)
445 {
446 if (mobj && mobj->subsector && mobj->subsector->sector->flags & SECF_SILENT)
447 return;
448
449 S_StartSound(mobj, sfx_id);
450 }
451
452 void S_LoopMobjSound(mobj_t *mobj, int sfx_id, int timeout)
453 {
454 if (mobj && mobj->subsector && mobj->subsector->sector->flags & SECF_SILENT)
455 return;
456
457 S_LoopSound(mobj, sfx_id, timeout);
458 }
459
460 void S_StartVoidSound(int sfx_id)
461 {
462 S_StartSound(NULL, sfx_id);
463 }
464
465 void S_LoopVoidSound(int sfx_id, int timeout)
466 {
467 S_LoopSound(NULL, sfx_id, timeout);
468 }
469
470 void S_StartLineSound(line_t *line, degenmobj_t *soundorg, int sfx_id)
471 {
472 if (line && line->frontsector && line->frontsector->flags & SECF_SILENT)
473 return;
474
475 S_StartSound((mobj_t *) soundorg, sfx_id);
476 }
477
478 void S_StartSound(void *origin, int sfx_id)
479 {
480 S_StartSoundAtVolume(origin, sfx_id, raven ? 127 : sfx_volume, 0);
481 }
482
483 void S_LoopSound(void *origin, int sfx_id, int timeout)
484 {
485 S_StartSoundAtVolume(origin, sfx_id, raven ? 127 : sfx_volume, timeout);
486 }
487
488 void S_StopSound(void *origin)
489 {
490 int cnum;
491
492 if (raven) return Heretic_S_StopSound(origin);
493
494 //jff 1/22/98 return if sound is not enabled
495 if (nosfxparm)
496 return;
497
498 for (cnum=0 ; cnum<numChannels ; cnum++)
499 if (channels[cnum].active && channels[cnum].origin == origin)
500 {
501 S_StopChannel(cnum);
502 break;
503 }
504 }
505
506 void S_StopSoundLoops(void)
507 {
508 int cnum;
509
510 if (nosfxparm)
511 return;
512
513 for (cnum = 0; cnum < numChannels; ++cnum)
514 if (channels[cnum].active && channels[cnum].loop)
515 S_StopChannel(cnum);
516 }
517
518 // [FG] disable sound cutoffs
519 int full_sounds;
520
521 void S_UnlinkSound(void *origin)
522 {
523 int cnum;
524
525 //jff 1/22/98 return if sound is not enabled
526 if (nosfxparm)
527 return;
528
529 if (origin)
530 {
531 for (cnum = 0; cnum < numChannels; cnum++)
532 {
533 if (channels[cnum].active && channels[cnum].origin == origin)
534 {
535 degenmobj_t *const sobj = &sobjs[cnum];
536 const mobj_t *const mobj = (mobj_t *) origin;
537 sobj->x = mobj->x;
538 sobj->y = mobj->y;
539 sobj->z = mobj->z;
540 channels[cnum].origin = (mobj_t *) sobj;
541 break;
542 }
543 }
544 }
545 }
546
547 //
548 // Stop and resume music, during game PAUSE.
549 //
550 void S_PauseSound(void)
551 {
552 //jff 1/22/98 return if music is not enabled
553 if (nomusicparm)
554 return;
555
556 if (mus_playing && !mus_paused)
557 {
558 I_PauseSong(mus_playing->handle);
559 mus_paused = true;
560 }
561 }
562
563 void S_ResumeSound(void)
564 {
565 //jff 1/22/98 return if music is not enabled
566 if (nomusicparm)
567 return;
568
569 if (mus_playing && mus_paused)
570 {
571 I_ResumeSong(mus_playing->handle);
572 mus_paused = false;
573 }
574 }
575
576
577 //
578 // Updates music & sounds
579 //
580 void S_UpdateSounds(void)
581 {
582 mobj_t *listener;
583 int cnum;
584
585 //jff 1/22/98 return if sound is not enabled
586 if (nosfxparm)
587 return;
588
589 #ifdef UPDATE_MUSIC
590 I_UpdateMusic();
591 #endif
592
593 listener = GetSoundListener();
594 if (sfx_volume == 0)
595 return;
596
597 if (map_format.sndseq)
598 {
599 // Update any Sequences
600 SN_UpdateActiveSequences();
601 }
602
603 for (cnum = 0; cnum < numChannels; cnum++)
604 {
605 channel_t *channel = &channels[cnum];
606
607 if (channel->active)
608 {
609 if (channel->loop && --channel->loop_timeout < 0)
610 {
611 S_StopChannel(cnum);
612 }
613 else if (I_SoundIsPlaying(channel->handle))
614 {
615 sfx_params_t params;
616
617 // check non-local sounds for distance clipping
618 // or modify their params
619 if (channel->origin && listener != channel->origin) // killough 3/20/98
620 {
621 if (S_AdjustSoundParams(listener, channel->origin, channel, &params))
622 {
623 I_UpdateSoundParams(channel->handle, &params);
624 channel->priority = params.priority;
625 }
626 else
627 {
628 raven ? S_StopSound(channel->origin) : S_StopChannel(cnum);
629 }
630 }
631 }
632 else // if channel is allocated but sound has stopped, free it
633 S_StopChannel(cnum);
634 }
635 }
636 }
637
638 // Starts some music with the music id found in sounds.h.
639 //
640 void S_StartMusic(int m_id)
641 {
642 S_ChangeMusic(m_id, false);
643 }
644
645 dboolean S_ChangeMusicByName(const char *name, dboolean looping)
646 {
647 int lump = W_CheckNumForName(name);
648
649 if (lump == LUMP_NOT_FOUND)
650 {
651 S_StopMusic();
652 return false;
653 }
654
655 S_ChangeMusInfoMusic(lump, looping);
656 return true;
657 }
658
659 void S_ChangeMusic(int musicnum, int looping)
660 {
661 musicinfo_t *music;
662
663 // current music which should play
664 musicnum_current = musicnum;
665 musinfo.current_item = -1;
666 S_music[mus_musinfo].lumpnum = -1;
667
668 //jff 1/22/98 return if music is not enabled
669 if (nomusicparm)
670 return;
671
672 if (musicnum <= mus_None || musicnum >= num_music)
673 I_Error("S_ChangeMusic: Bad music number %d", musicnum);
674
675 music = &S_music[musicnum];
676
677 if (mus_playing == music)
678 return;
679
680 // shutdown old music
681 S_StopMusic();
682
683 // get lumpnum if necessary
684 if (!music->lumpnum)
685 music->lumpnum = dsda_MusicIndexToLumpNum(musicnum);
686
687 // load & register it
688 music->data = W_LumpByNum(music->lumpnum);
689 music->handle = I_RegisterSong(music->data, W_LumpLength(music->lumpnum));
690
691 // play it
692 I_PlaySong(music->handle, looping);
693
694 mus_playing = music;
695
696 musinfo.current_item = -1;
697
698 // [crispy] MUSINFO value 0 is reserved for the map's default music
699 if (musinfo.items[0] == -1)
700 {
701 musinfo.items[0] = music->lumpnum;
702 S_music[mus_musinfo].lumpnum = -1;
703 }
704 }
705
706 void S_RestartMusic(void)
707 {
708 if (musinfo.current_item != -1)
709 {
710 S_ChangeMusInfoMusic(musinfo.current_item, true);
711 }
712 else
713 {
714 if (musicnum_current > mus_None && musicnum_current < num_music)
715 {
716 S_ChangeMusic(musicnum_current, true);
717 }
718 }
719 }
720
721 void S_ChangeMusInfoMusic(int lumpnum, int looping)
722 {
723 musicinfo_t *music;
724
725 if (dsda_SkipMode())
726 {
727 musinfo.current_item = lumpnum;
728 return;
729 }
730
731 //jff 1/22/98 return if music is not enabled
732 if (nomusicparm)
733 return;
734
735 if (mus_playing && mus_playing->lumpnum == lumpnum)
736 return;
737
738 music = &S_music[mus_musinfo];
739
740 if (music->lumpnum == lumpnum)
741 return;
742
743 // shutdown old music
744 S_StopMusic();
745
746 // save lumpnum
747 music->lumpnum = lumpnum;
748
749 // load & register it
750 music->data = W_LumpByNum(music->lumpnum);
751 music->handle = I_RegisterSong(music->data, W_LumpLength(music->lumpnum));
752
753 // play it
754 I_PlaySong(music->handle, looping);
755
756 mus_playing = music;
757
758 musinfo.current_item = lumpnum;
759 }
760
761 void S_StopMusic(void)
762 {
763 //jff 1/22/98 return if music is not enabled
764 if (nomusicparm)
765 return;
766
767 if (mus_playing)
768 {
769 if (mus_paused)
770 I_ResumeSong(mus_playing->handle);
771
772 I_StopSong(mus_playing->handle);
773 I_UnRegisterSong(mus_playing->handle);
774
775 mus_playing->data = 0;
776 mus_playing = 0;
777 }
778 }
779
780
781
782 void S_StopChannel(int cnum)
783 {
784 channel_t *c = &channels[cnum];
785
786 if (AmbChan == cnum)
787 AmbChan = -1;
788
789 //jff 1/22/98 return if sound is not enabled
790 if (nosfxparm)
791 return;
792
793 if (c->active)
794 {
795 // stop the sound playing
796 if (I_SoundIsPlaying(c->handle))
797 I_StopSound(c->handle);
798
799 c->active = false;
800 c->sfxinfo = NULL;
801 c->origin = NULL;
802 c->handle = 0;
803 }
804 }
805
806 //
807 // Changes volume, stereo-separation, and pitch variables
808 // from the norm of a sound effect to be played.
809 // If the sound is not audible, returns a 0.
810 // Otherwise, modifies parameters and returns 1.
811 //
812
813 int S_AdjustSoundParams(mobj_t *listener, mobj_t *source, channel_t *channel, sfx_params_t *params)
814 {
815 fixed_t adx, ady;
816 ufixed_t approx_dist;
817 angle_t angle;
818
819 //jff 1/22/98 return if sound is not enabled
820 if (nosfxparm)
821 return 0;
822
823 // e6y
824 if (!listener)
825 return 0;
826
827 if (channel)
828 {
829 params->ambient = channel->ambient;
830 params->attenuation = channel->attenuation;
831 params->volume_factor = channel->volume_factor;
832 params->loop = channel->loop;
833 params->loop_timeout = channel->loop_timeout;
834 }
835
836 // calculate the distance to sound origin
837 // and clip it if necessary
838 adx = D_abs(listener->x - source->x);
839 ady = D_abs(listener->y - source->y);
840
841 approx_dist = P_AproxDistance(adx, ady);
842 approx_dist >>= FRACBITS;
843
844 if (params->attenuation)
845 approx_dist *= params->attenuation;
846
847 if (approx_dist >= max_snd_dist)
848 return 0;
849
850 // angle of source to listener
851 angle = R_PointToAngle2(listener->x, listener->y, source->x, source->y);
852
853 if (angle <= listener->angle)
854 angle += 0xffffffff;
855 angle -= listener->angle;
856 angle >>= ANGLETOFINESHIFT;
857
858 // stereo separation
859 params->separation = 128 - (FixedMul(S_STEREO_SWING, finesine[angle]) >> FRACBITS);
860
861 // volume calculation
862 if (raven)
863 {
864 if (channel)
865 {
866 params->volume =
867 (soundCurve[approx_dist] * sfx_volume * 8 * channel->volume) >> 14;
868 }
869 else
870 {
871 // currently raven only adjusts on update (channel exists)
872 }
873 }
874 else
875 {
876 params->volume = (soundCurve[approx_dist] * sfx_volume * 8) >> 7;
877 if (params->volume_factor) {
878 params->volume *= params->volume_factor;
879 if (params->volume > 119)
880 params->volume = 119;
881 }
882 }
883
884 if (channel)
885 {
886 params->pitch = channel->pitch;
887 params->priority = channel->sfxinfo->priority;
888 if (!raven)
889 params->priority = 128 - params->priority;
890 }
891
892 // heretic_note: divides by 256 instead of the dist_adjust
893 params->priority *= (10 - approx_dist / dist_adjust);
894
895 return (params->volume > 0);
896 }
897
898 //
899 // S_getChannel :
900 // If none available, return -1. Otherwise channel #.
901 //
902
903 static int S_ChannelScore(channel_t *channel)
904 {
905 return channel->priority;
906 }
907
908 static int S_LowestScoreChannel(void)
909 {
910 int cnum;
911 int lowest_score = INT_MAX;
912 int lowest_cnum = channel_not_found;
913
914 for (cnum = 0; cnum < numChannels; ++cnum)
915 {
916 int score = S_ChannelScore(&channels[cnum]);
917
918 if (score < lowest_score)
919 {
920 lowest_score = score;
921 lowest_cnum = cnum;
922 }
923 }
924
925 return lowest_cnum;
926 }
927
928 static int S_getChannel(void *origin, sfxinfo_t *sfxinfo, sfx_params_t *params)
929 {
930 // channel number to use
931 int cnum;
932 channel_t *c;
933
934 //jff 1/22/98 return if sound is not enabled
935 if (nosfxparm)
936 return channel_not_found;
937
938 // Only allow one sound per origin
939 // Preserve the secret revealed sound, unless a new one is called
940 for (cnum = 0; cnum < numChannels; cnum++)
941 if (channels[cnum].active && channels[cnum].origin == origin &&
942 (comp[comp_sound] || channels[cnum].sfx_class == params->sfx_class) &&
943 (channels[cnum].sfx_class != sfx_class_secret || params->sfx_class == sfx_class_secret))
944 {
945 // The sound is already playing
946 if (channels[cnum].sfxinfo == sfxinfo && channels[cnum].loop && params->loop) {
947 channels[cnum].loop_timeout = params->loop_timeout;
948
949 return channel_not_found;
950 }
951
952 S_StopChannel(cnum);
953 break;
954 }
955
956 // Find an open channel
957 for (cnum = 0; cnum < numChannels; cnum++)
958 if (!channels[cnum].active)
959 break;
960
961 // None available
962 if (cnum == numChannels)
963 { // Look for lower priority
964 channel_t temp_channel;
965
966 memset(&temp_channel, 0, sizeof(temp_channel));
967 temp_channel.priority = params->priority;
968 temp_channel.volume = params->volume;
969
970 cnum = S_LowestScoreChannel();
971
972 if (cnum == channel_not_found)
973 return channel_not_found;
974
975 if (S_ChannelScore(&temp_channel) > S_ChannelScore(&channels[cnum]))
976 S_StopChannel(cnum);
977 else
978 return channel_not_found;
979 }
980
981 c = &channels[cnum]; // channel is decided to be cnum.
982 c->sfxinfo = sfxinfo;
983 c->origin = origin;
984 c->sfx_class = params->sfx_class;
985 return cnum;
986 }
987
988 // heretic
989
990 static dboolean S_StopSoundInfo(sfxinfo_t* sfx, sfx_params_t *params)
991 {
992 int i;
993 int priority;
994 int least_priority;
995 int found;
996
997 if (sfx->numchannels == -1)
998 return true;
999
1000 priority = params->priority;
1001 least_priority = -1;
1002 found = 0;
1003
1004 for (i = 0; i < numChannels; i++)
1005 {
1006 if (channels[i].active && channels[i].sfxinfo == sfx && channels[i].origin)
1007 {
1008 found++; //found one. Now, should we replace it??
1009 if (priority >= channels[i].priority)
1010 { // if we're gonna kill one, then this'll be it
1011 if (!channels[i].loop || priority > channels[i].priority)
1012 {
1013 least_priority = i;
1014 priority = channels[i].priority;
1015 }
1016 }
1017 }
1018 }
1019
1020 if (found < sfx->numchannels)
1021 return true;
1022
1023 if (least_priority >= 0)
1024 {
1025 S_StopChannel(least_priority);
1026
1027 return true;
1028 }
1029
1030 return false; // don't replace any sounds
1031 }
1032
1033 static int Raven_S_getChannel(mobj_t *listener, mobj_t *origin, sfxinfo_t *sfx, sfx_params_t *params)
1034 {
1035 int i;
1036 static int sndcount = 0;
1037
1038 for (i = 0; i < numChannels; i++)
1039 {
1040 // The sound is already playing
1041 if (channels[i].active &&
1042 channels[i].sfxinfo == sfx &&
1043 channels[i].origin == origin &&
1044 channels[i].loop && params->loop)
1045 {
1046 channels[i].loop_timeout = params->loop_timeout;
1047
1048 return channel_not_found;
1049 }
1050 }
1051
1052 if (!S_StopSoundInfo(sfx, params))
1053 return channel_not_found; // other sounds have greater priority
1054
1055 for (i = 0; i < numChannels; i++)
1056 {
1057 if (gamestate != GS_LEVEL || origin == listener)
1058 {
1059 i = numChannels;
1060 break; // let the player have more than one sound.
1061 }
1062 if (origin == channels[i].origin)
1063 { // only allow other mobjs one sound
1064 S_StopSound(channels[i].origin);
1065 break;
1066 }
1067 }
1068
1069 if (i >= numChannels)
1070 {
1071 // TODO: can ambient sounds even reach this flow?
1072 if (params->ambient)
1073 {
1074 if (AmbChan != -1 && sfx->priority <= channels[AmbChan].sfxinfo->priority)
1075 return channel_not_found; //ambient channel already in use
1076
1077 AmbChan = -1;
1078 }
1079
1080 for (i = 0; i < numChannels; i++)
1081 if (!channels[i].active)
1082 break;
1083
1084 if (i >= numChannels)
1085 {
1086 int chan;
1087
1088 //look for a lower priority sound to replace.
1089 sndcount++;
1090 if (sndcount >= numChannels)
1091 sndcount = 0;
1092
1093 for (chan = 0; chan < numChannels; chan++)
1094 {
1095 i = (sndcount + chan) % numChannels;
1096 if (params->priority >= channels[i].priority)
1097 {
1098 chan = -1; //denote that sound should be replaced.
1099 break;
1100 }
1101 }
1102
1103 if (chan != -1)
1104 return channel_not_found; //no free channels.
1105
1106 S_StopChannel(i);
1107 }
1108 }
1109
1110 return i;
1111 }
1112
1113 static mobj_t* GetSoundListener(void)
1114 {
1115 static degenmobj_t dummy_listener;
1116
1117 // If we are at the title screen, the display player doesn't have an
1118 // object yet, so return a pointer to a static dummy listener instead.
1119
1120 if (players[displayplayer].mo != NULL)
1121 {
1122 if (walkcamera.type > 1)
1123 {
1124 static mobj_t walkcamera_listener;
1125
1126 walkcamera_listener.x = walkcamera.x;
1127 walkcamera_listener.y = walkcamera.y;
1128 walkcamera_listener.z = walkcamera.z;
1129 walkcamera_listener.angle = walkcamera.angle;
1130
1131 return &walkcamera_listener;
1132 }
1133
1134 return players[displayplayer].mo;
1135 }
1136 else
1137 {
1138 dummy_listener.x = 0;
1139 dummy_listener.y = 0;
1140 dummy_listener.z = 0;
1141
1142 return (mobj_t *) &dummy_listener;
1143 }
1144 }
1145
1146 static void Raven_S_StartSoundAtVolume(void *_origin, int sound_id, int volume, int loop_timeout)
1147 {
1148 sfxinfo_t *sfx;
1149 mobj_t *origin;
1150 mobj_t *listener;
1151 sfx_params_t params;
1152 int dist;
1153 int cnum;
1154 angle_t angle;
1155 fixed_t absx;
1156 fixed_t absy;
1157
1158 origin = (mobj_t *)_origin;
1159 listener = GetSoundListener();
1160
1161 //jff 1/22/98 return if sound is not enabled
1162 if (nosfxparm)
1163 return;
1164
1165 if (sound_id == heretic_sfx_None)
1166 return;
1167
1168 if (origin == NULL)
1169 origin = listener;
1170
1171 sfx = &S_sfx[sound_id];
1172
1173 params.ambient = heretic && sound_id >= heretic_sfx_wind;
1174 params.attenuation = 0;
1175 params.volume_factor = 0;
1176 params.loop = loop_timeout > 0;
1177 params.loop_timeout = loop_timeout;
1178
1179 // calculate the distance before other stuff so that we can throw out
1180 // sounds that are beyond the hearing range.
1181 absx = abs(origin->x - listener->x);
1182 absy = abs(origin->y - listener->y);
1183 dist = P_AproxDistance(absx, absy);
1184 dist >>= FRACBITS;
1185
1186 if (dist >= max_snd_dist)
1187 return; //sound is beyond the hearing range...
1188 if (dist < 0)
1189 dist = 0;
1190
1191 params.priority = sfx->priority;
1192 params.priority *= (10 - (dist / dist_adjust));
1193
1194 params.sfx_class = sfx_class_none;
1195
1196 cnum = Raven_S_getChannel(listener, origin, sfx, &params);
1197 if (cnum == channel_not_found)
1198 return;
1199
1200 if (sfx->lumpnum <= 0)
1201 sfx->lumpnum = I_GetSfxLumpNum(sfx);
1202
1203 params.volume = (soundCurve[dist] * volume * sfx_volume * 8) >> 14;
1204
1205 if (origin == listener)
1206 params.separation = 128;
1207 else
1208 {
1209 angle = R_PointToAngle2(listener->x, listener->y, origin->x, origin->y);
1210 if (angle <= listener->angle)
1211 angle += 0xffffffff;
1212 angle -= listener->angle;
1213 angle >>= ANGLETOFINESHIFT;
1214
1215 // stereo separation
1216 params.separation = 128 - (FixedMul(S_STEREO_SWING,finesine[angle])>>FRACBITS);
1217 }
1218
1219 if (!hexen || sfx->pitch)
1220 {
1221 params.pitch = (byte) (NORM_PITCH + (M_Random() & 7) - (M_Random() & 7));
1222 }
1223 else
1224 {
1225 params.pitch = NORM_PITCH;
1226 }
1227
1228 channels[cnum].pitch = params.pitch;
1229 channels[cnum].handle = I_StartSound(sound_id, cnum, &params);
1230 channels[cnum].origin = origin;
1231 channels[cnum].sfxinfo = sfx;
1232 channels[cnum].priority = params.priority;
1233 channels[cnum].volume = volume; // original volume, not attenuated volume
1234 channels[cnum].ambient = params.ambient;
1235 channels[cnum].attenuation = params.attenuation;
1236 channels[cnum].volume_factor = params.volume_factor;
1237 channels[cnum].loop = params.loop;
1238 channels[cnum].loop_timeout = params.loop_timeout;
1239 channels[cnum].active = true;
1240 if (channels[cnum].ambient) // TODO: can ambient sounds even reach this flow?
1241 AmbChan = cnum;
1242 }
1243
1244 void S_StartAmbientSound(void *_origin, int sound_id, int volume)
1245 {
1246 sfxinfo_t *sfx;
1247 sfx_params_t params;
1248 mobj_t *origin;
1249 mobj_t *listener;
1250 int i;
1251
1252 origin = (mobj_t *)_origin;
1253 listener = GetSoundListener();
1254
1255 if (nosfxparm)
1256 return;
1257
1258 if (sound_id == heretic_sfx_None || volume == 0)
1259 return;
1260
1261 if (origin == NULL)
1262 origin = listener;
1263
1264 sfx = &S_sfx[sound_id];
1265
1266 if (sfx_volume > 0)
1267 params.volume = (volume * (sfx_volume + 1) * 8) >> 7;
1268 else
1269 params.volume = 0;
1270
1271 params.pitch = (byte) (NORM_PITCH - (M_Random() & 3) + (M_Random() & 3));
1272 params.priority = 1; // super low priority
1273 params.separation = 128;
1274 params.sfx_class = sfx_class_none;
1275 params.ambient = true;
1276 params.attenuation = 0;
1277 params.volume_factor = 0;
1278 params.loop = false;
1279 params.loop_timeout = 0;
1280
1281 // no priority checking, as ambient sounds would be the LOWEST.
1282 for (i = 0; i < numChannels; i++)
1283 if (channels[i].origin == NULL)
1284 break;
1285
1286 if (i >= numChannels)
1287 return;
1288
1289 if (sfx->lumpnum <= 0)
1290 sfx->lumpnum = I_GetSfxLumpNum(sfx);
1291
1292 channels[i].pitch = params.pitch;
1293 channels[i].handle = I_StartSound(sound_id, i, &params);
1294 channels[i].origin = origin;
1295 channels[i].sfxinfo = sfx;
1296 channels[i].priority = params.priority;
1297 channels[i].ambient = params.ambient;
1298 channels[i].attenuation = params.attenuation;
1299 channels[i].volume_factor = params.volume_factor;
1300 channels[i].loop = params.loop;
1301 channels[i].loop_timeout = params.loop_timeout;
1302 channels[i].active = true;
1303 }
1304
1305 static void Heretic_S_StopSound(void *_origin)
1306 {
1307 mobj_t *origin = _origin;
1308 int i;
1309
1310 //jff 1/22/98 return if sound is not enabled
1311 if (nosfxparm)
1312 return;
1313
1314 for (i = 0; i < numChannels; i++)
1315 {
1316 if (channels[i].active && channels[i].origin == origin)
1317 {
1318 S_StopChannel(i);
1319 }
1320 }
1321 }
1322
1323 // hexen
1324
1325 dboolean S_GetSoundPlayingInfo(void * origin, int sound_id)
1326 {
1327 int i;
1328 sfxinfo_t *sfx;
1329
1330 //jff 1/22/98 return if sound is not enabled
1331 if (nosfxparm)
1332 return false;
1333
1334 sfx = &S_sfx[sound_id];
1335
1336 for (i = 0; i < numChannels; i++)
1337 {
1338 if (channels[i].active && channels[i].sfxinfo == sfx && channels[i].origin == origin)
1339 {
1340 if (I_SoundIsPlaying(channels[i].handle))
1341 {
1342 return true;
1343 }
1344 }
1345 }
1346 return false;
1347 }
1348
1349 int S_GetSoundID(const char *name)
1350 {
1351 int i;
1352
1353 for (i = 0; i < num_sfx; i++)
1354 {
1355 if (!strcmp(S_sfx[i].tagname, name))
1356 {
1357 return i;
1358 }
1359 }
1360 return 0;
1361 }
1362
1363 void S_StartSongName(const char *songLump, dboolean loop)
1364 {
1365 int musicnum;
1366
1367 // lazy shortcut hack - this is a unique character
1368 switch (songLump[1])
1369 {
1370 case 'e':
1371 musicnum = hexen_mus_hexen;
1372 break;
1373 case 'u':
1374 musicnum = hexen_mus_hub;
1375 break;
1376 case 'a':
1377 musicnum = hexen_mus_hall;
1378 break;
1379 case 'r':
1380 musicnum = hexen_mus_orb;
1381 break;
1382 case 'h':
1383 musicnum = hexen_mus_chess;
1384 break;
1385 default:
1386 musicnum = hexen_mus_hub;
1387 break;
1388 }
1389
1390 S_ChangeMusic(musicnum, loop);
1391 }