2019-09-19 22:42:45 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Copyright (C) 2010-2019 EDuke32 developers and contributors
|
|
|
|
Copyright (C) 2019 Nuke.YKT
|
|
|
|
|
|
|
|
This file is part of NBlood.
|
|
|
|
|
|
|
|
NBlood is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License version 2
|
|
|
|
as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
2019-09-21 18:59:54 +00:00
|
|
|
#include "ns.h" // Must come before everything else!
|
|
|
|
|
2019-09-19 22:42:45 +00:00
|
|
|
#include "build.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "common_game.h"
|
|
|
|
#include "levels.h"
|
|
|
|
#include "sound.h"
|
2020-04-12 06:07:48 +00:00
|
|
|
#include "raze_music.h"
|
2020-04-12 06:09:38 +00:00
|
|
|
#include "raze_sound.h"
|
2020-07-26 21:59:29 +00:00
|
|
|
#include "gamecontrol.h"
|
2019-09-19 22:42:45 +00:00
|
|
|
|
2019-09-22 06:39:22 +00:00
|
|
|
BEGIN_BLD_NS
|
|
|
|
|
2019-09-19 22:42:45 +00:00
|
|
|
int soundRates[13] = {
|
|
|
|
11025,
|
|
|
|
11025,
|
|
|
|
11025,
|
|
|
|
11025,
|
|
|
|
11025,
|
|
|
|
22050,
|
|
|
|
22050,
|
|
|
|
22050,
|
|
|
|
22050,
|
|
|
|
44100,
|
|
|
|
44100,
|
|
|
|
44100,
|
|
|
|
44100,
|
|
|
|
};
|
2020-10-11 10:18:25 +00:00
|
|
|
enum { kChannelMax = 32 };
|
2019-09-19 22:42:45 +00:00
|
|
|
|
2019-12-17 18:37:05 +00:00
|
|
|
|
2020-07-25 21:35:39 +00:00
|
|
|
void ByteSwapSFX(SFX* pSFX)
|
|
|
|
{
|
|
|
|
#if B_BIG_ENDIAN == 1
|
2020-08-03 17:09:57 +00:00
|
|
|
pSFX->relVol = LittleLong(pSFX->relVol);
|
|
|
|
pSFX->pitch = LittleLong(pSFX->pitch);
|
|
|
|
pSFX->pitchRange = LittleLong(pSFX->pitchRange);
|
|
|
|
pSFX->format = LittleLong(pSFX->format);
|
|
|
|
pSFX->loopStart = LittleLong(pSFX->loopStart);
|
2020-07-25 21:35:39 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:37:05 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_AddBloodSFX
|
|
|
|
//
|
|
|
|
// Registers a new sound with the name "<lumpname>.sfx"
|
|
|
|
// Actual sound data is searched for in the ns_bloodraw namespace.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
static void S_AddBloodSFX(int lumpnum)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
2019-12-17 18:37:05 +00:00
|
|
|
auto sfxlump = fileSystem.ReadFile(lumpnum);
|
2020-07-25 21:35:39 +00:00
|
|
|
SFX* sfx = (SFX*)sfxlump.GetMem();
|
|
|
|
ByteSwapSFX(sfx);
|
2019-12-17 18:37:05 +00:00
|
|
|
FStringf rawname("%s.raw", sfx->rawName);
|
|
|
|
auto rawlump = fileSystem.FindFile(rawname);
|
|
|
|
int sfxnum;
|
2019-09-19 22:42:45 +00:00
|
|
|
|
2019-12-17 18:37:05 +00:00
|
|
|
if (rawlump != -1)
|
|
|
|
{
|
|
|
|
auto& S_sfx = soundEngine->GetSounds();
|
|
|
|
sfxnum = soundEngine->AddSoundLump(sfx->rawName, rawlump, 0, fileSystem.GetResourceId(lumpnum), 6);
|
|
|
|
if (sfx->format < 5 || sfx->format > 12)
|
|
|
|
{ // [0..4] + invalid formats
|
|
|
|
S_sfx[sfxnum].RawRate = 11025;
|
|
|
|
}
|
|
|
|
else if (sfx->format < 9)
|
|
|
|
{ // [5..8]
|
|
|
|
S_sfx[sfxnum].RawRate = 22050;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // [9..12]
|
|
|
|
S_sfx[sfxnum].RawRate = 44100;
|
|
|
|
}
|
|
|
|
S_sfx[sfxnum].bLoadRAW = true;
|
|
|
|
S_sfx[sfxnum].LoopStart = LittleLong(sfx->loopStart);
|
|
|
|
//S_sfx[sfxnum].Volume = sfx->relVol / 255.f; This cannot be done because this volume setting is optional.
|
2020-08-20 15:52:56 +00:00
|
|
|
S_sfx[sfxnum].UserData.Resize(3);
|
2019-12-17 18:37:05 +00:00
|
|
|
int* udata = (int*)S_sfx[sfxnum].UserData.Data();
|
|
|
|
udata[0] = sfx->pitch;
|
|
|
|
udata[1] = sfx->pitchRange;
|
2020-08-20 15:52:56 +00:00
|
|
|
udata[2] = sfx->relVol;
|
|
|
|
}
|
2019-12-17 18:37:05 +00:00
|
|
|
}
|
2019-09-19 22:42:45 +00:00
|
|
|
|
2019-12-17 18:37:05 +00:00
|
|
|
void sndInit(void)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
2019-12-17 18:37:05 +00:00
|
|
|
sfxInit();
|
|
|
|
for (int i = fileSystem.GetNumEntries() - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
auto type = fileSystem.GetResourceType(i);
|
2020-04-11 21:54:33 +00:00
|
|
|
if (!stricmp(type, "SFX"))
|
2019-12-17 18:37:05 +00:00
|
|
|
{
|
|
|
|
if (soundEngine->FindSoundByResID(fileSystem.GetResourceId(i)) == 0)
|
|
|
|
S_AddBloodSFX(i);
|
|
|
|
}
|
2020-04-11 21:54:33 +00:00
|
|
|
else if (!stricmp(type, "WAV") || !stricmp(type, "OGG") || !stricmp(type, "FLAC") || !stricmp(type, "VOC"))
|
2019-12-17 18:37:05 +00:00
|
|
|
{
|
2020-04-11 21:54:33 +00:00
|
|
|
soundEngine->AddSoundLump(fileSystem.GetFileFullName(i), i, 0, fileSystem.GetResourceId(i)| 0x40000000, 6); // mark the resource ID as special.
|
2019-12-17 18:37:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
soundEngine->HashSounds();
|
2019-09-19 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 18:37:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sndGetRate(int format)
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
2019-12-17 18:37:05 +00:00
|
|
|
if (format < 13)
|
|
|
|
return soundRates[format];
|
|
|
|
return 11025;
|
2019-11-10 22:58:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 22:42:45 +00:00
|
|
|
|
2019-12-17 18:37:05 +00:00
|
|
|
|
2020-11-07 04:13:45 +00:00
|
|
|
bool sndCheckPlaying(unsigned int nSound)
|
|
|
|
{
|
|
|
|
auto snd = soundEngine->FindSoundByResID(nSound);
|
|
|
|
return snd > 0 ? soundEngine->GetSoundPlayingInfo(SOURCE_Any, nullptr, snd) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndStopSample(unsigned int nSound)
|
|
|
|
{
|
|
|
|
auto snd = soundEngine->FindSoundByResID(nSound);
|
|
|
|
|
|
|
|
if (snd > 0)
|
|
|
|
{
|
|
|
|
soundEngine->StopSoundID(snd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 22:42:45 +00:00
|
|
|
void sndStartSample(const char *pzSound, int nVolume, int nChannel)
|
|
|
|
{
|
2019-10-28 21:19:50 +00:00
|
|
|
if (!SoundEnabled())
|
2019-09-19 22:42:45 +00:00
|
|
|
return;
|
|
|
|
if (!strlen(pzSound))
|
|
|
|
return;
|
2020-10-11 10:38:17 +00:00
|
|
|
assert(nChannel >= -1 && nChannel < kChannelMax);
|
2019-12-17 18:37:05 +00:00
|
|
|
auto snd = soundEngine->FindSound(pzSound);
|
|
|
|
if (snd > 0)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
2019-12-17 18:37:05 +00:00
|
|
|
soundEngine->StartSound(SOURCE_None, nullptr, nullptr, nChannel + 1, 0, snd, nVolume / 255.f, ATTN_NONE);
|
2019-09-19 22:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 17:20:25 +00:00
|
|
|
void sndStartSample(unsigned int nSound, int nVolume, int nChannel, bool bLoop, EChanFlags chanflags)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
2019-10-28 21:19:50 +00:00
|
|
|
if (!SoundEnabled())
|
2019-09-19 22:42:45 +00:00
|
|
|
return;
|
2020-10-11 10:38:17 +00:00
|
|
|
assert(nChannel >= -1 && nChannel < kChannelMax);
|
2019-12-17 18:37:05 +00:00
|
|
|
if (nChannel >= 7) nChannel = -1;
|
|
|
|
auto snd = soundEngine->FindSoundByResID(nSound);
|
|
|
|
if (snd > 0)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
2019-12-17 18:37:05 +00:00
|
|
|
if (nVolume < 0)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
2020-02-23 17:30:48 +00:00
|
|
|
auto udata = soundEngine->GetUserData(snd);
|
2020-08-11 18:17:56 +00:00
|
|
|
if (udata) nVolume = std::min(Scale(udata[2], 255, 100), 255);
|
2019-12-17 18:37:05 +00:00
|
|
|
else nVolume = 255;
|
2019-09-19 22:42:45 +00:00
|
|
|
}
|
2020-09-05 17:20:25 +00:00
|
|
|
if (bLoop) chanflags |= CHANF_LOOP;
|
|
|
|
soundEngine->StartSound(SOURCE_None, nullptr, nullptr, (nChannel + 1), chanflags, snd, nVolume / 255.f, ATTN_NONE);
|
2019-09-19 22:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:37:05 +00:00
|
|
|
void sndStartWavID(unsigned int nSound, int nVolume, int nChannel)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
2019-12-17 18:37:05 +00:00
|
|
|
return sndStartSample(nSound | 0x40000000, nVolume, nChannel);
|
2019-09-19 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 18:37:05 +00:00
|
|
|
void sndStartWavDisk(const char *pzFile, int nVolume, int nChannel)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
2020-01-03 23:07:20 +00:00
|
|
|
FString name = pzFile;
|
|
|
|
FixPathSeperator(name);
|
|
|
|
return sndStartSample(name, nVolume, nChannel);
|
2019-09-19 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 18:37:05 +00:00
|
|
|
void sndKillAllSounds(void)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
2019-12-17 18:37:05 +00:00
|
|
|
soundEngine->StopSound(CHAN_AUTO);
|
2019-09-19 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-22 06:39:22 +00:00
|
|
|
|
|
|
|
END_BLD_NS
|