Skip to content

Commit

Permalink
Add volume parameter to sound play functions, fix unused volume
Browse files Browse the repository at this point in the history
The functions of the client component `CSounds` had a volume parameter which was unused. In some cases, the wrong value (`0`, presumably for the flags) was passed as the volume, which is now changed to `1.0f`. The player ground skid sound was previously set to play only at `0.25f` volume though this parameter was unused, which is also changed to `1.0f` to preserve the historic behavior.

A parameter is added to the engine sound play functions to directly set the volume without having to acquire the lock again.

Fix sound position not being respected for hook hit and ground jump sounds as the position parameter was ignored in the `CSounds::PlayAndRecord` function. Add TODOs for issues with this function for demo recording.

Parameters are ordered consistently and default parameter values are removed.

Duplicate code in the `CSounds` play functions is reduced by reusing the `PlaySample`/`PlaySampleAt` functions.
  • Loading branch information
Robyt3 committed Sep 23, 2024
1 parent b880683 commit 271e46c
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 61 deletions.
12 changes: 6 additions & 6 deletions src/engine/client/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ void CSound::SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height)
m_aVoices[VoiceId].m_Rectangle.m_Height = maximum(0.0f, Height);
}

ISound::CVoiceHandle CSound::Play(int ChannelId, int SampleId, int Flags, vec2 Position)
ISound::CVoiceHandle CSound::Play(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position)
{
const CLockScope LockScope(m_SoundLock);

Expand Down Expand Up @@ -855,7 +855,7 @@ ISound::CVoiceHandle CSound::Play(int ChannelId, int SampleId, int Flags, vec2 P
{
m_aVoices[VoiceId].m_Tick = 0;
}
m_aVoices[VoiceId].m_Vol = 255;
m_aVoices[VoiceId].m_Vol = (int)(clamp(Volume, 0.0f, 1.0f) * 255.0f);
m_aVoices[VoiceId].m_Flags = Flags;
m_aVoices[VoiceId].m_Position = Position;
m_aVoices[VoiceId].m_Falloff = 0.0f;
Expand All @@ -867,14 +867,14 @@ ISound::CVoiceHandle CSound::Play(int ChannelId, int SampleId, int Flags, vec2 P
return CreateVoiceHandle(VoiceId, Age);
}

ISound::CVoiceHandle CSound::PlayAt(int ChannelId, int SampleId, int Flags, vec2 Position)
ISound::CVoiceHandle CSound::PlayAt(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position)
{
return Play(ChannelId, SampleId, Flags | ISound::FLAG_POS, Position);
return Play(ChannelId, SampleId, Flags | ISound::FLAG_POS, Volume, Position);
}

ISound::CVoiceHandle CSound::Play(int ChannelId, int SampleId, int Flags)
ISound::CVoiceHandle CSound::Play(int ChannelId, int SampleId, int Flags, float Volume)
{
return Play(ChannelId, SampleId, Flags, vec2(0.0f, 0.0f));
return Play(ChannelId, SampleId, Flags, Volume, vec2(0.0f, 0.0f));
}

void CSound::Pause(int SampleId)
Expand Down
6 changes: 3 additions & 3 deletions src/engine/client/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ class CSound : public IEngineSound
void SetVoiceCircle(CVoiceHandle Voice, float Radius) override REQUIRES(!m_SoundLock);
void SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height) override REQUIRES(!m_SoundLock);

CVoiceHandle Play(int ChannelId, int SampleId, int Flags, vec2 Position) REQUIRES(!m_SoundLock);
CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, vec2 Position) override REQUIRES(!m_SoundLock);
CVoiceHandle Play(int ChannelId, int SampleId, int Flags) override REQUIRES(!m_SoundLock);
CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) REQUIRES(!m_SoundLock);
CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) override REQUIRES(!m_SoundLock);
CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume) override REQUIRES(!m_SoundLock);
void Pause(int SampleId) override REQUIRES(!m_SoundLock);
void Stop(int SampleId) override REQUIRES(!m_SoundLock);
void StopAll() override REQUIRES(!m_SoundLock);
Expand Down
4 changes: 2 additions & 2 deletions src/engine/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class ISound : public IInterface
virtual void SetVoiceCircle(CVoiceHandle Voice, float Radius) = 0;
virtual void SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height) = 0;

