Add volume parameter to sound play functions, fix unused volume

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.
This commit is contained in:
Robert Müller 2024-05-26 11:22:57 +02:00
parent 986916400d
commit cc7aa4cb8e
11 changed files with 42 additions and 65 deletions

View file

@ -820,7 +820,7 @@ void CSound::SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height)
m_aVoices[VoiceId].m_Rectangle.m_Height = maximum(0.0f, 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); const CLockScope LockScope(m_SoundLock);
@ -856,7 +856,7 @@ ISound::CVoiceHandle CSound::Play(int ChannelId, int SampleId, int Flags, vec2 P
{ {
m_aVoices[VoiceId].m_Tick = 0; 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_Flags = Flags;
m_aVoices[VoiceId].m_Position = Position; m_aVoices[VoiceId].m_Position = Position;
m_aVoices[VoiceId].m_Falloff = 0.0f; m_aVoices[VoiceId].m_Falloff = 0.0f;
@ -868,14 +868,14 @@ ISound::CVoiceHandle CSound::Play(int ChannelId, int SampleId, int Flags, vec2 P
return CreateVoiceHandle(VoiceId, Age); 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) void CSound::Pause(int SampleId)

View file

@ -124,9 +124,9 @@ public:
void SetVoiceCircle(CVoiceHandle Voice, float Radius) override REQUIRES(!m_SoundLock); void SetVoiceCircle(CVoiceHandle Voice, float Radius) override REQUIRES(!m_SoundLock);
void SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height) 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 Play(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) REQUIRES(!m_SoundLock);
CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, vec2 Position) override 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) 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 Pause(int SampleId) override REQUIRES(!m_SoundLock);
void Stop(int SampleId) override REQUIRES(!m_SoundLock); void Stop(int SampleId) override REQUIRES(!m_SoundLock);
void StopAll() override REQUIRES(!m_SoundLock); void StopAll() override REQUIRES(!m_SoundLock);

View file

@ -86,8 +86,8 @@ public:
virtual void SetVoiceCircle(CVoiceHandle Voice, float Radius) = 0; virtual void SetVoiceCircle(CVoiceHandle Voice, float Radius) = 0;
virtual void SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height) = 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 PlayAt(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) = 0;
virtual CVoiceHandle Play(int ChannelId, int SampleId, int Flags) = 0; virtual CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume) = 0;
virtual void Pause(int SampleId) = 0; virtual void Pause(int SampleId) = 0;
virtual void Stop(int SampleId) = 0; virtual void Stop(int SampleId) = 0;
virtual void StopAll() = 0; virtual void StopAll() = 0;

View file

@ -822,7 +822,7 @@ void CChat::AddLine(int ClientId, int Team, const char *pLine)
{ {
if(g_Config.m_SndServerMessage) 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; m_aLastSoundPlayed[CHAT_SERVER] = Now;
} }
} }
@ -840,7 +840,7 @@ void CChat::AddLine(int ClientId, int Team, const char *pLine)
Client()->Notify("DDNet Chat", aBuf); Client()->Notify("DDNet Chat", aBuf);
if(g_Config.m_SndHighlight) 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; m_aLastSoundPlayed[CHAT_HIGHLIGHT] = Now;
} }
@ -863,7 +863,7 @@ void CChat::AddLine(int ClientId, int Team, const char *pLine)
#endif #endif
if(PlaySound) 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; m_aLastSoundPlayed[CHAT_CLIENT] = Now;
} }
} }

View file

@ -22,15 +22,15 @@ void CMapSounds::Play(int SoundId)
if(SoundId < 0 || SoundId >= m_Count) if(SoundId < 0 || SoundId >= m_Count)
return; return;
m_pClient->m_Sounds.PlaySample(CSounds::CHN_MAPSOUND, m_aSounds[SoundId], 1.0f, 0); m_pClient->m_Sounds.PlaySample(CSounds::CHN_MAPSOUND, m_aSounds[SoundId], 0, 1.0f);
} }
void CMapSounds::PlayAt(int SoundId, vec2 Pos) void CMapSounds::PlayAt(int SoundId, vec2 Position)
{ {
if(SoundId < 0 || SoundId >= m_Count) if(SoundId < 0 || SoundId >= m_Count)
return; return;
m_pClient->m_Sounds.PlaySampleAt(CSounds::CHN_MAPSOUND, m_aSounds[SoundId], 1.0f, Pos, 0); m_pClient->m_Sounds.PlaySampleAt(CSounds::CHN_MAPSOUND, m_aSounds[SoundId], 0, 1.0f, Position);
} }
void CMapSounds::OnMapLoad() void CMapSounds::OnMapLoad()
@ -163,7 +163,7 @@ void CMapSounds::OnRender()
if(!Source.m_pSource->m_Pan) if(!Source.m_pSource->m_Pan)
Flags |= ISound::FLAG_NO_PANNING; 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()->SetVoiceTimeOffset(Source.m_Voice, Offset);
Sound()->SetVoiceFalloff(Source.m_Voice, Source.m_pSource->m_Falloff / 255.0f); Sound()->SetVoiceFalloff(Source.m_Voice, Source.m_pSource->m_Falloff / 255.0f);
switch(Source.m_pSource->m_Shape.m_Type) switch(Source.m_pSource->m_Shape.m_Type)

