Remove the 'monoize' option from sound loading

Unnecessary with the AL_SOFT_source_spatialize extension, which has been
available for over a year.
This commit is contained in:
Chris Robinson 2020-01-29 10:29:27 -08:00 committed by Christoph Oelckers
parent 42fd84a09d
commit c095872b0a
6 changed files with 43 additions and 158 deletions

View File

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

View File

@ -108,10 +108,10 @@ public:
virtual void SetSfxVolume (float volume) = 0;
virtual void SetMusicVolume (float volume) = 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(uint8_t *sfxdata, int length, bool monoize=false, FSoundLoadBuffer *pBuffer = nullptr) = 0;
std::pair<SoundHandle,bool> LoadSoundVoc(uint8_t *sfxdata, int length, bool monoize=false);
virtual std::pair<SoundHandle,bool> LoadSoundRaw(uint8_t *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1, bool monoize = false) = 0;
virtual std::pair<SoundHandle, bool> LoadSoundBuffered(FSoundLoadBuffer *buffer, bool monoize);
virtual SoundHandle LoadSound(uint8_t *sfxdata, int length, FSoundLoadBuffer *pBuffer = nullptr) = 0;
SoundHandle LoadSoundVoc(uint8_t *sfxdata, int length);
virtual SoundHandle LoadSoundRaw(uint8_t *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1) = 0;
virtual SoundHandle LoadSoundBuffered(FSoundLoadBuffer *buffer);
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

