Load mono copies of multichannel sounds that are used in 3D

This commit is contained in:
Chris Robinson 2016-05-01 11:01:59 -07:00 committed by Christoph Oelckers
parent b443ac8f71
commit 0d402618a3
10 changed files with 154 additions and 42 deletions

View file

@ -500,6 +500,7 @@ int S_AddSoundLump (const char *logicalname, int lump)
sfxinfo_t newsfx;
newsfx.data.Clear();
newsfx.data3d.Clear();
newsfx.name = logicalname;
newsfx.lumpnum = lump;
newsfx.next = 0;

View file

@ -98,6 +98,7 @@ extern float S_GetMusicVolume (const char *music);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static void S_LoadSound3D(sfxinfo_t *sfx);
static bool S_CheckSoundLimit(sfxinfo_t *sfx, const FVector3 &pos, int near_limit, float limit_range, AActor *actor, int channel);
static bool S_IsChannelUsed(AActor *actor, int channel, int *seen);
static void S_ActivatePlayList(bool goBack);
@ -546,8 +547,11 @@ void S_UnloadSound (sfxinfo_t *sfx)
{
if (sfx->data.isValid())
{
if(sfx->data3d.isValid() && sfx->data != sfx->data3d)
GSnd->UnloadSound(sfx->data3d);
GSnd->UnloadSound(sfx->data);
sfx->data.Clear();
sfx->data3d.Clear();
DPrintf("Unloaded sound \"%s\" (%td)\n", sfx->name.GetChars(), sfx - &S_sfx[0]);
}
}
@ -1098,9 +1102,10 @@ static FSoundChan *S_StartSound(AActor *actor, const sector_t *sec, const FPolyO
if (attenuation > 0)
{
S_LoadSound3D(sfx);
SoundListener listener;
S_SetListener(listener, players[consoleplayer].camera);
chan = (FSoundChan*)GSnd->StartSound3D (sfx->data, &listener, float(volume), rolloff, float(attenuation), pitch, basepriority, pos, vel, channel, startflags, NULL);
chan = (FSoundChan*)GSnd->StartSound3D (sfx->data3d, &listener, float(volume), rolloff, float(attenuation), pitch, basepriority, pos, vel, channel, startflags, NULL);
}
else
{
@ -1194,12 +1199,13 @@ void S_RestartSound(FSoundChan *chan)
return;
}
S_LoadSound3D(sfx);
SoundListener listener;
S_SetListener(listener, players[consoleplayer].camera);
chan->ChanFlags &= ~(CHAN_EVICTED|CHAN_ABSTIME);
ochan = (FSoundChan*)GSnd->StartSound3D(sfx->data, &listener, chan->Volume, &chan->Rolloff, chan->DistanceScale, chan->Pitch,
chan->Priority, pos, vel, chan->EntChannel, startflags, chan);
ochan = (FSoundChan*)GSnd->StartSound3D(sfx->data3d, &listener, chan->Volume, &chan->Rolloff, chan->DistanceScale, chan->Pitch,
chan->Priority, pos, vel, chan->EntChannel, startflags, chan);
}
else
{
@ -1339,31 +1345,35 @@ sfxinfo_t *S_LoadSound(sfxinfo_t *sfx)
BYTE *sfxdata = new BYTE[size];
wlump.Read(sfxdata, size);
SDWORD dmxlen = LittleLong(((SDWORD *)sfxdata)[1]);
std::pair<SoundHandle,bool> snd;
// If the sound is voc, use the custom loader.
if (strncmp ((const char *)sfxdata, "Creative Voice File", 19) == 0)
{
sfx->data = GSnd->LoadSoundVoc(sfxdata, size);
snd = GSnd->LoadSoundVoc(sfxdata, size);
}
// If the sound is raw, just load it as such.
else if (sfx->bLoadRAW)
{
sfx->data = GSnd->LoadSoundRaw(sfxdata, size, sfx->RawRate, 1, 8, sfx->LoopStart);
snd = GSnd->LoadSoundRaw(sfxdata, size, sfx->RawRate, 1, 8, sfx->LoopStart);
}
// Otherwise, try the sound as DMX format.
else if (((BYTE *)sfxdata)[0] == 3 && ((BYTE *)sfxdata)[1] == 0 && dmxlen <= size - 8)
{
int frequency = LittleShort(((WORD *)sfxdata)[1]);
if (frequency == 0) frequency = 11025;
sfx->data = GSnd->LoadSoundRaw(sfxdata+8, dmxlen, frequency, 1, 8, sfx->LoopStart);
snd = GSnd->LoadSoundRaw(sfxdata+8, dmxlen, frequency, 1, 8, sfx->LoopStart);
}
// If that fails, let the sound system try and figure it out.
else
{
sfx->data = GSnd->LoadSound(sfxdata, size);
snd = GSnd->LoadSound(sfxdata, size);
}
delete[] sfxdata;
sfx->data = snd.first;
if(snd.second)
sfx->data3d = sfx->data;
}
if (!sfx->data.isValid())
@ -1379,6 +1389,53 @@ sfxinfo_t *S_LoadSound(sfxinfo_t *sfx)
return sfx;
}
static void S_LoadSound3D(sfxinfo_t *sfx)
{
if (GSnd->IsNull()) return;
if(sfx->data3d.isValid())
return;
unsigned int i;
DPrintf("Loading monoized sound \"%s\" (%td)\n", sfx->name.GetChars(), sfx - &S_sfx[0]);
int size = Wads.LumpLength(sfx->lumpnum);
if(size <= 0) return;
FWadLump wlump = Wads.OpenLumpNum(sfx->lumpnum);
BYTE *sfxdata = new BYTE[size];
wlump.Read(sfxdata, size);
SDWORD dmxlen = LittleLong(((SDWORD *)sfxdata)[1]);
std::pair<SoundHandle,bool> snd;
// If the sound is voc, use the custom loader.
if (strncmp ((const char *)sfxdata, "Creative Voice File", 19) == 0)
{
snd = GSnd->LoadSoundVoc(sfxdata, size, true);
}
// If the sound is raw, just load it as such.
else if (sfx->bLoadRAW)
{
snd = GSnd->LoadSoundRaw(sfxdata, size, sfx->RawRate, 1, 8, sfx->LoopStart, true);
}
// Otherwise, try the sound as DMX format.
else if (((BYTE *)sfxdata)[0] == 3 && ((BYTE *)sfxdata)[1] == 0 && dmxlen <= size - 8)
{
int frequency = LittleShort(((WORD *)sfxdata)[1]);
if (frequency == 0) frequency = 11025;
snd = GSnd->LoadSoundRaw(sfxdata+8, dmxlen, frequency, 1, 8, sfx->LoopStart, -1, true);
}
// If that fails, let the sound system try and figure it out.
else
{
snd = GSnd->LoadSound(sfxdata, size, true);
}
delete[] sfxdata;
sfx->data3d = snd.first;
}
//==========================================================================
//
// S_CheckSingular

