needs to be seperate lump ive just hardcoded sounds we´ve found for now this whole thing sucks, but idk how else to detect things ideally would just move intro sounds to the sound system entirely instead of relying on the stupid play sfx line special, but oh well cant do anything about it now
2458 lines
57 KiB
C
2458 lines
57 KiB
C
// SONIC ROBO BLAST 2
|
|
//-----------------------------------------------------------------------------
|
|
// Copyright (C) 1993-1996 by id Software, Inc.
|
|
// Copyright (C) 1998-2000 by DooM Legacy Team.
|
|
// Copyright (C) 1999-2018 by Sonic Team Junior.
|
|
//
|
|
// This program is free software distributed under the
|
|
// terms of the GNU General Public License, version 2.
|
|
// See the 'LICENSE' file for more details.
|
|
//-----------------------------------------------------------------------------
|
|
/// \file s_sound.c
|
|
/// \brief System-independent sound and music routines
|
|
|
|
#include "d_netcmd.h"
|
|
#include "doomdef.h"
|
|
#include "doomstat.h"
|
|
#include "command.h"
|
|
#include "g_game.h"
|
|
#include "m_argv.h"
|
|
#include "r_main.h" // R_PointToAngle2() used to calc stereo sep.
|
|
#include "r_things.h" // for skins
|
|
#include "i_system.h"
|
|
#include "i_sound.h"
|
|
#include "s_sound.h"
|
|
#include "w_wad.h"
|
|
#include "z_zone.h"
|
|
#include "d_main.h"
|
|
#include "r_sky.h" // skyflatnum
|
|
#include "p_local.h" // camera info
|
|
#include "m_misc.h" // for tunes command
|
|
#include "m_cond.h" // for conditionsets
|
|
#include "m_menu.h" // bird music stuff
|
|
|
|
#include "lua_hook.h" // MusicChange hook
|
|
#include "lua_hud.h" // LUA_HudEnabled(hud_musiccredit)
|
|
|
|
static boolean S_AdjustSoundParams(const mobj_t *listener, const mobj_t *source, INT32 *vol, INT32 *sep, sfxinfo_t *sfxinfo);
|
|
static void SetChannelsNum(void);
|
|
static void Command_Tunes_f(void);
|
|
static void Command_RestartAudio_f(void);
|
|
static void Command_RestartMusic_f(void); //mhhhm amiga type filters here i come uwu
|
|
static void Command_ShowMusicCredit_f(void);
|
|
|
|
// Sound system toggles
|
|
static void GameSounds_OnChange(void);
|
|
static void GameMusic_OnChange(void);
|
|
|
|
static void SoundCache_OnChange(void);
|
|
static void BufferSize_OnChange(void);
|
|
|
|
#ifdef HAVE_OPENMPT
|
|
static void ModFilter_OnChange(void);
|
|
static void StereoSep_OnChange(void);
|
|
static void AmigaFilter_OnChange(void);
|
|
#if OPENMPT_API_VERSION_MAJOR < 1 && OPENMPT_API_VERSION_MINOR > 4
|
|
static void AmigaType_OnChange(void);
|
|
#endif
|
|
#endif
|
|
|
|
static CV_PossibleValue_t audbuffersize_cons_t[] = {{128, "128"}, {256, "256"}, {512, "512"}, {1024, "1024"}, {2048, "2048"}, {4096, "4096"}, {0, NULL}};
|
|
consvar_t cv_audbuffersize = {"audiobuffersize", "2048", CV_SAVE|CV_CALL, audbuffersize_cons_t, BufferSize_OnChange, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
// stereo reverse
|
|
consvar_t stereoreverse = {"stereoreverse", "Off", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
// if true, all sounds are loaded at game startup
|
|
static CV_PossibleValue_t cachesounds_cons_t[] = {{0, "Off"}, {1, "Keep"}, {2, "On"}, {0, NULL}};
|
|
consvar_t cv_cachesound = {"cachesound", "1", CV_SAVE|CV_CALL|CV_NOINIT, cachesounds_cons_t, SoundCache_OnChange, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
// actual general (maximum) sound & music volume, saved into the config
|
|
static CV_PossibleValue_t soundvolume_cons_t[] = {{0, "MIN"}, {31, "MAX"}, {0, NULL}};
|
|
consvar_t cv_soundvolume = {"soundvolume", "18", CV_SAVE, soundvolume_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_digmusicvolume = {"digmusicvolume", "18", CV_SAVE, soundvolume_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
#ifndef NO_MIDI
|
|
consvar_t cv_midimusicvolume = {"midimusicvolume", "18", CV_SAVE, soundvolume_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
#endif
|
|
|
|
// number of channels available
|
|
static CV_PossibleValue_t numChannels_cons_t[] = {{0, "MIN"}, {255, "MAX"}, {0, NULL}};
|
|
consvar_t cv_numChannels = {"snd_channels", "64", CV_SAVE|CV_CALL, numChannels_cons_t, SetChannelsNum, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
static CV_PossibleValue_t samesoundlimit_cons_t[] = {{0, "MIN"}, {64, "MAX"}, {0, NULL}};
|
|
consvar_t cv_samesoundlimit = {"samesoundlimit", "0", CV_SAVE, samesoundlimit_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
//consvar_t cv_resetmusic = {"resetmusic", "No", CV_SAVE|CV_NOSHOWHELP, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
// Sound system toggles, saved into the config
|
|
consvar_t cv_gamedigimusic = {"digimusic", "On", CV_SAVE|CV_CALL|CV_NOINIT, CV_OnOff, GameMusic_OnChange, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_gamesounds = {"sounds", "On", CV_SAVE|CV_CALL|CV_NOINIT, CV_OnOff, GameSounds_OnChange, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
consvar_t cv_playmusicifunfocused = {"playmusicifunfocused", "No", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_playsoundifunfocused = {"playsoundsifunfocused", "No", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
consvar_t cv_pausemusic = {"playmusicifpaused", "No", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
//bird music stuff
|
|
static CV_PossibleValue_t music_resync_threshold_cons_t[] = {
|
|
{0, "MIN"},
|
|
{1000, "MAX"},
|
|
{0}
|
|
};
|
|
consvar_t cv_music_resync_threshold = {"music_resync_threshold", "0", CV_SAVE|CV_CALL, music_resync_threshold_cons_t, I_UpdateSongLagThreshold, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_music_resync_powerups_only = {"music_resync_powerups_only", "No", CV_SAVE|CV_CALL, CV_YesNo, I_UpdateSongLagThreshold, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
consvar_t cv_invincmusicfade = {"invincmusicfade", "300", CV_SAVE, CV_Unsigned, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_growmusicfade = {"growmusicfade", "500", CV_SAVE, CV_Unsigned, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_respawnfademusicout = {"respawnfademusicout", "1000", CV_SAVE, CV_Unsigned, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_respawnfademusicback = {"respawnfademusicback", "500", CV_SAVE, CV_Unsigned, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_resetspecialmusic = {"resetspecialmusic", "No", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_resume = {"resume", "No", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_fading = {"fading", "Off", CV_SAVE|CV_CALL, CV_OnOff, Bird_menu_Onchange, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
consvar_t cv_keepmusic = {"keepmusic", "No", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_skipintromusic = {"skipintromusic", "No", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_ignoremusicchanges = {"ignoremusicchanges", "No", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
enum
|
|
{
|
|
SKPMUS_OFF,
|
|
SKPMUS_SKIP,
|
|
SKPMUS_PLAYMUS,
|
|
SKPMUS_PLAYSKIP,
|
|
};
|
|
|
|
boolean keepmapmusic = false; // keep the current music on map restart
|
|
UINT8 skipintromus = SKPMUS_OFF; // skip the intro fanfare
|
|
static music_t keepmusic;
|
|
|
|
#ifdef HAVE_OPENMPT
|
|
openmpt_module *openmpt_mhandle = NULL;
|
|
|
|
static CV_PossibleValue_t interpolationfilter_cons_t[] = {{0, "Default"}, {1, "None"}, {2, "Linear"}, {4, "Cubic"}, {8, "Windowed sinc"}, {0, NULL}};
|
|
consvar_t cv_modfilter = {"modfilter", "4", CV_SAVE|CV_CALL, interpolationfilter_cons_t, ModFilter_OnChange, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
static CV_PossibleValue_t stereosep_cons_t[] = {{0, "MIN"}, {200, "MAX"}, {0, NULL}};
|
|
consvar_t cv_stereosep = {"stereoseperation", "100", CV_SAVE|CV_CALL, stereosep_cons_t, StereoSep_OnChange, 0, NULL, NULL, 0, 0, NULL}; //some tracker modules have nauseously high stereo width
|
|
|
|
static CV_PossibleValue_t amigafilter_cons_t[] = {{0, "Off"}, {1, "On"}, {0, NULL}};
|
|
consvar_t cv_amigafilter = {"amigafilter", "1", CV_SAVE|CV_CALL, amigafilter_cons_t, AmigaFilter_OnChange, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
#if OPENMPT_API_VERSION_MAJOR < 1 && OPENMPT_API_VERSION_MINOR > 4
|
|
static CV_PossibleValue_t amigatype_cons_t[] = {{0, "auto"}, {1, "a500"}, {2, "a1200"}, {0, NULL}};
|
|
consvar_t cv_amigatype = {"amigatype", "0", CV_SAVE|CV_CALL|CV_NOINIT, amigatype_cons_t, AmigaType_OnChange, 0, NULL, NULL, 0, 0, NULL};
|
|
#endif
|
|
#endif
|
|
|
|
#define S_MAX_VOLUME 127
|
|
|
|
// when to clip out sounds
|
|
// Does not fit the large outdoor areas.
|
|
// added 2-2-98 in 8 bit volume control (before (1200*0x10000))
|
|
#define S_CLIPPING_DIST (1536*0x10000)
|
|
|
|
// Distance to origin when sounds should be maxed out.
|
|
// This should relate to movement clipping resolution
|
|
// (see BLOCKMAP handling).
|
|
// Originally: (200*0x10000).
|
|
// added 2-2-98 in 8 bit volume control (before (160*0x10000))
|
|
#define S_CLOSE_DIST (160*0x10000)
|
|
|
|
// added 2-2-98 in 8 bit volume control (before remove the +4)
|
|
#define S_ATTENUATOR ((S_CLIPPING_DIST-S_CLOSE_DIST)>>(FRACBITS+4))
|
|
|
|
// Adjustable by menu.
|
|
#define NORM_VOLUME snd_MaxVolume
|
|
|
|
#define NORM_PITCH 128
|
|
#define NORM_PRIORITY 64
|
|
#define NORM_SEP 128
|
|
|
|
#define S_PITCH_PERTURB 1
|
|
#define S_STEREO_SWING (96*0x10000)
|
|
|
|
// percent attenuation from front to back
|
|
#define S_IFRACVOL 30
|
|
|
|
typedef struct
|
|
{
|
|
// sound information (if null, channel avail.)
|
|
sfxinfo_t *sfxinfo;
|
|
|
|
// origin of sound
|
|
const void *origin;
|
|
|
|
// initial volume of sound, which is applied after distance and direction
|
|
INT32 volume;
|
|
|
|
// handle of the sound being played
|
|
INT32 handle;
|
|
|
|
} channel_t;
|
|
|
|
// the set of channels available
|
|
static channel_t *channels = NULL;
|
|
static INT32 numofchannels = 0;
|
|
|
|
//
|
|
// Internals.
|
|
//
|
|
static void S_StopChannel(INT32 cnum);
|
|
|
|
//
|
|
// S_getChannel
|
|
//
|
|
// If none available, return -1. Otherwise channel #.
|
|
//
|
|
static INT32 S_getChannel(const void *origin, sfxinfo_t *sfxinfo)
|
|
{
|
|
// channel number to use
|
|
INT32 cnum;
|
|
|
|
// Find an open channel
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
{
|
|
if (!channels[cnum].sfxinfo)
|
|
break;
|
|
|
|
const boolean samesoundplaying = channels[cnum].sfxinfo == sfxinfo;
|
|
|
|
// Now checks if same sound is being played, rather
|
|
// than just one sound per mobj
|
|
if (samesoundplaying && (sfxinfo->pitch & SF_NOMULTIPLESOUND))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
if (samesoundplaying && sfxinfo->singularity == true)
|
|
{
|
|
S_StopChannel(cnum);
|
|
break;
|
|
}
|
|
|
|
const boolean sameorigin = origin && channels[cnum].origin == origin;
|
|
|
|
if (sameorigin && samesoundplaying)
|
|
{
|
|
if (sfxinfo->pitch & SF_NOINTERRUPT)
|
|
return -1;
|
|
|
|
S_StopChannel(cnum);
|
|
break;
|
|
}
|
|
|
|
if (sameorigin
|
|
&& channels[cnum].sfxinfo->name != sfxinfo->name
|
|
&& (channels[cnum].sfxinfo->pitch & SF_TOTALLYSINGLE) && (sfxinfo->pitch & SF_TOTALLYSINGLE))
|
|
{
|
|
S_StopChannel(cnum);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// None available
|
|
if (cnum == numofchannels)
|
|
{
|
|
// Look for lower priority
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
if (channels[cnum].sfxinfo->priority <= sfxinfo->priority)
|
|
break;
|
|
|
|
if (cnum == numofchannels)
|
|
{
|
|
// No lower priority. Sorry, Charlie.
|
|
return -1;
|
|
}
|
|
else
|
|
{
|
|
// Otherwise, kick out lower priority.
|
|
S_StopChannel(cnum);
|
|
}
|
|
}
|
|
|
|
return cnum;
|
|
}
|
|
|
|
void S_RegisterSoundStuff(void)
|
|
{
|
|
if (dedicated)
|
|
{
|
|
sound_disabled = true;
|
|
return;
|
|
}
|
|
|
|
CV_RegisterVar(&stereoreverse);
|
|
CV_RegisterVar(&cv_cachesound);
|
|
|
|
CV_RegisterVar(&cv_gamesounds);
|
|
CV_RegisterVar(&cv_gamedigimusic);
|
|
|
|
CV_RegisterVar(&cv_samesoundlimit);
|
|
|
|
// bird music stuff
|
|
CV_RegisterVar(&cv_playmusicifunfocused);
|
|
CV_RegisterVar(&cv_playsoundifunfocused);
|
|
CV_RegisterVar(&cv_pausemusic);
|
|
|
|
CV_RegisterVar(&cv_music_resync_threshold);
|
|
CV_RegisterVar(&cv_music_resync_powerups_only);
|
|
|
|
CV_RegisterVar(&cv_invincmusicfade);
|
|
CV_RegisterVar(&cv_growmusicfade);
|
|
|
|
CV_RegisterVar(&cv_respawnfademusicout);
|
|
CV_RegisterVar(&cv_respawnfademusicback);
|
|
|
|
CV_RegisterVar(&cv_resetspecialmusic);
|
|
|
|
CV_RegisterVar(&cv_resume);
|
|
CV_RegisterVar(&cv_fading);
|
|
// bird music stuff end
|
|
|
|
CV_RegisterVar(&cv_keepmusic);
|
|
CV_RegisterVar(&cv_skipintromusic);
|
|
CV_RegisterVar(&cv_ignoremusicchanges);
|
|
|
|
COM_AddCommand("tunes", Command_Tunes_f);
|
|
COM_AddCommand("restartaudio", Command_RestartAudio_f);
|
|
COM_AddCommand("restartmusic", Command_RestartMusic_f);
|
|
COM_AddCommand("showmusiccredit", Command_ShowMusicCredit_f);
|
|
}
|
|
|
|
static void SetChannelsNum(void)
|
|
{
|
|
// Allocating the internal channels for mixing
|
|
// (the maximum number of sounds rendered
|
|
// simultaneously) within zone memory.
|
|
if (channels)
|
|
S_StopSounds();
|
|
|
|
Z_Free(channels);
|
|
channels = NULL;
|
|
|
|
if (cv_numChannels.value)
|
|
channels = (channel_t *)Z_Calloc(cv_numChannels.value * sizeof (channel_t), PU_STATIC, NULL);
|
|
numofchannels = (channels ? cv_numChannels.value : 0);
|
|
}
|
|
|
|
// Retrieve the lump number of sfx
|
|
//
|
|
lumpnum_t S_GetSfxLumpNum(sfxinfo_t *sfx)
|
|
{
|
|
char namebuf[9];
|
|
lumpnum_t sfxlump;
|
|
|
|
sprintf(namebuf, "ds%s", sfx->name);
|
|
|
|
sfxlump = W_CheckNumForName(namebuf);
|
|
if (sfxlump != LUMPERROR)
|
|
return sfxlump;
|
|
|
|
strlcpy(namebuf, sfx->name, sizeof(namebuf));
|
|
|
|
sfxlump = W_CheckNumForName(namebuf);
|
|
if (sfxlump != LUMPERROR)
|
|
return sfxlump;
|
|
|
|
return W_GetNumForName("dsthok");
|
|
}
|
|
|
|
//
|
|
// Sound Status
|
|
//
|
|
|
|
boolean S_SoundDisabled(void)
|
|
{
|
|
return (sound_disabled || (window_notinfocus && !cv_playsoundifunfocused.value));
|
|
}
|
|
|
|
int S_CacheSound(void)
|
|
{
|
|
if (sound_disabled)
|
|
return SOUNDCACHE_OFF;
|
|
|
|
if (M_CheckParm("-precachesound"))
|
|
return SOUNDCACHE_PRECACHE;
|
|
|
|
return cv_cachesound.value;
|
|
}
|
|
|
|
// Stop all sounds, load level info, THEN start sounds.
|
|
void S_StopSounds(void)
|
|
{
|
|
INT32 cnum;
|
|
|
|
// kill all playing sounds at start of level
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
if (channels[cnum].sfxinfo)
|
|
S_StopChannel(cnum);
|
|
}
|
|
|
|
void S_StopSoundByID(void *origin, sfxenum_t sfx_id)
|
|
{
|
|
INT32 cnum;
|
|
|
|
// Sounds without origin can have multiple sources, they shouldn't
|
|
// be stopped by new sounds.
|
|
if (!origin)
|
|
return;
|
|
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
{
|
|
if (channels[cnum].sfxinfo == &S_sfx[sfx_id] && channels[cnum].origin == origin)
|
|
{
|
|
S_StopChannel(cnum);
|
|
}
|
|
}
|
|
}
|
|
|
|
void S_StopSoundByNum(sfxenum_t sfxnum)
|
|
{
|
|
INT32 cnum;
|
|
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
{
|
|
if (channels[cnum].sfxinfo == &S_sfx[sfxnum])
|
|
{
|
|
S_StopChannel(cnum);
|
|
}
|
|
}
|
|
}
|
|
|
|
static INT32 S_ScaleVolumeWithSplitscreen(INT32 volume)
|
|
{
|
|
fixed_t root = INT32_MAX;
|
|
|
|
if (splitscreen == 0)
|
|
{
|
|
return volume;
|
|
}
|
|
|
|
root = FixedSqrt((splitscreen + 1) * (FRACUNIT/3));
|
|
|
|
return FixedDiv(volume * FRACUNIT, root) / FRACUNIT;
|
|
}
|
|
|
|
static boolean S_CheckSameSoundLimit(sfxenum_t sfx_id)
|
|
{
|
|
INT32 cnum, scount = 0;
|
|
|
|
if (!cv_samesoundlimit.value)
|
|
return true;
|
|
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
if ((size_t)(channels[cnum].sfxinfo - S_sfx) == (size_t)sfx_id)
|
|
scount++;
|
|
|
|
//CONS_Printf("same sound count: %d for sfx: %d\n", scount, sfx_id);
|
|
|
|
if (scount >= cv_samesoundlimit.value)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
void S_StartSoundAtVolume(const void *origin_p, sfxenum_t sfx_id, INT32 volume)
|
|
{
|
|
const mobj_t *origin = (const mobj_t *)origin_p;
|
|
const boolean reverse = (stereoreverse.value ^ encoremode);
|
|
INT32 initial_volume;
|
|
|
|
sfxinfo_t *sfx;
|
|
INT32 sep, cnum;
|
|
boolean anyListeners = false;
|
|
boolean itsUs = false;
|
|
INT32 i;
|
|
|
|
listener_t listener[MAXSPLITSCREENPLAYERS];
|
|
mobj_t *listenmobj[MAXSPLITSCREENPLAYERS];
|
|
|
|
if (S_SoundDisabled() || !sound_started)
|
|
return;
|
|
|
|
// Don't want a sound? Okay then...
|
|
if (sfx_id == sfx_None)
|
|
return;
|
|
|
|
// reached same sound limit?
|
|
if (!S_CheckSameSoundLimit(sfx_id))
|
|
return;
|
|
|
|
memset(listener, 0, sizeof(listener));
|
|
memset(listenmobj, 0, sizeof(listenmobj));
|
|
|
|
for (i = 0; i <= splitscreen; i++)
|
|
{
|
|
player_t *player = &players[displayplayers[i]];
|
|
|
|
if (!player)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (player->awayviewtics)
|
|
{
|
|
listenmobj[i] = player->awayviewmobj;
|
|
}
|
|
else
|
|
{
|
|
listenmobj[i] = player->mo;
|
|
}
|
|
|
|
if (origin && origin == listenmobj[i] && !camera[i].freecam)
|
|
{
|
|
itsUs = true;
|
|
}
|
|
|
|
if (camera[i].chase && !player->awayviewtics)
|
|
{
|
|
listener[i].x = camera[i].x;
|
|
listener[i].y = camera[i].y;
|
|
listener[i].z = camera[i].z;
|
|
listener[i].angle = camera[i].angle;
|
|
anyListeners = true;
|
|
}
|
|
else if (listenmobj[i])
|
|
{
|
|
listener[i].x = listenmobj[i]->x;
|
|
listener[i].y = listenmobj[i]->y;
|
|
listener[i].z = listenmobj[i]->z;
|
|
listener[i].angle = listenmobj[i]->angle;
|
|
anyListeners = true;
|
|
}
|
|
}
|
|
|
|
if (origin && anyListeners == false)
|
|
{
|
|
// If a mobj is trying to make a noise, and no one is around to hear it, does it make a sound?
|
|
return;
|
|
}
|
|
|
|
// check for bogus sound #
|
|
I_Assert(sfx_id >= 1);
|
|
I_Assert(sfx_id < NUMSFX);
|
|
|
|
sfx = &S_sfx[sfx_id];
|
|
|
|
if (sfx->skinsound != -1 && origin && origin->skin)
|
|
{
|
|
// redirect player sound to the sound in the skin table
|
|
sfx_id = K_GetMobjSkin(origin)->soundsid[sfx->skinsound];
|
|
sfx = &S_sfx[sfx_id];
|
|
}
|
|
|
|
// i seriously get an aneurism by this shitty egg
|
|
if (fastncmp(sfx->name, "DNLT", 4))
|
|
return;
|
|
|
|
initial_volume = (origin ? S_ScaleVolumeWithSplitscreen(volume) : volume);
|
|
|
|
// Initialize sound parameters
|
|
sep = NORM_SEP;
|
|
|
|
i = 0; // sensible default
|
|
|
|
// Check to see if it is audible, and if not, modify the params
|
|
if (origin && !itsUs)
|
|
{
|
|
boolean audible = false;
|
|
|
|
if (splitscreen > 0)
|
|
{
|
|
fixed_t recdist = INT32_MAX;
|
|
UINT8 j = 0;
|
|
|
|
for (; j <= splitscreen; j++)
|
|
{
|
|
fixed_t thisdist = INT32_MAX;
|
|
|
|
if (!listenmobj[j])
|
|
{
|
|
continue;
|
|
}
|
|
|
|
thisdist = P_AproxDistance(listener[j].x - origin->x, listener[j].y - origin->y);
|
|
|
|
if (thisdist >= recdist)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
recdist = thisdist;
|
|
i = j;
|
|
}
|
|
}
|
|
|
|
if (listenmobj[i])
|
|
{
|
|
audible = S_AdjustSoundParams(listenmobj[i], origin, &volume, &sep, sfx);
|
|
}
|
|
|
|
if (!audible)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
// This is supposed to handle the loading/caching.
|
|
// For some odd reason, the caching is done nearly
|
|
// each time the sound is needed?
|
|
|
|
// cache data if necessary
|
|
// NOTE: set sfx->data NULL sfx->lump -1 to force a reload
|
|
if (!sfx->data)
|
|
{
|
|
sfx->data = I_GetSfx(sfx);
|
|
|
|
if (!sfx->data)
|
|
{
|
|
CONS_Alert(CONS_WARNING, "Tried to load invalid sfx_%s\n", sfx->name);
|
|
return; /* don't play it */
|
|
}
|
|
}
|
|
|
|
// Avoid channel reverse if surround
|
|
if (reverse)
|
|
{
|
|
sep = (~sep) & 255;
|
|
}
|
|
|
|
// At this point it is determined that a sound can and should be played, so find a free channel to play it on
|
|
cnum = S_getChannel(origin, sfx);
|
|
|
|
if (cnum < 0)
|
|
{
|
|
return; // If there's no free channels, there won't be any for anymore players either
|
|
}
|
|
|
|
// Now that we know we are going to play a sound, fill out this info
|
|
channels[cnum].sfxinfo = sfx;
|
|
channels[cnum].origin = origin;
|
|
channels[cnum].volume = initial_volume;
|
|
channels[cnum].handle = I_StartSound(sfx_id, volume, sep, cnum);
|
|
}
|
|
|
|
void S_StartSound(const void *origin, sfxenum_t sfx_id)
|
|
{
|
|
if (S_SoundDisabled())
|
|
return;
|
|
|
|
// the volume is handled 8 bits
|
|
S_StartSoundAtVolume(origin, sfx_id, 255);
|
|
}
|
|
|
|
void S_StopSound(void *origin)
|
|
{
|
|
INT32 cnum;
|
|
|
|
// Sounds without origin can have multiple sources, they shouldn't
|
|
// be stopped by new sounds.
|
|
if (!origin)
|
|
return;
|
|
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
{
|
|
if (channels[cnum].sfxinfo && channels[cnum].origin == origin)
|
|
{
|
|
S_StopChannel(cnum);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// Updates music & sounds
|
|
//
|
|
static INT32 actualsfxvolume; // check for change through console
|
|
static INT32 actualmusicvolume;
|
|
|
|
void S_UpdateSounds(void)
|
|
{
|
|
INT32 cnum, volume, sep;
|
|
boolean audible = false;
|
|
channel_t *c;
|
|
INT32 i;
|
|
|
|
listener_t listener[MAXSPLITSCREENPLAYERS];
|
|
mobj_t *listenmobj[MAXSPLITSCREENPLAYERS];
|
|
|
|
#ifndef NO_MIDI
|
|
const boolean currentmidi = (I_SongType() == MU_MID);
|
|
#endif
|
|
|
|
// Update sound/music volumes, if changed manually at console
|
|
if (actualsfxvolume != cv_soundvolume.value)
|
|
S_SetSfxVolume(cv_soundvolume.value);
|
|
#ifndef NO_MIDI
|
|
if (actualmusicvolume != (currentmidi ? cv_midimusicvolume.value : cv_digmusicvolume.value))
|
|
#else
|
|
if (actualmusicvolume != cv_digmusicvolume.value)
|
|
#endif
|
|
S_SetMusicVolume(-1);
|
|
|
|
memset(listener, 0, sizeof(listener));
|
|
|
|
// We're done now, if we're not in a level.
|
|
if (gamestate != GS_LEVEL)
|
|
{
|
|
#ifndef NOMUMBLE
|
|
// Stop Mumble cutting out. I'm sick of it.
|
|
I_UpdateMumble(NULL, listener[0]);
|
|
#endif
|
|
|
|
I_UpdateSound();
|
|
return;
|
|
}
|
|
|
|
if (dedicated || sound_disabled)
|
|
return;
|
|
|
|
memset(listenmobj, 0, sizeof(listenmobj));
|
|
|
|
for (i = 0; i <= splitscreen; i++)
|
|
{
|
|
player_t *player = &players[displayplayers[i]];
|
|
|
|
if (!player)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (player->awayviewtics)
|
|
{
|
|
listenmobj[i] = player->awayviewmobj;
|
|
}
|
|
else
|
|
{
|
|
listenmobj[i] = player->mo;
|
|
}
|
|
|
|
if (camera[i].chase && !player->awayviewtics)
|
|
{
|
|
listener[i].x = camera[i].x;
|
|
listener[i].y = camera[i].y;
|
|
listener[i].z = camera[i].z;
|
|
listener[i].angle = camera[i].angle;
|
|
}
|
|
else if (listenmobj[i])
|
|
{
|
|
listener[i].x = listenmobj[i]->x;
|
|
listener[i].y = listenmobj[i]->y;
|
|
listener[i].z = listenmobj[i]->z;
|
|
listener[i].angle = listenmobj[i]->angle;
|
|
}
|
|
}
|
|
|
|
#ifndef NOMUMBLE
|
|
I_UpdateMumble(players[consoleplayer].mo, listener[0]);
|
|
#endif
|
|
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
{
|
|
c = &channels[cnum];
|
|
|
|
if (c->sfxinfo == NULL)
|
|
continue;
|
|
|
|
// if channel is allocated but sound has stopped, free it
|
|
if (!I_SoundIsPlaying(c->handle))
|
|
{
|
|
S_StopChannel(cnum);
|
|
continue;
|
|
}
|
|
|
|
if (c->origin == NULL)
|
|
continue;
|
|
|
|
// initialize parameters
|
|
volume = c->volume; // 8 bits internal volume precision
|
|
sep = NORM_SEP;
|
|
|
|
// check non-local sounds for distance clipping
|
|
// or modify their params
|
|
boolean itsUs = false;
|
|
|
|
for (i = splitscreen; i >= 0; i--)
|
|
{
|
|
if (camera[i].freecam)
|
|
continue;
|
|
|
|
if (c->origin != listenmobj[i])
|
|
continue;
|
|
|
|
itsUs = true;
|
|
}
|
|
|
|
if (itsUs == false)
|
|
{
|
|
const mobj_t *origin = c->origin;
|
|
|
|
i = 0;
|
|
|
|
if (splitscreen > 0)
|
|
{
|
|
fixed_t recdist = INT32_MAX;
|
|
UINT8 j = 0;
|
|
|
|
for (; j <= splitscreen; j++)
|
|
{
|
|
fixed_t thisdist = INT32_MAX;
|
|
|
|
if (!listenmobj[j])
|
|
{
|
|
continue;
|
|
}
|
|
|
|
thisdist = P_AproxDistance(listener[j].x - origin->x, listener[j].y - origin->y);
|
|
|
|
if (thisdist >= recdist)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
recdist = thisdist;
|
|
i = j;
|
|
}
|
|
}
|
|
|
|
if (listenmobj[i])
|
|
{
|
|
audible = S_AdjustSoundParams(
|
|
listenmobj[i], c->origin,
|
|
&volume, &sep,
|
|
c->sfxinfo
|
|
);
|
|
}
|
|
|
|
if (audible)
|
|
I_UpdateSoundParams(c->handle, volume, sep);
|
|
else
|
|
S_StopChannel(cnum);
|
|
}
|
|
}
|
|
|
|
I_UpdateSound();
|
|
}
|
|
|
|
void S_SetSfxVolume(INT32 volume)
|
|
{
|
|
if (volume < 0 || volume > 31)
|
|
CONS_Alert(CONS_WARNING, "sfxvolume should be between 0-31\n");
|
|
|
|
CV_SetValue(&cv_soundvolume, volume&0x1F);
|
|
actualsfxvolume = cv_soundvolume.value; // check for change of var
|
|
|
|
// now hardware volume
|
|
I_SetSfxVolume(volume&0x1F);
|
|
}
|
|
|
|
void S_ClearSfx(void)
|
|
{
|
|
size_t i;
|
|
for (i = 1; i < NUMSFX; i++)
|
|
I_FreeSfx(S_sfx + i);
|
|
}
|
|
|
|
static void S_StopChannel(INT32 cnum)
|
|
{
|
|
channel_t *c = &channels[cnum];
|
|
|
|
if (c->sfxinfo)
|
|
{
|
|
// stop the sound playing
|
|
if (I_SoundIsPlaying(c->handle))
|
|
I_StopSound(c->handle);
|
|
|
|
c->sfxinfo = NULL;
|
|
}
|
|
|
|
c->origin = NULL;
|
|
}
|
|
|
|
//
|
|
// S_CalculateSoundDistance
|
|
//
|
|
// Calculates the distance between two points for a sound.
|
|
// Clips the distance to prevent overflow.
|
|
//
|
|
fixed_t S_CalculateSoundDistance(fixed_t sx1, fixed_t sy1, fixed_t sz1, fixed_t sx2, fixed_t sy2, fixed_t sz2)
|
|
{
|
|
fixed_t approx_dist, adx, ady;
|
|
|
|
// calculate the distance to sound origin and clip it if necessary
|
|
adx = abs((sx1>>FRACBITS) - (sx2>>FRACBITS));
|
|
ady = abs((sy1>>FRACBITS) - (sy2>>FRACBITS));
|
|
|
|
// From _GG1_ p.428. Approx. euclidian distance fast.
|
|
// Take Z into account
|
|
adx = adx + ady - ((adx < ady ? adx : ady)>>1);
|
|
ady = abs((sz1>>FRACBITS) - (sz2>>FRACBITS));
|
|
approx_dist = adx + ady - ((adx < ady ? adx : ady)>>1);
|
|
|
|
if (approx_dist >= FRACUNIT/2)
|
|
approx_dist = FRACUNIT/2-1;
|
|
|
|
approx_dist <<= FRACBITS;
|
|
|
|
return FixedDiv(approx_dist, mapobjectscale); // approx_dist
|
|
}
|
|
|
|
//
|
|
// 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.
|
|
//
|
|
static boolean S_AdjustSoundParams(const mobj_t *listener, const mobj_t *source, INT32 *vol, INT32 *sep, sfxinfo_t *sfxinfo)
|
|
{
|
|
const boolean reverse = (stereoreverse.value ^ encoremode);
|
|
|
|
fixed_t approx_dist;
|
|
|
|
listener_t listensource;
|
|
INT32 i;
|
|
|
|
// e6y
|
|
if (!listener)
|
|
return false;
|
|
|
|
// Init listensource with default listener
|
|
listensource.x = listener->x;
|
|
listensource.y = listener->y;
|
|
listensource.z = listener->z;
|
|
listensource.angle = listener->angle;
|
|
|
|
for (i = 0; i <= splitscreen; i++)
|
|
{
|
|
// If listener is a chasecam player, use the camera instead
|
|
if (listener == players[displayplayers[i]].mo && camera[i].chase)
|
|
{
|
|
listensource.x = camera[i].x;
|
|
listensource.y = camera[i].y;
|
|
listensource.z = camera[i].z;
|
|
listensource.angle = camera[i].angle;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (sfxinfo->pitch & SF_OUTSIDESOUND) // Rain special case
|
|
{
|
|
INT64 x, y, yl, yh, xl, xh;
|
|
fixed_t newdist;
|
|
|
|
if (R_PointInSubsectorFast(listensource.x, listensource.y)->sector->ceilingpic == skyflatnum)
|
|
approx_dist = 0;
|
|
else
|
|
{
|
|
// Essentially check in a 1024 unit radius of the player for an outdoor area.
|
|
#define RADIUSSTEP (64*FRACUNIT)
|
|
#define SEARCHRADIUS (16*RADIUSSTEP)
|
|
yl = listensource.y - SEARCHRADIUS;
|
|
yh = listensource.y + SEARCHRADIUS;
|
|
xl = listensource.x - SEARCHRADIUS;
|
|
xh = listensource.x + SEARCHRADIUS;
|
|
approx_dist = SEARCHRADIUS;
|
|
#undef SEARCHRADIUS
|
|
for (y = yl; y <= yh; y += RADIUSSTEP)
|
|
for (x = xl; x <= xh; x += RADIUSSTEP)
|
|
{
|
|
if (R_PointInSubsectorFast(x, y)->sector->ceilingpic == skyflatnum)
|
|
{
|
|
// Found the outdoors!
|
|
newdist = S_CalculateSoundDistance(listensource.x, listensource.y, 0, x, y, 0);
|
|
if (newdist < approx_dist)
|
|
{
|
|
approx_dist = newdist;
|
|
}
|
|
}
|
|
}
|
|
#undef RADIUSSTEP
|
|
}
|
|
}
|
|
else
|
|
{
|
|
approx_dist = S_CalculateSoundDistance(listensource.x, listensource.y, listensource.z,
|
|
source->x, source->y, source->z);
|
|
}
|
|
|
|
// Ring loss, deaths, etc, should all be heard louder.
|
|
if (sfxinfo->pitch & SF_X8AWAYSOUND)
|
|
approx_dist = FixedDiv(approx_dist, 8*FRACUNIT);
|
|
|
|
// Combine 8XAWAYSOUND with 4XAWAYSOUND and get.... 32XAWAYSOUND?
|
|
if (sfxinfo->pitch & SF_X4AWAYSOUND)
|
|
approx_dist = FixedDiv(approx_dist, 4*FRACUNIT);
|
|
|
|
if (sfxinfo->pitch & SF_X2AWAYSOUND)
|
|
approx_dist = FixedDiv(approx_dist, 2*FRACUNIT);
|
|
|
|
if (approx_dist > S_CLIPPING_DIST)
|
|
return false;
|
|
|
|
if (source->x == listensource.x && source->y == listensource.y)
|
|
{
|
|
*sep = NORM_SEP;
|
|
}
|
|
else
|
|
{
|
|
// angle of source to listener
|
|
angle_t angle = R_PointToAngle2(listensource.x, listensource.y, source->x, source->y);
|
|
|
|
if (angle > listensource.angle)
|
|
angle = angle - listensource.angle;
|
|
else
|
|
angle = angle + InvAngle(listensource.angle);
|
|
|
|
if (reverse)
|
|
angle = InvAngle(angle);
|
|
|
|
angle >>= ANGLETOFINESHIFT;
|
|
|
|
// stereo separation
|
|
*sep = 128 - (FixedMul(S_STEREO_SWING, FINESINE(angle))>>FRACBITS);
|
|
}
|
|
|
|
// volume calculation
|
|
/* not sure if it should be > (no =), but this matches the old behavior */
|
|
if (approx_dist >= S_CLOSE_DIST)
|
|
{
|
|
// distance effect
|
|
const INT32 n = (15 * ((S_CLIPPING_DIST - approx_dist)>>FRACBITS));
|
|
*vol = FixedMul(*vol * FRACUNIT / 255, n) / S_ATTENUATOR;
|
|
}
|
|
|
|
return (*vol > 0);
|
|
}
|
|
|
|
// Searches through the channels and checks if a sound is playing
|
|
// on the given origin.
|
|
INT32 S_OriginPlaying(void *origin)
|
|
{
|
|
INT32 cnum;
|
|
|
|
if (!origin)
|
|
return false;
|
|
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
if (channels[cnum].origin == origin)
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Searches through the channels and checks if a given id
|
|
// is playing anywhere.
|
|
INT32 S_IdPlaying(sfxenum_t id)
|
|
{
|
|
INT32 cnum;
|
|
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
if ((size_t)(channels[cnum].sfxinfo - S_sfx) == (size_t)id)
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Searches through the channels and checks for
|
|
// origin x playing sound id y.
|
|
INT32 S_SoundPlaying(void *origin, sfxenum_t id)
|
|
{
|
|
INT32 cnum;
|
|
|
|
if (!origin)
|
|
return 0;
|
|
|
|
for (cnum = 0; cnum < numofchannels; cnum++)
|
|
{
|
|
if (channels[cnum].origin == origin
|
|
&& (size_t)(channels[cnum].sfxinfo - S_sfx) == (size_t)id)
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
//
|
|
// S_StartSoundName
|
|
// Starts a sound using the given name.
|
|
#define MAXNEWSOUNDS 10
|
|
static sfxenum_t newsounds[MAXNEWSOUNDS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
void S_StartSoundName(void *mo, const char *soundname)
|
|
{
|
|
INT32 i, soundnum = 0;
|
|
|
|
// Search existing sounds...
|
|
for (i = sfx_None + 1; i < NUMSFX; i++)
|
|
{
|
|
const sfxinfo_t *sfx = &S_sfx[i];
|
|
|
|
if (!sfx->name)
|
|
continue;
|
|
|
|
if (fasticmp(sfx->name, soundname))
|
|
{
|
|
soundnum = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!soundnum)
|
|
{
|
|
for (i = 0; i < MAXNEWSOUNDS; i++)
|
|
{
|
|
if (newsounds[i] == 0)
|
|
break;
|
|
|
|
if (!S_IdPlaying(newsounds[i]))
|
|
{
|
|
S_RemoveSoundFx(newsounds[i]);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (i == MAXNEWSOUNDS)
|
|
{
|
|
CONS_Debug(DBG_GAMELOGIC, "Cannot load another extra sound!\n");
|
|
return;
|
|
}
|
|
|
|
soundnum = S_AddSoundFx(soundname, false, 0, false);
|
|
newsounds[i] = soundnum;
|
|
}
|
|
|
|
S_StartSound(mo, soundnum);
|
|
}
|
|
|
|
//
|
|
// Initializes sound stuff, including volume
|
|
// Sets channels, SFX volume,
|
|
// allocates channel buffer, sets S_sfx lookup.
|
|
//
|
|
void S_InitSfxChannels(INT32 sfxVolume)
|
|
{
|
|
INT32 i;
|
|
|
|
if (dedicated)
|
|
return;
|
|
|
|
S_SetSfxVolume(sfxVolume);
|
|
|
|
SetChannelsNum();
|
|
|
|
// Precache sounds if requested
|
|
if (S_CacheSound() == SOUNDCACHE_PRECACHE)
|
|
{
|
|
// Initialize external data (all sounds) at start, keep static.
|
|
CONS_Printf(M_GetText("Pre-caching sounds..."));
|
|
|
|
// NUMSFX is a big number, so merge the loops
|
|
for (i = 1; i < NUMSFX; i++)
|
|
{
|
|
S_sfx[i].lumpnum = LUMPERROR;
|
|
|
|
if (S_sfx[i].data)
|
|
continue;
|
|
|
|
if ((i < sfx_freeslot0 && S_sfx[i].name) ||
|
|
(i >= sfx_freeslot0 && S_sfx[i].priority))
|
|
S_sfx[i].data = I_GetSfx(&S_sfx[i]);
|
|
}
|
|
|
|
CONS_Printf(M_GetText("...pre-cached all sound data\n"));
|
|
}
|
|
else
|
|
{
|
|
// Note that sounds have not been cached (yet).
|
|
for (i = 1; i < NUMSFX; i++)
|
|
{
|
|
S_sfx[i].lumpnum = LUMPERROR;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// ------------------------
|
|
/// Music
|
|
/// ------------------------
|
|
|
|
#ifdef MUSICSLOT_COMPATIBILITY
|
|
const char *compat_special_music_slots[16] =
|
|
{
|
|
"titles", // 1036 title screen
|
|
"read_m", // 1037 intro
|
|
"lclear", // 1038 level clear
|
|
"invinc", // 1039 invincibility
|
|
"shoes", // 1040 super sneakers
|
|
"minvnc", // 1041 Mario invincibility
|
|
"drown", // 1042 drowning
|
|
"gmover", // 1043 game over
|
|
"xtlife", // 1044 extra life
|
|
"contsc", // 1045 continue screen
|
|
"supers", // 1046 Super Sonic
|
|
"chrsel", // 1047 character select
|
|
"credit", // 1048 credits
|
|
"racent", // 1049 Race Results
|
|
"stjr", // 1050 Sonic Team Jr. Presents
|
|
""
|
|
};
|
|
#endif
|
|
|
|
static music_t music;
|
|
static music_t queue;
|
|
|
|
static consvar_t *music_refade_cv;
|
|
|
|
/// ------------------------
|
|
/// Music Definitions
|
|
/// ------------------------
|
|
|
|
// Similar system to vissprite allocation. Exists so external pointers to musicdefs are not invalidated
|
|
// when allocating more space for musicdefs
|
|
#define MUSICDEFCHUNKBITS 6
|
|
#define MUSICDEFSPERCHUNK (1 << MUSICDEFCHUNKBITS)
|
|
#define MUSICDEFINDEXMASK (MUSICDEFSPERCHUNK - 1)
|
|
static musicdef_t **musicdefs = NULL;
|
|
static INT32 numchunks = 0;
|
|
INT32 nummusicdefs = 0;
|
|
|
|
//
|
|
// S_GetMusicCredit
|
|
//
|
|
// Return music credit with given index.
|
|
//
|
|
musicdef_t *S_GetMusicCredit(INT32 i)
|
|
{
|
|
if (i < 0 || i >= nummusicdefs)
|
|
return NULL;
|
|
|
|
INT32 chunk = i >> MUSICDEFCHUNKBITS;
|
|
i &= MUSICDEFINDEXMASK;
|
|
|
|
return &musicdefs[chunk][i];
|
|
}
|
|
|
|
//
|
|
// S_AddMusicCredit
|
|
//
|
|
// Return new music credit, allocates memory for it if needed.
|
|
//
|
|
static musicdef_t *S_AddMusicCredit(void)
|
|
{
|
|
INT32 chunk = nummusicdefs >> MUSICDEFCHUNKBITS;
|
|
INT32 i = nummusicdefs & MUSICDEFINDEXMASK;
|
|
|
|
// Allocate new chunk if needed. Other chunks stay valid
|
|
if (chunk == numchunks)
|
|
{
|
|
++numchunks;
|
|
musicdefs = (musicdef_t**)Z_Realloc(musicdefs, sizeof(musicdef_t*)*numchunks, PU_STATIC, NULL);
|
|
musicdefs[chunk] = Z_Calloc(sizeof(musicdef_t)*MUSICDEFSPERCHUNK, PU_STATIC, NULL);
|
|
}
|
|
|
|
// Store "id" of new musicdef (mostly exists only for lua)
|
|
musicdefs[chunk][i].num = nummusicdefs;
|
|
|
|
++nummusicdefs;
|
|
|
|
return &musicdefs[chunk][i];
|
|
}
|
|
|
|
struct cursongcredit cursongcredit = {}; // Currently displayed song credit info
|
|
|
|
static boolean ReadMusicDefFields(UINT16 wadnum, int line, char *stoken, musicdef_t **defp)
|
|
{
|
|
musicdef_t *def;
|
|
|
|
char *value;
|
|
char *textline;
|
|
|
|
if (fasticmp(stoken, "lump"))
|
|
{
|
|
value = strtok(NULL, " ");
|
|
|
|
if (!value)
|
|
{
|
|
CONS_Alert(CONS_WARNING,
|
|
"MUSICDEF: Field '%s' is missing name. (file %s, line %d)\n",
|
|
stoken, wadfiles[wadnum]->filename, line);
|
|
(void)strtok(NULL, " ");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
def = S_FindMusicCredit(value);
|
|
|
|
// Nothing found, add to the end.
|
|
if (!def)
|
|
{
|
|
def = S_AddMusicCredit();
|
|
|
|
STRBUFCPY(def->name, value);
|
|
strlwr(def->name);
|
|
def->hash = FNV1a_QuickCaseHash(def->name, 6);
|
|
}
|
|
|
|
(*defp) = def;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
value = strtok(NULL, "");
|
|
|
|
if (value)
|
|
{
|
|
// Find the equals sign.
|
|
value = strchr(value, '=');
|
|
}
|
|
|
|
if (!value)
|
|
{
|
|
CONS_Alert(CONS_WARNING,
|
|
"MUSICDEF: Field '%s' is missing value. (file %s, line %d)\n",
|
|
stoken, wadfiles[wadnum]->filename, line);
|
|
(void)strtok(NULL, " ");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
def = (*defp);
|
|
|
|
if (!def)
|
|
{
|
|
CONS_Alert(CONS_ERROR,
|
|
"MUSICDEF: No music definition before field '%s'. (file %s, line %d)\n",
|
|
stoken, wadfiles[wadnum]->filename, line);
|
|
return false;
|
|
}
|
|
|
|
// Skip the equals sign.
|
|
value++;
|
|
|
|
// Now skip funny whitespace.
|
|
value += strspn(value, "\t ");
|
|
|
|
textline = value;
|
|
|
|
// turn _ into spaces.
|
|
#define ADDDEF(field)\
|
|
STRBUFCPY(def->field, textline);\
|
|
for (textline = def->field; *textline; textline++)\
|
|
if (*textline == '_') *textline = ' ';
|
|
|
|
if (fasticmp(stoken, "usage"))
|
|
{
|
|
ADDDEF(usage);
|
|
}
|
|
else if (fasticmp(stoken, "source"))
|
|
{
|
|
ADDDEF(source);
|
|
}
|
|
else if (fasticmp(stoken, "title"))
|
|
{
|
|
def->use_info = true;
|
|
ADDDEF(title);
|
|
}
|
|
else if (fasticmp(stoken, "alttitle"))
|
|
{
|
|
ADDDEF(alttitle);
|
|
}
|
|
else if (fasticmp(stoken, "authors"))
|
|
{
|
|
ADDDEF(authors);
|
|
}
|
|
else
|
|
CONS_Alert(CONS_WARNING, "MUSICDEF: Invalid field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line);
|
|
#undef ADDDEF
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void S_LoadMusicDefs(UINT16 wadnum)
|
|
{
|
|
UINT16 lumpnum;
|
|
char *lump;
|
|
char *musdeftext;
|
|
size_t size;
|
|
|
|
char *lf;
|
|
char *stoken;
|
|
|
|
size_t nlf;
|
|
size_t ncr;
|
|
|
|
musicdef_t *def = NULL;
|
|
int line = 1; // for better error msgs
|
|
|
|
for (int k = 0; k < 2; k++)
|
|
{
|
|
const char *lumpname = (k == 1 ? "MUSCINFO" : "MUSICDEF"); //check for MUSCINFO lump on 2nd iteration
|
|
lumpnum = W_CheckNumForNamePwad(lumpname, wadnum, 0);
|
|
|
|
if (lumpnum == INT16_MAX)
|
|
continue;
|
|
|
|
lump = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE);
|
|
size = W_LumpLengthPwad(wadnum, lumpnum);
|
|
|
|
// Null-terminated MUSICDEF lump.
|
|
musdeftext = malloc(size+1);
|
|
if (!musdeftext)
|
|
I_Error("S_LoadMusicDefs: No more free memory for the parser\n");
|
|
memcpy(musdeftext, lump, size);
|
|
musdeftext[size] = '\0';
|
|
|
|
// Find music def
|
|
stoken = musdeftext;
|
|
for (;;)
|
|
{
|
|
lf = strpbrk(stoken, "\r\n");
|
|
if (lf)
|
|
{
|
|
if (*lf == '\n')
|
|
nlf = 1;
|
|
else
|
|
nlf = 0;
|
|
*lf++ = '\0'; /* now we can delimit to here */
|
|
}
|
|
|
|
stoken = strtok(stoken, " ");
|
|
if (stoken)
|
|
{
|
|
if (!ReadMusicDefFields(wadnum, line, stoken, &def))
|
|
break;
|
|
}
|
|
|
|
if (lf)
|
|
{
|
|
do
|
|
{
|
|
line += nlf;
|
|
ncr = strspn(lf, "\r");/* skip CR */
|
|
lf += ncr;
|
|
nlf = strspn(lf, "\n");
|
|
lf += nlf;
|
|
}
|
|
while (nlf || ncr) ;
|
|
|
|
stoken = lf; /* now the next nonempty line */
|
|
}
|
|
else
|
|
break; /* EOF */
|
|
}
|
|
|
|
free(musdeftext);
|
|
}
|
|
}
|
|
|
|
//
|
|
// S_InitMusicDefs
|
|
//
|
|
// Simply load music defs in all wads.
|
|
//
|
|
void S_InitMusicDefs(void)
|
|
{
|
|
UINT16 i;
|
|
for (i = 0; i < numwadfiles; i++)
|
|
S_LoadMusicDefs(i);
|
|
}
|
|
|
|
//
|
|
// S_FindMusicCredit
|
|
//
|
|
// Returns musicdef of specified song, or null if musicdef for it doesn't exist
|
|
//
|
|
musicdef_t *S_FindMusicCredit(const char *musname)
|
|
{
|
|
UINT32 hash = FNV1a_QuickCaseHash(musname, 6);
|
|
musicdef_t *def;
|
|
|
|
for (INT32 i = 0; i < nummusicdefs; ++i)
|
|
{
|
|
def = S_GetMusicCredit(i);
|
|
|
|
if (!def)
|
|
continue;
|
|
|
|
if (hash != def->hash)
|
|
continue;
|
|
|
|
if (!fasticmp(def->name, musname))
|
|
continue;
|
|
|
|
return def;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void S_ResetMusicCredit(void)
|
|
{
|
|
memset(&cursongcredit, 0, sizeof(cursongcredit));
|
|
}
|
|
|
|
//
|
|
// S_ShowSpecifiedMusicCredit
|
|
//
|
|
// Display song's credit on screen
|
|
//
|
|
void S_ShowSpecifiedMusicCredit(const char *musname)
|
|
{
|
|
musicdef_t *def;
|
|
|
|
if (music_disabled)
|
|
return;
|
|
|
|
if (!cv_songcredits.value || demo.rewinding)
|
|
return;
|
|
|
|
def = S_FindMusicCredit(musname);
|
|
|
|
if (def && !LUA_HookMusicCredit(def))
|
|
{
|
|
cursongcredit.def = def;
|
|
cursongcredit.anim = 5*TICRATE;
|
|
cursongcredit.x = 0;
|
|
cursongcredit.trans = NUMTRANSMAPS;
|
|
}
|
|
}
|
|
|
|
//
|
|
// S_ShowMusicCredit
|
|
//
|
|
// Display current song's credit on screen
|
|
//
|
|
void S_ShowMusicCredit(void)
|
|
{
|
|
S_ShowSpecifiedMusicCredit(music.name);
|
|
}
|
|
|
|
/// ------------------------
|
|
/// Music Status
|
|
/// ------------------------
|
|
|
|
boolean S_MusicDisabled(void)
|
|
{
|
|
return (music_disabled);
|
|
}
|
|
|
|
boolean S_MusicPlaying(void)
|
|
{
|
|
return I_SongPlaying();
|
|
}
|
|
|
|
boolean S_MusicPaused(void)
|
|
{
|
|
return I_SongPaused();
|
|
}
|
|
|
|
boolean S_MusicNotInFocus(void)
|
|
{
|
|
return (window_notinfocus && !cv_playmusicifunfocused.value);
|
|
}
|
|
|
|
musictype_t S_MusicType(void)
|
|
{
|
|
return I_SongType();
|
|
}
|
|
|
|
const char *S_MusicName(void)
|
|
{
|
|
return music.name;
|
|
}
|
|
|
|
boolean S_MusicInfo(char *mname, UINT16 *mflags, boolean *looping)
|
|
{
|
|
if (!I_SongPlaying())
|
|
return false;
|
|
|
|
strncpy(mname, music.name, 7);
|
|
mname[6] = 0;
|
|
*mflags = music.flags;
|
|
*looping = music.looping;
|
|
|
|
return (boolean)mname[0];
|
|
}
|
|
|
|
boolean S_MusicExists(const char *mname, boolean checkMIDI, boolean checkDigi)
|
|
{
|
|
return (checkDigi ? W_CheckNumForName(va("O_%s", mname)) != LUMPERROR : false)
|
|
|| (checkMIDI ? W_CheckNumForName(va("D_%s", mname)) != LUMPERROR : false);
|
|
}
|
|
|
|
/// ------------------------
|
|
/// Music Effects
|
|
/// ------------------------
|
|
|
|
boolean S_SpeedMusic(float speed)
|
|
{
|
|
return I_SetSongSpeed(speed);
|
|
}
|
|
|
|
/// ------------------------
|
|
/// Music Seeking
|
|
/// ------------------------
|
|
|
|
UINT32 S_GetMusicLength(void)
|
|
{
|
|
return I_GetSongLength();
|
|
}
|
|
|
|
boolean S_SetMusicLoopPoint(UINT32 looppoint)
|
|
{
|
|
return I_SetSongLoopPoint(looppoint);
|
|
}
|
|
|
|
UINT32 S_GetMusicLoopPoint(void)
|
|
{
|
|
return I_GetSongLoopPoint();
|
|
}
|
|
|
|
boolean S_SetMusicPosition(UINT32 position)
|
|
{
|
|
return I_SetSongPosition(position);
|
|
}
|
|
|
|
UINT32 S_GetMusicPosition(void)
|
|
{
|
|
return I_GetSongPosition();
|
|
}
|
|
|
|
/// ------------------------
|
|
/// Music Playback
|
|
/// ------------------------
|
|
|
|
static lumpnum_t S_GetMusicLumpNum(const char *mname)
|
|
{
|
|
if (S_MusicExists(mname, false, true)) // check non midis first
|
|
return W_GetNumForName(va("o_%s", mname));
|
|
|
|
if (S_MusicExists(mname, true, false))
|
|
{
|
|
#ifdef NO_MIDI
|
|
CONS_Alert(CONS_ERROR, "A MIDI music lump %.6s was found,\nbut SRB2Kart does not support MIDI output.\nWe apologise for the inconvenience.\n", mname);
|
|
return LUMPERROR;
|
|
#else
|
|
return W_GetNumForName(va("d_%s", mname));
|
|
#endif
|
|
}
|
|
|
|
CONS_Alert(CONS_ERROR, "Music %.6s could not be loaded: lump not found!\n", mname);
|
|
|
|
return LUMPERROR;
|
|
}
|
|
|
|
static boolean S_LoadMusic(const char *mname)
|
|
{
|
|
lumpnum_t mlumpnum;
|
|
void *mdata;
|
|
|
|
if (S_MusicDisabled())
|
|
return false;
|
|
|
|
mlumpnum = S_GetMusicLumpNum(mname);
|
|
|
|
if (mlumpnum == LUMPERROR)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// load & register it
|
|
mdata = W_CacheLumpNum(mlumpnum, PU_MUSIC);
|
|
|
|
if (I_LoadSong(mdata, W_LumpLength(mlumpnum)))
|
|
{
|
|
strncpy(music.name, mname, 7);
|
|
music.name[6] = 0;
|
|
music.data = mdata;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static void S_UnloadMusic(void)
|
|
{
|
|
I_UnloadSong();
|
|
|
|
#ifndef HAVE_SDL //SDL uses RWOPS
|
|
Z_ChangeTag(music.data, PU_CACHE);
|
|
#endif
|
|
music.data = NULL;
|
|
|
|
music.name[0] = 0;
|
|
music.flags = 0;
|
|
music.looping = false;
|
|
|
|
music_refade_cv = NULL;
|
|
}
|
|
|
|
static boolean S_PlayMusic(boolean looping, UINT32 fadeinms)
|
|
{
|
|
if (S_MusicDisabled())
|
|
return false;
|
|
|
|
I_UpdateSongLagThreshold();
|
|
|
|
if ((!fadeinms && !I_PlaySong(looping)) ||
|
|
(fadeinms && !I_FadeInPlaySong(fadeinms, looping)))
|
|
{
|
|
S_UnloadMusic();
|
|
return false;
|
|
}
|
|
|
|
S_SetMusicVolume(-1); // initialize music volume
|
|
|
|
if (S_MusicNotInFocus())
|
|
I_SetMusicVolume(0);
|
|
|
|
return true;
|
|
}
|
|
|
|
static void S_QueueMusic(const char *mmusic, UINT16 mflags, boolean looping, UINT32 position, UINT32 fadeinms)
|
|
{
|
|
strncpy(queue.name, mmusic, 7);
|
|
queue.flags = mflags;
|
|
queue.looping = looping;
|
|
queue.position = position;
|
|
queue.fadeinms = fadeinms;
|
|
}
|
|
|
|
static void S_ClearQueue(void)
|
|
{
|
|
memset(&queue, 0, sizeof(music_t));
|
|
}
|
|
|
|
static void S_ChangeMusicToQueue(void)
|
|
{
|
|
S_ChangeMusicEx(queue.name, queue.flags, queue.looping, queue.position, 0, queue.fadeinms);
|
|
S_ClearQueue();
|
|
}
|
|
|
|
// keep copy of music name before lua hook change
|
|
// for custom vote music etc
|
|
static char checkmusic[7] = {0};
|
|
|
|
void S_ChangeMusicEx(const char *mmusic, UINT16 mflags, boolean looping, UINT32 position, UINT32 prefadems, UINT32 fadeinms)
|
|
{
|
|
char newmusic[7] = {0};
|
|
|
|
struct MusicChange hook_param = {
|
|
newmusic,
|
|
&mflags,
|
|
&looping,
|
|
&position,
|
|
&prefadems,
|
|
&fadeinms
|
|
};
|
|
|
|
if (S_MusicDisabled()
|
|
|| demo.rewinding // Don't mess with music while rewinding!
|
|
|| demo.title) // SRB2Kart: Demos don't interrupt title screen music
|
|
return;
|
|
|
|
strncpy(newmusic, mmusic, 6);
|
|
strncpy(checkmusic, mmusic, 6);
|
|
|
|
if (LUA_HookMusicChange(music.name, &hook_param))
|
|
return;
|
|
|
|
// No Music (empty string)
|
|
if (newmusic[0] == 0)
|
|
{
|
|
if (prefadems)
|
|
I_FadeSong(0, prefadems, &S_StopMusic);
|
|
else
|
|
S_StopMusic();
|
|
return;
|
|
}
|
|
|
|
if (prefadems && S_MusicPlaying()) // queue music change for after fade // allow even if the music is the same
|
|
{
|
|
CONS_Debug(DBG_DETAILED, "Now fading out song %s\n", music.name);
|
|
S_QueueMusic(newmusic, mflags, looping, position, fadeinms);
|
|
I_FadeSong(0, prefadems, S_ChangeMusicToQueue);
|
|
return;
|
|
}
|
|
else if (strnicmp(music.name, newmusic, 6) || (mflags & MUSIC_FORCERESET))
|
|
{
|
|
CONS_Debug(DBG_DETAILED, "Now playing song %s\n", newmusic);
|
|
|
|
S_StopMusic();
|
|
|
|
if (!S_LoadMusic(newmusic))
|
|
{
|
|
CONS_Alert(CONS_ERROR, "Music %.6s could not be loaded!\n", newmusic);
|
|
return;
|
|
}
|
|
|
|
music.flags = mflags;
|
|
music.looping = looping;
|
|
|
|
if (!S_PlayMusic(looping, fadeinms))
|
|
{
|
|
CONS_Alert(CONS_ERROR, "Music %.6s could not be played!\n", newmusic);
|
|
return;
|
|
}
|
|
|
|
if (position)
|
|
I_SetSongPosition(position);
|
|
|
|
I_SetSongTrack(mflags & MUSIC_TRACKMASK);
|
|
}
|
|
else if (fadeinms) // let fades happen with same music
|
|
{
|
|
I_SetSongPosition(position);
|
|
I_FadeSong(100, fadeinms, NULL);
|
|
}
|
|
else // reset volume to 100 with same music
|
|
{
|
|
I_StopFadingSong();
|
|
I_FadeSong(100, 500, NULL);
|
|
}
|
|
}
|
|
|
|
void S_ChangeMusicSpecial(const char *mmusic)
|
|
{
|
|
if (cv_resetspecialmusic.value)
|
|
S_ChangeMusic(mmusic, MUSIC_FORCERESET, true);
|
|
else
|
|
S_ChangeMusicInternal(mmusic, true);
|
|
}
|
|
|
|
void S_StopMusic(void)
|
|
{
|
|
if (!I_SongPlaying()
|
|
|| demo.rewinding // Don't mess with music while rewinding!
|
|
|| demo.title) // SRB2Kart: Demos don't interrupt title screen music
|
|
return;
|
|
|
|
mapmusic.resume = (cv_resume.value && fasticmp(music.name, mapmusic.name)) ? I_GetSongPosition() : 0;
|
|
|
|
if (I_SongPaused())
|
|
I_ResumeSong();
|
|
|
|
S_SpeedMusic(1.0f);
|
|
I_StopSong();
|
|
S_UnloadMusic(); // for now, stopping also means you unload the song
|
|
}
|
|
|
|
//
|
|
// Stop and resume music, during game PAUSE.
|
|
//
|
|
void S_PauseAudio(void)
|
|
{
|
|
if (I_SongPlaying() && !I_SongPaused())
|
|
I_PauseSong();
|
|
}
|
|
|
|
void S_ResumeAudio(void)
|
|
{
|
|
if (S_MusicNotInFocus())
|
|
return;
|
|
|
|
if (!paused && I_SongPlaying() && I_SongPaused())
|
|
I_ResumeSong();
|
|
}
|
|
|
|
void S_SetMusicVolume(INT32 volume)
|
|
{
|
|
#ifdef NO_MIDI
|
|
consvar_t *musicvolume = &cv_digmusicvolume;
|
|
#else
|
|
consvar_t *musicvolume = (I_SongType() == MU_MID) ? &cv_midimusicvolume : &cv_digmusicvolume;
|
|
#endif
|
|
|
|
if (volume < 0)
|
|
volume = musicvolume->value;
|
|
|
|
if (volume < 0 || volume > 31)
|
|
CONS_Alert(CONS_WARNING, "Music volume should be between 0-31\n");
|
|
|
|
CV_SetValue(musicvolume, volume&31);
|
|
actualmusicvolume = musicvolume->value; //check for change of var
|
|
|
|
I_SetMusicVolume(volume&31);
|
|
}
|
|
|
|
void S_SetRestoreMusicFadeInCvar(consvar_t *cv)
|
|
{
|
|
music_refade_cv = cv;
|
|
}
|
|
|
|
int S_GetRestoreMusicFadeIn(void)
|
|
{
|
|
if (music_refade_cv && cv_fading.value)
|
|
return music_refade_cv->value;
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
/// ------------------------
|
|
/// Music Fading
|
|
/// ------------------------
|
|
|
|
void S_SetInternalMusicVolume(INT32 volume)
|
|
{
|
|
I_SetInternalMusicVolume(min(max(volume, 0), 100));
|
|
}
|
|
|
|
void S_StopFadingMusic(void)
|
|
{
|
|
I_StopFadingSong();
|
|
}
|
|
|
|
boolean S_FadeMusicFromVolume(UINT8 target_volume, INT16 source_volume, UINT32 ms)
|
|
{
|
|
if (source_volume < 0)
|
|
return I_FadeSong(target_volume, ms, NULL);
|
|
|
|
return I_FadeSongFromVolume(target_volume, source_volume, ms, NULL);
|
|
}
|
|
|
|
boolean S_FadeOutStopMusic(UINT32 ms)
|
|
{
|
|
return I_FadeSong(0, ms, &S_StopMusic);
|
|
}
|
|
|
|
/// ------------------------
|
|
/// Init & Others
|
|
/// ------------------------
|
|
|
|
static const char *musicexception_list[] = {
|
|
"vote", "voteea", "voteeb", "racent", "krwin",
|
|
"krok", "krlose", "krfail", "kbwin", "kbok",
|
|
"kblose", "kstart", "estart", "wait2j", "titles",
|
|
"kinvnc", "kgrow", // grow and invinc
|
|
"SPBA_W", "SPBA_L", // SPB Attack music
|
|
"CHRSHF", "CHRSHP" // no clue what those are tbh
|
|
};
|
|
|
|
// check if the current music is smth we dont want to keep (vote music, etc)
|
|
static boolean S_CheckMusicException(void)
|
|
{
|
|
// dumb hack but dont keepmusic music that is supposed to reset
|
|
if (music.flags & MUSIC_RELOADRESET)
|
|
return true;
|
|
|
|
if (!fasticmp(music.name, mapmusic.name))
|
|
return true;
|
|
|
|
// in case somehow the mapmusic was replaced with smth we dont want to keep
|
|
for (size_t i = 0; i < sizeof(musicexception_list)/sizeof(musicexception_list[0]); i++)
|
|
{
|
|
if (fasticmp(music.name, musicexception_list[i]) || fasticmp(checkmusic, musicexception_list[i]))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void S_ResetKeepAndSpecialMus(void)
|
|
{
|
|
keepmapmusic = false;
|
|
skipintromus = SKPMUS_OFF;
|
|
}
|
|
|
|
boolean S_MapMusPlaying(void)
|
|
{
|
|
return S_MusicPlaying() && (strcasecmp(music.name, mapmusic.name) == 0);
|
|
}
|
|
|
|
// determine if we should keep the music on a map restart
|
|
// this gets called BEFORE the level gets loaded in G_DoLoadLevel
|
|
void S_KeepMusic(void)
|
|
{
|
|
static INT16 oldmap = -1;
|
|
static boolean oldencore = false;
|
|
|
|
keepmapmusic = false;
|
|
|
|
if (!cv_keepmusic.value)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// copy over mapmusicname stuff into temporary var
|
|
memset(keepmusic.name, 0, sizeof(keepmusic.name));
|
|
strlcpy(keepmusic.name, mapmusic.name, sizeof(keepmusic.name));
|
|
keepmusic.name[6] = 0;
|
|
|
|
if (oldmap == gamemap && oldencore == encoremode)
|
|
{
|
|
keepmapmusic = (S_CheckMusicException() == false);
|
|
}
|
|
|
|
oldencore = encoremode;
|
|
oldmap = gamemap;
|
|
}
|
|
|
|
// Sets up the map music in case it should be reloaded
|
|
// Special case for keepmusic
|
|
void S_HandleReloadResetMusic(void)
|
|
{
|
|
if (!(mapmusic.flags & MUSIC_RELOADRESET))
|
|
return;
|
|
|
|
// replace mapmusic with our saved keepmusic stuff
|
|
if (keepmapmusic)
|
|
{
|
|
// this is horrible, but oh well
|
|
strlcpy(mapmusic.name, keepmusic.name, sizeof(mapmusic.name));
|
|
}
|
|
else
|
|
{
|
|
strlcpy(mapmusic.name, mapheaderinfo[gamemap-1]->musname, sizeof(mapmusic.name));
|
|
}
|
|
|
|
mapmusic.name[6] = 0;
|
|
mapmusic.flags = (mapheaderinfo[gamemap-1]->mustrack & MUSIC_TRACKMASK);
|
|
mapmusic.position = mapheaderinfo[gamemap-1]->muspos;
|
|
mapmusic.resume = 0;
|
|
}
|
|
|
|
// TODO: make this a seperate lump
|
|
// would rather NOT hardcode stuff like this
|
|
|
|
typedef struct
|
|
{
|
|
const char *name; // sfx to check
|
|
UINT8 action; // what those sfx do
|
|
} sfxintro_t;
|
|
|
|
static sfxintro_t introsfxlist[] = {
|
|
{"KMAPPF", SKPMUS_PLAYSKIP}, {"DPFINT", SKPMUS_PLAYSKIP}, // RA Extended
|
|
{"D00DMM", SKPMUS_PLAYMUS}, {"D00DCE", SKPMUS_PLAYMUS}, {"D00DSD", SKPMUS_PLAYMUS}, // RA Extended
|
|
{"D00DLD", SKPMUS_PLAYSKIP}, {"D00DGE", SKPMUS_PLAYSKIP}, // RA Extended
|
|
{"SSVO", SKPMUS_PLAYMUS}, {"SSVOH", SKPMUS_PLAYMUS}, // subsonic
|
|
{"NFMMM", SKPMUS_PLAYSKIP}, {"NFMMME", SKPMUS_PLAYSKIP}, {"SRALLY", SKPMUS_PLAYSKIP}, // sparkcup
|
|
{"AMLUY0", SKPMUS_PLAYSKIP}, // ceroba
|
|
{"HOURIN", SKPMUS_PLAYSKIP}, {"HOURIM", SKPMUS_PLAYSKIP}, // hourglass
|
|
{"KAIJIS", SKPMUS_PLAYMUS}, // mauromania
|
|
{"SKINTO", SKPMUS_PLAYMUS}, // shovelknight
|
|
{"WALU01", SKPMUS_PLAYMUS}, {"WADE00", SKPMUS_PLAYMUS}, {"INTRO", SKPMUS_PLAYSKIP}, // pata
|
|
{"WARINT", SKPMUS_PLAYSKIP}, {"PTIMED", SKPMUS_PLAYSKIP}, {"PTISEW", SKPMUS_PLAYSKIP}, // pizza tower pata
|
|
{"PTIFIN", SKPMUS_PLAYSKIP}, {"PTIFAC", SKPMUS_PLAYSKIP}, {"PTISPA", SKPMUS_PLAYSKIP}, // pizza tower pata
|
|
{"K4TM10", SKPMUS_PLAYSKIP}, {"K4TM20", SKPMUS_PLAYSKIP}, {"K4TM30", SKPMUS_PLAYSKIP}, // boon county pata
|
|
{"MBMWIN", SKPMUS_PLAYSKIP}, // ivo motobug
|
|
{"START", SKPMUS_PLAYMUS}, {"STARR", SKPMUS_PLAYMUS}, {"3STA", SKPMUS_PLAYMUS}, // assaultpack
|
|
{"SLFARE", SKPMUS_PLAYSKIP}, {"LTSAGO", SKPMUS_PLAYSKIP}, // secret slide
|
|
{"SPKINT", SKPMUS_PLAYSKIP}, // cotopack spelunky
|
|
{"LOZOPN", SKPMUS_PLAYSKIP}, {"LOZOPM", SKPMUS_PLAYSKIP}, // nostalgia pack zelda
|
|
{"MLINTR", SKPMUS_PLAYSKIP}, {"RALLYY", SKPMUS_PLAYSKIP}, // coffee cup
|
|
};
|
|
|
|
// :chaosleep:
|
|
static UINT8 S_CheckIntroSfx(void)
|
|
{
|
|
size_t i, h;
|
|
sfxinfo_t *sfx = NULL;
|
|
INT32 sfxnum = sfx_None;
|
|
UINT8 ret = SKPMUS_PLAYMUS; // for now default to just playing sounds while the music is playing
|
|
// useful for finding intros that were missed
|
|
|
|
// go through the whole map
|
|
// and scan for any lines with the play sfx special
|
|
// we cannot really detect here if they are supposed to play on map start
|
|
// but the list contains things that are "known" to be intros
|
|
// this sucks immense ass but what can you do
|
|
for (i = 0; i < numlines; i++)
|
|
{
|
|
const line_t *line = &lines[i];
|
|
|
|
// "play sfx" special
|
|
if (line->special != 414)
|
|
continue;
|
|
|
|
// if theres a sound name in the toptexture
|
|
// this will give us a valid sound id
|
|
sfxnum = sides[line->sidenum[0]].toptexture; //P_AproxDistance(line->dx, line->dy)>>FRACBITS;
|
|
|
|
// no valid sound id?
|
|
if (sfxnum <= sfx_None || sfxnum >= NUMSFX)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
sfx = &S_sfx[sfxnum];
|
|
|
|
// somehow sound does not exist
|
|
if (!sfx)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// now check if the corresponding sound matches anything from our list
|
|
for (h = 0; h < sizeof(introsfxlist)/sizeof(introsfxlist[0]); h++)
|
|
{
|
|
// case insensitive, for some reason some sounds freeslot lowercase?
|
|
if (fasticmp(sfx->name, introsfxlist[h].name))
|
|
{
|
|
const UINT8 skipaction = introsfxlist[h].action;
|
|
|
|
// intros that plays music or otherwise wont fit with the music playing
|
|
// have precendence over EVERYTHING
|
|
if (skipaction == SKPMUS_PLAYSKIP)
|
|
return SKPMUS_PLAYSKIP;
|
|
|
|
// otherwise keep going
|
|
|
|
// always prefer higher priority
|
|
// not really needed currently
|
|
// but idk if i edit this again some other time
|
|
if (skipaction > ret)
|
|
ret = skipaction;
|
|
}
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static UINT8 S_SkipIntroMusic(void)
|
|
{
|
|
if (!cv_skipintromusic.value)
|
|
return SKPMUS_OFF;
|
|
|
|
// dont skip if we disabled music, weird? but this would skip sounds that play on map start as well otherwise
|
|
if (music_disabled)
|
|
return SKPMUS_OFF;
|
|
|
|
// check if menu music is playing, otherwise it may continue playing
|
|
if (fasticmp(music.name, "titles"))
|
|
return SKPMUS_OFF;
|
|
|
|
// wandering balls changes its song when the race starts Zzz...
|
|
CLEANUP(Z_Pfree) char *maptitle = G_BuildMapTitle(gamemap);
|
|
if (maptitle && fasticmp(maptitle, "Wandering Falls"))
|
|
{
|
|
return SKPMUS_OFF;
|
|
}
|
|
|
|
// without sound there wont be any intro playing
|
|
if (sound_disabled)
|
|
return SKPMUS_SKIP;
|
|
// yes i sometimes play with only music but no sound
|
|
|
|
return S_CheckIntroSfx();
|
|
}
|
|
|
|
//
|
|
// Per level startup code.
|
|
// Kills playing sounds at start of level,
|
|
// determines music if any, changes music.
|
|
//
|
|
void S_InitMapMusic(void)
|
|
{
|
|
S_HandleReloadResetMusic();
|
|
|
|
if (keepmapmusic)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Starting ambience should always be restarted
|
|
// lug: but not when we keep the map music lol
|
|
S_StopMusic();
|
|
}
|
|
|
|
void S_HandleMusicStart(void)
|
|
{
|
|
skipintromus = S_SkipIntroMusic();
|
|
|
|
if (skipintromus)
|
|
return;
|
|
|
|
if (leveltime < MUSICSTARTTIME) // SRB2Kart
|
|
S_ChangeMusicInternal((encoremode ? "estart" : "kstart"), false); // S_StopMusic();
|
|
}
|
|
|
|
void S_StartMapMusic(void)
|
|
{
|
|
// no need to constantly run this after race has started
|
|
if (leveltime > MUSICSTARTTIME)
|
|
return;
|
|
|
|
if (keepmapmusic)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (skipintromus == SKPMUS_SKIP || skipintromus == SKPMUS_PLAYMUS)
|
|
{
|
|
if (leveltime < starttime)
|
|
S_ChangeMusicEx(mapmusic.name, mapmusic.flags, true, mapmusic.position, 0, 0);
|
|
else if (leveltime == MUSICSTARTTIME)
|
|
S_ShowMusicCredit();
|
|
|
|
return;
|
|
}
|
|
|
|
if (skipintromus)
|
|
{
|
|
if (leveltime == MUSICSTARTTIME)
|
|
{
|
|
S_ChangeMusicEx(mapmusic.name, mapmusic.flags, true, mapmusic.position, 0, 0);
|
|
S_ShowMusicCredit();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (leveltime < starttime) // SRB2Kart
|
|
S_ChangeMusicInternal((encoremode ? "estart" : "kstart"), false); // yes this will be spammed otherwise encore and some stuff WILL overwrite it
|
|
else if (leveltime == starttime) // The GO! sound stops the level start ambience
|
|
S_StopMusic();
|
|
else if (leveltime == MUSICSTARTTIME) // Plays the music after the starting countdown.
|
|
{
|
|
S_ChangeMusicEx(mapmusic.name, mapmusic.flags, true, mapmusic.position, 0, 0);
|
|
S_ShowMusicCredit();
|
|
}
|
|
}
|
|
|
|
void S_RestartMusic(void)
|
|
{
|
|
S_StopMusic();
|
|
I_ShutdownMusic();
|
|
I_InitMusic();
|
|
|
|
S_SetMusicVolume(-1);
|
|
|
|
S_ResetKeepAndSpecialMus();
|
|
|
|
if (Playing()) // Gotta make sure the player is in a level
|
|
P_RestoreMusic(&players[consoleplayer]);
|
|
else
|
|
S_ChangeMusicInternal("titles", looptitle);
|
|
}
|
|
|
|
static void Command_Tunes_f(void)
|
|
{
|
|
const char *tunearg;
|
|
UINT16 tunenum, track = 0;
|
|
UINT32 position = 0;
|
|
const size_t argc = COM_Argc();
|
|
|
|
if (argc < 2) //tunes slot ...
|
|
{
|
|
CONS_Printf("tunes <name/num> [track] [speed] [position] / <-show> / <-default> / <-none>:\n");
|
|
CONS_Printf(M_GetText("Play an arbitrary music lump. If a map number is used, 'MAP##M' is played.\n"));
|
|
CONS_Printf(M_GetText("If the format supports multiple songs, you can specify which one to play.\n\n"));
|
|
CONS_Printf(M_GetText("* With \"-show\", shows the currently playing tune and track.\n"));
|
|
CONS_Printf(M_GetText("* With \"-default\", returns to the default music for the map.\n"));
|
|
CONS_Printf(M_GetText("* With \"-none\", any music playing will be stopped.\n"));
|
|
return;
|
|
}
|
|
|
|
tunearg = COM_Argv(1);
|
|
tunenum = (UINT16)atoi(tunearg);
|
|
track = 0;
|
|
|
|
if (fasticmp(tunearg, "-show"))
|
|
{
|
|
CONS_Printf(M_GetText("The current tune is: %s [track %d]\n"),
|
|
mapmusic.name, (mapmusic.flags & MUSIC_TRACKMASK));
|
|
return;
|
|
}
|
|
|
|
if (fasticmp(tunearg, "-none"))
|
|
{
|
|
S_StopMusic();
|
|
return;
|
|
}
|
|
else if (fasticmp(tunearg, "-default"))
|
|
{
|
|
tunearg = mapheaderinfo[gamemap-1]->musname;
|
|
track = mapheaderinfo[gamemap-1]->mustrack;
|
|
}
|
|
else if (!tunearg[2] && toupper(tunearg[0]) >= 'A' && toupper(tunearg[0]) <= 'Z')
|
|
tunenum = (UINT16)M_MapNumber(tunearg[0], tunearg[1]);
|
|
|
|
if (tunenum && tunenum >= 1036)
|
|
{
|
|
CONS_Alert(CONS_NOTICE, M_GetText("Valid music slots are 1 to 1035.\n"));
|
|
return;
|
|
}
|
|
|
|
if (!tunenum && strlen(tunearg) > 6) // This is automatic -- just show the error just in case
|
|
CONS_Alert(CONS_NOTICE, M_GetText("Music name too long - truncated to six characters.\n"));
|
|
|
|
if (argc > 2)
|
|
track = (UINT16)atoi(COM_Argv(2))-1;
|
|
|
|
if (tunenum)
|
|
snprintf(mapmusic.name, 7, "%sM", G_BuildMapName(tunenum));
|
|
else
|
|
strlcpy(mapmusic.name, tunearg, sizeof(mapmusic.name));
|
|
|
|
if (argc > 4)
|
|
position = (UINT32)atoi(COM_Argv(4));
|
|
|
|
mapmusic.flags = (track & MUSIC_TRACKMASK);
|
|
mapmusic.position = position;
|
|
mapmusic.resume = 0;
|
|
|
|
S_ChangeMusicEx(mapmusic.name, mapmusic.flags, true, mapmusic.position, 0, 0);
|
|
|
|
if (argc > 3)
|
|
{
|
|
float speed = (float)atof(COM_Argv(3));
|
|
if (speed > 0.0f)
|
|
S_SpeedMusic(speed);
|
|
}
|
|
}
|
|
|
|
static void Command_RestartAudio_f(void)
|
|
{
|
|
if (dedicated) // No point in doing anything if game is a dedicated server.
|
|
return;
|
|
|
|
S_StopMusic();
|
|
S_StopSounds();
|
|
I_ShutdownMusic();
|
|
I_ShutdownSound();
|
|
I_StartupSound();
|
|
I_InitMusic();
|
|
|
|
// These must be called or no sound and music until manually set.
|
|
|
|
I_SetSfxVolume(cv_soundvolume.value);
|
|
S_SetMusicVolume(-1);
|
|
|
|
S_StartSound(NULL, sfx_strpst);
|
|
|
|
S_ResetKeepAndSpecialMus();
|
|
|
|
if (Playing()) // Gotta make sure the player is in a level
|
|
P_RestoreMusic(&players[consoleplayer]);
|
|
else
|
|
S_ChangeMusicInternal("titles", looptitle);
|
|
}
|
|
|
|
static void Command_RestartMusic_f(void) //same as RestartAudio but only music gets restarted
|
|
{
|
|
S_RestartMusic();
|
|
}
|
|
|
|
static void Command_ShowMusicCredit_f(void)
|
|
{
|
|
const char *musname = music.name;
|
|
|
|
if (COM_Argc() > 1)
|
|
{
|
|
musname = COM_Argv(1);
|
|
}
|
|
else
|
|
{
|
|
S_ShowMusicCredit();
|
|
}
|
|
|
|
musicdef_t *def = S_FindMusicCredit(musname);
|
|
|
|
if (def)
|
|
CONS_Printf("%.6s - %.255s\n", musname, def->source);
|
|
}
|
|
|
|
static void GameSounds_OnChange(void)
|
|
{
|
|
if (M_CheckParm("-nosound") || M_CheckParm("-noaudio"))
|
|
return;
|
|
|
|
if (sound_disabled && cv_gamesounds.value)
|
|
{
|
|
sound_disabled = false;
|
|
I_StartupSound(); // will return early if initialised
|
|
S_InitSfxChannels(cv_soundvolume.value);
|
|
S_StartSound(NULL, sfx_strpst);
|
|
}
|
|
else if (!sound_disabled && !cv_gamesounds.value)
|
|
{
|
|
sound_disabled = true;
|
|
S_StopSounds();
|
|
}
|
|
}
|
|
|
|
static void SoundCache_OnChange(void)
|
|
{
|
|
if (S_CacheSound() != SOUNDCACHE_OFF)
|
|
{
|
|
S_InitSfxChannels(cv_soundvolume.value);
|
|
}
|
|
else
|
|
{
|
|
S_ClearSfx();
|
|
|
|
if (!sound_disabled)
|
|
S_InitSfxChannels(cv_soundvolume.value);
|
|
}
|
|
}
|
|
|
|
void GameMusic_OnChange(void)
|
|
{
|
|
if (M_CheckParm("-nomusic") || M_CheckParm("-noaudio"))
|
|
return;
|
|
|
|
if (music_disabled && cv_gamedigimusic.value)
|
|
{
|
|
music_disabled = false;
|
|
I_StartupSound(); // will return early if initialised
|
|
I_InitMusic();
|
|
|
|
S_ResetKeepAndSpecialMus();
|
|
|
|
if (Playing())
|
|
P_RestoreMusic(&players[consoleplayer]);
|
|
else
|
|
S_ChangeMusicInternal("titles", looptitle);
|
|
}
|
|
else if (!music_disabled && !cv_gamedigimusic.value)
|
|
{
|
|
music_disabled = true;
|
|
S_StopMusic();
|
|
}
|
|
}
|
|
|
|
#ifdef HAVE_OPENMPT
|
|
static void ModFilter_OnChange(void)
|
|
{
|
|
if (openmpt_mhandle)
|
|
openmpt_module_set_render_param(openmpt_mhandle, OPENMPT_MODULE_RENDER_INTERPOLATIONFILTER_LENGTH, cv_modfilter.value);
|
|
}
|
|
|
|
static void StereoSep_OnChange(void)
|
|
{
|
|
if (openmpt_mhandle)
|
|
openmpt_module_set_render_param(openmpt_mhandle, OPENMPT_MODULE_RENDER_STEREOSEPARATION_PERCENT, cv_stereosep.value);
|
|
}
|
|
|
|
static void AmigaFilter_OnChange(void)
|
|
{
|
|
if (openmpt_mhandle)
|
|
#if OPENMPT_API_VERSION_MAJOR < 1 && OPENMPT_API_VERSION_MINOR > 4
|
|
openmpt_module_ctl_set_boolean(openmpt_mhandle, "render.resampler.emulate_amiga", cv_amigafilter.value);
|
|
#else
|
|
openmpt_module_ctl_set(openmpt_mhandle, "render.resampler.emulate_amiga", cv_amigafilter.value ? "1" : "0");
|
|
#endif
|
|
}
|
|
|
|
|
|
#if OPENMPT_API_VERSION_MAJOR < 1 && OPENMPT_API_VERSION_MINOR > 4
|
|
static void AmigaType_OnChange(void)
|
|
{
|
|
if (openmpt_mhandle)
|
|
openmpt_module_ctl_set_text(openmpt_mhandle, "render.resampler.emulate_amiga_type", cv_amigatype.string);
|
|
|
|
if (sound_started)
|
|
S_RestartMusic(); // need to restart the music system or else it wont work
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
static void BufferSize_OnChange(void)
|
|
{
|
|
if (sound_started)
|
|
COM_ImmedExecute("restartaudio");
|
|
}
|