Rearange menu of mpegplayer. Add new menu with "settings" and "quit", and remove...
[kugel-rb.git] / apps / plugins / doom / s_sound.c
blob4b863b5872576cecc7e88d01723e705a7070c042
1 /* Emacs style mode select -*- C++ -*-
2 *-----------------------------------------------------------------------------
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
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.
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.
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.
27 * DESCRIPTION: Platform-independent sound code
29 *-----------------------------------------------------------------------------*/
31 #include "i_system.h"
32 #include "i_sound.h"
33 #include "sounds.h"
34 #include "s_sound.h"
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"
43 #include "doomstat.h"
45 #include "rockmacros.h"
47 // when to clip out sounds
48 // Does not fit the large outdoor areas.
49 #define S_CLIPPING_DIST (1200<<FRACBITS)
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).
56 #define S_CLOSE_DIST (160*0x10000)
57 #define S_ATTENUATOR ((S_CLIPPING_DIST-S_CLOSE_DIST)>>FRACBITS)
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)
65 typedef struct
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;
73 // the set of channels available
74 static channel_t* channels;
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.
79 int snd_SfxVolume = 15;
81 // Maximum volume of music. Useless so far.
82 int snd_MusicVolume = 15;
84 // whether songs are mus_paused
85 static boolean mus_paused;
87 // music currently being played
88 static musicinfo_t* mus_playing=0;
90 // following is set
91 // by the defaults code in M_misc:
92 // number of channels available
93 int default_numChannels;
94 int numChannels;
96 //jff 3/17/98 to keep track of last IDMUS specified music num
97 int idmusnum;
100 // Internals.
103 void S_StopChannel(int cnum);
105 int S_AdjustSoundParams(mobj_t *listener, mobj_t *source,
106 int *vol, int *sep, int *pitch);
108 static int S_getChannel(void *origin, sfxinfo_t *sfxinfo, int is_pickup);
111 // Initializes sound stuff, including volume
112 // Sets channels, SFX and music volume,
113 // allocates channel buffer, sets S_sfx lookup.
116 void S_Init(int sfxVolume,int musicVolume )
118 //jff 1/22/98 skip sound init if sound not enabled
119 numChannels = default_numChannels;
120 if (enable_sound)
122 int i;
124 printf("S_Init: default sfx volume %d\n", sfxVolume);
126 // Whatever these did with DMX, these are rather dummies now.
127 I_SetChannels();
129 S_SetSfxVolume(sfxVolume);
131 S_SetMusicVolume(musicVolume);
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));
139 // no sounds are playing, and they are not mus_paused
140 mus_paused = 0;
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;
149 // Per level startup code.
150 // Kills playing sounds at start of level,
151 // determines music if any, changes music.
153 void S_Start(void)
155 int cnum, mnum;
157 if (enable_sound)
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);
165 // start new music for the level
166 mus_paused = 0;
168 if (gamemode == commercial)
169 mnum = mus_runnin + gamemap - 1;
170 else
172 int spmus[]= // Song - Who? - Where?
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
185 if (gameepisode < 4)
186 mnum = mus_e1m1 + (gameepisode-1)*9 + gamemap-1;
187 else
188 mnum = spmus[gamemap-1];
191 S_ChangeMusic(mnum, true);
195 void S_StartSoundAtVolume(void *origin_p, int sfx_id, int volume)
197 int sep, pitch, priority, cnum, is_pickup;
198 sfxinfo_t* sfx;
199 mobj_t* origin = (mobj_t *) origin_p;
201 if (!enable_sound)
202 return;
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;
207 // check for bogus sound #
208 if (sfx_id < 1 || sfx_id > NUMSFX)
209 I_Error("S_StartSoundAtVolume: Bad sfx #: %d", sfx_id);
211 sfx = &S_sfx[sfx_id];
213 // Initialize sound parameters
214 if (sfx->link)
216 pitch = sfx->pitch;
217 priority = sfx->priority;
218 volume += sfx->volume;
220 if (volume < 1)
221 return;
223 if (volume > snd_SfxVolume)
224 volume = snd_SfxVolume;
226 else
228 pitch = NORM_PITCH;
229 priority = NORM_PRIORITY;
233 // Check to see if it is audible,
234 // and if not, modify the params
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;
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);
255 if (pitch<0)
256 pitch = 0;
258 if (pitch>255)
259 pitch = 255;
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))
266 S_StopChannel(cnum);
267 break;
270 // try to find a channel
271 cnum = S_getChannel(origin, sfx, is_pickup);
273 if (cnum<0)
274 return;
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;
281 // increase the usefulness
282 if (sfx->usefulness++ < 0)
283 sfx->usefulness = 1;
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);
289 void S_StartSound(void *origin, int sfx_id)
291 S_StartSoundAtVolume(origin, sfx_id, snd_SfxVolume);
294 void S_StopSound(void *origin)
296 int cnum;
298 if (!enable_sound)
299 return;
301 for (cnum=0 ; cnum<numChannels ; cnum++)
302 if (channels[cnum].sfxinfo && channels[cnum].origin == origin)
304 S_StopChannel(cnum);
305 break;
310 // Stop and resume music, during game PAUSE.
312 void S_PauseSound(void)
314 if (!enable_sound)
315 return;
317 if (mus_playing && !mus_paused)
319 I_PauseSong(mus_playing->handle);
320 mus_paused = true;
324 void S_ResumeSound(void)
326 if (!enable_sound)
327 return;
329 if (mus_playing && mus_paused)
331 I_ResumeSong(mus_playing->handle);
332 mus_paused = false;
337 // Updates music & sounds
339 void S_UpdateSounds(void* listener_p)
341 int cnum;
343 mobj_t* listener = (mobj_t*)listener_p;
345 if (!enable_sound)
346 return;
348 for (cnum=0 ; cnum<numChannels ; cnum++)
350 sfxinfo_t* sfx;
351 channel_t* c = &channels[cnum];
353 if ((sfx = c->sfxinfo))
355 if (I_SoundIsPlaying(c->handle))
357 // initialize parameters
358 int volume = snd_SfxVolume;
359 int sep = NORM_PITCH;
360 int pitch = NORM_SEP;
362 if (sfx->link)
364 pitch = sfx->pitch;
365 volume += sfx->volume;
366 if (volume < 1)
368 S_StopChannel(cnum);
369 continue;
371 else
372 if (volume > snd_SfxVolume)
373 volume = snd_SfxVolume;
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
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);
387 else
388 S_StopChannel(cnum);
393 void S_SetMusicVolume(int volume)
395 if (!enable_sound)
396 return;
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;
404 void S_SetSfxVolume(int volume)
406 if (!enable_sound)
407 return;
409 if (volume < 0 || volume > 127)
410 I_Error("S_SetSfxVolume: Attempt to set sfx volume at %d", volume);
411 snd_SfxVolume = volume;
415 // Starts some music with the music id found in sounds.h.
417 void S_StartMusic(int m_id)
419 if (!enable_sound)
420 return;
421 S_ChangeMusic(m_id, false);
424 void S_ChangeMusic(int musicnum, int looping)
426 musicinfo_t *music;
428 if (!enable_sound)
429 return;
431 if (musicnum <= mus_None || musicnum >= NUMMUSIC)
432 I_Error("S_ChangeMusic: Bad music number %d", musicnum);
434 music = &S_music[musicnum];
436 if (mus_playing == music)
437 return;
439 // shutdown old music
440 S_StopMusic();
442 // get lumpnum if neccessary
443 if (!music->lumpnum)
445 char namebuf[9];
446 snprintf(namebuf,sizeof(namebuf),"d_%s", music->name);
447 music->lumpnum = W_GetNumForName(namebuf);
450 // load & register it
451 music->data = W_CacheLumpNum(music->lumpnum);
452 // music->handle = I_RegisterSong(music->data, W_LumpLength(music->lumpnum));
454 // load & register it
455 // music->data = (void *) W_CacheLumpNum(music->lumpnum);
456 music->handle = I_RegisterSong(music->data);
458 // play it
459 I_PlaySong(music->handle, looping);
461 mus_playing = music;
465 void S_StopMusic(void)
467 if (!enable_sound)
468 return;
470 if (mus_playing)
472 if (mus_paused)
473 I_ResumeSong(mus_playing->handle);
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
480 mus_playing->data = 0;
481 mus_playing = 0;
485 void S_StopChannel(int cnum)
487 int i;
488 channel_t* c = &channels[cnum];
490 if (!enable_sound)
491 return;
493 if (c->sfxinfo)
495 // stop the sound playing
496 if (I_SoundIsPlaying(c->handle))
497 I_StopSound(c->handle);
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;
505 // degrade usefulness of sound data
506 c->sfxinfo->usefulness--;
507 c->sfxinfo = 0;
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.
517 int S_AdjustSoundParams(mobj_t *listener, mobj_t *source,
518 int *vol, int *sep, int *pitch)
520 (void)pitch;
521 fixed_t adx, ady, approx_dist;
522 angle_t angle;
524 if (!enable_sound)
525 return 0;
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);
532 // From _GG1_ p.428. Appox. eucledian distance fast.
533 approx_dist = adx + ady - ((adx < ady ? adx : ady)>>1);
535 if (!approx_dist) // killough 11/98: handle zero-distance as special case
537 *sep = NORM_SEP;
538 *vol = snd_SfxVolume;
539 return *vol > 0;
542 if (approx_dist > S_CLIPPING_DIST)
543 return 0;
545 // angle of source to listener
546 angle = R_PointToAngle2(listener->x, listener->y, source->x, source->y);
548 if (angle <= listener->angle)
549 angle += 0xffffffff;
550 angle -= listener->angle;
551 angle >>= ANGLETOFINESHIFT;
553 // stereo separation
554 *sep = 128 - (FixedMul(S_STEREO_SWING,finesine[angle])>>FRACBITS);
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;
564 return (*vol > 0);
568 // S_getChannel :
569 // If none available, return -1. Otherwise channel #.
571 // killough 4/25/98: made static, added is_pickup argument
573 static int S_getChannel(void *origin, sfxinfo_t *sfxinfo, int is_pickup)
575 // channel number to use
576 int cnum;
577 channel_t* c;
579 if (!enable_sound)
580 return -1;
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)
587 S_StopChannel(cnum);
588 break;
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.
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;