diff --git a/src/engine/client/sound.cpp b/src/engine/client/sound.cpp index c04fd1db746..63df6cdb507 100644 --- a/src/engine/client/sound.cpp +++ b/src/engine/client/sound.cpp @@ -820,7 +820,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); @@ -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_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; @@ -868,14 +868,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) diff --git a/src/engine/client/sound.h b/src/engine/client/sound.h index dfd166abbb2..f7cc4b82cc8 100644 --- a/src/engine/client/sound.h +++ b/src/engine/client/sound.h @@ -124,9 +124,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); diff --git a/src/engine/sound.h b/src/engine/sound.h index 846aa8aa4a8..e4462a95a16 100644 --- a/src/engine/sound.h +++ b/src/engine/sound.h @@ -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; diff --git a/src/game/client/components/chat.cpp b/src/game/client/components/chat.cpp index c31098abc1c..87797872645 100644 --- a/src/game/client/components/chat.cpp +++ b/src/game/client/components/chat.cpp @@ -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; } } @@ -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; } @@ -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; } } diff --git a/src/game/client/components/mapsounds.cpp b/src/game/client/components/mapsounds.cpp index 05eafd829f1..eee0017fc6e 100644 --- a/src/game/client/components/mapsounds.cpp +++ b/src/game/client/components/mapsounds.cpp @@ -22,15 +22,15 @@ void CMapSounds::Play(int SoundId) if(SoundId < 0 || SoundId >= m_Count) 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) 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() @@ -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) diff --git a/src/game/client/components/mapsounds.h b/src/game/client/components/mapsounds.h index 2f89d402b75..a623303aa9e 100644 --- a/src/game/client/components/mapsounds.h +++ b/src/game/client/components/mapsounds.h @@ -34,7 +34,7 @@ class CMapSounds : public CComponent virtual int Sizeof() const override { return sizeof(*this); } void Play(int SoundId); - void PlayAt(int SoundId, vec2 Pos); + void PlayAt(int SoundId, vec2 Position); virtual void OnMapLoad() override; virtual void OnRender() override; diff --git a/src/game/client/components/players.cpp b/src/game/client/components/players.cpp index 85ea937ba0c..3876c5331dc 100644 --- a/src/game/client/components/players.cpp +++ b/src/game/client/components/players.cpp @@ -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(); } diff --git a/src/game/client/components/sounds.cpp b/src/game/client/components/sounds.cpp index 8717bf1e751..d04a20fdf69 100644 --- a/src/game/client/components/sounds.cpp +++ b/src/game/client/components/sounds.cpp @@ -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 desync 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) @@ -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); } diff --git a/src/game/client/components/sounds.h b/src/game/client/components/sounds.h index b412425dadb..8671f3bf82f 100644 --- a/src/game/client/components/sounds.h +++ b/src/game/client/components/sounds.h @@ -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 diff --git a/src/game/client/components/voting.cpp b/src/game/client/components/voting.cpp index 8a0282fdbb0..462a14e8da6 100644 --- a/src/game/client/components/voting.cpp +++ b/src/game/client/components/voting.cpp @@ -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); } } } diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 39e957dab54..439c8b8705a 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -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); } } }