View file

@ -36,6 +36,9 @@ struct sfxinfo_t
// Next field is for use by the system sound interface.
// A non-null data means the sound has been loaded.
SoundHandle data;
// Also for the sound interface. Used for 3D positional
// sounds, may be the same as data.
SoundHandle data3d;
FString name; // [RH] Sound name defined in SNDINFO
int lumpnum; // lump number of sfx

View file

@ -2510,7 +2510,7 @@ void FMODSoundRenderer::UpdateSounds()
//
//==========================================================================
SoundHandle FMODSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend)
std::pair<SoundHandle,bool> FMODSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend, bool monoize)
{
FMOD_CREATESOUNDEXINFO exinfo;
SoundHandle retval = { NULL };
@ -2518,7 +2518,7 @@ SoundHandle FMODSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int frequ
if (length <= 0)
{
return retval;
return std::make_pair(retval, true);
}
InitCreateSoundExInfo(&exinfo);
@ -2550,7 +2550,7 @@ SoundHandle FMODSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int frequ
break;
default:
return retval;
return std::make_pair(retval, true);
}
const FMOD_MODE samplemode = FMOD_3D | FMOD_OPENMEMORY | FMOD_SOFTWARE | FMOD_OPENRAW;
@ -2561,7 +2561,7 @@ SoundHandle FMODSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int frequ
if (result != FMOD_OK)
{
DPrintf("Failed to allocate sample: Error %d\n", result);
return retval;
return std::make_pair(retval, true);
}
if (loopstart >= 0)
@ -2572,7 +2572,7 @@ SoundHandle FMODSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int frequ
}
retval.data = sample;
return retval;
return std::make_pair(retval, true);
}
//==========================================================================
@ -2581,12 +2581,12 @@ SoundHandle FMODSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int frequ
//
//==========================================================================
SoundHandle FMODSoundRenderer::LoadSound(BYTE *sfxdata, int length)
std::pair<SoundHandle,bool> FMODSoundRenderer::LoadSound(BYTE *sfxdata, int length, bool monoize)
{
FMOD_CREATESOUNDEXINFO exinfo;
SoundHandle retval = { NULL };
if (length == 0) return retval;
if (length == 0) return std::make_pair(retval, true);
InitCreateSoundExInfo(&exinfo);
exinfo.length = length;
@ -2599,11 +2599,11 @@ SoundHandle FMODSoundRenderer::LoadSound(BYTE *sfxdata, int length)
if (result != FMOD_OK)
{
DPrintf("Failed to allocate sample: Error %d\n", result);
return retval;
return std::make_pair(retval, true);
}
SetCustomLoopPts(sample);
retval.data = sample;
return retval;
return std::make_pair(retval, true);
}
//==========================================================================

