Skip to content

Commit

Permalink
[audiosource] added 3d attenuation
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Feb 13, 2025
1 parent e7bfb5a commit 763558b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
6 changes: 6 additions & 0 deletions editor/Widgets/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,7 @@ void Properties::ShowAudioSource(shared_ptr<AudioSource> audio_source) const
bool mute = audio_source->GetMute();
bool play_on_start = audio_source->GetPlayOnStart();
bool loop = audio_source->GetLoop();
bool is_3d = audio_source->GetIs3d();
float volume = audio_source->GetVolume();
float pitch = audio_source->GetPitch();
float pan = audio_source->GetPan();
Expand All @@ -1073,6 +1074,10 @@ void Properties::ShowAudioSource(shared_ptr<AudioSource> audio_source) const
ImGui::Text("Loop");
ImGui::SameLine(column_pos_x); ImGui::Checkbox("##audioSourceLoop", &loop);

// loop
ImGui::Text("3D");
ImGui::SameLine(column_pos_x); ImGui::Checkbox("##audioSourceIs3D", &is_3d);

// volume
ImGui::Text("Volume");
ImGui::SameLine(column_pos_x); ImGui::SliderFloat("##audioSourceVolume", &volume, 0.0f, 1.0f);
Expand All @@ -1089,6 +1094,7 @@ void Properties::ShowAudioSource(shared_ptr<AudioSource> audio_source) const
if (mute != audio_source->GetMute()) audio_source->SetMute(mute);
if (play_on_start != audio_source->GetPlayOnStart()) audio_source->SetPlayOnStart(play_on_start);
if (loop != audio_source->GetLoop()) audio_source->SetLoop(loop);
if (loop != 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);
if (pan != audio_source->GetPan()) audio_source->SetPan(pan);
Expand Down
41 changes: 33 additions & 8 deletions runtime/World/Components/AudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ in an action of contract, tort or otherwise, arising from, out of or in
connection with the software or the use or other dealings in the software.
*/

//= includes ===================
//= includes ========================
#include "pch.h"
#include "AudioSource.h"
#include "../../IO/FileStream.h"
SP_WARNINGS_OFF
#include <SDL3/SDL_audio.h>
//==============================
SP_WARNINGS_ON
#include "../../IO/FileStream.h"
#include "../../Rendering/Renderer.h"
#include "Camera.h"
#include "../Entity.h"
//===================================

using namespace std;
using namespace spartan::math;
Expand Down Expand Up @@ -122,12 +127,32 @@ namespace spartan

void AudioSource::OnTick()
{
if (m_loop && m_is_playing)
{
if (SDL_GetAudioStreamAvailable(m_stream) == 0) // buffer empty, restart
if (m_is_playing)
{
if (m_loop)
{
if (SDL_GetAudioStreamAvailable(m_stream) == 0) // buffer empty, restart
{
Stop(); // destroy stream
Play(); // create stream
}
}

if (m_is_3d)
{
Stop(); // destroy stream
Play(); // create stream
// todo: implement panning to stick a dot product in there

// attenuation
if (Camera* camera = Renderer::GetCamera().get())
{
Vector3 camera_position = camera->GetEntity()->GetPosition();
Vector3 sound_position = GetEntity()->GetPosition();
float distance_squared = Vector3::DistanceSquared(camera_position, sound_position);
float volume = 1.0f / (1.0f + distance_squared); // inverse square law
volume = max(0.0f, min(volume, 1.0f));
SP_LOG_INFO("%f", volume);
SetVolume(volume);
}
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions runtime/World/Components/AudioSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ namespace spartan
bool GetLoop() const { return m_loop; }
void SetLoop(const bool loop) { m_loop = loop; }

bool GetIs3d() const { return m_is_3d; }
void SetIs3d(const bool is_3d) { m_is_3d = is_3d; }

float GetVolume() const { return m_volume; }
void SetVolume(float volume);

Expand All @@ -72,16 +75,15 @@ namespace spartan
void SetPan(float pan);

private:
std::string m_name = "N/A";
bool m_mute = false;
bool m_loop = true;
bool m_audio_clip_loaded = false;
bool m_play_on_start = true;
float m_volume = 1.0f;
float m_pitch = 1.0f;
float m_pan = 0.0f;
bool m_is_playing = false;

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_pitch = 1.0f;
float m_pan = 0.0f;
bool m_is_playing = false;
uint8_t* m_buffer = nullptr;
uint32_t m_length = 0;
SDL_AudioStream* m_stream = nullptr;
Expand Down

0 comments on commit 763558b

Please sign in to comment.