@ -985,14 +985,11 @@ float OpenALSoundRenderer::GetOutputRate()
}
std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend, bool monoize)
SoundHandle OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend)
{
SoundHandle retval = { NULL };
if(length == 0) return std::make_pair(retval, true);
/* Only downmix to mono if we can't spatialize multi-channel sounds. */
monoize = monoize && !AL.SOFT_source_spatialize;
if(length == 0) return retval;
if(bits == -8)
{
@ -1002,33 +999,6 @@ std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata,
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)
{
@ -1044,7 +1014,7 @@ std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata,
if(format == AL_NONE || frequency <= 0)
{
Printf("Unhandled format: %d bit, %d channel, %d hz\n", bits, channels, frequency);
return std::make_pair(retval, true);
return retval;
}
length -= length%(channels*bits/8);
@ -1057,7 +1027,7 @@ std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata,
Printf("Failed to buffer data: %s\n", alGetString(err));
alDeleteBuffers(1, &buffer);
getALError();
return std::make_pair(retval, true);
return retval;
}
if((loopstart > 0 || loopend > 0) && AL.SOFT_loop_points)
@ -1081,10 +1051,10 @@ std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata,
}
retval.data = MAKE_PTRID(buffer);
return std::make_pair(retval, AL.SOFT_source_spatialize || channels==1);
return retval;
}
std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int length, bool monoize, FSoundLoadBuffer *pBuffer)
SoundHandle OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int length, FSoundLoadBuffer *pBuffer)
{
SoundHandle retval = { NULL };
ALenum format = AL_NONE;
@ -1094,19 +1064,14 @@ std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int
uint32_t loop_start = 0, loop_end = ~0u;
bool startass = false, endass = false;
/* Only downmix to mono if we can't spatialize multi-channel sounds. */
monoize = monoize && !AL.SOFT_source_spatialize;
FindLoopTags(sfxdata, length, &loop_start, &startass, &loop_end, &endass);
auto decoder = CreateDecoder(sfxdata, length, true);
if (!decoder)
{
return std::make_pair(retval, true);
}
return retval;
SoundDecoder_GetInfo(decoder, &srate, &chans, &type);
int samplesize = 1;
if (chans == ChannelConfig_Mono || monoize)
if (chans == ChannelConfig_Mono)
{
if (type == SampleType_UInt8) format = AL_FORMAT_MONO8, samplesize = 1;
if (type == SampleType_Int16) format = AL_FORMAT_MONO16, samplesize = 2;
@ -1122,7 +1087,7 @@ std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int
SoundDecoder_Close(decoder);
Printf("Unsupported audio format: %s, %s\n", GetChannelConfigName(chans),
GetSampleTypeName(type));
return std::make_pair(retval, true);
return retval;
}
std::vector<uint8_t> data;
@ -1138,36 +1103,6 @@ std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int
data.resize(total);
SoundDecoder_Close(decoder);
if(chans != ChannelConfig_Mono && monoize)
{
size_t chancount = GetChannelCount(chans);
size_t frames = data.size() / chancount /
(type == SampleType_Int16 ? 2 : 1);
if(type == SampleType_Int16)
{
short *sfxdata = (short*)&data[0];
for(size_t i = 0;i < frames;i++)
{
int sum = 0;
for(size_t c = 0;c < chancount;c++)
sum += sfxdata[i*chancount + c];
sfxdata[i] = short(sum / chancount);
}
}
else if(type == SampleType_UInt8)
{
uint8_t *sfxdata = (uint8_t*)&data[0];
for(size_t i = 0;i < frames;i++)
{
int sum = 0;
for(size_t c = 0;c < chancount;c++)
sum += sfxdata[i*chancount + c] - 128;
sfxdata[i] = uint8_t((sum / chancount) + 128);
}
}
data.resize((data.size()/chancount));
}
ALenum err;
ALuint buffer = 0;
alGenBuffers(1, &buffer);
@ -1177,7 +1112,7 @@ std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int
Printf("Failed to buffer data: %s\n", alGetString(err));
alDeleteBuffers(1, &buffer);
getALError();
return std::make_pair(retval, true);
return retval;
}
if (!startass) loop_start = Scale(loop_start, srate, 1000);
@ -1204,10 +1139,10 @@ std::pair<SoundHandle,bool> OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int
pBuffer->type = type;
pBuffer->srate = srate;
}
return std::make_pair(retval, AL.SOFT_source_spatialize || chans == ChannelConfig_Mono || monoize);
return retval;
}
std::pair<SoundHandle, bool> OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBuffer *pBuffer, bool monoize)
SoundHandle OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBuffer *pBuffer)
{
SoundHandle retval = { NULL };
ALenum format = AL_NONE;
@ -1216,10 +1151,7 @@ std::pair<SoundHandle, bool> OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBu
auto chans = pBuffer->chans;
uint32_t loop_start = pBuffer->loop_start, loop_end = pBuffer->loop_end;
/* Only downmix to mono if we can't spatialize multi-channel sounds. */
monoize = monoize && !AL.SOFT_source_spatialize;
if (chans == ChannelConfig_Mono || monoize)
if (chans == ChannelConfig_Mono)
{
if (type == SampleType_UInt8) format = AL_FORMAT_MONO8;
if (type == SampleType_Int16) format = AL_FORMAT_MONO16;
@ -1234,41 +1166,11 @@ std::pair<SoundHandle, bool> OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBu
{
Printf("Unsupported audio format: %s, %s\n", GetChannelConfigName(chans),
GetSampleTypeName(type));
return std::make_pair(retval, true);
return retval;
}
auto &data = pBuffer->mBuffer;
if (pBuffer->chans == ChannelConfig_Stereo && monoize)
{
size_t chancount = GetChannelCount(chans);
size_t frames = data.size() / chancount /
(type == SampleType_Int16 ? 2 : 1);
if (type == SampleType_Int16)
{
short *sfxdata = (short*)&data[0];
for (size_t i = 0; i < frames; i++)
{
int sum = 0;
for (size_t c = 0; c < chancount; c++)
sum += sfxdata[i*chancount + c];
sfxdata[i] = short(sum / chancount);
}
}
else if (type == SampleType_UInt8)
{
uint8_t *sfxdata = (uint8_t*)&data[0];
for (size_t i = 0; i < frames; i++)
{
int sum = 0;
for (size_t c = 0; c < chancount; c++)
sum += sfxdata[i*chancount + c] - 128;
sfxdata[i] = uint8_t((sum / chancount) + 128);
}
}
data.resize(data.size() / chancount);
}
ALenum err;
ALuint buffer = 0;
alGenBuffers(1, &buffer);
@ -1278,7 +1180,7 @@ std::pair<SoundHandle, bool> OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBu
Printf("Failed to buffer data: %s\n", alGetString(err));
alDeleteBuffers(1, &buffer);
getALError();
return std::make_pair(retval, true);
return retval;
}
// the loop points were already validated by the previous load.
@ -1291,7 +1193,7 @@ std::pair<SoundHandle, bool> OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBu
}
retval.data = MAKE_PTRID(buffer);
return std::make_pair(retval, AL.SOFT_source_spatialize || chans == ChannelConfig_Mono || monoize);
return retval;
}
void OpenALSoundRenderer::UnloadSound(SoundHandle sfx)

