From 21af612224a2a16f50a3b186c5dfb0e50f0818f8 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 26 Apr 2017 20:51:06 +0200 Subject: [PATCH] - avoid double loading sound data for monoization. --- src/s_sound.cpp | 92 ++++++++++++++------------ src/s_sound.h | 4 +- src/sound/i_sound.cpp | 14 ++-- src/sound/i_sound.h | 13 +++- src/sound/i_soundinternal.h | 2 +- src/sound/oalsound.cpp | 117 ++++++++++++++++++++++++++++++---- src/sound/oalsound.h | 3 +- src/sound/sndfile_decoder.cpp | 6 +- src/sound/sndfile_decoder.h | 2 +- 9 files changed, 189 insertions(+), 64 deletions(-) diff --git a/src/s_sound.cpp b/src/s_sound.cpp index ccf5f554a..7f1d9dfb9 100644 --- a/src/s_sound.cpp +++ b/src/s_sound.cpp @@ -126,7 +126,7 @@ extern float S_GetMusicVolume (const char *music); // PRIVATE FUNCTION PROTOTYPES --------------------------------------------- -static void S_LoadSound3D(sfxinfo_t *sfx); +static void S_LoadSound3D(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer); 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); @@ -577,7 +577,10 @@ void S_CacheSound (sfxinfo_t *sfx) } else { - S_LoadSound(sfx); + // Since we do not know in what format the sound will be used, we have to cache both. + FSoundLoadBuffer SoundBuffer; + S_LoadSound(sfx, &SoundBuffer); + S_LoadSound3D(sfx, &SoundBuffer); sfx->bUsed = true; } } @@ -906,6 +909,7 @@ static FSoundChan *S_StartSound(AActor *actor, const sector_t *sec, const FPolyO FSoundChan *chan; FVector3 pos, vel; FRolloffInfo *rolloff; + FSoundLoadBuffer SoundBuffer; if (sound_id <= 0 || volume <= 0 || nosfx || nosound ) return NULL; @@ -1050,7 +1054,7 @@ static FSoundChan *S_StartSound(AActor *actor, const sector_t *sec, const FPolyO } // Make sure the sound is loaded. - sfx = S_LoadSound(sfx); + sfx = S_LoadSound(sfx, &SoundBuffer); // The empty sound never plays. if (sfx->lumpnum == sfx_empty) @@ -1150,7 +1154,7 @@ static FSoundChan *S_StartSound(AActor *actor, const sector_t *sec, const FPolyO if (attenuation > 0) { - S_LoadSound3D(sfx); + S_LoadSound3D(sfx, &SoundBuffer); SoundListener listener; S_SetListener(listener, players[consoleplayer].camera); chan = (FSoundChan*)GSnd->StartSound3D (sfx->data3d, &listener, float(volume), rolloff, float(attenuation), pitch, basepriority, pos, vel, channel, startflags, NULL); @@ -1213,12 +1217,13 @@ void S_RestartSound(FSoundChan *chan) FSoundChan *ochan; sfxinfo_t *sfx = &S_sfx[chan->SoundID]; + FSoundLoadBuffer SoundBuffer; // If this is a singular sound, don't play it if it's already playing. if (sfx->bSingular && S_CheckSingular(chan->SoundID)) return; - sfx = S_LoadSound(sfx); + sfx = S_LoadSound(sfx, &SoundBuffer); // The empty sound never plays. if (sfx->lumpnum == sfx_empty) @@ -1247,7 +1252,7 @@ void S_RestartSound(FSoundChan *chan) return; } - S_LoadSound3D(sfx); + S_LoadSound3D(sfx, &SoundBuffer); SoundListener listener; S_SetListener(listener, players[consoleplayer].camera); @@ -1392,7 +1397,7 @@ void S_PlaySound(AActor *a, int chan, FSoundID sid, float vol, float atten, bool // //========================================================================== -sfxinfo_t *S_LoadSound(sfxinfo_t *sfx) +sfxinfo_t *S_LoadSound(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer) { if (GSnd->IsNull()) return sfx; @@ -1452,7 +1457,7 @@ sfxinfo_t *S_LoadSound(sfxinfo_t *sfx) // If that fails, let the sound system try and figure it out. else { - snd = GSnd->LoadSound(sfxdata, size); + snd = GSnd->LoadSound(sfxdata, size, false, pBuffer); } delete[] sfxdata; @@ -1474,7 +1479,7 @@ sfxinfo_t *S_LoadSound(sfxinfo_t *sfx) return sfx; } -static void S_LoadSound3D(sfxinfo_t *sfx) +static void S_LoadSound3D(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer) { if (GSnd->IsNull()) return; @@ -1483,40 +1488,47 @@ static void S_LoadSound3D(sfxinfo_t *sfx) DPrintf(DMSG_NOTIFY, "Loading monoized sound \"%s\" (%td)\n", sfx->name.GetChars(), sfx - &S_sfx[0]); - int size = Wads.LumpLength(sfx->lumpnum); - if(size <= 0) return; + if (pBuffer->mBuffer.Size() > 0) + { + GSnd->LoadSoundBuffered(pBuffer, true); + } + else + { + int size = Wads.LumpLength(sfx->lumpnum); + if (size <= 0) return; - FWadLump wlump = Wads.OpenLumpNum(sfx->lumpnum); - uint8_t *sfxdata = new uint8_t[size]; - wlump.Read(sfxdata, size); - int32_t dmxlen = LittleLong(((int32_t *)sfxdata)[1]); - std::pair snd; + FWadLump wlump = Wads.OpenLumpNum(sfx->lumpnum); + uint8_t *sfxdata = new uint8_t[size]; + wlump.Read(sfxdata, size); + int32_t dmxlen = LittleLong(((int32_t *)sfxdata)[1]); + std::pair 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 (((uint8_t *)sfxdata)[0] == 3 && ((uint8_t *)sfxdata)[1] == 0 && dmxlen <= size - 8) - { - int frequency = LittleShort(((uint16_t *)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; + // 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 (((uint8_t *)sfxdata)[0] == 3 && ((uint8_t *)sfxdata)[1] == 0 && dmxlen <= size - 8) + { + int frequency = LittleShort(((uint16_t *)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, pBuffer); + } + delete[] sfxdata; - sfx->data3d = snd.first; + sfx->data3d = snd.first; + } } //========================================================================== diff --git a/src/s_sound.h b/src/s_sound.h index 4d67c0514..3a64ba76f 100644 --- a/src/s_sound.h +++ b/src/s_sound.h @@ -212,6 +212,7 @@ struct FSoundChan : public FISoundChannel float Point[3]; // Sound is not attached to any source. }; }; + extern FSoundChan *Channels; void S_ReturnChannel(FSoundChan *chan); @@ -299,6 +300,7 @@ void S_PlaySound(AActor *a, int chan, FSoundID sid, float vol, float atten, bool #define ATTN_IDLE 1.001f #define ATTN_STATIC 3.f // diminish very rapidly with distance +struct FSoundLoadBuffer; int S_PickReplacement (int refid); void S_CacheRandomSound (sfxinfo_t *sfx); @@ -384,7 +386,7 @@ int S_AddPlayerSoundExisting (const char *playerclass, const int gender, int ref void S_MarkPlayerSounds (const char *playerclass); void S_ShrinkPlayerSoundLists (); void S_UnloadSound (sfxinfo_t *sfx); -sfxinfo_t *S_LoadSound(sfxinfo_t *sfx); +sfxinfo_t *S_LoadSound(sfxinfo_t *sfx, FSoundLoadBuffer *pBuffer = nullptr); unsigned int S_GetMSLength(FSoundID sound); void S_ParseMusInfo(); bool S_ParseTimeTag(const char *tag, bool *as_samples, unsigned int *time); diff --git a/src/sound/i_sound.cpp b/src/sound/i_sound.cpp index e0b0dd280..939734e2f 100644 --- a/src/sound/i_sound.cpp +++ b/src/sound/i_sound.cpp @@ -120,7 +120,7 @@ public: void SetMusicVolume (float volume) { } - std::pair LoadSound(uint8_t *sfxdata, int length, bool monoize) + std::pair LoadSound(uint8_t *sfxdata, int length, bool monoize, FSoundLoadBuffer *pBuffer) { SoundHandle retval = { NULL }; return std::make_pair(retval, true); @@ -576,6 +576,12 @@ std::pair SoundRenderer::LoadSoundVoc(uint8_t *sfxdata, int le return retval; } +std::pair SoundRenderer::LoadSoundBuffered(FSoundLoadBuffer *buffer, bool monoize) +{ + SoundHandle retval = { NULL }; + return std::make_pair(retval, true); +} + SoundDecoder *SoundRenderer::CreateDecoder(FileReader *reader) { SoundDecoder *decoder = NULL; @@ -604,14 +610,14 @@ SoundDecoder *SoundRenderer::CreateDecoder(FileReader *reader) // Default readAll implementation, for decoders that can't do anything better -TArray SoundDecoder::readAll() +TArray SoundDecoder::readAll() { - TArray output; + TArray output; unsigned total = 0; unsigned got; output.Resize(total+32768); - while((got=(unsigned)read(&output[total], output.Size()-total)) > 0) + while((got=(unsigned)read((char*)&output[total], output.Size()-total)) > 0) { total += got; output.Resize(total*2); diff --git a/src/sound/i_sound.h b/src/sound/i_sound.h index ed6473bd8..d6c4a7d78 100644 --- a/src/sound/i_sound.h +++ b/src/sound/i_sound.h @@ -87,6 +87,16 @@ typedef bool (*SoundStreamCallback)(SoundStream *stream, void *buff, int len, vo struct SoundDecoder; class MIDIDevice; +struct FSoundLoadBuffer +{ + TArray mBuffer; + uint32_t loop_start; + uint32_t loop_end; + ChannelConfig chans; + SampleType type; + int srate; +}; + class SoundRenderer { public: @@ -97,9 +107,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 LoadSound(uint8_t *sfxdata, int length, bool monoize=false) = 0; + virtual std::pair LoadSound(uint8_t *sfxdata, int length, bool monoize=false, FSoundLoadBuffer *pBuffer = nullptr) = 0; std::pair LoadSoundVoc(uint8_t *sfxdata, int length, bool monoize=false); virtual std::pair LoadSoundRaw(uint8_t *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1, bool monoize = false) = 0; + virtual std::pair LoadSoundBuffered(FSoundLoadBuffer *buffer, bool monoize); 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 diff --git a/src/sound/i_soundinternal.h b/src/sound/i_soundinternal.h index 47d2b923e..b1ff4efce 100644 --- a/src/sound/i_soundinternal.h +++ b/src/sound/i_soundinternal.h @@ -131,7 +131,7 @@ struct SoundDecoder virtual void getInfo(int *samplerate, ChannelConfig *chans, SampleType *type) = 0; virtual size_t read(char *buffer, size_t bytes) = 0; - virtual TArray readAll(); + virtual TArray readAll(); virtual bool seek(size_t ms_offset, bool ms, bool mayrestart) = 0; virtual size_t getSampleOffset() = 0; virtual size_t getSampleLength() { return 0; } diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index d9b53b0d5..30aa8b3e1 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -1205,7 +1205,7 @@ std::pair OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata, void FindLoopTags(FileReader *fr, uint32_t *start, bool *startass, uint32_t *end, bool *endass); -std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int length, bool monoize) +std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int length, bool monoize, FSoundLoadBuffer *pBuffer) { SoundHandle retval = { NULL }; MemoryReader reader((const char*)sfxdata, length); @@ -1223,29 +1223,30 @@ std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int } std::unique_ptr decoder(CreateDecoder(&reader)); - if(!decoder) return std::make_pair(retval, true); + if (!decoder) return std::make_pair(retval, true); decoder->getInfo(&srate, &chans, &type); int samplesize = 1; - if(chans == ChannelConfig_Mono || monoize) + if (chans == ChannelConfig_Mono || monoize) { - if(type == SampleType_UInt8) format = AL_FORMAT_MONO8, samplesize = 1; - if(type == SampleType_Int16) format = AL_FORMAT_MONO16, samplesize = 2; + if (type == SampleType_UInt8) format = AL_FORMAT_MONO8, samplesize = 1; + if (type == SampleType_Int16) format = AL_FORMAT_MONO16, samplesize = 2; } - else if(chans == ChannelConfig_Stereo) + else if (chans == ChannelConfig_Stereo) { - if(type == SampleType_UInt8) format = AL_FORMAT_STEREO8, samplesize = 2; - if(type == SampleType_Int16) format = AL_FORMAT_STEREO16, samplesize = 4; + if (type == SampleType_UInt8) format = AL_FORMAT_STEREO8, samplesize = 2; + if (type == SampleType_Int16) format = AL_FORMAT_STEREO16, samplesize = 4; } - if(format == AL_NONE) + if (format == AL_NONE) { Printf("Unsupported audio format: %s, %s\n", GetChannelConfigName(chans), - GetSampleTypeName(type)); + GetSampleTypeName(type)); return std::make_pair(retval, true); } - TArray data = decoder->readAll(); + TArray data = decoder->readAll(); + if(chans != ChannelConfig_Mono && monoize) { size_t chancount = GetChannelCount(chans); @@ -1289,7 +1290,7 @@ std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int } if (!startass) loop_start = Scale(loop_start, srate, 1000); - if (!endass) loop_end = Scale(loop_end, srate, 1000); + if (!endass && loop_end != ~0u) loop_end = Scale(loop_end, srate, 1000); const uint32_t samples = data.Size() / samplesize; if (loop_start > samples) loop_start = 0; if (loop_end > samples) loop_end = samples; @@ -1302,6 +1303,98 @@ std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int // no console messages here, please! } + retval.data = MAKE_PTRID(buffer); + if (pBuffer != nullptr) + { + pBuffer->mBuffer = std::move(data); + pBuffer->loop_start = loop_start; + pBuffer->loop_end = loop_end; + pBuffer->chans = chans; + pBuffer->type = type; + pBuffer->srate = srate; + } + return std::make_pair(retval, (chans == ChannelConfig_Mono || monoize)); +} + +std::pair OpenALSoundRenderer::LoadSoundBuffered(FSoundLoadBuffer *pBuffer, bool monoize) +{ + SoundHandle retval = { NULL }; + ALenum format = AL_NONE; + int srate = pBuffer->srate; + auto type = pBuffer->type; + auto chans = pBuffer->chans; + uint32_t loop_start = pBuffer->loop_start, loop_end = pBuffer->loop_end; + + if (chans == ChannelConfig_Mono || monoize) + { + if (type == SampleType_UInt8) format = AL_FORMAT_MONO8; + if (type == SampleType_Int16) format = AL_FORMAT_MONO16; + } + else if (chans == ChannelConfig_Stereo) + { + if (type == SampleType_UInt8) format = AL_FORMAT_STEREO8; + if (type == SampleType_Int16) format = AL_FORMAT_STEREO16; + } + + if (format == AL_NONE) + { + Printf("Unsupported audio format: %s, %s\n", GetChannelConfigName(chans), + GetSampleTypeName(type)); + return std::make_pair(retval, true); + } + + TArray &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(unsigned(data.Size() / chancount)); + } + + ALenum err; + ALuint buffer = 0; + alGenBuffers(1, &buffer); + alBufferData(buffer, format, &data[0], data.Size(), srate); + if ((err = getALError()) != AL_NO_ERROR) + { + Printf("Failed to buffer data: %s\n", alGetString(err)); + alDeleteBuffers(1, &buffer); + getALError(); + return std::make_pair(retval, true); + } + + // the loop points were already validated by the previous load. + if ((loop_start > 0 || loop_end > 0) && loop_end > loop_start && AL.SOFT_loop_points) + { + ALint loops[2] = { static_cast(loop_start), static_cast(loop_end) }; + DPrintf(DMSG_NOTIFY, "Setting loop points %d -> %d\n", loops[0], loops[1]); + alBufferiv(buffer, AL_LOOP_POINTS_SOFT, loops); + // no console messages here, please! + } retval.data = MAKE_PTRID(buffer); return std::make_pair(retval, (chans == ChannelConfig_Mono || monoize)); diff --git a/src/sound/oalsound.h b/src/sound/oalsound.h index 82cb4a736..212ba5c46 100644 --- a/src/sound/oalsound.h +++ b/src/sound/oalsound.h @@ -87,7 +87,8 @@ public: virtual void SetSfxVolume(float volume); virtual void SetMusicVolume(float volume); - virtual std::pair LoadSound(uint8_t *sfxdata, int length, bool monoize); + virtual std::pair LoadSound(uint8_t *sfxdata, int length, bool monoize, FSoundLoadBuffer *buffer); + virtual std::pair LoadSoundBuffered(FSoundLoadBuffer *buffer, bool monoize); virtual std::pair LoadSoundRaw(uint8_t *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); diff --git a/src/sound/sndfile_decoder.cpp b/src/sound/sndfile_decoder.cpp index b403b46ed..a46f0146b 100644 --- a/src/sound/sndfile_decoder.cpp +++ b/src/sound/sndfile_decoder.cpp @@ -175,16 +175,16 @@ size_t SndFileDecoder::read(char *buffer, size_t bytes) return total * SndInfo.channels * 2; } -TArray SndFileDecoder::readAll() +TArray SndFileDecoder::readAll() { if(SndInfo.frames <= 0) return SoundDecoder::readAll(); int framesize = 2 * SndInfo.channels; - TArray output; + TArray output; output.Resize((unsigned)(SndInfo.frames * framesize)); - size_t got = read(&output[0], output.Size()); + size_t got = read((char*)&output[0], output.Size()); output.Resize((unsigned)got); return output; diff --git a/src/sound/sndfile_decoder.h b/src/sound/sndfile_decoder.h index 53fa6e6a9..0d5c83a41 100644 --- a/src/sound/sndfile_decoder.h +++ b/src/sound/sndfile_decoder.h @@ -16,7 +16,7 @@ struct SndFileDecoder : public SoundDecoder virtual void getInfo(int *samplerate, ChannelConfig *chans, SampleType *type); virtual size_t read(char *buffer, size_t bytes); - virtual TArray readAll(); + virtual TArray readAll(); virtual bool seek(size_t ms_offset, bool ms, bool mayrestart); virtual size_t getSampleOffset(); virtual size_t getSampleLength();