virtual CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, vec2 Position) = 0;
virtual CVoiceHandle Play(int ChannelId, int SampleId, int Flags) = 0;
virtual CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) = 0;
virtual CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume) = 0;
virtual void Pause(int SampleId) = 0;
virtual void Stop(int SampleId) = 0;
virtual void StopAll() = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/game/client/components/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ void CChat::AddLine(int ClientId, int Team, const char *pLine)
{
if(g_Config.m_SndServerMessage)
{
m_pClient->m_Sounds.Play(CSounds::CHN_GUI, SOUND_CHAT_SERVER, 0);
m_pClient->m_Sounds.Play(CSounds::CHN_GUI, SOUND_CHAT_SERVER, 1.0f);
m_aLastSoundPlayed[CHAT_SERVER] = Now;
}
}
Expand All @@ -840,7 +840,7 @@ void CChat::AddLine(int ClientId, int Team, const char *pLine)
Client()->Notify("DDNet Chat", aBuf);
if(g_Config.m_SndHighlight)
{
m_pClient->m_Sounds.Play(CSounds::CHN_GUI, SOUND_CHAT_HIGHLIGHT, 0);
m_pClient->m_Sounds.Play(CSounds::CHN_GUI, SOUND_CHAT_HIGHLIGHT, 1.0f);
m_aLastSoundPlayed[CHAT_HIGHLIGHT] = Now;
}

Expand All @@ -863,7 +863,7 @@ void CChat::AddLine(int ClientId, int Team, const char *pLine)
#endif
if(PlaySound)
{
m_pClient->m_Sounds.Play(CSounds::CHN_GUI, SOUND_CHAT_CLIENT, 0);
m_pClient->m_Sounds.Play(CSounds::CHN_GUI, SOUND_CHAT_CLIENT, 1.0f);
m_aLastSoundPlayed[CHAT_CLIENT] = Now;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/client/components/mapsounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void CMapSounds::OnRender()
if(!Source.m_pSource->m_Pan)
Flags |= ISound::FLAG_NO_PANNING;

Source.m_Voice = m_pClient->m_Sounds.PlaySampleAt(CSounds::CHN_MAPSOUND, m_aSounds[Source.m_Sound], 1.0f, vec2(fx2f(Source.m_pSource->m_Position.x), fx2f(Source.m_pSource->m_Position.y)), Flags);
Source.m_Voice = m_pClient->m_Sounds.PlaySampleAt(CSounds::CHN_MAPSOUND, m_aSounds[Source.m_Sound], Flags, 1.0f, vec2(fx2f(Source.m_pSource->m_Position.x), fx2f(Source.m_pSource->m_Position.y)));
Sound()->SetVoiceTimeOffset(Source.m_Voice, Offset);
Sound()->SetVoiceFalloff(Source.m_Voice, Source.m_pSource->m_Falloff / 255.0f);
switch(Source.m_pSource->m_Shape.m_Type)
Expand Down
2 changes: 1 addition & 1 deletion src/game/client/components/players.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ void CPlayers::RenderPlayer(
if(time() - m_SkidSoundTime > time_freq() / 10)
{
if(g_Config.m_SndGame)
m_pClient->m_Sounds.PlayAt(CSounds::CHN_WORLD, SOUND_PLAYER_SKID, 0.25f, Position);
m_pClient->m_Sounds.PlayAt(CSounds::CHN_WORLD, SOUND_PLAYER_SKID, 1.0f, Position);
m_SkidSoundTime = time();
}

Expand Down
53 changes: 15 additions & 38 deletions src/game/client/components/sounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,49 +180,26 @@ void CSounds::Enqueue(int Channel, int SetId)
m_aQueue[m_QueuePos++].m_SetId = SetId;
}

void CSounds::PlayAndRecord(int Channel, int SetId, float Vol, vec2 Pos)
void CSounds::PlayAndRecord(int Channel, int SetId, float Volume, vec2 Position)
{
// TODO: Volume and position are currently not recorded for sounds played with this function
// TODO: This also causes unsycned sounds during demo playback of demos recorded on high ping servers:
// https://github.com/ddnet/ddnet/issues/1282
CNetMsg_Sv_SoundGlobal Msg;
Msg.m_SoundId = SetId;
Client()->SendPackMsgActive(&Msg, MSGFLAG_NOSEND | MSGFLAG_RECORD);

Play(Channel, SetId, Vol);
PlayAt(Channel, SetId, Volume, Position);
}

void CSounds::Play(int Channel, int SetId, float Vol)
void CSounds::Play(int Channel, int SetId, float Volume)
{
if(m_pClient->m_SuppressEvents)
return;
if(Channel == CHN_MUSIC && !g_Config.m_SndMusic)
return;

int SampleId = GetSampleId(SetId);
if(SampleId == -1)
return;

int Flags = 0;
if(Channel == CHN_MUSIC)
Flags = ISound::FLAG_LOOP;

Sound()->Play(Channel, SampleId, Flags);
PlaySample(Channel, GetSampleId(SetId), 0, Volume);
}

void CSounds::PlayAt(int Channel, int SetId, float Vol, vec2 Pos)
void CSounds::PlayAt(int Channel, int SetId, float Volume, vec2 Position)
{
if(m_pClient->m_SuppressEvents)
return;
if(Channel == CHN_MUSIC && !g_Config.m_SndMusic)
return;

int SampleId = GetSampleId(SetId);
if(SampleId == -1)
return;

int Flags = 0;
if(Channel == CHN_MUSIC)
Flags = ISound::FLAG_LOOP;

Sound()->PlayAt(Channel, SampleId, Flags, Pos);
PlaySampleAt(Channel, GetSampleId(SetId), 0, Volume, Position);
}

void CSounds::Stop(int SetId)
Expand All @@ -248,24 +225,24 @@ bool CSounds::IsPlaying(int SetId)
return false;
}