View file

@ -34,7 +34,7 @@ public:
virtual int Sizeof() const override { return sizeof(*this); } virtual int Sizeof() const override { return sizeof(*this); }
void Play(int SoundId); void Play(int SoundId);
void PlayAt(int SoundId, vec2 Pos); void PlayAt(int SoundId, vec2 Position);
virtual void OnMapLoad() override; virtual void OnMapLoad() override;
virtual void OnRender() override; virtual void OnRender() override;

View file

@ -526,7 +526,7 @@ void CPlayers::RenderPlayer(
if(time() - m_SkidSoundTime > time_freq() / 10) if(time() - m_SkidSoundTime > time_freq() / 10)
{ {
if(g_Config.m_SndGame) 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(); m_SkidSoundTime = time();
} }

View file

@ -180,49 +180,26 @@ void CSounds::Enqueue(int Channel, int SetId)
m_aQueue[m_QueuePos++].m_SetId = 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 desync sounds during demo playback of demos recorded on high ping servers:
// https://github.com/ddnet/ddnet/issues/1282
CNetMsg_Sv_SoundGlobal Msg; CNetMsg_Sv_SoundGlobal Msg;
Msg.m_SoundId = SetId; Msg.m_SoundId = SetId;
Client()->SendPackMsgActive(&Msg, MSGFLAG_NOSEND | MSGFLAG_RECORD); 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) PlaySample(Channel, GetSampleId(SetId), 0, Volume);
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);
} }
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) PlaySampleAt(Channel, GetSampleId(SetId), 0, Volume, Position);
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);
} }
void CSounds::Stop(int SetId) void CSounds::Stop(int SetId)
@ -248,24 +225,24 @@ bool CSounds::IsPlaying(int SetId)
return false; 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(); return ISound::CVoiceHandle();
if(Channel == CHN_MUSIC) if(Channel == CHN_MUSIC)
Flags |= ISound::FLAG_LOOP; 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(); return ISound::CVoiceHandle();
if(Channel == CHN_MUSIC) if(Channel == CHN_MUSIC)
Flags |= ISound::FLAG_LOOP; Flags |= ISound::FLAG_LOOP;
return Sound()->PlayAt(Channel, SampleId, Flags, Pos); return Sound()->PlayAt(Channel, SampleId, Flags, Volume, Position);
} }

View file

@ -61,14 +61,14 @@ public:
void ClearQueue(); void ClearQueue();
void Enqueue(int Channel, int SetId); void Enqueue(int Channel, int SetId);
void Play(int Channel, int SetId, float Vol); void Play(int Channel, int SetId, float Volume);
void PlayAt(int Channel, int SetId, float Vol, vec2 Pos); void PlayAt(int Channel, int SetId, float Volume, vec2 Position);
void PlayAndRecord(int Channel, int SetId, float Vol, vec2 Pos); void PlayAndRecord(int Channel, int SetId, float Volume, vec2 Position);
void Stop(int SetId); void Stop(int SetId);
bool IsPlaying(int SetId); bool IsPlaying(int SetId);
ISound::CVoiceHandle PlaySample(int Channel, int SampleId, float Vol, int Flags = 0); ISound::CVoiceHandle PlaySample(int Channel, int SampleId, int Flags, float Volume);
ISound::CVoiceHandle PlaySampleAt(int Channel, int SampleId, float Vol, vec2 Pos, int Flags = 0); ISound::CVoiceHandle PlaySampleAt(int Channel, int SampleId, int Flags, float Volume, vec2 Position);
}; };
#endif #endif

View file

@ -269,7 +269,7 @@ void CVoting::OnMessage(int MsgType, void *pRawMsg)
char aBuf[512]; char aBuf[512];
str_format(aBuf, sizeof(aBuf), "%s (%s)", m_aDescription, m_aReason); str_format(aBuf, sizeof(aBuf), "%s (%s)", m_aDescription, m_aReason);
Client()->Notify("DDNet Vote", aBuf); 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);
} }
} }
} }

View file

@ -956,7 +956,7 @@ void CEditor::DoAudioPreview(CUIRect View, const void *pPlayPauseButtonId, const
if(SampleId != m_ToolbarPreviewSound && m_ToolbarPreviewSound >= 0 && Sound()->IsPlaying(m_ToolbarPreviewSound)) if(SampleId != m_ToolbarPreviewSound && m_ToolbarPreviewSound >= 0 && Sound()->IsPlaying(m_ToolbarPreviewSound))
Sound()->Pause(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);
} }
} }
} }