View file

@ -15,8 +15,8 @@ public:
void SetSfxVolume (float volume);
void SetMusicVolume (float volume);
SoundHandle LoadSound(BYTE *sfxdata, int length);
SoundHandle LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1);
std::pair<SoundHandle,bool> LoadSound(BYTE *sfxdata, int length, bool monoize);
std::pair<SoundHandle,bool> LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1, bool monoize = false);
void UnloadSound (SoundHandle sfx);
unsigned int GetMSLength(SoundHandle sfx);
unsigned int GetSampleLength(SoundHandle sfx);

View file

@ -135,15 +135,15 @@ public:
void SetMusicVolume (float volume)
{
}
SoundHandle LoadSound(BYTE *sfxdata, int length)
std::pair<SoundHandle,bool> LoadSound(BYTE *sfxdata, int length, bool monoize)
{
SoundHandle retval = { NULL };
return retval;
return std::make_pair(retval, true);
}
SoundHandle LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend)
std::pair<SoundHandle,bool> LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend, bool monoize)
{
SoundHandle retval = { NULL };
return retval;
return std::make_pair(retval, true);
}
void UnloadSound (SoundHandle sfx)
{
@ -456,7 +456,7 @@ FString SoundStream::GetStats()
//
//==========================================================================
SoundHandle SoundRenderer::LoadSoundVoc(BYTE *sfxdata, int length)
std::pair<SoundHandle,bool> SoundRenderer::LoadSoundVoc(BYTE *sfxdata, int length, bool monoize)
{
BYTE * data = NULL;
int len, frequency, channels, bits, loopstart, loopend;
@ -600,7 +600,7 @@ SoundHandle SoundRenderer::LoadSoundVoc(BYTE *sfxdata, int length)
}
} while (false);
SoundHandle retval = LoadSoundRaw(data, len, frequency, channels, bits, loopstart, loopend);
std::pair<SoundHandle,bool> retval = LoadSoundRaw(data, len, frequency, channels, bits, loopstart, loopend, monoize);
if (data) delete[] data;
return retval;
}

View file

@ -95,9 +95,10 @@ public:
virtual bool IsNull() { return false; }
virtual void SetSfxVolume (float volume) = 0;
virtual void SetMusicVolume (float volume) = 0;
virtual SoundHandle LoadSound(BYTE *sfxdata, int length) = 0;
SoundHandle LoadSoundVoc(BYTE *sfxdata, int length);
virtual SoundHandle LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1) = 0;
// Returns a pair containing a sound handle and a boolean indicating the sound can be used in 3D.
virtual std::pair<SoundHandle,bool> LoadSound(BYTE *sfxdata, int length, bool monoize=false) = 0;
std::pair<SoundHandle,bool> LoadSoundVoc(BYTE *sfxdata, int length, bool monoize=false);
virtual std::pair<SoundHandle,bool> LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1, bool monoize = false) = 0;
virtual void UnloadSound (SoundHandle sfx) = 0; // unloads a sound from memory
virtual unsigned int GetMSLength(SoundHandle sfx) = 0; // Gets the length of a sound at its default frequency
virtual unsigned int GetSampleLength(SoundHandle sfx) = 0; // Gets the length of a sound at its default frequency

View file

@ -91,6 +91,11 @@ struct SoundHandle
bool isValid() const { return data != NULL; }
void Clear() { data = NULL; }
bool operator==(const SoundHandle &rhs) const
{ return data == rhs.data; }
bool operator!=(const SoundHandle &rhs) const
{ return !(*this == rhs); }
};
struct FISoundChannel

View file

