Skip to content

Commit

Permalink
[audiosource] implemented pitch control
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Feb 13, 2025
1 parent 8f431d9 commit f6c7f38
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 6 additions & 0 deletions editor/Widgets/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,7 @@ void Properties::ShowAudioSource(shared_ptr<AudioSource> 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
Expand All @@ -1072,6 +1073,10 @@ void Properties::ShowAudioSource(shared_ptr<AudioSource> 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);
Expand All @@ -1086,6 +1091,7 @@ void Properties::ShowAudioSource(shared_ptr<AudioSource> 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();
Expand Down
11 changes: 11 additions & 0 deletions runtime/World/Components/AudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ namespace spartan
m_is_playing = true;

SetVolume(m_volume);
SetPitch(m_pitch);
}

void AudioSource::Stop()
Expand Down Expand Up @@ -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));
}
}
}
16 changes: 10 additions & 6 deletions runtime/World/Components/AudioSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f6c7f38

Please sign in to comment.