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 "music.h"
|
|
|
|
#include "fx_man.h"
|
|
|
|
#include "common_game.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "levels.h"
|
|
|
|
#include "resource.h"
|
|
|
|
#include "sound.h"
|
|
|
|
#include "renderlayer.h"
|
2019-10-24 18:28:46 +00:00
|
|
|
#include "al_midi.h"
|
2019-11-01 21:17:15 +00:00
|
|
|
#include "openaudio.h"
|
2019-11-10 22:58:51 +00:00
|
|
|
#include "z_music.h"
|
2019-09-19 22:42:45 +00:00
|
|
|
|
2019-09-22 06:39:22 +00:00
|
|
|
BEGIN_BLD_NS
|
|
|
|
|
2019-10-31 19:17:49 +00:00
|
|
|
Resource& gSoundRes = fileSystem;
|
2019-09-19 22:42:45 +00:00
|
|
|
|
|
|
|
int soundRates[13] = {
|
|
|
|
11025,
|
|
|
|
11025,
|
|
|
|
11025,
|
|
|
|
11025,
|
|
|
|
11025,
|
|
|
|
22050,
|
|
|
|
22050,
|
|
|
|
22050,
|
|
|
|
22050,
|
|
|
|
44100,
|
|
|
|
44100,
|
|
|
|
44100,
|
|
|
|
44100,
|
|
|
|
};
|
2019-10-25 20:53:41 +00:00
|
|
|
#define kChannelMax 32
|
2019-09-19 22:42:45 +00:00
|
|
|
|
|
|
|
int sndGetRate(int format)
|
|
|
|
{
|
|
|
|
if (format < 13)
|
|
|
|
return soundRates[format];
|
|
|
|
return 11025;
|
|
|
|
}
|
|
|
|
|
2019-10-25 20:53:41 +00:00
|
|
|
SAMPLE2D Channel[kChannelMax];
|
2019-09-19 22:42:45 +00:00
|
|
|
|
|
|
|
SAMPLE2D * FindChannel(void)
|
|
|
|
{
|
2019-10-25 20:53:41 +00:00
|
|
|
for (int i = kChannelMax - 1; i >= 0; i--)
|
2019-10-19 19:11:39 +00:00
|
|
|
if (Channel[i].at5 == 0) return &Channel[i];
|
|
|
|
consoleSysMsg("No free channel available for sample");
|
|
|
|
//ThrowError("No free channel available for sample");
|
2019-09-19 22:42:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-11 23:43:07 +00:00
|
|
|
int sndPlaySong(const char *mapname, const char* songName, bool bLoop)
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
2019-11-11 23:43:07 +00:00
|
|
|
return Mus_Play(mapname, songName, bLoop);
|
2019-11-10 22:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool sndIsSongPlaying(void)
|
|
|
|
{
|
|
|
|
// Not used
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndFadeSong(int nTime)
|
|
|
|
{
|
|
|
|
// not implemented
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndStopSong(void)
|
|
|
|
{
|
|
|
|
Mus_Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndSetFXVolume(int nVolume)
|
|
|
|
{
|
|
|
|
snd_fxvolume = nVolume;
|
|
|
|
FX_SetVolume(nVolume);
|
|
|
|
}
|
|
|
|
|
2019-09-19 22:42:45 +00:00
|
|
|
|
|
|
|
void SoundCallback(intptr_t val)
|
|
|
|
{
|
|
|
|
SAMPLE2D *pChannel = (SAMPLE2D*)val;
|
|
|
|
pChannel->at0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndKillSound(SAMPLE2D *pChannel);
|
|
|
|
|
|
|
|
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;
|
2019-10-25 20:53:41 +00:00
|
|
|
dassert(nChannel >= -1 && nChannel < kChannelMax);
|
2019-09-19 22:42:45 +00:00
|
|
|
SAMPLE2D *pChannel;
|
|
|
|
if (nChannel == -1)
|
|
|
|
pChannel = FindChannel();
|
|
|
|
else
|
|
|
|
pChannel = &Channel[nChannel];
|
|
|
|
if (pChannel->at0 > 0)
|
|
|
|
sndKillSound(pChannel);
|
|
|
|
pChannel->at5 = gSoundRes.Lookup(pzSound, "RAW");
|
|
|
|
if (!pChannel->at5)
|
|
|
|
return;
|
2019-10-30 23:50:45 +00:00
|
|
|
int nSize = pChannel->at5->Size();
|
2019-09-19 22:42:45 +00:00
|
|
|
char *pData = (char*)gSoundRes.Lock(pChannel->at5);
|
|
|
|
pChannel->at0 = FX_PlayRaw(pData, nSize, sndGetRate(1), 0, nVolume, nVolume, nVolume, nVolume, 1.f, (intptr_t)&pChannel->at0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndStartSample(unsigned int nSound, int nVolume, int nChannel, bool bLoop)
|
|
|
|
{
|
2019-10-28 21:19:50 +00:00
|
|
|
if (!SoundEnabled())
|
2019-09-19 22:42:45 +00:00
|
|
|
return;
|
2019-10-25 20:53:41 +00:00
|
|
|
dassert(nChannel >= -1 && nChannel < kChannelMax);
|
2019-09-19 22:42:45 +00:00
|
|
|
DICTNODE *hSfx = gSoundRes.Lookup(nSound, "SFX");
|
|
|
|
if (!hSfx)
|
|
|
|
return;
|
|
|
|
SFX *pEffect = (SFX*)gSoundRes.Lock(hSfx);
|
|
|
|
dassert(pEffect != NULL);
|
|
|
|
SAMPLE2D *pChannel;
|
|
|
|
if (nChannel == -1)
|
|
|
|
pChannel = FindChannel();
|
|
|
|
else
|
|
|
|
pChannel = &Channel[nChannel];
|
|
|
|
if (pChannel->at0 > 0)
|
|
|
|
sndKillSound(pChannel);
|
|
|
|
pChannel->at5 = gSoundRes.Lookup(pEffect->rawName, "RAW");
|
|
|
|
if (!pChannel->at5)
|
|
|
|
return;
|
|
|
|
if (nVolume < 0)
|
|
|
|
nVolume = pEffect->relVol;
|
2019-10-30 23:50:45 +00:00
|
|
|
int nSize = pChannel->at5->Size();
|
2019-09-19 22:42:45 +00:00
|
|
|
int nLoopEnd = nSize - 1;
|
|
|
|
if (nLoopEnd < 0)
|
|
|
|
nLoopEnd = 0;
|
|
|
|
if (nSize <= 0)
|
|
|
|
return;
|
|
|
|
char *pData = (char*)gSoundRes.Lock(pChannel->at5);
|
|
|
|
if (nChannel < 0)
|
|
|
|
bLoop = false;
|
|
|
|
if (bLoop)
|
|
|
|
{
|
|
|
|
pChannel->at0 = FX_PlayLoopedRaw(pData, nSize, pData + pEffect->loopStart, pData + nLoopEnd, sndGetRate(pEffect->format),
|
|
|
|
0, nVolume, nVolume, nVolume, nVolume, 1.f, (intptr_t)&pChannel->at0);
|
|
|
|
pChannel->at4 |= 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pChannel->at0 = FX_PlayRaw(pData, nSize, sndGetRate(pEffect->format), 0, nVolume, nVolume, nVolume, nVolume, 1.f, (intptr_t)&pChannel->at0);
|
|
|
|
pChannel->at4 &= ~1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndStartWavID(unsigned int nSound, int nVolume, int nChannel)
|
|
|
|
{
|
2019-10-28 21:19:50 +00:00
|
|
|
if (!SoundEnabled())
|
2019-09-19 22:42:45 +00:00
|
|
|
return;
|
2019-10-25 20:53:41 +00:00
|
|
|
dassert(nChannel >= -1 && nChannel < kChannelMax);
|
2019-09-19 22:42:45 +00:00
|
|
|
SAMPLE2D *pChannel;
|
|
|
|
if (nChannel == -1)
|
|
|
|
pChannel = FindChannel();
|
|
|
|
else
|
|
|
|
pChannel = &Channel[nChannel];
|
|
|
|
if (pChannel->at0 > 0)
|
|
|
|
sndKillSound(pChannel);
|
|
|
|
pChannel->at5 = gSoundRes.Lookup(nSound, "WAV");
|
|
|
|
if (!pChannel->at5)
|
|
|
|
return;
|
|
|
|
char *pData = (char*)gSoundRes.Lock(pChannel->at5);
|
2019-10-30 23:50:45 +00:00
|
|
|
pChannel->at0 = FX_Play(pData, pChannel->at5->Size(), 0, -1, 0, nVolume, nVolume, nVolume, nVolume, 1.f, (intptr_t)&pChannel->at0);
|
2019-09-19 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sndKillSound(SAMPLE2D *pChannel)
|
|
|
|
{
|
|
|
|
if (pChannel->at4 & 1)
|
|
|
|
{
|
|
|
|
FX_EndLooping(pChannel->at0);
|
|
|
|
pChannel->at4 &= ~1;
|
|
|
|
}
|
|
|
|
FX_StopSound(pChannel->at0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndStartWavDisk(const char *pzFile, int nVolume, int nChannel)
|
|
|
|
{
|
2019-10-25 20:53:41 +00:00
|
|
|
dassert(nChannel >= -1 && nChannel < kChannelMax);
|
2019-09-19 22:42:45 +00:00
|
|
|
SAMPLE2D *pChannel;
|
|
|
|
if (nChannel == -1)
|
|
|
|
pChannel = FindChannel();
|
|
|
|
else
|
|
|
|
pChannel = &Channel[nChannel];
|
|
|
|
if (pChannel->at0 > 0)
|
|
|
|
sndKillSound(pChannel);
|
2019-10-20 22:13:17 +00:00
|
|
|
auto hFile = kopenFileReader(pzFile, 0);
|
|
|
|
if (!hFile.isOpen())
|
2019-09-19 22:42:45 +00:00
|
|
|
return;
|
2019-10-20 22:13:17 +00:00
|
|
|
int nLength = hFile.GetLength();
|
2019-10-31 19:17:49 +00:00
|
|
|
char* pData = nullptr;
|
|
|
|
cacheAllocateBlock((intptr_t*)pData, nLength, nullptr); // use this obsolete call to indicate that some work is needed here!
|
2019-09-19 22:42:45 +00:00
|
|
|
if (!pData)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-10-20 22:13:17 +00:00
|
|
|
hFile.Read(pData, nLength);
|
2019-09-19 22:42:45 +00:00
|
|
|
pChannel->at5 = (DICTNODE*)pData;
|
|
|
|
pChannel->at4 |= 2;
|
|
|
|
pChannel->at0 = FX_Play(pData, nLength, 0, -1, 0, nVolume, nVolume, nVolume, nVolume, 1.f, (intptr_t)&pChannel->at0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndKillAllSounds(void)
|
|
|
|
{
|
2019-10-25 20:53:41 +00:00
|
|
|
for (int i = 0; i < kChannelMax; i++)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
|
|
|
SAMPLE2D *pChannel = &Channel[i];
|
|
|
|
if (pChannel->at0 > 0)
|
|
|
|
sndKillSound(pChannel);
|
|
|
|
if (pChannel->at5)
|
|
|
|
{
|
|
|
|
if (pChannel->at4 & 2)
|
|
|
|
{
|
|
|
|
pChannel->at4 &= ~2;
|
|
|
|
}
|
2019-10-31 19:17:49 +00:00
|
|
|
else // This 'else' needs to be removed once the file system is up (when cacheAllocateBlock gets replaced.)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
|
|
|
gSoundRes.Unlock(pChannel->at5);
|
|
|
|
}
|
|
|
|
pChannel->at5 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sndProcess(void)
|
|
|
|
{
|
2019-10-25 20:53:41 +00:00
|
|
|
for (int i = 0; i < kChannelMax; i++)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
|
|
|
if (Channel[i].at0 <= 0 && Channel[i].at5)
|
|
|
|
{
|
|
|
|
if (Channel[i].at4 & 2)
|
|
|
|
{
|
|
|
|
Channel[i].at4 &= ~2;
|
|
|
|
}
|
2019-10-31 19:17:49 +00:00
|
|
|
else // This 'else' needs to be removed once the file system is up (when cacheAllocateBlock gets replaced.)
|
2019-09-19 22:42:45 +00:00
|
|
|
{
|
|
|
|
gSoundRes.Unlock(Channel[i].at5);
|
|
|
|
}
|
|
|
|
Channel[i].at5 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitSoundDevice(void)
|
|
|
|
{
|
|
|
|
#ifdef MIXERTYPEWIN
|
|
|
|
void *initdata = (void *)win_gethwnd(); // used for DirectSound
|
|
|
|
#else
|
|
|
|
void *initdata = NULL;
|
|
|
|
#endif
|
|
|
|
int nStatus;
|
2019-10-22 00:01:05 +00:00
|
|
|
nStatus = FX_Init(snd_numvoices, snd_numchannels, snd_mixrate, initdata);
|
2019-09-19 22:42:45 +00:00
|
|
|
if (nStatus != 0)
|
|
|
|
{
|
|
|
|
initprintf("InitSoundDevice: %s\n", FX_ErrorString(nStatus));
|
|
|
|
return;
|
|
|
|
}
|
2019-10-22 00:01:05 +00:00
|
|
|
snd_reversestereo.Callback();
|
|
|
|
snd_fxvolume.Callback();
|
2019-09-19 22:42:45 +00:00
|
|
|
FX_SetCallBack(SoundCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeinitSoundDevice(void)
|
|
|
|
{
|
|
|
|
int nStatus = FX_Shutdown();
|
|
|
|
if (nStatus != 0)
|
|
|
|
ThrowError(FX_ErrorString(nStatus));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool sndActive = false;
|
|
|
|
|
|
|
|
void sndTerm(void)
|
|
|
|
{
|
|
|
|
if (!sndActive)
|
|
|
|
return;
|
|
|
|
sndActive = false;
|
|
|
|
sndStopSong();
|
|
|
|
DeinitSoundDevice();
|
2019-11-10 22:58:51 +00:00
|
|
|
//DeinitMusicDevice();
|
2019-09-19 22:42:45 +00:00
|
|
|
}
|
|
|
|
extern char *pUserSoundRFF;
|
|
|
|
void sndInit(void)
|
|
|
|
{
|
|
|
|
memset(Channel, 0, sizeof(Channel));
|
2019-11-10 22:58:51 +00:00
|
|
|
#if 0
|
2019-09-19 22:42:45 +00:00
|
|
|
pSongPtr = NULL;
|
|
|
|
nSongSize = 0;
|
|
|
|
bWaveMusic = false;
|
|
|
|
nWaveMusicHandle = -1;
|
2019-11-10 22:58:51 +00:00
|
|
|
#endif
|
2019-09-19 22:42:45 +00:00
|
|
|
InitSoundDevice();
|
2019-11-10 22:58:51 +00:00
|
|
|
//InitMusicDevice();
|
2019-09-19 22:42:45 +00:00
|
|
|
//atexit(sndTerm);
|
|
|
|
sndActive = true;
|
|
|
|
}
|
2019-09-22 06:39:22 +00:00
|
|
|
|
|
|
|
END_BLD_NS
|