Skip to content

Commit

Permalink
[audiosource] implemented mute functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Feb 13, 2025
1 parent dfc79a0 commit 23f7676
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 41 deletions.
14 changes: 1 addition & 13 deletions editor/Widgets/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,8 +1049,6 @@ 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();
float pan = audio_source->GetPan();
//========================================================

// Audio clip
Expand Down Expand Up @@ -1082,22 +1080,12 @@ void Properties::ShowAudioSource(shared_ptr<AudioSource> audio_source) const
ImGui::Text("Volume");
ImGui::SameLine(column_pos_x); ImGui::SliderFloat("##audioSourceVolume", &volume, 0.0f, 1.0f);

// pitch
ImGui::Text("Pitch");
ImGui::SameLine(column_pos_x); ImGui::SliderFloat("##audioSourcePitch", &pitch, 0.0f, 3.0f);

// pan
ImGui::Text("Pan");
ImGui::SameLine(column_pos_x); ImGui::SliderFloat("##audioSourcePan", &pan, -1.0f, 1.0f);

//= MAP =========================================================================================
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 (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);
if (pan != audio_source->GetPan()) audio_source->SetPan(pan);
//===============================================================================================
}
component_end();
Expand Down
26 changes: 7 additions & 19 deletions runtime/World/Components/AudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ namespace spartan
float camera_dot_sound = abs(Vector3::Dot(camera->GetEntity()->GetForward(), camera_to_sound));

// todo
// Mix_SetPanning() is probably the key function
// Using something SDL_SetAudioStreamPutCallback or similar to have a callback
// in which we can modualte the bytes of each channel to do panning
}

// attenuation
{
// inverse square law with a rolloff factor
float distance_squared = Vector3::DistanceSquared(camera_position, sound_position);
const float rolloff_factor = 20.0f;
const float rolloff_factor = 15.0f;
m_attenuation = 1.0f / (1.0f + (distance_squared / (rolloff_factor * rolloff_factor)));
m_attenuation = max(0.0f, min(m_attenuation, 1.0f));

Expand All @@ -175,8 +176,6 @@ namespace spartan
stream->Write(m_loop);
stream->Write(m_play_on_start);
stream->Write(m_volume);
stream->Write(m_pitch);
stream->Write(m_pan);
}

void AudioSource::Deserialize(FileStream* stream)
Expand All @@ -185,8 +184,6 @@ namespace spartan
stream->Read(&m_loop);
stream->Read(&m_play_on_start);
stream->Read(&m_volume);
stream->Read(&m_pitch);
stream->Read(&m_pan);
}

void AudioSource::SetAudioClip(const string& file_path)
Expand All @@ -203,7 +200,6 @@ namespace spartan
if (m_is_playing)
return;

// create an audio stream for conversion (assuming source and device specs are the same)
m_stream = SDL_CreateAudioStream(&audio_device::spec, &audio_device::spec);
CHECK_SDL_ERROR(SDL_BindAudioStream(audio_device::id, m_stream));
CHECK_SDL_ERROR(SDL_ResumeAudioStreamDevice(m_stream));
Expand Down Expand Up @@ -240,25 +236,17 @@ namespace spartan
return;

m_mute = mute;
SetVolume(m_volume);
}

void AudioSource::SetVolume(float volume)
{
m_volume = clamp(volume, 0.0f, 1.0f);

if (m_is_playing)
{
CHECK_SDL_ERROR(SDL_SetAudioDeviceGain(audio_device::id, m_volume * m_attenuation));
{
float mute = m_mute ? 0.0f : 1.0f;
CHECK_SDL_ERROR(SDL_SetAudioDeviceGain(audio_device::id, m_volume * m_attenuation * mute));
}
}

void AudioSource::SetPitch(float pitch)
{
m_pitch = clamp(pitch, 0.0f, 3.0f);
}

void AudioSource::SetPan(float pan)
{
m_pan = clamp(pan, -1.0f, 1.0f);
}
}
10 changes: 1 addition & 9 deletions runtime/World/Components/AudioSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,14 @@ namespace spartan
float GetVolume() const { return m_volume; }
void SetVolume(float volume);

float GetPitch() const { return m_pitch; }
void SetPitch(float pitch);

float GetPan() const { return m_pan; }
void SetPan(float pan);

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;
float m_pitch = 1.0f;
float m_pan = 0.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 23f7676

Please sign in to comment.