From f6c7f386ddeef0113fde9f56c531e13bf7961218 Mon Sep 17 00:00:00 2001 From: Panos Karabelas Date: Thu, 13 Feb 2025 16:37:27 +0000 Subject: [PATCH] [audiosource] implemented pitch control --- editor/Widgets/Properties.cpp | 6 ++++++ runtime/World/Components/AudioSource.cpp | 11 +++++++++++ runtime/World/Components/AudioSource.h | 16 ++++++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/editor/Widgets/Properties.cpp b/editor/Widgets/Properties.cpp index f2882357c..404a00591 100644 --- a/editor/Widgets/Properties.cpp +++ b/editor/Widgets/Properties.cpp @@ -1049,6 +1049,7 @@ void Properties::ShowAudioSource(shared_ptr audio_source) const bool loop = audio_source->GetLoop(); bool is_3d = audio_source->GetIs3d(); float volume = audio_source->GetVolume(); + float pitch = audio_source->GetPitch(); //======================================================== // Audio clip @@ -1072,6 +1073,10 @@ void Properties::ShowAudioSource(shared_ptr audio_source) const ImGui::Text("Loop"); ImGui::SameLine(column_pos_x); ImGui::Checkbox("##audioSourceLoop", &loop); + // Pitch + ImGui::Text("Pitch"); + ImGui::SameLine(column_pos_x); ImGui::SliderFloat("##audioSourcePitch", &pitch, 0.0f, 3.0f); + // loop ImGui::Text("3D"); ImGui::SameLine(column_pos_x); ImGui::Checkbox("##audioSourceIs3D", &is_3d); @@ -1086,6 +1091,7 @@ void Properties::ShowAudioSource(shared_ptr audio_source) const if (loop != audio_source->GetLoop()) audio_source->SetLoop(loop); if (is_3d != audio_source->GetIs3d()) audio_source->SetIs3d(is_3d); if (volume != audio_source->GetVolume()) audio_source->SetVolume(volume); + if (pitch != audio_source->GetPitch()) audio_source->SetPitch(pitch); //=============================================================================================== } component_end(); diff --git a/runtime/World/Components/AudioSource.cpp b/runtime/World/Components/AudioSource.cpp index c19711533..5f525649b 100644 --- a/runtime/World/Components/AudioSource.cpp +++ b/runtime/World/Components/AudioSource.cpp @@ -208,6 +208,7 @@ namespace spartan m_is_playing = true; SetVolume(m_volume); + SetPitch(m_pitch); } void AudioSource::Stop() @@ -249,4 +250,14 @@ namespace spartan CHECK_SDL_ERROR(SDL_SetAudioDeviceGain(audio_device::id, m_volume * m_attenuation * mute)); } } + + void AudioSource::SetPitch(const float pitch) + { + m_pitch = clamp(pitch, 0.0f, 3.0f); + + if (m_is_playing) + { + CHECK_SDL_ERROR(SDL_SetAudioStreamFrequencyRatio(m_stream, m_pitch)); + } + } } diff --git a/runtime/World/Components/AudioSource.h b/runtime/World/Components/AudioSource.h index c20def71d..a3917eff4 100644 --- a/runtime/World/Components/AudioSource.h +++ b/runtime/World/Components/AudioSource.h @@ -68,14 +68,18 @@ namespace spartan float GetVolume() const { return m_volume; } void SetVolume(float volume); + float GetPitch() const { return m_pitch; } + void SetPitch(const float pitch); + private: std::string m_name = "N/A"; - bool m_is_3d = false; - bool m_mute = false; - bool m_loop = true; - bool m_play_on_start = true; - float m_volume = 1.0f; - float m_attenuation = 1.0f; + bool m_is_3d = false; + bool m_mute = false; + bool m_loop = true; + bool m_play_on_start = true; + float m_volume = 1.0f; + float m_pitch = 1.0f; + float m_attenuation = 1.0f; bool m_is_playing = false; uint8_t* m_buffer = nullptr; uint32_t m_length = 0;