View File

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

View File

@ -188,12 +188,9 @@ void SoundEngine::CacheSound (sfxinfo_t *sfx)
void SoundEngine::UnloadSound (sfxinfo_t *sfx)
{
if (sfx->data3d.isValid() && sfx->data != sfx->data3d)
GSnd->UnloadSound(sfx->data3d);
if (sfx->data.isValid())
GSnd->UnloadSound(sfx->data);
sfx->data.Clear();
sfx->data3d.Clear();
}
//==========================================================================
@ -579,7 +576,7 @@ FSoundChan *SoundEngine::StartSound(int type, const void *source,
if (attenuation > 0 && type != SOURCE_None)
{
LoadSound3D(sfx, &SoundBuffer);
chan = (FSoundChan*)GSnd->StartSound3D (sfx->data3d, &listener, float(volume), rolloff, float(attenuation), pitch, basepriority, pos, vel, channel, startflags, NULL);
chan = (FSoundChan*)GSnd->StartSound3D (sfx->data, &listener, float(volume), rolloff, float(attenuation), pitch, basepriority, pos, vel, channel, startflags, NULL);
}
else
{
@ -685,7 +682,7 @@ void SoundEngine::RestartChannel(FSoundChan *chan)
LoadSound3D(sfx, &SoundBuffer);
chan->ChanFlags &= ~(CHANF_EVICTED|CHANF_ABSTIME);
ochan = (FSoundChan*)GSnd->StartSound3D(sfx->data3d, &listener, chan->Volume, &chan->Rolloff, chan->DistanceScale, chan->Pitch,
ochan = (FSoundChan*)GSnd->StartSound3D(sfx->data, &listener, chan->Volume, &chan->Rolloff, chan->DistanceScale, chan->Pitch,
chan->Priority, pos, vel, chan->EntChannel, startflags, chan);
}
else
@ -745,34 +742,29 @@ sfxinfo_t *SoundEngine::LoadSound(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer)
if (size > 8)
{
int32_t dmxlen = LittleLong(((int32_t *)sfxdata.Data())[1]);
std::pair<SoundHandle,bool> snd;
// If the sound is voc, use the custom loader.
if (strncmp ((const char *)sfxdata.Data(), "Creative Voice File", 19) == 0)
{
snd = GSnd->LoadSoundVoc(sfxdata.Data(), size);
sfx->data = GSnd->LoadSoundVoc(sfxdata.Data(), size);
}
// If the sound is raw, just load it as such.
else if (sfx->bLoadRAW)
{
snd = GSnd->LoadSoundRaw(sfxdata.Data(), size, sfx->RawRate, 1, 8, sfx->LoopStart);
sfx->data = GSnd->LoadSoundRaw(sfxdata.Data(), size, sfx->RawRate, 1, 8, sfx->LoopStart);
}
// Otherwise, try the sound as DMX format.
else if (((uint8_t *)sfxdata.Data())[0] == 3 && ((uint8_t *)sfxdata.Data())[1] == 0 && dmxlen <= size - 8)
{
int frequency = LittleShort(((uint16_t *)sfxdata.Data())[1]);
if (frequency == 0) frequency = 11025;
snd = GSnd->LoadSoundRaw(sfxdata.Data()+8, dmxlen, frequency, 1, 8, sfx->LoopStart);
sfx->data = GSnd->LoadSoundRaw(sfxdata.Data()+8, dmxlen, frequency, 1, 8, sfx->LoopStart);
}
// If that fails, let the sound system try and figure it out.
else
{
snd = GSnd->LoadSound(sfxdata.Data(), size, false, pBuffer);
sfx->data = GSnd->LoadSound(sfxdata.Data(), size, pBuffer);
}
sfx->data = snd.first;
if(snd.second)
sfx->data3d = sfx->data;
}
if (!sfx->data.isValid())
@ -792,16 +784,14 @@ void SoundEngine::LoadSound3D(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer)
{
if (GSnd->IsNull()) return;
if(sfx->data3d.isValid())
if(sfx->data.isValid())
return;
//DPrintf(DMSG_NOTIFY, "Loading monoized sound \"%s\" (%td)\n", sfx->name.GetChars(), sfx - &S_sfx[0]);
std::pair<SoundHandle, bool> snd;
if (pBuffer->mBuffer.size() > 0)
{
snd = GSnd->LoadSoundBuffered(pBuffer, true);
sfx->data = GSnd->LoadSoundBuffered(pBuffer);
}
else if (sfx->lumpnum >= 0)
{
@ -813,28 +803,26 @@ void SoundEngine::LoadSound3D(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer)
// If the sound is voc, use the custom loader.
if (strncmp((const char *)sfxdata.Data(), "Creative Voice File", 19) == 0)
{
snd = GSnd->LoadSoundVoc(sfxdata.Data(), size, true);
sfx->data = GSnd->LoadSoundVoc(sfxdata.Data(), size);
}
// If the sound is raw, just load it as such.
else if (sfx->bLoadRAW)
{
snd = GSnd->LoadSoundRaw(sfxdata.Data(), size, sfx->RawRate, 1, 8, sfx->LoopStart, true);
sfx->data = GSnd->LoadSoundRaw(sfxdata.Data(), size, sfx->RawRate, 1, 8, sfx->LoopStart);
}
// Otherwise, try the sound as DMX format.
else if (((uint8_t *)sfxdata.Data())[0] == 3 && ((uint8_t *)sfxdata.Data())[1] == 0 && dmxlen <= size - 8)
{
int frequency = LittleShort(((uint16_t *)sfxdata.Data())[1]);
if (frequency == 0) frequency = 11025;
snd = GSnd->LoadSoundRaw(sfxdata.Data() + 8, dmxlen, frequency, 1, 8, sfx->LoopStart, -1, true);
sfx->data = GSnd->LoadSoundRaw(sfxdata.Data() + 8, dmxlen, frequency, 1, 8, sfx->LoopStart, -1);
}
// If that fails, let the sound system try and figure it out.
else
{
snd = GSnd->LoadSound(sfxdata.Data(), size, true, pBuffer);
sfx->data = GSnd->LoadSound(sfxdata.Data(), size, pBuffer);
}
}
sfx->data3d = snd.first;
}
//==========================================================================
@ -1535,7 +1523,6 @@ int SoundEngine::AddSoundLump(const char* logicalname, int lump, int CurrentPitc
sfxinfo_t &newsfx = S_sfx.Last();
newsfx.data.Clear();
newsfx.data3d.Clear();
newsfx.name = logicalname;
newsfx.lumpnum = lump;
newsfx.next = 0;

View File

@ -18,9 +18,6 @@ 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
@ -59,7 +56,6 @@ struct sfxinfo_t
void Clear()
{
data.Clear();
data3d.Clear();
lumpnum = -1; // lump number of sfx
next = -1;
index = 0; // [RH] For hashing