@ -1019,11 +1019,11 @@ float OpenALSoundRenderer::GetOutputRate()
}
SoundHandle OpenALSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend)
std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend, bool monoize)
{
SoundHandle retval = { NULL };
if(length == 0) return retval;
if(length == 0) return std::make_pair(retval, true);
if(bits == -8)
{
@ -1033,6 +1033,33 @@ SoundHandle OpenALSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int fre
bits = -bits;
}
if(channels > 1 && monoize)
{
size_t frames = length / channels * 8 / bits;
if(bits == 16)
{
for(size_t i = 0;i < frames;i++)
{
int sum = 0;
for(int c = 0;c < channels;c++)
sum = ((short*)sfxdata)[i*channels + c];
((short*)sfxdata)[i] = sum / channels;
}
}
else if(bits == 8)
{
for(size_t i = 0;i < frames;i++)
{
int sum = 0;
for(int c = 0;c < channels;c++)
sum = sfxdata[i*channels + c] - 128;
sfxdata[i] = (sum / channels) + 128;
}
}
length /= channels;
channels = 1;
}
ALenum format = AL_NONE;
if(bits == 16)
{
@ -1048,7 +1075,7 @@ SoundHandle OpenALSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int fre
if(format == AL_NONE || frequency <= 0)
{
Printf("Unhandled format: %d bit, %d channel, %d hz\n", bits, channels, frequency);
return retval;
return std::make_pair(retval, true);
}
length -= length%(channels*bits/8);
@ -1061,7 +1088,7 @@ SoundHandle OpenALSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int fre
Printf("Failed to buffer data: %s\n", alGetString(err));
alDeleteBuffers(1, &buffer);
getALError();
return retval;
return std::make_pair(retval, true);
}
if((loopstart > 0 || loopend > 0) && AL.SOFT_loop_points)
@ -1085,10 +1112,10 @@ SoundHandle OpenALSoundRenderer::LoadSoundRaw(BYTE *sfxdata, int length, int fre
}
retval.data = MAKE_PTRID(buffer);
return retval;
return std::make_pair(retval, channels==1);
}
SoundHandle OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int length)
std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int length, bool monoize)
{
SoundHandle retval = { NULL };
MemoryReader reader((const char*)sfxdata, length);
@ -1098,15 +1125,15 @@ SoundHandle OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int length)
int srate;
SoundDecoder *decoder = CreateDecoder(&reader);
if(!decoder) return retval;
if(!decoder) return std::make_pair(retval, true);
decoder->getInfo(&srate, &chans, &type);
if(chans == ChannelConfig_Mono)
if(chans == ChannelConfig_Mono || monoize)
{
if(type == SampleType_UInt8) format = AL_FORMAT_MONO8;
if(type == SampleType_Int16) format = AL_FORMAT_MONO16;
}
if(chans == ChannelConfig_Stereo)
else if(chans == ChannelConfig_Stereo)
{
if(type == SampleType_UInt8) format = AL_FORMAT_STEREO8;
if(type == SampleType_Int16) format = AL_FORMAT_STEREO16;
@ -1117,10 +1144,28 @@ SoundHandle OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int length)
Printf("Unsupported audio format: %s, %s\n", GetChannelConfigName(chans),
GetSampleTypeName(type));
delete decoder;
return retval;
return std::make_pair(retval, true);
}
TArray<char> data = decoder->readAll();
if(chans != ChannelConfig_Mono && monoize)
{
// TODO: Handle this better if ChannelConfig ever gets more channel configurations.
size_t frames = data.Size() / 2 / (type == SampleType_Int16 ? 2 : 1);
if(type == SampleType_Int16)
{
short *sfxdata = (short*)&data[0];
for(size_t i = 0;i < frames;i++)
sfxdata[i] = (sfxdata[i*2 + 0]-0 + sfxdata[i*2 + 1]-0)/2;
}
else if(type == SampleType_UInt8)
{
BYTE *sfxdata = (BYTE*)&data[0];
for(size_t i = 0;i < frames;i++)
sfxdata[i] = (sfxdata[i*2 + 0]-128 + sfxdata[i*2 + 1]-128)/2 + 128;
}
data.Resize(data.Size()/2);
}
ALuint buffer = 0;
alGenBuffers(1, &buffer);
@ -1133,12 +1178,12 @@ SoundHandle OpenALSoundRenderer::LoadSound(BYTE *sfxdata, int length)
alDeleteBuffers(1, &buffer);
getALError();
delete decoder;
return retval;
return std::make_pair(retval, true);
}
retval.data = MAKE_PTRID(buffer);
delete decoder;
return retval;
return std::make_pair(retval, (chans == ChannelConfig_Mono || monoize));
}
void OpenALSoundRenderer::UnloadSound(SoundHandle sfx)

View file

@ -71,8 +71,8 @@ public:
virtual void SetSfxVolume(float volume);
virtual void SetMusicVolume(float volume);
virtual SoundHandle LoadSound(BYTE *sfxdata, int length);
virtual SoundHandle LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1);
virtual std::pair<SoundHandle,bool> LoadSound(BYTE *sfxdata, int length, bool monoize);
virtual std::pair<SoundHandle,bool> LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1, bool monoize = false);
virtual void UnloadSound(SoundHandle sfx);
virtual unsigned int GetMSLength(SoundHandle sfx);
virtual unsigned int GetSampleLength(SoundHandle sfx);