Skip to content

Commit

Permalink
- allow setting loop points for WAV sounds or other simple formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Jan 15, 2023
1 parent 57695a3 commit 841402a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion source/common/audio/sound/i_sound.cpp
Expand Up @@ -131,7 +131,7 @@ class NullSoundRenderer : public SoundRenderer
void SetMusicVolume (float volume)
{
}
SoundHandle LoadSound(uint8_t *sfxdata, int length)
SoundHandle LoadSound(uint8_t *sfxdata, int length, int def_loop_start, int def_loop_end)
{
SoundHandle retval = { NULL };
return retval;
Expand Down
2 changes: 1 addition & 1 deletion source/common/audio/sound/i_sound.h
Expand Up @@ -105,7 +105,7 @@ class SoundRenderer
virtual bool IsNull() { return false; }
virtual void SetSfxVolume (float volume) = 0;
virtual void SetMusicVolume (float volume) = 0;
virtual SoundHandle LoadSound(uint8_t *sfxdata, int length) = 0;
virtual SoundHandle LoadSound(uint8_t *sfxdata, int length, int def_loop_start, int def_loop_end) = 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 void UnloadSound (SoundHandle sfx) = 0; // unloads a sound from memory
Expand Down
13 changes: 11 additions & 2 deletions source/common/audio/sound/oalsound.cpp
Expand Up @@ -1096,7 +1096,7 @@ SoundHandle OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata, int length, int
return retval;
}

SoundHandle OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int length)
SoundHandle OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int length, int def_loop_start, int def_loop_end)
{
SoundHandle retval = { NULL };
ALenum format = AL_NONE;
Expand All @@ -1106,7 +1106,16 @@ SoundHandle OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int length)
uint32_t loop_start = 0, loop_end = ~0u;
zmusic_bool startass = false, endass = false;

FindLoopTags(sfxdata, length, &loop_start, &startass, &loop_end, &endass);
if (def_loop_start < 0)
{
FindLoopTags(sfxdata, length, &loop_start, &startass, &loop_end, &endass);
}
else
{
loop_start = def_loop_start;
loop_end = def_loop_end;
startass = endass = true;
}
auto decoder = CreateDecoder(sfxdata, length, true);
if (!decoder)
return retval;
Expand Down
2 changes: 1 addition & 1 deletion source/common/audio/sound/oalsound.h
Expand Up @@ -34,7 +34,7 @@ class OpenALSoundRenderer : public SoundRenderer

virtual void SetSfxVolume(float volume);
virtual void SetMusicVolume(float volume);
virtual SoundHandle LoadSound(uint8_t *sfxdata, int length);
virtual SoundHandle LoadSound(uint8_t *sfxdata, int length, int def_loop_start, int def_loop_end);
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);
Expand Down
4 changes: 2 additions & 2 deletions source/common/audio/sound/s_sound.cpp
Expand Up @@ -382,7 +382,7 @@ static float CalcPitch(int pitchmask, float defpitch, float defpitchmax)
{
if (defpitchmax > 0.0 && defpitch != defpitchmax)
{
defpitch = pr_soundpitch.GenRand_Real1() * (defpitchmax - defpitch) + defpitch;
defpitch = (float)pr_soundpitch.GenRand_Real1() * (defpitchmax - defpitch) + defpitch;
}
return defpitch;
}
Expand Down Expand Up @@ -769,7 +769,7 @@ sfxinfo_t *SoundEngine::LoadSound(sfxinfo_t *sfx)
// If that fails, let the sound system try and figure it out.
else
{
sfx->data = GSnd->LoadSound(sfxdata.Data(), size);
sfx->data = GSnd->LoadSound(sfxdata.Data(), size, sfx->LoopStart, sfx->LoopEnd);
}
}

Expand Down
1 change: 1 addition & 0 deletions source/common/audio/sound/s_soundinternal.h
Expand Up @@ -102,6 +102,7 @@ constexpr FSoundID INVALID_SOUND = FSoundID::fromInt(-1);

int RawRate = 0; // Sample rate to use when bLoadRAW is true
int LoopStart = -1; // -1 means no specific loop defined
int LoopEnd = -1; // -1 means no specific loop defined

FSoundID link = NO_LINK;
constexpr static FSoundID NO_LINK = FSoundID::fromInt(-1);
Expand Down
15 changes: 15 additions & 0 deletions source/core/music/s_advsound.cpp
Expand Up @@ -62,6 +62,7 @@ enum SICommands
SI_PitchSet,
SI_PitchSetDuke,
SI_DukeFlags,
SI_Loop,
};


Expand Down Expand Up @@ -95,6 +96,7 @@ static const char *SICommandStrings[] =
"$pitchset",
"$pitchsetduke",
"$dukeflags",
"$loop",
NULL
};

Expand Down Expand Up @@ -380,6 +382,19 @@ static void S_AddSNDINFO (int lump)

}

case SI_Loop: {
// dukesound <logical name> <start> <end>
// Sets loop points for the given sound in samples. Only really useful for WAV - for Ogg and FLAC use the metadata they can contain.
sc.MustGetString();
auto sfxid = soundEngine->FindSoundTentative(sc.String, DEFAULT_LIMIT);
auto sfx = soundEngine->GetWritableSfx(sfxid);
sc.MustGetNumber();
sfx->LoopStart = sc.Number;
if (sc.CheckNumber())
sfx->LoopEnd = sc.Number;
break;
}


default:
{ // Got a logical sound mapping
Expand Down
2 changes: 1 addition & 1 deletion source/games/duke/src/sounds.cpp
Expand Up @@ -526,7 +526,7 @@ int S_PlaySound3D(FSoundID soundid, DDukeActor* actor, const DVector3& pos, int
{
if (explosion && underwater)
{
pitch = chan->Pitch? chan->Pitch * (0.55 / 128) : 0.55; // todo: fix pitch storage in backend.
pitch = float(chan->Pitch? chan->Pitch * (0.55 / 128) : 0.55); // todo: fix pitch storage in backend.
soundEngine->SetPitch(chan, pitch);
}
chan->UserData = (currentCommentarySound != NO_SOUND);
Expand Down

0 comments on commit 841402a

Please sign in to comment.