123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include "doomstat.h"
- #include "s_sound.h"
- #include "i_sound.h"
- #include "i_system.h"
- #include "d_main.h"
- #include "r_main.h"
- #include "m_random.h"
- #include "w_wad.h"
- #include "lprintf.h"
- #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)
- #define NORM_PITCH 128
- #define NORM_PRIORITY 64
- #define NORM_SEP 128
- #define S_STEREO_SWING (96<<FRACBITS)
- const char* S_music_files[NUMMUSIC]; // cournia - stores music file names
- typedef struct
- {
- sfxinfo_t *sfxinfo; // sound information (if null, channel avail.)
- void *origin; // origin of sound
- int handle; // handle of the sound being played
- int is_pickup; // killough 4/25/98: whether sound is a player's weapon
- int volume; // JDC: perform overrides based on dynamic volume instead of static priority
- } channel_t;
- // 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 default_numChannels;
- 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);
- // 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)
- {
- //jff 1/22/98 skip sound init if sound not enabled
- numChannels = default_numChannels;
- if (snd_card && !nosfxparm)
- {
- int i;
- lprintf(LO_CONFIRM, "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
- channels =
- (channel_t *) calloc(numChannels,sizeof(channel_t));
- // 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;
- }
- }
- void S_Stop(void)
- {
- int cnum;
- //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);
- }
- //
- // Per level startup code.
- // Kills playing sounds at start of level,
- // determines music if any, changes music.
- //
- void S_Start(void)
- {
- int mnum;
- // kill all playing sounds at start of level
- // (trust me - a good idea)
- S_Stop();
- //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)
- {
- 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 || (compatibility_level >= prboom_2_compatibility && sfx_id == sfx_noway);
- sfx_id &= ~PICKUP_SOUND;
-
- if (sfx_id < 1 || sfx_id > NUMSFX)
- I_Error("S_StartSoundAtVolume: Bad sfx #: %d", sfx_id);
- sfx = &S_sfx[sfx_id];
-
- 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;
- }
-
-
- if (!origin || origin == players[displayplayer].mo) {
- sep = NORM_SEP;
- volume *= 8;
- } 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;
-
- 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;
- #if 0
-
- for (cnum=0 ; cnum<numChannels ; cnum++)
- if (channels[cnum].sfxinfo && channels[cnum].origin == origin &&
- (comp[comp_sound] || channels[cnum].is_pickup == is_pickup))
- {
- S_StopChannel(cnum);
- break;
- }
-
-
- cnum = S_getChannel(origin, sfx, is_pickup);
- #else
-
-
-
-
-
-
- cnum = numChannels;
- for (cnum=0; cnum<numChannels ; cnum++) {
- if ( channels[cnum].origin == origin &&
- channels[cnum].is_pickup == is_pickup) {
- break;
- }
- }
- if ( cnum == numChannels ) {
-
- for (cnum=0; cnum<numChannels ; cnum++) {
- if ( !channels[cnum].sfxinfo ) {
- break;
- }
- }
- }
- if ( cnum == numChannels ) {
-
-
-
- int lowestVolume = volume+1;
- int test;
- for (test=0; test<numChannels ; test++) {
- if ( channels[test].volume < lowestVolume ) {
- lowestVolume = channels[test].volume;
- cnum = test;
- }
- }
- }
-
- if ( cnum == numChannels ) {
-
- printf( "dropping sound for no channels available\n" );
- return;
- }
-
-
- channels[cnum].sfxinfo = sfx;
- channels[cnum].origin = origin;
- channels[cnum].volume = volume;
- channels[cnum].is_pickup = is_pickup;
- #endif
-
-
- if (cnum<0)
- return;
-
-
- if (sfx->lumpnum < 0 && (sfx->lumpnum = I_GetSfxLumpNum(sfx)) < 0)
- return;
-
- if (sfx->usefulness++ < 0)
- sfx->usefulness = 1;
-
- {
- int h = I_StartSound(sfx_id, cnum, volume, sep, pitch, priority);
- if (h != -1) channels[cnum].handle = h;
- }
- }
- void S_StartSound(void *origin, int sfx_id)
- {
- S_StartSoundAtVolume(origin, sfx_id, snd_SfxVolume);
- }
- void S_StopSound(void *origin)
- {
- int cnum;
-
- if (!snd_card || nosfxparm)
- return;
- for (cnum=0 ; cnum<numChannels ; cnum++)
- if (channels[cnum].sfxinfo && channels[cnum].origin == origin)
- {
- S_StopChannel(cnum);
- break;
- }
- }
- void S_PauseSound(void)
- {
-
- if (!mus_card || nomusicparm)
- return;
- if (mus_playing && !mus_paused)
- {
- I_PauseSong(mus_playing->handle);
- mus_paused = true;
- }
- }
- void S_ResumeSound(void)
- {
-
- if (!mus_card || nomusicparm)
- return;
- if (mus_playing && mus_paused)
- {
- I_ResumeSong(mus_playing->handle);
- mus_paused = false;
- }
- }
- void S_UpdateSounds(void* listener_p)
- {
- mobj_t *listener = (mobj_t*) listener_p;
- int cnum;
-
- if (!snd_card || nosfxparm)
- return;
- #ifdef UPDATE_MUSIC
- I_UpdateMusic();
- #endif
- for (cnum=0 ; cnum<numChannels ; cnum++)
- {
- sfxinfo_t *sfx;
- channel_t *c = &channels[cnum];
- if ((sfx = c->sfxinfo))
- {
- if (I_SoundIsPlaying(c->handle))
- {
-
- 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;
- }
-
-
- if (c->origin && listener_p != c->origin) {
- if (!S_AdjustSoundParams(listener, c->origin,
- &volume, &sep, &pitch))
- S_StopChannel(cnum);
- else
- I_UpdateSoundParams(c->handle, volume, sep, pitch);
- }
- }
- else
- S_StopChannel(cnum);
- }
- }
- }
- void S_SetMusicVolume(int volume)
- {
-
- if (!mus_card || nomusicparm)
- return;
- if (volume < 0 || volume > 15)
- I_Error("S_SetMusicVolume: Attempt to set music volume at %d", volume);
- I_SetMusicVolume(volume);
- snd_MusicVolume = volume;
- }
- void S_SetSfxVolume(int volume)
- {
-
- if (!snd_card || nosfxparm)
- return;
- if (volume < 0 || volume > 127)
- I_Error("S_SetSfxVolume: Attempt to set sfx volume at %d", volume);
- snd_SfxVolume = volume;
- }
- void S_StartMusic(int m_id)
- {
-
- if (!mus_card || nomusicparm)
- return;
- S_ChangeMusic(m_id, false);
- }
- void S_ChangeMusic(int musicnum, int looping)
- {
- musicinfo_t *music;
- int music_file_failed;
- char music_filename[ 1024 ];
- (void)music_file_failed;
- (void)music_filename;
-
- if (!mus_card || nomusicparm)
- return;
- if (musicnum <= mus_None || musicnum >= NUMMUSIC)
- I_Error("S_ChangeMusic: Bad music number %d", musicnum);
- music = &S_music[musicnum];
- #ifndef IPHONE
- if (mus_playing == music)
- return;
- #endif
-
- S_StopMusic();
-
- if (!music->lumpnum)
- {
- char namebuf[9];
- sprintf(namebuf, "d_%s", music->name);
- music->lumpnum = W_GetNumForName(namebuf);
- }
- music_file_failed = 1;
-
- if (lumpinfo[music->lumpnum].source == source_iwad)
- {
-
-
- I_FindFile(S_music_files[musicnum], "", music_filename);
- if ( music_filename[0] != '\0' )
- {
- music_file_failed = I_RegisterMusic(music_filename, music);
- free(music_filename);
- }
- }
- if (music_file_failed)
- {
-
-
- music->data = W_CacheLumpNum(music->lumpnum);
- music->handle = I_RegisterSong(music->data, W_LumpLength(music->lumpnum));
- }
-
- I_PlaySong(music->handle, looping);
- mus_playing = music;
- }
- void S_StopMusic(void)
- {
-
- 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);
- if (mus_playing->lumpnum >= 0)
- W_UnlockLumpNum(mus_playing->lumpnum);
- mus_playing->data = 0;
- mus_playing = 0;
- }
- }
- void S_StopChannel(int cnum)
- {
- int i;
- channel_t *c = &channels[cnum];
-
- if (!snd_card || nosfxparm)
- return;
- if (c->sfxinfo)
- {
-
- if (I_SoundIsPlaying(c->handle))
- I_StopSound(c->handle);
-
-
- for (i=0 ; i<numChannels ; i++)
- if (cnum != i && c->sfxinfo == channels[i].sfxinfo)
- break;
-
- c->sfxinfo->usefulness--;
- c->sfxinfo = 0;
- }
- }
- int S_AdjustSoundParams(mobj_t *listener, mobj_t *source,
- int *vol, int *sep, int *pitch)
- {
- fixed_t adx, ady,approx_dist;
- angle_t angle;
-
- if (!snd_card || nosfxparm)
- return 0;
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (!listener)
- return 0;
-
-
- adx = D_abs(listener->x - source->x);
- ady = D_abs(listener->y - source->y);
-
- approx_dist = adx + ady - ((adx < ady ? adx : ady)>>1);
- if (!approx_dist)
- {
- *sep = NORM_SEP;
- *vol = snd_SfxVolume * 8;
- return *vol > 0;
- }
- if (approx_dist > S_CLIPPING_DIST)
- return 0;
-
- angle = R_PointToAngle2(listener->x, listener->y, source->x, source->y);
- if (angle <= listener->angle)
- angle += 0xffffffff;
- angle -= listener->angle;
- angle >>= ANGLETOFINESHIFT;
-
- *sep = 128 - (FixedMul(S_STEREO_SWING,finesine[angle])>>FRACBITS);
-
- if (approx_dist < S_CLOSE_DIST)
- *vol = snd_SfxVolume*8;
- else
-
- *vol = (snd_SfxVolume * ((S_CLIPPING_DIST-approx_dist)>>FRACBITS) * 8)
- / S_ATTENUATOR;
- return (*vol > 0);
- }
- #ifndef IPHONE
- static int S_getChannel(void *origin, sfxinfo_t *sfxinfo, int is_pickup)
- {
-
- int cnum;
- channel_t *c;
-
- if (!snd_card || nosfxparm)
- return -1;
-
- 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;
- }
-
- if (cnum == numChannels)
- {
- for (cnum=0 ; cnum<numChannels ; cnum++)
- if (channels[cnum].sfxinfo->priority >= sfxinfo->priority)
- break;
- if (cnum == numChannels)
- return -1;
- else
- S_StopChannel(cnum);
- }
- c = &channels[cnum];
- c->sfxinfo = sfxinfo;
- c->origin = origin;
- c->is_pickup = is_pickup;
- return cnum;
- }
- #endif
|