ISound::CVoiceHandle CSounds::PlaySample(int Channel, int SampleId, float Vol, int Flags)
ISound::CVoiceHandle CSounds::PlaySample(int Channel, int SampleId, int Flags, float Volume)
{
if((Channel == CHN_MUSIC && !g_Config.m_SndMusic) || SampleId == -1)
if(m_pClient->m_SuppressEvents || (Channel == CHN_MUSIC && !g_Config.m_SndMusic) || SampleId == -1)
return ISound::CVoiceHandle();

if(Channel == CHN_MUSIC)
Flags |= ISound::FLAG_LOOP;

return Sound()->Play(Channel, SampleId, Flags);
return Sound()->Play(Channel, SampleId, Flags, Volume);
}

ISound::CVoiceHandle CSounds::PlaySampleAt(int Channel, int SampleId, float Vol, vec2 Pos, int Flags)
ISound::CVoiceHandle CSounds::PlaySampleAt(int Channel, int SampleId, int Flags, float Volume, vec2 Position)
{
if((Channel == CHN_MUSIC && !g_Config.m_SndMusic) || SampleId == -1)
if(m_pClient->m_SuppressEvents || (Channel == CHN_MUSIC && !g_Config.m_SndMusic) || SampleId == -1)
return ISound::CVoiceHandle();

if(Channel == CHN_MUSIC)
Flags |= ISound::FLAG_LOOP;

return Sound()->PlayAt(Channel, SampleId, Flags, Pos);
return Sound()->PlayAt(Channel, SampleId, Flags, Volume, Position);
}
10 changes: 5 additions & 5 deletions src/game/client/components/sounds.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ class CSounds : public CComponent

void ClearQueue();
void Enqueue(int Channel, int SetId);
void Play(int Channel, int SetId, float Vol);
void PlayAt(int Channel, int SetId, float Vol, vec2 Pos);
void PlayAndRecord(int Channel, int SetId, float Vol, vec2 Pos);
void Play(int Channel, int SetId, float Volume);
void PlayAt(int Channel, int SetId, float Volume, vec2 Position);
void PlayAndRecord(int Channel, int SetId, float Volume, vec2 Position);
void Stop(int SetId);
bool IsPlaying(int SetId);

ISound::CVoiceHandle PlaySample(int Channel, int SampleId, float Vol, int Flags = 0);
ISound::CVoiceHandle PlaySampleAt(int Channel, int SampleId, float Vol, vec2 Pos, int Flags = 0);
ISound::CVoiceHandle PlaySample(int Channel, int SampleId, int Flags, float Volume);
ISound::CVoiceHandle PlaySampleAt(int Channel, int SampleId, int Flags, float Volume, vec2 Position);
};

#endif
2 changes: 1 addition & 1 deletion src/game/client/components/voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void CVoting::OnMessage(int MsgType, void *pRawMsg)
char aBuf[512];
str_format(aBuf, sizeof(aBuf), "%s (%s)", m_aDescription, m_aReason);
Client()->Notify("DDNet Vote", aBuf);
m_pClient->m_Sounds.Play(CSounds::CHN_GUI, SOUND_CHAT_HIGHLIGHT, 0);
m_pClient->m_Sounds.Play(CSounds::CHN_GUI, SOUND_CHAT_HIGHLIGHT, 1.0f);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ void CEditor::DoAudioPreview(CUIRect View, const void *pPlayPauseButtonId, const
if(SampleId != m_ToolbarPreviewSound && m_ToolbarPreviewSound >= 0 && Sound()->IsPlaying(m_ToolbarPreviewSound))
Sound()->Pause(m_ToolbarPreviewSound);

Sound()->Play(CSounds::CHN_GUI, SampleId, ISound::FLAG_PREVIEW);
Sound()->Play(CSounds::CHN_GUI, SampleId, ISound::FLAG_PREVIEW, 1.0f);
}
}
}
Expand Down

0 comments on commit 271e46c

Please sign in to comment.